dma_v3.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902
  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/module.h>
  58. #include <linux/pci.h>
  59. #include <linux/gfp.h>
  60. #include <linux/dmaengine.h>
  61. #include <linux/dma-mapping.h>
  62. #include <linux/prefetch.h>
  63. #include "../dmaengine.h"
  64. #include "registers.h"
  65. #include "hw.h"
  66. #include "dma.h"
  67. #include "dma_v2.h"
  68. /* ioat hardware assumes at least two sources for raid operations */
  69. #define src_cnt_to_sw(x) ((x) + 2)
  70. #define src_cnt_to_hw(x) ((x) - 2)
  71. #define ndest_to_sw(x) ((x) + 1)
  72. #define ndest_to_hw(x) ((x) - 1)
  73. #define src16_cnt_to_sw(x) ((x) + 9)
  74. #define src16_cnt_to_hw(x) ((x) - 9)
  75. /* provide a lookup table for setting the source address in the base or
  76. * extended descriptor of an xor or pq descriptor
  77. */
  78. static const u8 xor_idx_to_desc = 0xe0;
  79. static const u8 xor_idx_to_field[] = { 1, 4, 5, 6, 7, 0, 1, 2 };
  80. static const u8 pq_idx_to_desc = 0xf8;
  81. static const u8 pq16_idx_to_desc[] = { 0, 0, 1, 1, 1, 1, 1, 1, 1,
  82. 2, 2, 2, 2, 2, 2, 2 };
  83. static const u8 pq_idx_to_field[] = { 1, 4, 5, 0, 1, 2, 4, 5 };
  84. static const u8 pq16_idx_to_field[] = { 1, 4, 1, 2, 3, 4, 5, 6, 7,
  85. 0, 1, 2, 3, 4, 5, 6 };
  86. /*
  87. * technically sources 1 and 2 do not require SED, but the op will have
  88. * at least 9 descriptors so that's irrelevant.
  89. */
  90. static const u8 pq16_idx_to_sed[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0,
  91. 1, 1, 1, 1, 1, 1, 1 };
  92. static void ioat3_eh(struct ioat2_dma_chan *ioat);
  93. static dma_addr_t xor_get_src(struct ioat_raw_descriptor *descs[2], int idx)
  94. {
  95. struct ioat_raw_descriptor *raw = descs[xor_idx_to_desc >> idx & 1];
  96. return raw->field[xor_idx_to_field[idx]];
  97. }
  98. static void xor_set_src(struct ioat_raw_descriptor *descs[2],
  99. dma_addr_t addr, u32 offset, int idx)
  100. {
  101. struct ioat_raw_descriptor *raw = descs[xor_idx_to_desc >> idx & 1];
  102. raw->field[xor_idx_to_field[idx]] = addr + offset;
  103. }
  104. static dma_addr_t pq_get_src(struct ioat_raw_descriptor *descs[2], int idx)
  105. {
  106. struct ioat_raw_descriptor *raw = descs[pq_idx_to_desc >> idx & 1];
  107. return raw->field[pq_idx_to_field[idx]];
  108. }
  109. static dma_addr_t pq16_get_src(struct ioat_raw_descriptor *desc[3], int idx)
  110. {
  111. struct ioat_raw_descriptor *raw = desc[pq16_idx_to_desc[idx]];
  112. return raw->field[pq16_idx_to_field[idx]];
  113. }
  114. static void pq_set_src(struct ioat_raw_descriptor *descs[2],
  115. dma_addr_t addr, u32 offset, u8 coef, int idx)
  116. {
  117. struct ioat_pq_descriptor *pq = (struct ioat_pq_descriptor *) descs[0];
  118. struct ioat_raw_descriptor *raw = descs[pq_idx_to_desc >> idx & 1];
  119. raw->field[pq_idx_to_field[idx]] = addr + offset;
  120. pq->coef[idx] = coef;
  121. }
  122. static int sed_get_pq16_pool_idx(int src_cnt)
  123. {
  124. return pq16_idx_to_sed[src_cnt];
  125. }
  126. static bool is_jf_ioat(struct pci_dev *pdev)
  127. {
  128. switch (pdev->device) {
  129. case PCI_DEVICE_ID_INTEL_IOAT_JSF0:
  130. case PCI_DEVICE_ID_INTEL_IOAT_JSF1:
  131. case PCI_DEVICE_ID_INTEL_IOAT_JSF2:
  132. case PCI_DEVICE_ID_INTEL_IOAT_JSF3:
  133. case PCI_DEVICE_ID_INTEL_IOAT_JSF4:
  134. case PCI_DEVICE_ID_INTEL_IOAT_JSF5:
  135. case PCI_DEVICE_ID_INTEL_IOAT_JSF6:
  136. case PCI_DEVICE_ID_INTEL_IOAT_JSF7:
  137. case PCI_DEVICE_ID_INTEL_IOAT_JSF8:
  138. case PCI_DEVICE_ID_INTEL_IOAT_JSF9:
  139. return true;
  140. default:
  141. return false;
  142. }
  143. }
  144. static bool is_snb_ioat(struct pci_dev *pdev)
  145. {
  146. switch (pdev->device) {
  147. case PCI_DEVICE_ID_INTEL_IOAT_SNB0:
  148. case PCI_DEVICE_ID_INTEL_IOAT_SNB1:
  149. case PCI_DEVICE_ID_INTEL_IOAT_SNB2:
  150. case PCI_DEVICE_ID_INTEL_IOAT_SNB3:
  151. case PCI_DEVICE_ID_INTEL_IOAT_SNB4:
  152. case PCI_DEVICE_ID_INTEL_IOAT_SNB5:
  153. case PCI_DEVICE_ID_INTEL_IOAT_SNB6:
  154. case PCI_DEVICE_ID_INTEL_IOAT_SNB7:
  155. case PCI_DEVICE_ID_INTEL_IOAT_SNB8:
  156. case PCI_DEVICE_ID_INTEL_IOAT_SNB9:
  157. return true;
  158. default:
  159. return false;
  160. }
  161. }
  162. static bool is_ivb_ioat(struct pci_dev *pdev)
  163. {
  164. switch (pdev->device) {
  165. case PCI_DEVICE_ID_INTEL_IOAT_IVB0:
  166. case PCI_DEVICE_ID_INTEL_IOAT_IVB1:
  167. case PCI_DEVICE_ID_INTEL_IOAT_IVB2:
  168. case PCI_DEVICE_ID_INTEL_IOAT_IVB3:
  169. case PCI_DEVICE_ID_INTEL_IOAT_IVB4:
  170. case PCI_DEVICE_ID_INTEL_IOAT_IVB5:
  171. case PCI_DEVICE_ID_INTEL_IOAT_IVB6:
  172. case PCI_DEVICE_ID_INTEL_IOAT_IVB7:
  173. case PCI_DEVICE_ID_INTEL_IOAT_IVB8:
  174. case PCI_DEVICE_ID_INTEL_IOAT_IVB9:
  175. return true;
  176. default:
  177. return false;
  178. }
  179. }
  180. static bool is_hsw_ioat(struct pci_dev *pdev)
  181. {
  182. switch (pdev->device) {
  183. case PCI_DEVICE_ID_INTEL_IOAT_HSW0:
  184. case PCI_DEVICE_ID_INTEL_IOAT_HSW1:
  185. case PCI_DEVICE_ID_INTEL_IOAT_HSW2:
  186. case PCI_DEVICE_ID_INTEL_IOAT_HSW3:
  187. case PCI_DEVICE_ID_INTEL_IOAT_HSW4:
  188. case PCI_DEVICE_ID_INTEL_IOAT_HSW5:
  189. case PCI_DEVICE_ID_INTEL_IOAT_HSW6:
  190. case PCI_DEVICE_ID_INTEL_IOAT_HSW7:
  191. case PCI_DEVICE_ID_INTEL_IOAT_HSW8:
  192. case PCI_DEVICE_ID_INTEL_IOAT_HSW9:
  193. return true;
  194. default:
  195. return false;
  196. }
  197. }
  198. static bool is_xeon_cb32(struct pci_dev *pdev)
  199. {
  200. return is_jf_ioat(pdev) || is_snb_ioat(pdev) || is_ivb_ioat(pdev) ||
  201. is_hsw_ioat(pdev);
  202. }
  203. static bool is_bwd_ioat(struct pci_dev *pdev)
  204. {
  205. switch (pdev->device) {
  206. case PCI_DEVICE_ID_INTEL_IOAT_BWD0:
  207. case PCI_DEVICE_ID_INTEL_IOAT_BWD1:
  208. case PCI_DEVICE_ID_INTEL_IOAT_BWD2:
  209. case PCI_DEVICE_ID_INTEL_IOAT_BWD3:
  210. return true;
  211. default:
  212. return false;
  213. }
  214. }
  215. static bool is_bwd_noraid(struct pci_dev *pdev)
  216. {
  217. switch (pdev->device) {
  218. case PCI_DEVICE_ID_INTEL_IOAT_BWD2:
  219. case PCI_DEVICE_ID_INTEL_IOAT_BWD3:
  220. return true;
  221. default:
  222. return false;
  223. }
  224. }
  225. static void pq16_set_src(struct ioat_raw_descriptor *desc[3],
  226. dma_addr_t addr, u32 offset, u8 coef, unsigned idx)
  227. {
  228. struct ioat_pq_descriptor *pq = (struct ioat_pq_descriptor *)desc[0];
  229. struct ioat_pq16a_descriptor *pq16 =
  230. (struct ioat_pq16a_descriptor *)desc[1];
  231. struct ioat_raw_descriptor *raw = desc[pq16_idx_to_desc[idx]];
  232. raw->field[pq16_idx_to_field[idx]] = addr + offset;
  233. if (idx < 8)
  234. pq->coef[idx] = coef;
  235. else
  236. pq16->coef[idx - 8] = coef;
  237. }
  238. static struct ioat_sed_ent *
  239. ioat3_alloc_sed(struct ioatdma_device *device, unsigned int hw_pool)
  240. {
  241. struct ioat_sed_ent *sed;
  242. gfp_t flags = __GFP_ZERO | GFP_ATOMIC;
  243. sed = kmem_cache_alloc(device->sed_pool, flags);
  244. if (!sed)
  245. return NULL;
  246. sed->hw_pool = hw_pool;
  247. sed->hw = dma_pool_alloc(device->sed_hw_pool[hw_pool],
  248. flags, &sed->dma);
  249. if (!sed->hw) {
  250. kmem_cache_free(device->sed_pool, sed);
  251. return NULL;
  252. }
  253. return sed;
  254. }
  255. static void ioat3_free_sed(struct ioatdma_device *device, struct ioat_sed_ent *sed)
  256. {
  257. if (!sed)
  258. return;
  259. dma_pool_free(device->sed_hw_pool[sed->hw_pool], sed->hw, sed->dma);
  260. kmem_cache_free(device->sed_pool, sed);
  261. }
  262. static void ioat3_dma_unmap(struct ioat2_dma_chan *ioat,
  263. struct ioat_ring_ent *desc, int idx)
  264. {
  265. struct ioat_chan_common *chan = &ioat->base;
  266. struct pci_dev *pdev = chan->device->pdev;
  267. size_t len = desc->len;
  268. size_t offset = len - desc->hw->size;
  269. struct dma_async_tx_descriptor *tx = &desc->txd;
  270. enum dma_ctrl_flags flags = tx->flags;
  271. switch (desc->hw->ctl_f.op) {
  272. case IOAT_OP_COPY:
  273. if (!desc->hw->ctl_f.null) /* skip 'interrupt' ops */
  274. ioat_dma_unmap(chan, flags, len, desc->hw);
  275. break;
  276. case IOAT_OP_XOR_VAL:
  277. case IOAT_OP_XOR: {
  278. struct ioat_xor_descriptor *xor = desc->xor;
  279. struct ioat_ring_ent *ext;
  280. struct ioat_xor_ext_descriptor *xor_ex = NULL;
  281. int src_cnt = src_cnt_to_sw(xor->ctl_f.src_cnt);
  282. struct ioat_raw_descriptor *descs[2];
  283. int i;
  284. if (src_cnt > 5) {
  285. ext = ioat2_get_ring_ent(ioat, idx + 1);
  286. xor_ex = ext->xor_ex;
  287. }
  288. if (!(flags & DMA_COMPL_SKIP_SRC_UNMAP)) {
  289. descs[0] = (struct ioat_raw_descriptor *) xor;
  290. descs[1] = (struct ioat_raw_descriptor *) xor_ex;
  291. for (i = 0; i < src_cnt; i++) {
  292. dma_addr_t src = xor_get_src(descs, i);
  293. ioat_unmap(pdev, src - offset, len,
  294. PCI_DMA_TODEVICE, flags, 0);
  295. }
  296. /* dest is a source in xor validate operations */
  297. if (xor->ctl_f.op == IOAT_OP_XOR_VAL) {
  298. ioat_unmap(pdev, xor->dst_addr - offset, len,
  299. PCI_DMA_TODEVICE, flags, 1);
  300. break;
  301. }
  302. }
  303. if (!(flags & DMA_COMPL_SKIP_DEST_UNMAP))
  304. ioat_unmap(pdev, xor->dst_addr - offset, len,
  305. PCI_DMA_FROMDEVICE, flags, 1);
  306. break;
  307. }
  308. case IOAT_OP_PQ_VAL:
  309. case IOAT_OP_PQ: {
  310. struct ioat_pq_descriptor *pq = desc->pq;
  311. struct ioat_ring_ent *ext;
  312. struct ioat_pq_ext_descriptor *pq_ex = NULL;
  313. int src_cnt = src_cnt_to_sw(pq->ctl_f.src_cnt);
  314. struct ioat_raw_descriptor *descs[2];
  315. int i;
  316. if (src_cnt > 3) {
  317. ext = ioat2_get_ring_ent(ioat, idx + 1);
  318. pq_ex = ext->pq_ex;
  319. }
  320. /* in the 'continue' case don't unmap the dests as sources */
  321. if (dmaf_p_disabled_continue(flags))
  322. src_cnt--;
  323. else if (dmaf_continue(flags))
  324. src_cnt -= 3;
  325. if (!(flags & DMA_COMPL_SKIP_SRC_UNMAP)) {
  326. descs[0] = (struct ioat_raw_descriptor *) pq;
  327. descs[1] = (struct ioat_raw_descriptor *) pq_ex;
  328. for (i = 0; i < src_cnt; i++) {
  329. dma_addr_t src = pq_get_src(descs, i);
  330. ioat_unmap(pdev, src - offset, len,
  331. PCI_DMA_TODEVICE, flags, 0);
  332. }
  333. /* the dests are sources in pq validate operations */
  334. if (pq->ctl_f.op == IOAT_OP_XOR_VAL) {
  335. if (!(flags & DMA_PREP_PQ_DISABLE_P))
  336. ioat_unmap(pdev, pq->p_addr - offset,
  337. len, PCI_DMA_TODEVICE, flags, 0);
  338. if (!(flags & DMA_PREP_PQ_DISABLE_Q))
  339. ioat_unmap(pdev, pq->q_addr - offset,
  340. len, PCI_DMA_TODEVICE, flags, 0);
  341. break;
  342. }
  343. }
  344. if (!(flags & DMA_COMPL_SKIP_DEST_UNMAP)) {
  345. if (!(flags & DMA_PREP_PQ_DISABLE_P))
  346. ioat_unmap(pdev, pq->p_addr - offset, len,
  347. PCI_DMA_BIDIRECTIONAL, flags, 1);
  348. if (!(flags & DMA_PREP_PQ_DISABLE_Q))
  349. ioat_unmap(pdev, pq->q_addr - offset, len,
  350. PCI_DMA_BIDIRECTIONAL, flags, 1);
  351. }
  352. break;
  353. }
  354. case IOAT_OP_PQ_16S:
  355. case IOAT_OP_PQ_VAL_16S: {
  356. struct ioat_pq_descriptor *pq = desc->pq;
  357. int src_cnt = src16_cnt_to_sw(pq->ctl_f.src_cnt);
  358. struct ioat_raw_descriptor *descs[4];
  359. int i;
  360. /* in the 'continue' case don't unmap the dests as sources */
  361. if (dmaf_p_disabled_continue(flags))
  362. src_cnt--;
  363. else if (dmaf_continue(flags))
  364. src_cnt -= 3;
  365. if (!(flags & DMA_COMPL_SKIP_SRC_UNMAP)) {
  366. descs[0] = (struct ioat_raw_descriptor *)pq;
  367. descs[1] = (struct ioat_raw_descriptor *)(desc->sed->hw);
  368. descs[2] = (struct ioat_raw_descriptor *)(&desc->sed->hw->b[0]);
  369. for (i = 0; i < src_cnt; i++) {
  370. dma_addr_t src = pq16_get_src(descs, i);
  371. ioat_unmap(pdev, src - offset, len,
  372. PCI_DMA_TODEVICE, flags, 0);
  373. }
  374. /* the dests are sources in pq validate operations */
  375. if (pq->ctl_f.op == IOAT_OP_XOR_VAL) {
  376. if (!(flags & DMA_PREP_PQ_DISABLE_P))
  377. ioat_unmap(pdev, pq->p_addr - offset,
  378. len, PCI_DMA_TODEVICE,
  379. flags, 0);
  380. if (!(flags & DMA_PREP_PQ_DISABLE_Q))
  381. ioat_unmap(pdev, pq->q_addr - offset,
  382. len, PCI_DMA_TODEVICE,
  383. flags, 0);
  384. break;
  385. }
  386. }
  387. if (!(flags & DMA_COMPL_SKIP_DEST_UNMAP)) {
  388. if (!(flags & DMA_PREP_PQ_DISABLE_P))
  389. ioat_unmap(pdev, pq->p_addr - offset, len,
  390. PCI_DMA_BIDIRECTIONAL, flags, 1);
  391. if (!(flags & DMA_PREP_PQ_DISABLE_Q))
  392. ioat_unmap(pdev, pq->q_addr - offset, len,
  393. PCI_DMA_BIDIRECTIONAL, flags, 1);
  394. }
  395. break;
  396. }
  397. default:
  398. dev_err(&pdev->dev, "%s: unknown op type: %#x\n",
  399. __func__, desc->hw->ctl_f.op);
  400. }
  401. }
  402. static bool desc_has_ext(struct ioat_ring_ent *desc)
  403. {
  404. struct ioat_dma_descriptor *hw = desc->hw;
  405. if (hw->ctl_f.op == IOAT_OP_XOR ||
  406. hw->ctl_f.op == IOAT_OP_XOR_VAL) {
  407. struct ioat_xor_descriptor *xor = desc->xor;
  408. if (src_cnt_to_sw(xor->ctl_f.src_cnt) > 5)
  409. return true;
  410. } else if (hw->ctl_f.op == IOAT_OP_PQ ||
  411. hw->ctl_f.op == IOAT_OP_PQ_VAL) {
  412. struct ioat_pq_descriptor *pq = desc->pq;
  413. if (src_cnt_to_sw(pq->ctl_f.src_cnt) > 3)
  414. return true;
  415. }
  416. return false;
  417. }
  418. static u64 ioat3_get_current_completion(struct ioat_chan_common *chan)
  419. {
  420. u64 phys_complete;
  421. u64 completion;
  422. completion = *chan->completion;
  423. phys_complete = ioat_chansts_to_addr(completion);
  424. dev_dbg(to_dev(chan), "%s: phys_complete: %#llx\n", __func__,
  425. (unsigned long long) phys_complete);
  426. return phys_complete;
  427. }
  428. static bool ioat3_cleanup_preamble(struct ioat_chan_common *chan,
  429. u64 *phys_complete)
  430. {
  431. *phys_complete = ioat3_get_current_completion(chan);
  432. if (*phys_complete == chan->last_completion)
  433. return false;
  434. clear_bit(IOAT_COMPLETION_ACK, &chan->state);
  435. mod_timer(&chan->timer, jiffies + COMPLETION_TIMEOUT);
  436. return true;
  437. }
  438. static void
  439. desc_get_errstat(struct ioat2_dma_chan *ioat, struct ioat_ring_ent *desc)
  440. {
  441. struct ioat_dma_descriptor *hw = desc->hw;
  442. switch (hw->ctl_f.op) {
  443. case IOAT_OP_PQ_VAL:
  444. case IOAT_OP_PQ_VAL_16S:
  445. {
  446. struct ioat_pq_descriptor *pq = desc->pq;
  447. /* check if there's error written */
  448. if (!pq->dwbes_f.wbes)
  449. return;
  450. /* need to set a chanerr var for checking to clear later */
  451. if (pq->dwbes_f.p_val_err)
  452. *desc->result |= SUM_CHECK_P_RESULT;
  453. if (pq->dwbes_f.q_val_err)
  454. *desc->result |= SUM_CHECK_Q_RESULT;
  455. return;
  456. }
  457. default:
  458. return;
  459. }
  460. }
  461. /**
  462. * __cleanup - reclaim used descriptors
  463. * @ioat: channel (ring) to clean
  464. *
  465. * The difference from the dma_v2.c __cleanup() is that this routine
  466. * handles extended descriptors and dma-unmapping raid operations.
  467. */
  468. static void __cleanup(struct ioat2_dma_chan *ioat, dma_addr_t phys_complete)
  469. {
  470. struct ioat_chan_common *chan = &ioat->base;
  471. struct ioatdma_device *device = chan->device;
  472. struct ioat_ring_ent *desc;
  473. bool seen_current = false;
  474. int idx = ioat->tail, i;
  475. u16 active;
  476. dev_dbg(to_dev(chan), "%s: head: %#x tail: %#x issued: %#x\n",
  477. __func__, ioat->head, ioat->tail, ioat->issued);
  478. /*
  479. * At restart of the channel, the completion address and the
  480. * channel status will be 0 due to starting a new chain. Since
  481. * it's new chain and the first descriptor "fails", there is
  482. * nothing to clean up. We do not want to reap the entire submitted
  483. * chain due to this 0 address value and then BUG.
  484. */
  485. if (!phys_complete)
  486. return;
  487. active = ioat2_ring_active(ioat);
  488. for (i = 0; i < active && !seen_current; i++) {
  489. struct dma_async_tx_descriptor *tx;
  490. smp_read_barrier_depends();
  491. prefetch(ioat2_get_ring_ent(ioat, idx + i + 1));
  492. desc = ioat2_get_ring_ent(ioat, idx + i);
  493. dump_desc_dbg(ioat, desc);
  494. /* set err stat if we are using dwbes */
  495. if (device->cap & IOAT_CAP_DWBES)
  496. desc_get_errstat(ioat, desc);
  497. tx = &desc->txd;
  498. if (tx->cookie) {
  499. dma_cookie_complete(tx);
  500. ioat3_dma_unmap(ioat, desc, idx + i);
  501. if (tx->callback) {
  502. tx->callback(tx->callback_param);
  503. tx->callback = NULL;
  504. }
  505. }
  506. if (tx->phys == phys_complete)
  507. seen_current = true;
  508. /* skip extended descriptors */
  509. if (desc_has_ext(desc)) {
  510. BUG_ON(i + 1 >= active);
  511. i++;
  512. }
  513. /* cleanup super extended descriptors */
  514. if (desc->sed) {
  515. ioat3_free_sed(device, desc->sed);
  516. desc->sed = NULL;
  517. }
  518. }
  519. smp_mb(); /* finish all descriptor reads before incrementing tail */
  520. ioat->tail = idx + i;
  521. BUG_ON(active && !seen_current); /* no active descs have written a completion? */
  522. chan->last_completion = phys_complete;
  523. if (active - i == 0) {
  524. dev_dbg(to_dev(chan), "%s: cancel completion timeout\n",
  525. __func__);
  526. clear_bit(IOAT_COMPLETION_PENDING, &chan->state);
  527. mod_timer(&chan->timer, jiffies + IDLE_TIMEOUT);
  528. }
  529. /* 5 microsecond delay per pending descriptor */
  530. writew(min((5 * (active - i)), IOAT_INTRDELAY_MASK),
  531. chan->device->reg_base + IOAT_INTRDELAY_OFFSET);
  532. }
  533. static void ioat3_cleanup(struct ioat2_dma_chan *ioat)
  534. {
  535. struct ioat_chan_common *chan = &ioat->base;
  536. u64 phys_complete;
  537. spin_lock_bh(&chan->cleanup_lock);
  538. if (ioat3_cleanup_preamble(chan, &phys_complete))
  539. __cleanup(ioat, phys_complete);
  540. if (is_ioat_halted(*chan->completion)) {
  541. u32 chanerr = readl(chan->reg_base + IOAT_CHANERR_OFFSET);
  542. if (chanerr & IOAT_CHANERR_HANDLE_MASK) {
  543. mod_timer(&chan->timer, jiffies + IDLE_TIMEOUT);
  544. ioat3_eh(ioat);
  545. }
  546. }
  547. spin_unlock_bh(&chan->cleanup_lock);
  548. }
  549. static void ioat3_cleanup_event(unsigned long data)
  550. {
  551. struct ioat2_dma_chan *ioat = to_ioat2_chan((void *) data);
  552. ioat3_cleanup(ioat);
  553. writew(IOAT_CHANCTRL_RUN, ioat->base.reg_base + IOAT_CHANCTRL_OFFSET);
  554. }
  555. static void ioat3_restart_channel(struct ioat2_dma_chan *ioat)
  556. {
  557. struct ioat_chan_common *chan = &ioat->base;
  558. u64 phys_complete;
  559. ioat2_quiesce(chan, 0);
  560. if (ioat3_cleanup_preamble(chan, &phys_complete))
  561. __cleanup(ioat, phys_complete);
  562. __ioat2_restart_chan(ioat);
  563. }
  564. static void ioat3_eh(struct ioat2_dma_chan *ioat)
  565. {
  566. struct ioat_chan_common *chan = &ioat->base;
  567. struct pci_dev *pdev = to_pdev(chan);
  568. struct ioat_dma_descriptor *hw;
  569. u64 phys_complete;
  570. struct ioat_ring_ent *desc;
  571. u32 err_handled = 0;
  572. u32 chanerr_int;
  573. u32 chanerr;
  574. /* cleanup so tail points to descriptor that caused the error */
  575. if (ioat3_cleanup_preamble(chan, &phys_complete))
  576. __cleanup(ioat, phys_complete);
  577. chanerr = readl(chan->reg_base + IOAT_CHANERR_OFFSET);
  578. pci_read_config_dword(pdev, IOAT_PCI_CHANERR_INT_OFFSET, &chanerr_int);
  579. dev_dbg(to_dev(chan), "%s: error = %x:%x\n",
  580. __func__, chanerr, chanerr_int);
  581. desc = ioat2_get_ring_ent(ioat, ioat->tail);
  582. hw = desc->hw;
  583. dump_desc_dbg(ioat, desc);
  584. switch (hw->ctl_f.op) {
  585. case IOAT_OP_XOR_VAL:
  586. if (chanerr & IOAT_CHANERR_XOR_P_OR_CRC_ERR) {
  587. *desc->result |= SUM_CHECK_P_RESULT;
  588. err_handled |= IOAT_CHANERR_XOR_P_OR_CRC_ERR;
  589. }
  590. break;
  591. case IOAT_OP_PQ_VAL:
  592. case IOAT_OP_PQ_VAL_16S:
  593. if (chanerr & IOAT_CHANERR_XOR_P_OR_CRC_ERR) {
  594. *desc->result |= SUM_CHECK_P_RESULT;
  595. err_handled |= IOAT_CHANERR_XOR_P_OR_CRC_ERR;
  596. }
  597. if (chanerr & IOAT_CHANERR_XOR_Q_ERR) {
  598. *desc->result |= SUM_CHECK_Q_RESULT;
  599. err_handled |= IOAT_CHANERR_XOR_Q_ERR;
  600. }
  601. break;
  602. }
  603. /* fault on unhandled error or spurious halt */
  604. if (chanerr ^ err_handled || chanerr == 0) {
  605. dev_err(to_dev(chan), "%s: fatal error (%x:%x)\n",
  606. __func__, chanerr, err_handled);
  607. BUG();
  608. }
  609. writel(chanerr, chan->reg_base + IOAT_CHANERR_OFFSET);
  610. pci_write_config_dword(pdev, IOAT_PCI_CHANERR_INT_OFFSET, chanerr_int);
  611. /* mark faulting descriptor as complete */
  612. *chan->completion = desc->txd.phys;
  613. spin_lock_bh(&ioat->prep_lock);
  614. ioat3_restart_channel(ioat);
  615. spin_unlock_bh(&ioat->prep_lock);
  616. }
  617. static void check_active(struct ioat2_dma_chan *ioat)
  618. {
  619. struct ioat_chan_common *chan = &ioat->base;
  620. if (ioat2_ring_active(ioat)) {
  621. mod_timer(&chan->timer, jiffies + COMPLETION_TIMEOUT);
  622. return;
  623. }
  624. if (test_and_clear_bit(IOAT_CHAN_ACTIVE, &chan->state))
  625. mod_timer(&chan->timer, jiffies + IDLE_TIMEOUT);
  626. else if (ioat->alloc_order > ioat_get_alloc_order()) {
  627. /* if the ring is idle, empty, and oversized try to step
  628. * down the size
  629. */
  630. reshape_ring(ioat, ioat->alloc_order - 1);
  631. /* keep shrinking until we get back to our minimum
  632. * default size
  633. */
  634. if (ioat->alloc_order > ioat_get_alloc_order())
  635. mod_timer(&chan->timer, jiffies + IDLE_TIMEOUT);
  636. }
  637. }
  638. static void ioat3_timer_event(unsigned long data)
  639. {
  640. struct ioat2_dma_chan *ioat = to_ioat2_chan((void *) data);
  641. struct ioat_chan_common *chan = &ioat->base;
  642. dma_addr_t phys_complete;
  643. u64 status;
  644. status = ioat_chansts(chan);
  645. /* when halted due to errors check for channel
  646. * programming errors before advancing the completion state
  647. */
  648. if (is_ioat_halted(status)) {
  649. u32 chanerr;
  650. chanerr = readl(chan->reg_base + IOAT_CHANERR_OFFSET);
  651. dev_err(to_dev(chan), "%s: Channel halted (%x)\n",
  652. __func__, chanerr);
  653. if (test_bit(IOAT_RUN, &chan->state))
  654. BUG_ON(is_ioat_bug(chanerr));
  655. else /* we never got off the ground */
  656. return;
  657. }
  658. /* if we haven't made progress and we have already
  659. * acknowledged a pending completion once, then be more
  660. * forceful with a restart
  661. */
  662. spin_lock_bh(&chan->cleanup_lock);
  663. if (ioat_cleanup_preamble(chan, &phys_complete))
  664. __cleanup(ioat, phys_complete);
  665. else if (test_bit(IOAT_COMPLETION_ACK, &chan->state)) {
  666. spin_lock_bh(&ioat->prep_lock);
  667. ioat3_restart_channel(ioat);
  668. spin_unlock_bh(&ioat->prep_lock);
  669. spin_unlock_bh(&chan->cleanup_lock);
  670. return;
  671. } else {
  672. set_bit(IOAT_COMPLETION_ACK, &chan->state);
  673. mod_timer(&chan->timer, jiffies + COMPLETION_TIMEOUT);
  674. }
  675. if (ioat2_ring_active(ioat))
  676. mod_timer(&chan->timer, jiffies + COMPLETION_TIMEOUT);
  677. else {
  678. spin_lock_bh(&ioat->prep_lock);
  679. check_active(ioat);
  680. spin_unlock_bh(&ioat->prep_lock);
  681. }
  682. spin_unlock_bh(&chan->cleanup_lock);
  683. }
  684. static enum dma_status
  685. ioat3_tx_status(struct dma_chan *c, dma_cookie_t cookie,
  686. struct dma_tx_state *txstate)
  687. {
  688. struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
  689. enum dma_status ret;
  690. ret = dma_cookie_status(c, cookie, txstate);
  691. if (ret == DMA_SUCCESS)
  692. return ret;
  693. ioat3_cleanup(ioat);
  694. return dma_cookie_status(c, cookie, txstate);
  695. }
  696. static struct dma_async_tx_descriptor *
  697. __ioat3_prep_xor_lock(struct dma_chan *c, enum sum_check_flags *result,
  698. dma_addr_t dest, dma_addr_t *src, unsigned int src_cnt,
  699. size_t len, unsigned long flags)
  700. {
  701. struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
  702. struct ioat_ring_ent *compl_desc;
  703. struct ioat_ring_ent *desc;
  704. struct ioat_ring_ent *ext;
  705. size_t total_len = len;
  706. struct ioat_xor_descriptor *xor;
  707. struct ioat_xor_ext_descriptor *xor_ex = NULL;
  708. struct ioat_dma_descriptor *hw;
  709. int num_descs, with_ext, idx, i;
  710. u32 offset = 0;
  711. u8 op = result ? IOAT_OP_XOR_VAL : IOAT_OP_XOR;
  712. BUG_ON(src_cnt < 2);
  713. num_descs = ioat2_xferlen_to_descs(ioat, len);
  714. /* we need 2x the number of descriptors to cover greater than 5
  715. * sources
  716. */
  717. if (src_cnt > 5) {
  718. with_ext = 1;
  719. num_descs *= 2;
  720. } else
  721. with_ext = 0;
  722. /* completion writes from the raid engine may pass completion
  723. * writes from the legacy engine, so we need one extra null
  724. * (legacy) descriptor to ensure all completion writes arrive in
  725. * order.
  726. */
  727. if (likely(num_descs) && ioat2_check_space_lock(ioat, num_descs+1) == 0)
  728. idx = ioat->head;
  729. else
  730. return NULL;
  731. i = 0;
  732. do {
  733. struct ioat_raw_descriptor *descs[2];
  734. size_t xfer_size = min_t(size_t, len, 1 << ioat->xfercap_log);
  735. int s;
  736. desc = ioat2_get_ring_ent(ioat, idx + i);
  737. xor = desc->xor;
  738. /* save a branch by unconditionally retrieving the
  739. * extended descriptor xor_set_src() knows to not write
  740. * to it in the single descriptor case
  741. */
  742. ext = ioat2_get_ring_ent(ioat, idx + i + 1);
  743. xor_ex = ext->xor_ex;
  744. descs[0] = (struct ioat_raw_descriptor *) xor;
  745. descs[1] = (struct ioat_raw_descriptor *) xor_ex;
  746. for (s = 0; s < src_cnt; s++)
  747. xor_set_src(descs, src[s], offset, s);
  748. xor->size = xfer_size;
  749. xor->dst_addr = dest + offset;
  750. xor->ctl = 0;
  751. xor->ctl_f.op = op;
  752. xor->ctl_f.src_cnt = src_cnt_to_hw(src_cnt);
  753. len -= xfer_size;
  754. offset += xfer_size;
  755. dump_desc_dbg(ioat, desc);
  756. } while ((i += 1 + with_ext) < num_descs);
  757. /* last xor descriptor carries the unmap parameters and fence bit */
  758. desc->txd.flags = flags;
  759. desc->len = total_len;
  760. if (result)
  761. desc->result = result;
  762. xor->ctl_f.fence = !!(flags & DMA_PREP_FENCE);
  763. /* completion descriptor carries interrupt bit */
  764. compl_desc = ioat2_get_ring_ent(ioat, idx + i);
  765. compl_desc->txd.flags = flags & DMA_PREP_INTERRUPT;
  766. hw = compl_desc->hw;
  767. hw->ctl = 0;
  768. hw->ctl_f.null = 1;
  769. hw->ctl_f.int_en = !!(flags & DMA_PREP_INTERRUPT);
  770. hw->ctl_f.compl_write = 1;
  771. hw->size = NULL_DESC_BUFFER_SIZE;
  772. dump_desc_dbg(ioat, compl_desc);
  773. /* we leave the channel locked to ensure in order submission */
  774. return &compl_desc->txd;
  775. }
  776. static struct dma_async_tx_descriptor *
  777. ioat3_prep_xor(struct dma_chan *chan, dma_addr_t dest, dma_addr_t *src,
  778. unsigned int src_cnt, size_t len, unsigned long flags)
  779. {
  780. return __ioat3_prep_xor_lock(chan, NULL, dest, src, src_cnt, len, flags);
  781. }
  782. struct dma_async_tx_descriptor *
  783. ioat3_prep_xor_val(struct dma_chan *chan, dma_addr_t *src,
  784. unsigned int src_cnt, size_t len,
  785. enum sum_check_flags *result, unsigned long flags)
  786. {
  787. /* the cleanup routine only sets bits on validate failure, it
  788. * does not clear bits on validate success... so clear it here
  789. */
  790. *result = 0;
  791. return __ioat3_prep_xor_lock(chan, result, src[0], &src[1],
  792. src_cnt - 1, len, flags);
  793. }
  794. static void
  795. dump_pq_desc_dbg(struct ioat2_dma_chan *ioat, struct ioat_ring_ent *desc, struct ioat_ring_ent *ext)
  796. {
  797. struct device *dev = to_dev(&ioat->base);
  798. struct ioat_pq_descriptor *pq = desc->pq;
  799. struct ioat_pq_ext_descriptor *pq_ex = ext ? ext->pq_ex : NULL;
  800. struct ioat_raw_descriptor *descs[] = { (void *) pq, (void *) pq_ex };
  801. int src_cnt = src_cnt_to_sw(pq->ctl_f.src_cnt);
  802. int i;
  803. dev_dbg(dev, "desc[%d]: (%#llx->%#llx) flags: %#x"
  804. " sz: %#10.8x ctl: %#x (op: %#x int: %d compl: %d pq: '%s%s'"
  805. " src_cnt: %d)\n",
  806. desc_id(desc), (unsigned long long) desc->txd.phys,
  807. (unsigned long long) (pq_ex ? pq_ex->next : pq->next),
  808. desc->txd.flags, pq->size, pq->ctl, pq->ctl_f.op, pq->ctl_f.int_en,
  809. pq->ctl_f.compl_write,
  810. pq->ctl_f.p_disable ? "" : "p", pq->ctl_f.q_disable ? "" : "q",
  811. pq->ctl_f.src_cnt);
  812. for (i = 0; i < src_cnt; i++)
  813. dev_dbg(dev, "\tsrc[%d]: %#llx coef: %#x\n", i,
  814. (unsigned long long) pq_get_src(descs, i), pq->coef[i]);
  815. dev_dbg(dev, "\tP: %#llx\n", pq->p_addr);
  816. dev_dbg(dev, "\tQ: %#llx\n", pq->q_addr);
  817. dev_dbg(dev, "\tNEXT: %#llx\n", pq->next);
  818. }
  819. static void dump_pq16_desc_dbg(struct ioat2_dma_chan *ioat,
  820. struct ioat_ring_ent *desc)
  821. {
  822. struct device *dev = to_dev(&ioat->base);
  823. struct ioat_pq_descriptor *pq = desc->pq;
  824. struct ioat_raw_descriptor *descs[] = { (void *)pq,
  825. (void *)pq,
  826. (void *)pq };
  827. int src_cnt = src16_cnt_to_sw(pq->ctl_f.src_cnt);
  828. int i;
  829. if (desc->sed) {
  830. descs[1] = (void *)desc->sed->hw;
  831. descs[2] = (void *)desc->sed->hw + 64;
  832. }
  833. dev_dbg(dev, "desc[%d]: (%#llx->%#llx) flags: %#x"
  834. " sz: %#x ctl: %#x (op: %#x int: %d compl: %d pq: '%s%s'"
  835. " src_cnt: %d)\n",
  836. desc_id(desc), (unsigned long long) desc->txd.phys,
  837. (unsigned long long) pq->next,
  838. desc->txd.flags, pq->size, pq->ctl,
  839. pq->ctl_f.op, pq->ctl_f.int_en,
  840. pq->ctl_f.compl_write,
  841. pq->ctl_f.p_disable ? "" : "p", pq->ctl_f.q_disable ? "" : "q",
  842. pq->ctl_f.src_cnt);
  843. for (i = 0; i < src_cnt; i++) {
  844. dev_dbg(dev, "\tsrc[%d]: %#llx coef: %#x\n", i,
  845. (unsigned long long) pq16_get_src(descs, i),
  846. pq->coef[i]);
  847. }
  848. dev_dbg(dev, "\tP: %#llx\n", pq->p_addr);
  849. dev_dbg(dev, "\tQ: %#llx\n", pq->q_addr);
  850. }
  851. static struct dma_async_tx_descriptor *
  852. __ioat3_prep_pq_lock(struct dma_chan *c, enum sum_check_flags *result,
  853. const dma_addr_t *dst, const dma_addr_t *src,
  854. unsigned int src_cnt, const unsigned char *scf,
  855. size_t len, unsigned long flags)
  856. {
  857. struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
  858. struct ioat_chan_common *chan = &ioat->base;
  859. struct ioatdma_device *device = chan->device;
  860. struct ioat_ring_ent *compl_desc;
  861. struct ioat_ring_ent *desc;
  862. struct ioat_ring_ent *ext;
  863. size_t total_len = len;
  864. struct ioat_pq_descriptor *pq;
  865. struct ioat_pq_ext_descriptor *pq_ex = NULL;
  866. struct ioat_dma_descriptor *hw;
  867. u32 offset = 0;
  868. u8 op = result ? IOAT_OP_PQ_VAL : IOAT_OP_PQ;
  869. int i, s, idx, with_ext, num_descs;
  870. int cb32 = (device->version < IOAT_VER_3_3) ? 1 : 0;
  871. dev_dbg(to_dev(chan), "%s\n", __func__);
  872. /* the engine requires at least two sources (we provide
  873. * at least 1 implied source in the DMA_PREP_CONTINUE case)
  874. */
  875. BUG_ON(src_cnt + dmaf_continue(flags) < 2);
  876. num_descs = ioat2_xferlen_to_descs(ioat, len);
  877. /* we need 2x the number of descriptors to cover greater than 3
  878. * sources (we need 1 extra source in the q-only continuation
  879. * case and 3 extra sources in the p+q continuation case.
  880. */
  881. if (src_cnt + dmaf_p_disabled_continue(flags) > 3 ||
  882. (dmaf_continue(flags) && !dmaf_p_disabled_continue(flags))) {
  883. with_ext = 1;
  884. num_descs *= 2;
  885. } else
  886. with_ext = 0;
  887. /* completion writes from the raid engine may pass completion
  888. * writes from the legacy engine, so we need one extra null
  889. * (legacy) descriptor to ensure all completion writes arrive in
  890. * order.
  891. */
  892. if (likely(num_descs) &&
  893. ioat2_check_space_lock(ioat, num_descs + cb32) == 0)
  894. idx = ioat->head;
  895. else
  896. return NULL;
  897. i = 0;
  898. do {
  899. struct ioat_raw_descriptor *descs[2];
  900. size_t xfer_size = min_t(size_t, len, 1 << ioat->xfercap_log);
  901. desc = ioat2_get_ring_ent(ioat, idx + i);
  902. pq = desc->pq;
  903. /* save a branch by unconditionally retrieving the
  904. * extended descriptor pq_set_src() knows to not write
  905. * to it in the single descriptor case
  906. */
  907. ext = ioat2_get_ring_ent(ioat, idx + i + with_ext);
  908. pq_ex = ext->pq_ex;
  909. descs[0] = (struct ioat_raw_descriptor *) pq;
  910. descs[1] = (struct ioat_raw_descriptor *) pq_ex;
  911. for (s = 0; s < src_cnt; s++)
  912. pq_set_src(descs, src[s], offset, scf[s], s);
  913. /* see the comment for dma_maxpq in include/linux/dmaengine.h */
  914. if (dmaf_p_disabled_continue(flags))
  915. pq_set_src(descs, dst[1], offset, 1, s++);
  916. else if (dmaf_continue(flags)) {
  917. pq_set_src(descs, dst[0], offset, 0, s++);
  918. pq_set_src(descs, dst[1], offset, 1, s++);
  919. pq_set_src(descs, dst[1], offset, 0, s++);
  920. }
  921. pq->size = xfer_size;
  922. pq->p_addr = dst[0] + offset;
  923. pq->q_addr = dst[1] + offset;
  924. pq->ctl = 0;
  925. pq->ctl_f.op = op;
  926. /* we turn on descriptor write back error status */
  927. if (device->cap & IOAT_CAP_DWBES)
  928. pq->ctl_f.wb_en = result ? 1 : 0;
  929. pq->ctl_f.src_cnt = src_cnt_to_hw(s);
  930. pq->ctl_f.p_disable = !!(flags & DMA_PREP_PQ_DISABLE_P);
  931. pq->ctl_f.q_disable = !!(flags & DMA_PREP_PQ_DISABLE_Q);
  932. len -= xfer_size;
  933. offset += xfer_size;
  934. } while ((i += 1 + with_ext) < num_descs);
  935. /* last pq descriptor carries the unmap parameters and fence bit */
  936. desc->txd.flags = flags;
  937. desc->len = total_len;
  938. if (result)
  939. desc->result = result;
  940. pq->ctl_f.fence = !!(flags & DMA_PREP_FENCE);
  941. dump_pq_desc_dbg(ioat, desc, ext);
  942. if (!cb32) {
  943. pq->ctl_f.int_en = !!(flags & DMA_PREP_INTERRUPT);
  944. pq->ctl_f.compl_write = 1;
  945. compl_desc = desc;
  946. } else {
  947. /* completion descriptor carries interrupt bit */
  948. compl_desc = ioat2_get_ring_ent(ioat, idx + i);
  949. compl_desc->txd.flags = flags & DMA_PREP_INTERRUPT;
  950. hw = compl_desc->hw;
  951. hw->ctl = 0;
  952. hw->ctl_f.null = 1;
  953. hw->ctl_f.int_en = !!(flags & DMA_PREP_INTERRUPT);
  954. hw->ctl_f.compl_write = 1;
  955. hw->size = NULL_DESC_BUFFER_SIZE;
  956. dump_desc_dbg(ioat, compl_desc);
  957. }
  958. /* we leave the channel locked to ensure in order submission */
  959. return &compl_desc->txd;
  960. }
  961. static struct dma_async_tx_descriptor *
  962. __ioat3_prep_pq16_lock(struct dma_chan *c, enum sum_check_flags *result,
  963. const dma_addr_t *dst, const dma_addr_t *src,
  964. unsigned int src_cnt, const unsigned char *scf,
  965. size_t len, unsigned long flags)
  966. {
  967. struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
  968. struct ioat_chan_common *chan = &ioat->base;
  969. struct ioatdma_device *device = chan->device;
  970. struct ioat_ring_ent *desc;
  971. size_t total_len = len;
  972. struct ioat_pq_descriptor *pq;
  973. u32 offset = 0;
  974. u8 op;
  975. int i, s, idx, num_descs;
  976. /* this function only handles src_cnt 9 - 16 */
  977. BUG_ON(src_cnt < 9);
  978. /* this function is only called with 9-16 sources */
  979. op = result ? IOAT_OP_PQ_VAL_16S : IOAT_OP_PQ_16S;
  980. dev_dbg(to_dev(chan), "%s\n", __func__);
  981. num_descs = ioat2_xferlen_to_descs(ioat, len);
  982. /*
  983. * 16 source pq is only available on cb3.3 and has no completion
  984. * write hw bug.
  985. */
  986. if (num_descs && ioat2_check_space_lock(ioat, num_descs) == 0)
  987. idx = ioat->head;
  988. else
  989. return NULL;
  990. i = 0;
  991. do {
  992. struct ioat_raw_descriptor *descs[4];
  993. size_t xfer_size = min_t(size_t, len, 1 << ioat->xfercap_log);
  994. desc = ioat2_get_ring_ent(ioat, idx + i);
  995. pq = desc->pq;
  996. descs[0] = (struct ioat_raw_descriptor *) pq;
  997. desc->sed = ioat3_alloc_sed(device,
  998. sed_get_pq16_pool_idx(src_cnt));
  999. if (!desc->sed) {
  1000. dev_err(to_dev(chan),
  1001. "%s: no free sed entries\n", __func__);
  1002. return NULL;
  1003. }
  1004. pq->sed_addr = desc->sed->dma;
  1005. desc->sed->parent = desc;
  1006. descs[1] = (struct ioat_raw_descriptor *)desc->sed->hw;
  1007. descs[2] = (void *)descs[1] + 64;
  1008. for (s = 0; s < src_cnt; s++)
  1009. pq16_set_src(descs, src[s], offset, scf[s], s);
  1010. /* see the comment for dma_maxpq in include/linux/dmaengine.h */
  1011. if (dmaf_p_disabled_continue(flags))
  1012. pq16_set_src(descs, dst[1], offset, 1, s++);
  1013. else if (dmaf_continue(flags)) {
  1014. pq16_set_src(descs, dst[0], offset, 0, s++);
  1015. pq16_set_src(descs, dst[1], offset, 1, s++);
  1016. pq16_set_src(descs, dst[1], offset, 0, s++);
  1017. }
  1018. pq->size = xfer_size;
  1019. pq->p_addr = dst[0] + offset;
  1020. pq->q_addr = dst[1] + offset;
  1021. pq->ctl = 0;
  1022. pq->ctl_f.op = op;
  1023. pq->ctl_f.src_cnt = src16_cnt_to_hw(s);
  1024. /* we turn on descriptor write back error status */
  1025. if (device->cap & IOAT_CAP_DWBES)
  1026. pq->ctl_f.wb_en = result ? 1 : 0;
  1027. pq->ctl_f.p_disable = !!(flags & DMA_PREP_PQ_DISABLE_P);
  1028. pq->ctl_f.q_disable = !!(flags & DMA_PREP_PQ_DISABLE_Q);
  1029. len -= xfer_size;
  1030. offset += xfer_size;
  1031. } while (++i < num_descs);
  1032. /* last pq descriptor carries the unmap parameters and fence bit */
  1033. desc->txd.flags = flags;
  1034. desc->len = total_len;
  1035. if (result)
  1036. desc->result = result;
  1037. pq->ctl_f.fence = !!(flags & DMA_PREP_FENCE);
  1038. /* with cb3.3 we should be able to do completion w/o a null desc */
  1039. pq->ctl_f.int_en = !!(flags & DMA_PREP_INTERRUPT);
  1040. pq->ctl_f.compl_write = 1;
  1041. dump_pq16_desc_dbg(ioat, desc);
  1042. /* we leave the channel locked to ensure in order submission */
  1043. return &desc->txd;
  1044. }
  1045. static struct dma_async_tx_descriptor *
  1046. ioat3_prep_pq(struct dma_chan *chan, dma_addr_t *dst, dma_addr_t *src,
  1047. unsigned int src_cnt, const unsigned char *scf, size_t len,
  1048. unsigned long flags)
  1049. {
  1050. struct dma_device *dma = chan->device;
  1051. /* specify valid address for disabled result */
  1052. if (flags & DMA_PREP_PQ_DISABLE_P)
  1053. dst[0] = dst[1];
  1054. if (flags & DMA_PREP_PQ_DISABLE_Q)
  1055. dst[1] = dst[0];
  1056. /* handle the single source multiply case from the raid6
  1057. * recovery path
  1058. */
  1059. if ((flags & DMA_PREP_PQ_DISABLE_P) && src_cnt == 1) {
  1060. dma_addr_t single_source[2];
  1061. unsigned char single_source_coef[2];
  1062. BUG_ON(flags & DMA_PREP_PQ_DISABLE_Q);
  1063. single_source[0] = src[0];
  1064. single_source[1] = src[0];
  1065. single_source_coef[0] = scf[0];
  1066. single_source_coef[1] = 0;
  1067. return (src_cnt > 8) && (dma->max_pq > 8) ?
  1068. __ioat3_prep_pq16_lock(chan, NULL, dst, single_source,
  1069. 2, single_source_coef, len,
  1070. flags) :
  1071. __ioat3_prep_pq_lock(chan, NULL, dst, single_source, 2,
  1072. single_source_coef, len, flags);
  1073. } else {
  1074. return (src_cnt > 8) && (dma->max_pq > 8) ?
  1075. __ioat3_prep_pq16_lock(chan, NULL, dst, src, src_cnt,
  1076. scf, len, flags) :
  1077. __ioat3_prep_pq_lock(chan, NULL, dst, src, src_cnt,
  1078. scf, len, flags);
  1079. }
  1080. }
  1081. struct dma_async_tx_descriptor *
  1082. ioat3_prep_pq_val(struct dma_chan *chan, dma_addr_t *pq, dma_addr_t *src,
  1083. unsigned int src_cnt, const unsigned char *scf, size_t len,
  1084. enum sum_check_flags *pqres, unsigned long flags)
  1085. {
  1086. struct dma_device *dma = chan->device;
  1087. /* specify valid address for disabled result */
  1088. if (flags & DMA_PREP_PQ_DISABLE_P)
  1089. pq[0] = pq[1];
  1090. if (flags & DMA_PREP_PQ_DISABLE_Q)
  1091. pq[1] = pq[0];
  1092. /* the cleanup routine only sets bits on validate failure, it
  1093. * does not clear bits on validate success... so clear it here
  1094. */
  1095. *pqres = 0;
  1096. return (src_cnt > 8) && (dma->max_pq > 8) ?
  1097. __ioat3_prep_pq16_lock(chan, pqres, pq, src, src_cnt, scf, len,
  1098. flags) :
  1099. __ioat3_prep_pq_lock(chan, pqres, pq, src, src_cnt, scf, len,
  1100. flags);
  1101. }
  1102. static struct dma_async_tx_descriptor *
  1103. ioat3_prep_pqxor(struct dma_chan *chan, dma_addr_t dst, dma_addr_t *src,
  1104. unsigned int src_cnt, size_t len, unsigned long flags)
  1105. {
  1106. struct dma_device *dma = chan->device;
  1107. unsigned char scf[src_cnt];
  1108. dma_addr_t pq[2];
  1109. memset(scf, 0, src_cnt);
  1110. pq[0] = dst;
  1111. flags |= DMA_PREP_PQ_DISABLE_Q;
  1112. pq[1] = dst; /* specify valid address for disabled result */
  1113. return (src_cnt > 8) && (dma->max_pq > 8) ?
  1114. __ioat3_prep_pq16_lock(chan, NULL, pq, src, src_cnt, scf, len,
  1115. flags) :
  1116. __ioat3_prep_pq_lock(chan, NULL, pq, src, src_cnt, scf, len,
  1117. flags);
  1118. }
  1119. struct dma_async_tx_descriptor *
  1120. ioat3_prep_pqxor_val(struct dma_chan *chan, dma_addr_t *src,
  1121. unsigned int src_cnt, size_t len,
  1122. enum sum_check_flags *result, unsigned long flags)
  1123. {
  1124. struct dma_device *dma = chan->device;
  1125. unsigned char scf[src_cnt];
  1126. dma_addr_t pq[2];
  1127. /* the cleanup routine only sets bits on validate failure, it
  1128. * does not clear bits on validate success... so clear it here
  1129. */
  1130. *result = 0;
  1131. memset(scf, 0, src_cnt);
  1132. pq[0] = src[0];
  1133. flags |= DMA_PREP_PQ_DISABLE_Q;
  1134. pq[1] = pq[0]; /* specify valid address for disabled result */
  1135. return (src_cnt > 8) && (dma->max_pq > 8) ?
  1136. __ioat3_prep_pq16_lock(chan, result, pq, &src[1], src_cnt - 1,
  1137. scf, len, flags) :
  1138. __ioat3_prep_pq_lock(chan, result, pq, &src[1], src_cnt - 1,
  1139. scf, len, flags);
  1140. }
  1141. static struct dma_async_tx_descriptor *
  1142. ioat3_prep_interrupt_lock(struct dma_chan *c, unsigned long flags)
  1143. {
  1144. struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
  1145. struct ioat_ring_ent *desc;
  1146. struct ioat_dma_descriptor *hw;
  1147. if (ioat2_check_space_lock(ioat, 1) == 0)
  1148. desc = ioat2_get_ring_ent(ioat, ioat->head);
  1149. else
  1150. return NULL;
  1151. hw = desc->hw;
  1152. hw->ctl = 0;
  1153. hw->ctl_f.null = 1;
  1154. hw->ctl_f.int_en = 1;
  1155. hw->ctl_f.fence = !!(flags & DMA_PREP_FENCE);
  1156. hw->ctl_f.compl_write = 1;
  1157. hw->size = NULL_DESC_BUFFER_SIZE;
  1158. hw->src_addr = 0;
  1159. hw->dst_addr = 0;
  1160. desc->txd.flags = flags;
  1161. desc->len = 1;
  1162. dump_desc_dbg(ioat, desc);
  1163. /* we leave the channel locked to ensure in order submission */
  1164. return &desc->txd;
  1165. }
  1166. static void ioat3_dma_test_callback(void *dma_async_param)
  1167. {
  1168. struct completion *cmp = dma_async_param;
  1169. complete(cmp);
  1170. }
  1171. #define IOAT_NUM_SRC_TEST 6 /* must be <= 8 */
  1172. static int ioat_xor_val_self_test(struct ioatdma_device *device)
  1173. {
  1174. int i, src_idx;
  1175. struct page *dest;
  1176. struct page *xor_srcs[IOAT_NUM_SRC_TEST];
  1177. struct page *xor_val_srcs[IOAT_NUM_SRC_TEST + 1];
  1178. dma_addr_t dma_srcs[IOAT_NUM_SRC_TEST + 1];
  1179. dma_addr_t dest_dma;
  1180. struct dma_async_tx_descriptor *tx;
  1181. struct dma_chan *dma_chan;
  1182. dma_cookie_t cookie;
  1183. u8 cmp_byte = 0;
  1184. u32 cmp_word;
  1185. u32 xor_val_result;
  1186. int err = 0;
  1187. struct completion cmp;
  1188. unsigned long tmo;
  1189. struct device *dev = &device->pdev->dev;
  1190. struct dma_device *dma = &device->common;
  1191. u8 op = 0;
  1192. dev_dbg(dev, "%s\n", __func__);
  1193. if (!dma_has_cap(DMA_XOR, dma->cap_mask))
  1194. return 0;
  1195. for (src_idx = 0; src_idx < IOAT_NUM_SRC_TEST; src_idx++) {
  1196. xor_srcs[src_idx] = alloc_page(GFP_KERNEL);
  1197. if (!xor_srcs[src_idx]) {
  1198. while (src_idx--)
  1199. __free_page(xor_srcs[src_idx]);
  1200. return -ENOMEM;
  1201. }
  1202. }
  1203. dest = alloc_page(GFP_KERNEL);
  1204. if (!dest) {
  1205. while (src_idx--)
  1206. __free_page(xor_srcs[src_idx]);
  1207. return -ENOMEM;
  1208. }
  1209. /* Fill in src buffers */
  1210. for (src_idx = 0; src_idx < IOAT_NUM_SRC_TEST; src_idx++) {
  1211. u8 *ptr = page_address(xor_srcs[src_idx]);
  1212. for (i = 0; i < PAGE_SIZE; i++)
  1213. ptr[i] = (1 << src_idx);
  1214. }
  1215. for (src_idx = 0; src_idx < IOAT_NUM_SRC_TEST; src_idx++)
  1216. cmp_byte ^= (u8) (1 << src_idx);
  1217. cmp_word = (cmp_byte << 24) | (cmp_byte << 16) |
  1218. (cmp_byte << 8) | cmp_byte;
  1219. memset(page_address(dest), 0, PAGE_SIZE);
  1220. dma_chan = container_of(dma->channels.next, struct dma_chan,
  1221. device_node);
  1222. if (dma->device_alloc_chan_resources(dma_chan) < 1) {
  1223. err = -ENODEV;
  1224. goto out;
  1225. }
  1226. /* test xor */
  1227. op = IOAT_OP_XOR;
  1228. dest_dma = dma_map_page(dev, dest, 0, PAGE_SIZE, DMA_FROM_DEVICE);
  1229. for (i = 0; i < IOAT_NUM_SRC_TEST; i++)
  1230. dma_srcs[i] = dma_map_page(dev, xor_srcs[i], 0, PAGE_SIZE,
  1231. DMA_TO_DEVICE);
  1232. tx = dma->device_prep_dma_xor(dma_chan, dest_dma, dma_srcs,
  1233. IOAT_NUM_SRC_TEST, PAGE_SIZE,
  1234. DMA_PREP_INTERRUPT |
  1235. DMA_COMPL_SKIP_SRC_UNMAP |
  1236. DMA_COMPL_SKIP_DEST_UNMAP);
  1237. if (!tx) {
  1238. dev_err(dev, "Self-test xor prep failed\n");
  1239. err = -ENODEV;
  1240. goto dma_unmap;
  1241. }
  1242. async_tx_ack(tx);
  1243. init_completion(&cmp);
  1244. tx->callback = ioat3_dma_test_callback;
  1245. tx->callback_param = &cmp;
  1246. cookie = tx->tx_submit(tx);
  1247. if (cookie < 0) {
  1248. dev_err(dev, "Self-test xor setup failed\n");
  1249. err = -ENODEV;
  1250. goto dma_unmap;
  1251. }
  1252. dma->device_issue_pending(dma_chan);
  1253. tmo = wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000));
  1254. if (dma->device_tx_status(dma_chan, cookie, NULL) != DMA_SUCCESS) {
  1255. dev_err(dev, "Self-test xor timed out\n");
  1256. err = -ENODEV;
  1257. goto dma_unmap;
  1258. }
  1259. dma_unmap_page(dev, dest_dma, PAGE_SIZE, DMA_FROM_DEVICE);
  1260. for (i = 0; i < IOAT_NUM_SRC_TEST; i++)
  1261. dma_unmap_page(dev, dma_srcs[i], PAGE_SIZE, DMA_TO_DEVICE);
  1262. dma_sync_single_for_cpu(dev, dest_dma, PAGE_SIZE, DMA_FROM_DEVICE);
  1263. for (i = 0; i < (PAGE_SIZE / sizeof(u32)); i++) {
  1264. u32 *ptr = page_address(dest);
  1265. if (ptr[i] != cmp_word) {
  1266. dev_err(dev, "Self-test xor failed compare\n");
  1267. err = -ENODEV;
  1268. goto free_resources;
  1269. }
  1270. }
  1271. dma_sync_single_for_device(dev, dest_dma, PAGE_SIZE, DMA_FROM_DEVICE);
  1272. /* skip validate if the capability is not present */
  1273. if (!dma_has_cap(DMA_XOR_VAL, dma_chan->device->cap_mask))
  1274. goto free_resources;
  1275. op = IOAT_OP_XOR_VAL;
  1276. /* validate the sources with the destintation page */
  1277. for (i = 0; i < IOAT_NUM_SRC_TEST; i++)
  1278. xor_val_srcs[i] = xor_srcs[i];
  1279. xor_val_srcs[i] = dest;
  1280. xor_val_result = 1;
  1281. for (i = 0; i < IOAT_NUM_SRC_TEST + 1; i++)
  1282. dma_srcs[i] = dma_map_page(dev, xor_val_srcs[i], 0, PAGE_SIZE,
  1283. DMA_TO_DEVICE);
  1284. tx = dma->device_prep_dma_xor_val(dma_chan, dma_srcs,
  1285. IOAT_NUM_SRC_TEST + 1, PAGE_SIZE,
  1286. &xor_val_result, DMA_PREP_INTERRUPT |
  1287. DMA_COMPL_SKIP_SRC_UNMAP |
  1288. DMA_COMPL_SKIP_DEST_UNMAP);
  1289. if (!tx) {
  1290. dev_err(dev, "Self-test zero prep failed\n");
  1291. err = -ENODEV;
  1292. goto dma_unmap;
  1293. }
  1294. async_tx_ack(tx);
  1295. init_completion(&cmp);
  1296. tx->callback = ioat3_dma_test_callback;
  1297. tx->callback_param = &cmp;
  1298. cookie = tx->tx_submit(tx);
  1299. if (cookie < 0) {
  1300. dev_err(dev, "Self-test zero setup failed\n");
  1301. err = -ENODEV;
  1302. goto dma_unmap;
  1303. }
  1304. dma->device_issue_pending(dma_chan);
  1305. tmo = wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000));
  1306. if (dma->device_tx_status(dma_chan, cookie, NULL) != DMA_SUCCESS) {
  1307. dev_err(dev, "Self-test validate timed out\n");
  1308. err = -ENODEV;
  1309. goto dma_unmap;
  1310. }
  1311. for (i = 0; i < IOAT_NUM_SRC_TEST + 1; i++)
  1312. dma_unmap_page(dev, dma_srcs[i], PAGE_SIZE, DMA_TO_DEVICE);
  1313. if (xor_val_result != 0) {
  1314. dev_err(dev, "Self-test validate failed compare\n");
  1315. err = -ENODEV;
  1316. goto free_resources;
  1317. }
  1318. /* test for non-zero parity sum */
  1319. op = IOAT_OP_XOR_VAL;
  1320. xor_val_result = 0;
  1321. for (i = 0; i < IOAT_NUM_SRC_TEST + 1; i++)
  1322. dma_srcs[i] = dma_map_page(dev, xor_val_srcs[i], 0, PAGE_SIZE,
  1323. DMA_TO_DEVICE);
  1324. tx = dma->device_prep_dma_xor_val(dma_chan, dma_srcs,
  1325. IOAT_NUM_SRC_TEST + 1, PAGE_SIZE,
  1326. &xor_val_result, DMA_PREP_INTERRUPT |
  1327. DMA_COMPL_SKIP_SRC_UNMAP |
  1328. DMA_COMPL_SKIP_DEST_UNMAP);
  1329. if (!tx) {
  1330. dev_err(dev, "Self-test 2nd zero prep failed\n");
  1331. err = -ENODEV;
  1332. goto dma_unmap;
  1333. }
  1334. async_tx_ack(tx);
  1335. init_completion(&cmp);
  1336. tx->callback = ioat3_dma_test_callback;
  1337. tx->callback_param = &cmp;
  1338. cookie = tx->tx_submit(tx);
  1339. if (cookie < 0) {
  1340. dev_err(dev, "Self-test 2nd zero setup failed\n");
  1341. err = -ENODEV;
  1342. goto dma_unmap;
  1343. }
  1344. dma->device_issue_pending(dma_chan);
  1345. tmo = wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000));
  1346. if (dma->device_tx_status(dma_chan, cookie, NULL) != DMA_SUCCESS) {
  1347. dev_err(dev, "Self-test 2nd validate timed out\n");
  1348. err = -ENODEV;
  1349. goto dma_unmap;
  1350. }
  1351. if (xor_val_result != SUM_CHECK_P_RESULT) {
  1352. dev_err(dev, "Self-test validate failed compare\n");
  1353. err = -ENODEV;
  1354. goto dma_unmap;
  1355. }
  1356. for (i = 0; i < IOAT_NUM_SRC_TEST + 1; i++)
  1357. dma_unmap_page(dev, dma_srcs[i], PAGE_SIZE, DMA_TO_DEVICE);
  1358. goto free_resources;
  1359. dma_unmap:
  1360. if (op == IOAT_OP_XOR) {
  1361. dma_unmap_page(dev, dest_dma, PAGE_SIZE, DMA_FROM_DEVICE);
  1362. for (i = 0; i < IOAT_NUM_SRC_TEST; i++)
  1363. dma_unmap_page(dev, dma_srcs[i], PAGE_SIZE,
  1364. DMA_TO_DEVICE);
  1365. } else if (op == IOAT_OP_XOR_VAL) {
  1366. for (i = 0; i < IOAT_NUM_SRC_TEST + 1; i++)
  1367. dma_unmap_page(dev, dma_srcs[i], PAGE_SIZE,
  1368. DMA_TO_DEVICE);
  1369. }
  1370. free_resources:
  1371. dma->device_free_chan_resources(dma_chan);
  1372. out:
  1373. src_idx = IOAT_NUM_SRC_TEST;
  1374. while (src_idx--)
  1375. __free_page(xor_srcs[src_idx]);
  1376. __free_page(dest);
  1377. return err;
  1378. }
  1379. static int ioat3_dma_self_test(struct ioatdma_device *device)
  1380. {
  1381. int rc = ioat_dma_self_test(device);
  1382. if (rc)
  1383. return rc;
  1384. rc = ioat_xor_val_self_test(device);
  1385. if (rc)
  1386. return rc;
  1387. return 0;
  1388. }
  1389. static int ioat3_irq_reinit(struct ioatdma_device *device)
  1390. {
  1391. int msixcnt = device->common.chancnt;
  1392. struct pci_dev *pdev = device->pdev;
  1393. int i;
  1394. struct msix_entry *msix;
  1395. struct ioat_chan_common *chan;
  1396. int err = 0;
  1397. switch (device->irq_mode) {
  1398. case IOAT_MSIX:
  1399. for (i = 0; i < msixcnt; i++) {
  1400. msix = &device->msix_entries[i];
  1401. chan = ioat_chan_by_index(device, i);
  1402. devm_free_irq(&pdev->dev, msix->vector, chan);
  1403. }
  1404. pci_disable_msix(pdev);
  1405. break;
  1406. case IOAT_MSIX_SINGLE:
  1407. msix = &device->msix_entries[0];
  1408. chan = ioat_chan_by_index(device, 0);
  1409. devm_free_irq(&pdev->dev, msix->vector, chan);
  1410. pci_disable_msix(pdev);
  1411. break;
  1412. case IOAT_MSI:
  1413. chan = ioat_chan_by_index(device, 0);
  1414. devm_free_irq(&pdev->dev, pdev->irq, chan);
  1415. pci_disable_msi(pdev);
  1416. break;
  1417. case IOAT_INTX:
  1418. chan = ioat_chan_by_index(device, 0);
  1419. devm_free_irq(&pdev->dev, pdev->irq, chan);
  1420. break;
  1421. default:
  1422. return 0;
  1423. }
  1424. device->irq_mode = IOAT_NOIRQ;
  1425. err = ioat_dma_setup_interrupts(device);
  1426. return err;
  1427. }
  1428. static int ioat3_reset_hw(struct ioat_chan_common *chan)
  1429. {
  1430. /* throw away whatever the channel was doing and get it
  1431. * initialized, with ioat3 specific workarounds
  1432. */
  1433. struct ioatdma_device *device = chan->device;
  1434. struct pci_dev *pdev = device->pdev;
  1435. u32 chanerr;
  1436. u16 dev_id;
  1437. int err;
  1438. ioat2_quiesce(chan, msecs_to_jiffies(100));
  1439. chanerr = readl(chan->reg_base + IOAT_CHANERR_OFFSET);
  1440. writel(chanerr, chan->reg_base + IOAT_CHANERR_OFFSET);
  1441. if (device->version < IOAT_VER_3_3) {
  1442. /* clear any pending errors */
  1443. err = pci_read_config_dword(pdev,
  1444. IOAT_PCI_CHANERR_INT_OFFSET, &chanerr);
  1445. if (err) {
  1446. dev_err(&pdev->dev,
  1447. "channel error register unreachable\n");
  1448. return err;
  1449. }
  1450. pci_write_config_dword(pdev,
  1451. IOAT_PCI_CHANERR_INT_OFFSET, chanerr);
  1452. /* Clear DMAUNCERRSTS Cfg-Reg Parity Error status bit
  1453. * (workaround for spurious config parity error after restart)
  1454. */
  1455. pci_read_config_word(pdev, IOAT_PCI_DEVICE_ID_OFFSET, &dev_id);
  1456. if (dev_id == PCI_DEVICE_ID_INTEL_IOAT_TBG0) {
  1457. pci_write_config_dword(pdev,
  1458. IOAT_PCI_DMAUNCERRSTS_OFFSET,
  1459. 0x10);
  1460. }
  1461. }
  1462. err = ioat2_reset_sync(chan, msecs_to_jiffies(200));
  1463. if (err) {
  1464. dev_err(&pdev->dev, "Failed to reset!\n");
  1465. return err;
  1466. }
  1467. if (device->irq_mode != IOAT_NOIRQ && is_bwd_ioat(pdev))
  1468. err = ioat3_irq_reinit(device);
  1469. return err;
  1470. }
  1471. static void ioat3_intr_quirk(struct ioatdma_device *device)
  1472. {
  1473. struct dma_device *dma;
  1474. struct dma_chan *c;
  1475. struct ioat_chan_common *chan;
  1476. u32 errmask;
  1477. dma = &device->common;
  1478. /*
  1479. * if we have descriptor write back error status, we mask the
  1480. * error interrupts
  1481. */
  1482. if (device->cap & IOAT_CAP_DWBES) {
  1483. list_for_each_entry(c, &dma->channels, device_node) {
  1484. chan = to_chan_common(c);
  1485. errmask = readl(chan->reg_base +
  1486. IOAT_CHANERR_MASK_OFFSET);
  1487. errmask |= IOAT_CHANERR_XOR_P_OR_CRC_ERR |
  1488. IOAT_CHANERR_XOR_Q_ERR;
  1489. writel(errmask, chan->reg_base +
  1490. IOAT_CHANERR_MASK_OFFSET);
  1491. }
  1492. }
  1493. }
  1494. int ioat3_dma_probe(struct ioatdma_device *device, int dca)
  1495. {
  1496. struct pci_dev *pdev = device->pdev;
  1497. int dca_en = system_has_dca_enabled(pdev);
  1498. struct dma_device *dma;
  1499. struct dma_chan *c;
  1500. struct ioat_chan_common *chan;
  1501. bool is_raid_device = false;
  1502. int err;
  1503. device->enumerate_channels = ioat2_enumerate_channels;
  1504. device->reset_hw = ioat3_reset_hw;
  1505. device->self_test = ioat3_dma_self_test;
  1506. device->intr_quirk = ioat3_intr_quirk;
  1507. dma = &device->common;
  1508. dma->device_prep_dma_memcpy = ioat2_dma_prep_memcpy_lock;
  1509. dma->device_issue_pending = ioat2_issue_pending;
  1510. dma->device_alloc_chan_resources = ioat2_alloc_chan_resources;
  1511. dma->device_free_chan_resources = ioat2_free_chan_resources;
  1512. dma_cap_set(DMA_INTERRUPT, dma->cap_mask);
  1513. dma->device_prep_dma_interrupt = ioat3_prep_interrupt_lock;
  1514. device->cap = readl(device->reg_base + IOAT_DMA_CAP_OFFSET);
  1515. if (is_xeon_cb32(pdev) || is_bwd_noraid(pdev))
  1516. device->cap &= ~(IOAT_CAP_XOR | IOAT_CAP_PQ | IOAT_CAP_RAID16SS);
  1517. /* dca is incompatible with raid operations */
  1518. if (dca_en && (device->cap & (IOAT_CAP_XOR|IOAT_CAP_PQ)))
  1519. device->cap &= ~(IOAT_CAP_XOR|IOAT_CAP_PQ);
  1520. if (device->cap & IOAT_CAP_XOR) {
  1521. is_raid_device = true;
  1522. dma->max_xor = 8;
  1523. dma_cap_set(DMA_XOR, dma->cap_mask);
  1524. dma->device_prep_dma_xor = ioat3_prep_xor;
  1525. dma_cap_set(DMA_XOR_VAL, dma->cap_mask);
  1526. dma->device_prep_dma_xor_val = ioat3_prep_xor_val;
  1527. }
  1528. if (device->cap & IOAT_CAP_PQ) {
  1529. is_raid_device = true;
  1530. dma->device_prep_dma_pq = ioat3_prep_pq;
  1531. dma->device_prep_dma_pq_val = ioat3_prep_pq_val;
  1532. dma_cap_set(DMA_PQ, dma->cap_mask);
  1533. dma_cap_set(DMA_PQ_VAL, dma->cap_mask);
  1534. if (device->cap & IOAT_CAP_RAID16SS) {
  1535. dma_set_maxpq(dma, 16, 0);
  1536. } else {
  1537. dma_set_maxpq(dma, 8, 0);
  1538. }
  1539. if (!(device->cap & IOAT_CAP_XOR)) {
  1540. dma->device_prep_dma_xor = ioat3_prep_pqxor;
  1541. dma->device_prep_dma_xor_val = ioat3_prep_pqxor_val;
  1542. dma_cap_set(DMA_XOR, dma->cap_mask);
  1543. dma_cap_set(DMA_XOR_VAL, dma->cap_mask);
  1544. if (device->cap & IOAT_CAP_RAID16SS) {
  1545. dma->max_xor = 16;
  1546. } else {
  1547. dma->max_xor = 8;
  1548. }
  1549. }
  1550. }
  1551. dma->device_tx_status = ioat3_tx_status;
  1552. device->cleanup_fn = ioat3_cleanup_event;
  1553. device->timer_fn = ioat3_timer_event;
  1554. /* starting with CB3.3 super extended descriptors are supported */
  1555. if (device->cap & IOAT_CAP_RAID16SS) {
  1556. char pool_name[14];
  1557. int i;
  1558. /* allocate sw descriptor pool for SED */
  1559. device->sed_pool = kmem_cache_create("ioat_sed",
  1560. sizeof(struct ioat_sed_ent), 0, 0, NULL);
  1561. if (!device->sed_pool)
  1562. return -ENOMEM;
  1563. for (i = 0; i < MAX_SED_POOLS; i++) {
  1564. snprintf(pool_name, 14, "ioat_hw%d_sed", i);
  1565. /* allocate SED DMA pool */
  1566. device->sed_hw_pool[i] = dma_pool_create(pool_name,
  1567. &pdev->dev,
  1568. SED_SIZE * (i + 1), 64, 0);
  1569. if (!device->sed_hw_pool[i])
  1570. goto sed_pool_cleanup;
  1571. }
  1572. }
  1573. err = ioat_probe(device);
  1574. if (err)
  1575. return err;
  1576. ioat_set_tcp_copy_break(262144);
  1577. list_for_each_entry(c, &dma->channels, device_node) {
  1578. chan = to_chan_common(c);
  1579. writel(IOAT_DMA_DCA_ANY_CPU,
  1580. chan->reg_base + IOAT_DCACTRL_OFFSET);
  1581. }
  1582. err = ioat_register(device);
  1583. if (err)
  1584. return err;
  1585. ioat_kobject_add(device, &ioat2_ktype);
  1586. if (dca)
  1587. device->dca = ioat3_dca_init(pdev, device->reg_base);
  1588. return 0;
  1589. sed_pool_cleanup:
  1590. if (device->sed_pool) {
  1591. int i;
  1592. kmem_cache_destroy(device->sed_pool);
  1593. for (i = 0; i < MAX_SED_POOLS; i++)
  1594. if (device->sed_hw_pool[i])
  1595. dma_pool_destroy(device->sed_hw_pool[i]);
  1596. }
  1597. return -ENOMEM;
  1598. }
  1599. void ioat3_dma_remove(struct ioatdma_device *device)
  1600. {
  1601. if (device->sed_pool) {
  1602. int i;
  1603. kmem_cache_destroy(device->sed_pool);
  1604. for (i = 0; i < MAX_SED_POOLS; i++)
  1605. if (device->sed_hw_pool[i])
  1606. dma_pool_destroy(device->sed_hw_pool[i]);
  1607. }
  1608. }