dma_v3.c 56 KB

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