dma_v3.c 52 KB

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