init.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435
  1. /*
  2. * Intel I/OAT DMA Linux driver
  3. * Copyright(c) 2004 - 2015 Intel Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * The full GNU General Public License is included in this distribution in
  15. * the file called "COPYING".
  16. *
  17. */
  18. #include <linux/init.h>
  19. #include <linux/module.h>
  20. #include <linux/slab.h>
  21. #include <linux/pci.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/dmaengine.h>
  24. #include <linux/delay.h>
  25. #include <linux/dma-mapping.h>
  26. #include <linux/workqueue.h>
  27. #include <linux/prefetch.h>
  28. #include <linux/dca.h>
  29. #include <linux/aer.h>
  30. #include <linux/sizes.h>
  31. #include "dma.h"
  32. #include "registers.h"
  33. #include "hw.h"
  34. #include "../dmaengine.h"
  35. MODULE_VERSION(IOAT_DMA_VERSION);
  36. MODULE_LICENSE("Dual BSD/GPL");
  37. MODULE_AUTHOR("Intel Corporation");
  38. static const struct pci_device_id ioat_pci_tbl[] = {
  39. /* I/OAT v3 platforms */
  40. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_TBG0) },
  41. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_TBG1) },
  42. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_TBG2) },
  43. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_TBG3) },
  44. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_TBG4) },
  45. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_TBG5) },
  46. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_TBG6) },
  47. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_TBG7) },
  48. /* I/OAT v3.2 platforms */
  49. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_JSF0) },
  50. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_JSF1) },
  51. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_JSF2) },
  52. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_JSF3) },
  53. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_JSF4) },
  54. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_JSF5) },
  55. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_JSF6) },
  56. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_JSF7) },
  57. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_JSF8) },
  58. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_JSF9) },
  59. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_SNB0) },
  60. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_SNB1) },
  61. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_SNB2) },
  62. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_SNB3) },
  63. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_SNB4) },
  64. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_SNB5) },
  65. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_SNB6) },
  66. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_SNB7) },
  67. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_SNB8) },
  68. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_SNB9) },
  69. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_IVB0) },
  70. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_IVB1) },
  71. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_IVB2) },
  72. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_IVB3) },
  73. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_IVB4) },
  74. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_IVB5) },
  75. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_IVB6) },
  76. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_IVB7) },
  77. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_IVB8) },
  78. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_IVB9) },
  79. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_HSW0) },
  80. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_HSW1) },
  81. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_HSW2) },
  82. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_HSW3) },
  83. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_HSW4) },
  84. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_HSW5) },
  85. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_HSW6) },
  86. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_HSW7) },
  87. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_HSW8) },
  88. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_HSW9) },
  89. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_BDX0) },
  90. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_BDX1) },
  91. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_BDX2) },
  92. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_BDX3) },
  93. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_BDX4) },
  94. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_BDX5) },
  95. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_BDX6) },
  96. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_BDX7) },
  97. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_BDX8) },
  98. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_BDX9) },
  99. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_SKX) },
  100. /* I/OAT v3.3 platforms */
  101. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_BWD0) },
  102. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_BWD1) },
  103. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_BWD2) },
  104. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_BWD3) },
  105. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_BDXDE0) },
  106. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_BDXDE1) },
  107. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_BDXDE2) },
  108. { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_IOAT_BDXDE3) },
  109. { 0, }
  110. };
  111. MODULE_DEVICE_TABLE(pci, ioat_pci_tbl);
  112. static int ioat_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id);
  113. static void ioat_remove(struct pci_dev *pdev);
  114. static void
  115. ioat_init_channel(struct ioatdma_device *ioat_dma,
  116. struct ioatdma_chan *ioat_chan, int idx);
  117. static void ioat_intr_quirk(struct ioatdma_device *ioat_dma);
  118. static void ioat_enumerate_channels(struct ioatdma_device *ioat_dma);
  119. static int ioat3_dma_self_test(struct ioatdma_device *ioat_dma);
  120. static int ioat_dca_enabled = 1;
  121. module_param(ioat_dca_enabled, int, 0644);
  122. MODULE_PARM_DESC(ioat_dca_enabled, "control support of dca service (default: 1)");
  123. int ioat_pending_level = 4;
  124. module_param(ioat_pending_level, int, 0644);
  125. MODULE_PARM_DESC(ioat_pending_level,
  126. "high-water mark for pushing ioat descriptors (default: 4)");
  127. static char ioat_interrupt_style[32] = "msix";
  128. module_param_string(ioat_interrupt_style, ioat_interrupt_style,
  129. sizeof(ioat_interrupt_style), 0644);
  130. MODULE_PARM_DESC(ioat_interrupt_style,
  131. "set ioat interrupt style: msix (default), msi, intx");
  132. struct kmem_cache *ioat_cache;
  133. struct kmem_cache *ioat_sed_cache;
  134. static bool is_jf_ioat(struct pci_dev *pdev)
  135. {
  136. switch (pdev->device) {
  137. case PCI_DEVICE_ID_INTEL_IOAT_JSF0:
  138. case PCI_DEVICE_ID_INTEL_IOAT_JSF1:
  139. case PCI_DEVICE_ID_INTEL_IOAT_JSF2:
  140. case PCI_DEVICE_ID_INTEL_IOAT_JSF3:
  141. case PCI_DEVICE_ID_INTEL_IOAT_JSF4:
  142. case PCI_DEVICE_ID_INTEL_IOAT_JSF5:
  143. case PCI_DEVICE_ID_INTEL_IOAT_JSF6:
  144. case PCI_DEVICE_ID_INTEL_IOAT_JSF7:
  145. case PCI_DEVICE_ID_INTEL_IOAT_JSF8:
  146. case PCI_DEVICE_ID_INTEL_IOAT_JSF9:
  147. return true;
  148. default:
  149. return false;
  150. }
  151. }
  152. static bool is_snb_ioat(struct pci_dev *pdev)
  153. {
  154. switch (pdev->device) {
  155. case PCI_DEVICE_ID_INTEL_IOAT_SNB0:
  156. case PCI_DEVICE_ID_INTEL_IOAT_SNB1:
  157. case PCI_DEVICE_ID_INTEL_IOAT_SNB2:
  158. case PCI_DEVICE_ID_INTEL_IOAT_SNB3:
  159. case PCI_DEVICE_ID_INTEL_IOAT_SNB4:
  160. case PCI_DEVICE_ID_INTEL_IOAT_SNB5:
  161. case PCI_DEVICE_ID_INTEL_IOAT_SNB6:
  162. case PCI_DEVICE_ID_INTEL_IOAT_SNB7:
  163. case PCI_DEVICE_ID_INTEL_IOAT_SNB8:
  164. case PCI_DEVICE_ID_INTEL_IOAT_SNB9:
  165. return true;
  166. default:
  167. return false;
  168. }
  169. }
  170. static bool is_ivb_ioat(struct pci_dev *pdev)
  171. {
  172. switch (pdev->device) {
  173. case PCI_DEVICE_ID_INTEL_IOAT_IVB0:
  174. case PCI_DEVICE_ID_INTEL_IOAT_IVB1:
  175. case PCI_DEVICE_ID_INTEL_IOAT_IVB2:
  176. case PCI_DEVICE_ID_INTEL_IOAT_IVB3:
  177. case PCI_DEVICE_ID_INTEL_IOAT_IVB4:
  178. case PCI_DEVICE_ID_INTEL_IOAT_IVB5:
  179. case PCI_DEVICE_ID_INTEL_IOAT_IVB6:
  180. case PCI_DEVICE_ID_INTEL_IOAT_IVB7:
  181. case PCI_DEVICE_ID_INTEL_IOAT_IVB8:
  182. case PCI_DEVICE_ID_INTEL_IOAT_IVB9:
  183. return true;
  184. default:
  185. return false;
  186. }
  187. }
  188. static bool is_hsw_ioat(struct pci_dev *pdev)
  189. {
  190. switch (pdev->device) {
  191. case PCI_DEVICE_ID_INTEL_IOAT_HSW0:
  192. case PCI_DEVICE_ID_INTEL_IOAT_HSW1:
  193. case PCI_DEVICE_ID_INTEL_IOAT_HSW2:
  194. case PCI_DEVICE_ID_INTEL_IOAT_HSW3:
  195. case PCI_DEVICE_ID_INTEL_IOAT_HSW4:
  196. case PCI_DEVICE_ID_INTEL_IOAT_HSW5:
  197. case PCI_DEVICE_ID_INTEL_IOAT_HSW6:
  198. case PCI_DEVICE_ID_INTEL_IOAT_HSW7:
  199. case PCI_DEVICE_ID_INTEL_IOAT_HSW8:
  200. case PCI_DEVICE_ID_INTEL_IOAT_HSW9:
  201. return true;
  202. default:
  203. return false;
  204. }
  205. }
  206. static bool is_bdx_ioat(struct pci_dev *pdev)
  207. {
  208. switch (pdev->device) {
  209. case PCI_DEVICE_ID_INTEL_IOAT_BDX0:
  210. case PCI_DEVICE_ID_INTEL_IOAT_BDX1:
  211. case PCI_DEVICE_ID_INTEL_IOAT_BDX2:
  212. case PCI_DEVICE_ID_INTEL_IOAT_BDX3:
  213. case PCI_DEVICE_ID_INTEL_IOAT_BDX4:
  214. case PCI_DEVICE_ID_INTEL_IOAT_BDX5:
  215. case PCI_DEVICE_ID_INTEL_IOAT_BDX6:
  216. case PCI_DEVICE_ID_INTEL_IOAT_BDX7:
  217. case PCI_DEVICE_ID_INTEL_IOAT_BDX8:
  218. case PCI_DEVICE_ID_INTEL_IOAT_BDX9:
  219. return true;
  220. default:
  221. return false;
  222. }
  223. }
  224. static inline bool is_skx_ioat(struct pci_dev *pdev)
  225. {
  226. return (pdev->device == PCI_DEVICE_ID_INTEL_IOAT_SKX) ? true : false;
  227. }
  228. static bool is_xeon_cb32(struct pci_dev *pdev)
  229. {
  230. return is_jf_ioat(pdev) || is_snb_ioat(pdev) || is_ivb_ioat(pdev) ||
  231. is_hsw_ioat(pdev) || is_bdx_ioat(pdev) || is_skx_ioat(pdev);
  232. }
  233. bool is_bwd_ioat(struct pci_dev *pdev)
  234. {
  235. switch (pdev->device) {
  236. case PCI_DEVICE_ID_INTEL_IOAT_BWD0:
  237. case PCI_DEVICE_ID_INTEL_IOAT_BWD1:
  238. case PCI_DEVICE_ID_INTEL_IOAT_BWD2:
  239. case PCI_DEVICE_ID_INTEL_IOAT_BWD3:
  240. /* even though not Atom, BDX-DE has same DMA silicon */
  241. case PCI_DEVICE_ID_INTEL_IOAT_BDXDE0:
  242. case PCI_DEVICE_ID_INTEL_IOAT_BDXDE1:
  243. case PCI_DEVICE_ID_INTEL_IOAT_BDXDE2:
  244. case PCI_DEVICE_ID_INTEL_IOAT_BDXDE3:
  245. return true;
  246. default:
  247. return false;
  248. }
  249. }
  250. static bool is_bwd_noraid(struct pci_dev *pdev)
  251. {
  252. switch (pdev->device) {
  253. case PCI_DEVICE_ID_INTEL_IOAT_BWD2:
  254. case PCI_DEVICE_ID_INTEL_IOAT_BWD3:
  255. case PCI_DEVICE_ID_INTEL_IOAT_BDXDE0:
  256. case PCI_DEVICE_ID_INTEL_IOAT_BDXDE1:
  257. case PCI_DEVICE_ID_INTEL_IOAT_BDXDE2:
  258. case PCI_DEVICE_ID_INTEL_IOAT_BDXDE3:
  259. return true;
  260. default:
  261. return false;
  262. }
  263. }
  264. /*
  265. * Perform a IOAT transaction to verify the HW works.
  266. */
  267. #define IOAT_TEST_SIZE 2000
  268. static void ioat_dma_test_callback(void *dma_async_param)
  269. {
  270. struct completion *cmp = dma_async_param;
  271. complete(cmp);
  272. }
  273. /**
  274. * ioat_dma_self_test - Perform a IOAT transaction to verify the HW works.
  275. * @ioat_dma: dma device to be tested
  276. */
  277. static int ioat_dma_self_test(struct ioatdma_device *ioat_dma)
  278. {
  279. int i;
  280. u8 *src;
  281. u8 *dest;
  282. struct dma_device *dma = &ioat_dma->dma_dev;
  283. struct device *dev = &ioat_dma->pdev->dev;
  284. struct dma_chan *dma_chan;
  285. struct dma_async_tx_descriptor *tx;
  286. dma_addr_t dma_dest, dma_src;
  287. dma_cookie_t cookie;
  288. int err = 0;
  289. struct completion cmp;
  290. unsigned long tmo;
  291. unsigned long flags;
  292. src = kzalloc(IOAT_TEST_SIZE, GFP_KERNEL);
  293. if (!src)
  294. return -ENOMEM;
  295. dest = kzalloc(IOAT_TEST_SIZE, GFP_KERNEL);
  296. if (!dest) {
  297. kfree(src);
  298. return -ENOMEM;
  299. }
  300. /* Fill in src buffer */
  301. for (i = 0; i < IOAT_TEST_SIZE; i++)
  302. src[i] = (u8)i;
  303. /* Start copy, using first DMA channel */
  304. dma_chan = container_of(dma->channels.next, struct dma_chan,
  305. device_node);
  306. if (dma->device_alloc_chan_resources(dma_chan) < 1) {
  307. dev_err(dev, "selftest cannot allocate chan resource\n");
  308. err = -ENODEV;
  309. goto out;
  310. }
  311. dma_src = dma_map_single(dev, src, IOAT_TEST_SIZE, DMA_TO_DEVICE);
  312. if (dma_mapping_error(dev, dma_src)) {
  313. dev_err(dev, "mapping src buffer failed\n");
  314. err = -ENOMEM;
  315. goto free_resources;
  316. }
  317. dma_dest = dma_map_single(dev, dest, IOAT_TEST_SIZE, DMA_FROM_DEVICE);
  318. if (dma_mapping_error(dev, dma_dest)) {
  319. dev_err(dev, "mapping dest buffer failed\n");
  320. err = -ENOMEM;
  321. goto unmap_src;
  322. }
  323. flags = DMA_PREP_INTERRUPT;
  324. tx = ioat_dma->dma_dev.device_prep_dma_memcpy(dma_chan, dma_dest,
  325. dma_src, IOAT_TEST_SIZE,
  326. flags);
  327. if (!tx) {
  328. dev_err(dev, "Self-test prep failed, disabling\n");
  329. err = -ENODEV;
  330. goto unmap_dma;
  331. }
  332. async_tx_ack(tx);
  333. init_completion(&cmp);
  334. tx->callback = ioat_dma_test_callback;
  335. tx->callback_param = &cmp;
  336. cookie = tx->tx_submit(tx);
  337. if (cookie < 0) {
  338. dev_err(dev, "Self-test setup failed, disabling\n");
  339. err = -ENODEV;
  340. goto unmap_dma;
  341. }
  342. dma->device_issue_pending(dma_chan);
  343. tmo = wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000));
  344. if (tmo == 0 ||
  345. dma->device_tx_status(dma_chan, cookie, NULL)
  346. != DMA_COMPLETE) {
  347. dev_err(dev, "Self-test copy timed out, disabling\n");
  348. err = -ENODEV;
  349. goto unmap_dma;
  350. }
  351. if (memcmp(src, dest, IOAT_TEST_SIZE)) {
  352. dev_err(dev, "Self-test copy failed compare, disabling\n");
  353. err = -ENODEV;
  354. goto unmap_dma;
  355. }
  356. unmap_dma:
  357. dma_unmap_single(dev, dma_dest, IOAT_TEST_SIZE, DMA_FROM_DEVICE);
  358. unmap_src:
  359. dma_unmap_single(dev, dma_src, IOAT_TEST_SIZE, DMA_TO_DEVICE);
  360. free_resources:
  361. dma->device_free_chan_resources(dma_chan);
  362. out:
  363. kfree(src);
  364. kfree(dest);
  365. return err;
  366. }
  367. /**
  368. * ioat_dma_setup_interrupts - setup interrupt handler
  369. * @ioat_dma: ioat dma device
  370. */
  371. int ioat_dma_setup_interrupts(struct ioatdma_device *ioat_dma)
  372. {
  373. struct ioatdma_chan *ioat_chan;
  374. struct pci_dev *pdev = ioat_dma->pdev;
  375. struct device *dev = &pdev->dev;
  376. struct msix_entry *msix;
  377. int i, j, msixcnt;
  378. int err = -EINVAL;
  379. u8 intrctrl = 0;
  380. if (!strcmp(ioat_interrupt_style, "msix"))
  381. goto msix;
  382. if (!strcmp(ioat_interrupt_style, "msi"))
  383. goto msi;
  384. if (!strcmp(ioat_interrupt_style, "intx"))
  385. goto intx;
  386. dev_err(dev, "invalid ioat_interrupt_style %s\n", ioat_interrupt_style);
  387. goto err_no_irq;
  388. msix:
  389. /* The number of MSI-X vectors should equal the number of channels */
  390. msixcnt = ioat_dma->dma_dev.chancnt;
  391. for (i = 0; i < msixcnt; i++)
  392. ioat_dma->msix_entries[i].entry = i;
  393. err = pci_enable_msix_exact(pdev, ioat_dma->msix_entries, msixcnt);
  394. if (err)
  395. goto msi;
  396. for (i = 0; i < msixcnt; i++) {
  397. msix = &ioat_dma->msix_entries[i];
  398. ioat_chan = ioat_chan_by_index(ioat_dma, i);
  399. err = devm_request_irq(dev, msix->vector,
  400. ioat_dma_do_interrupt_msix, 0,
  401. "ioat-msix", ioat_chan);
  402. if (err) {
  403. for (j = 0; j < i; j++) {
  404. msix = &ioat_dma->msix_entries[j];
  405. ioat_chan = ioat_chan_by_index(ioat_dma, j);
  406. devm_free_irq(dev, msix->vector, ioat_chan);
  407. }
  408. goto msi;
  409. }
  410. }
  411. intrctrl |= IOAT_INTRCTRL_MSIX_VECTOR_CONTROL;
  412. ioat_dma->irq_mode = IOAT_MSIX;
  413. goto done;
  414. msi:
  415. err = pci_enable_msi(pdev);
  416. if (err)
  417. goto intx;
  418. err = devm_request_irq(dev, pdev->irq, ioat_dma_do_interrupt, 0,
  419. "ioat-msi", ioat_dma);
  420. if (err) {
  421. pci_disable_msi(pdev);
  422. goto intx;
  423. }
  424. ioat_dma->irq_mode = IOAT_MSI;
  425. goto done;
  426. intx:
  427. err = devm_request_irq(dev, pdev->irq, ioat_dma_do_interrupt,
  428. IRQF_SHARED, "ioat-intx", ioat_dma);
  429. if (err)
  430. goto err_no_irq;
  431. ioat_dma->irq_mode = IOAT_INTX;
  432. done:
  433. if (is_bwd_ioat(pdev))
  434. ioat_intr_quirk(ioat_dma);
  435. intrctrl |= IOAT_INTRCTRL_MASTER_INT_EN;
  436. writeb(intrctrl, ioat_dma->reg_base + IOAT_INTRCTRL_OFFSET);
  437. return 0;
  438. err_no_irq:
  439. /* Disable all interrupt generation */
  440. writeb(0, ioat_dma->reg_base + IOAT_INTRCTRL_OFFSET);
  441. ioat_dma->irq_mode = IOAT_NOIRQ;
  442. dev_err(dev, "no usable interrupts\n");
  443. return err;
  444. }
  445. static void ioat_disable_interrupts(struct ioatdma_device *ioat_dma)
  446. {
  447. /* Disable all interrupt generation */
  448. writeb(0, ioat_dma->reg_base + IOAT_INTRCTRL_OFFSET);
  449. }
  450. static int ioat_probe(struct ioatdma_device *ioat_dma)
  451. {
  452. int err = -ENODEV;
  453. struct dma_device *dma = &ioat_dma->dma_dev;
  454. struct pci_dev *pdev = ioat_dma->pdev;
  455. struct device *dev = &pdev->dev;
  456. ioat_dma->completion_pool = dma_pool_create("completion_pool", dev,
  457. sizeof(u64),
  458. SMP_CACHE_BYTES,
  459. SMP_CACHE_BYTES);
  460. if (!ioat_dma->completion_pool) {
  461. err = -ENOMEM;
  462. goto err_out;
  463. }
  464. ioat_enumerate_channels(ioat_dma);
  465. dma_cap_set(DMA_MEMCPY, dma->cap_mask);
  466. dma->dev = &pdev->dev;
  467. if (!dma->chancnt) {
  468. dev_err(dev, "channel enumeration error\n");
  469. goto err_setup_interrupts;
  470. }
  471. err = ioat_dma_setup_interrupts(ioat_dma);
  472. if (err)
  473. goto err_setup_interrupts;
  474. err = ioat3_dma_self_test(ioat_dma);
  475. if (err)
  476. goto err_self_test;
  477. return 0;
  478. err_self_test:
  479. ioat_disable_interrupts(ioat_dma);
  480. err_setup_interrupts:
  481. dma_pool_destroy(ioat_dma->completion_pool);
  482. err_out:
  483. return err;
  484. }
  485. static int ioat_register(struct ioatdma_device *ioat_dma)
  486. {
  487. int err = dma_async_device_register(&ioat_dma->dma_dev);
  488. if (err) {
  489. ioat_disable_interrupts(ioat_dma);
  490. dma_pool_destroy(ioat_dma->completion_pool);
  491. }
  492. return err;
  493. }
  494. static void ioat_dma_remove(struct ioatdma_device *ioat_dma)
  495. {
  496. struct dma_device *dma = &ioat_dma->dma_dev;
  497. ioat_disable_interrupts(ioat_dma);
  498. ioat_kobject_del(ioat_dma);
  499. dma_async_device_unregister(dma);
  500. dma_pool_destroy(ioat_dma->completion_pool);
  501. INIT_LIST_HEAD(&dma->channels);
  502. }
  503. /**
  504. * ioat_enumerate_channels - find and initialize the device's channels
  505. * @ioat_dma: the ioat dma device to be enumerated
  506. */
  507. static void ioat_enumerate_channels(struct ioatdma_device *ioat_dma)
  508. {
  509. struct ioatdma_chan *ioat_chan;
  510. struct device *dev = &ioat_dma->pdev->dev;
  511. struct dma_device *dma = &ioat_dma->dma_dev;
  512. u8 xfercap_log;
  513. int i;
  514. INIT_LIST_HEAD(&dma->channels);
  515. dma->chancnt = readb(ioat_dma->reg_base + IOAT_CHANCNT_OFFSET);
  516. dma->chancnt &= 0x1f; /* bits [4:0] valid */
  517. if (dma->chancnt > ARRAY_SIZE(ioat_dma->idx)) {
  518. dev_warn(dev, "(%d) exceeds max supported channels (%zu)\n",
  519. dma->chancnt, ARRAY_SIZE(ioat_dma->idx));
  520. dma->chancnt = ARRAY_SIZE(ioat_dma->idx);
  521. }
  522. xfercap_log = readb(ioat_dma->reg_base + IOAT_XFERCAP_OFFSET);
  523. xfercap_log &= 0x1f; /* bits [4:0] valid */
  524. if (xfercap_log == 0)
  525. return;
  526. dev_dbg(dev, "%s: xfercap = %d\n", __func__, 1 << xfercap_log);
  527. for (i = 0; i < dma->chancnt; i++) {
  528. ioat_chan = devm_kzalloc(dev, sizeof(*ioat_chan), GFP_KERNEL);
  529. if (!ioat_chan)
  530. break;
  531. ioat_init_channel(ioat_dma, ioat_chan, i);
  532. ioat_chan->xfercap_log = xfercap_log;
  533. spin_lock_init(&ioat_chan->prep_lock);
  534. if (ioat_reset_hw(ioat_chan)) {
  535. i = 0;
  536. break;
  537. }
  538. }
  539. dma->chancnt = i;
  540. }
  541. /**
  542. * ioat_free_chan_resources - release all the descriptors
  543. * @chan: the channel to be cleaned
  544. */
  545. static void ioat_free_chan_resources(struct dma_chan *c)
  546. {
  547. struct ioatdma_chan *ioat_chan = to_ioat_chan(c);
  548. struct ioatdma_device *ioat_dma = ioat_chan->ioat_dma;
  549. struct ioat_ring_ent *desc;
  550. const int total_descs = 1 << ioat_chan->alloc_order;
  551. int descs;
  552. int i;
  553. /* Before freeing channel resources first check
  554. * if they have been previously allocated for this channel.
  555. */
  556. if (!ioat_chan->ring)
  557. return;
  558. ioat_stop(ioat_chan);
  559. ioat_reset_hw(ioat_chan);
  560. spin_lock_bh(&ioat_chan->cleanup_lock);
  561. spin_lock_bh(&ioat_chan->prep_lock);
  562. descs = ioat_ring_space(ioat_chan);
  563. dev_dbg(to_dev(ioat_chan), "freeing %d idle descriptors\n", descs);
  564. for (i = 0; i < descs; i++) {
  565. desc = ioat_get_ring_ent(ioat_chan, ioat_chan->head + i);
  566. ioat_free_ring_ent(desc, c);
  567. }
  568. if (descs < total_descs)
  569. dev_err(to_dev(ioat_chan), "Freeing %d in use descriptors!\n",
  570. total_descs - descs);
  571. for (i = 0; i < total_descs - descs; i++) {
  572. desc = ioat_get_ring_ent(ioat_chan, ioat_chan->tail + i);
  573. dump_desc_dbg(ioat_chan, desc);
  574. ioat_free_ring_ent(desc, c);
  575. }
  576. for (i = 0; i < ioat_chan->desc_chunks; i++) {
  577. dma_free_coherent(to_dev(ioat_chan), SZ_2M,
  578. ioat_chan->descs[i].virt,
  579. ioat_chan->descs[i].hw);
  580. ioat_chan->descs[i].virt = NULL;
  581. ioat_chan->descs[i].hw = 0;
  582. }
  583. ioat_chan->desc_chunks = 0;
  584. kfree(ioat_chan->ring);
  585. ioat_chan->ring = NULL;
  586. ioat_chan->alloc_order = 0;
  587. dma_pool_free(ioat_dma->completion_pool, ioat_chan->completion,
  588. ioat_chan->completion_dma);
  589. spin_unlock_bh(&ioat_chan->prep_lock);
  590. spin_unlock_bh(&ioat_chan->cleanup_lock);
  591. ioat_chan->last_completion = 0;
  592. ioat_chan->completion_dma = 0;
  593. ioat_chan->dmacount = 0;
  594. }
  595. /* ioat_alloc_chan_resources - allocate/initialize ioat descriptor ring
  596. * @chan: channel to be initialized
  597. */
  598. static int ioat_alloc_chan_resources(struct dma_chan *c)
  599. {
  600. struct ioatdma_chan *ioat_chan = to_ioat_chan(c);
  601. struct ioat_ring_ent **ring;
  602. u64 status;
  603. int order;
  604. int i = 0;
  605. u32 chanerr;
  606. /* have we already been set up? */
  607. if (ioat_chan->ring)
  608. return 1 << ioat_chan->alloc_order;
  609. /* Setup register to interrupt and write completion status on error */
  610. writew(IOAT_CHANCTRL_RUN, ioat_chan->reg_base + IOAT_CHANCTRL_OFFSET);
  611. /* allocate a completion writeback area */
  612. /* doing 2 32bit writes to mmio since 1 64b write doesn't work */
  613. ioat_chan->completion =
  614. dma_pool_zalloc(ioat_chan->ioat_dma->completion_pool,
  615. GFP_NOWAIT, &ioat_chan->completion_dma);
  616. if (!ioat_chan->completion)
  617. return -ENOMEM;
  618. writel(((u64)ioat_chan->completion_dma) & 0x00000000FFFFFFFF,
  619. ioat_chan->reg_base + IOAT_CHANCMP_OFFSET_LOW);
  620. writel(((u64)ioat_chan->completion_dma) >> 32,
  621. ioat_chan->reg_base + IOAT_CHANCMP_OFFSET_HIGH);
  622. order = IOAT_MAX_ORDER;
  623. ring = ioat_alloc_ring(c, order, GFP_NOWAIT);
  624. if (!ring)
  625. return -ENOMEM;
  626. spin_lock_bh(&ioat_chan->cleanup_lock);
  627. spin_lock_bh(&ioat_chan->prep_lock);
  628. ioat_chan->ring = ring;
  629. ioat_chan->head = 0;
  630. ioat_chan->issued = 0;
  631. ioat_chan->tail = 0;
  632. ioat_chan->alloc_order = order;
  633. set_bit(IOAT_RUN, &ioat_chan->state);
  634. spin_unlock_bh(&ioat_chan->prep_lock);
  635. spin_unlock_bh(&ioat_chan->cleanup_lock);
  636. ioat_start_null_desc(ioat_chan);
  637. /* check that we got off the ground */
  638. do {
  639. udelay(1);
  640. status = ioat_chansts(ioat_chan);
  641. } while (i++ < 20 && !is_ioat_active(status) && !is_ioat_idle(status));
  642. if (is_ioat_active(status) || is_ioat_idle(status))
  643. return 1 << ioat_chan->alloc_order;
  644. chanerr = readl(ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
  645. dev_WARN(to_dev(ioat_chan),
  646. "failed to start channel chanerr: %#x\n", chanerr);
  647. ioat_free_chan_resources(c);
  648. return -EFAULT;
  649. }
  650. /* common channel initialization */
  651. static void
  652. ioat_init_channel(struct ioatdma_device *ioat_dma,
  653. struct ioatdma_chan *ioat_chan, int idx)
  654. {
  655. struct dma_device *dma = &ioat_dma->dma_dev;
  656. struct dma_chan *c = &ioat_chan->dma_chan;
  657. unsigned long data = (unsigned long) c;
  658. ioat_chan->ioat_dma = ioat_dma;
  659. ioat_chan->reg_base = ioat_dma->reg_base + (0x80 * (idx + 1));
  660. spin_lock_init(&ioat_chan->cleanup_lock);
  661. ioat_chan->dma_chan.device = dma;
  662. dma_cookie_init(&ioat_chan->dma_chan);
  663. list_add_tail(&ioat_chan->dma_chan.device_node, &dma->channels);
  664. ioat_dma->idx[idx] = ioat_chan;
  665. timer_setup(&ioat_chan->timer, ioat_timer_event, 0);
  666. tasklet_init(&ioat_chan->cleanup_task, ioat_cleanup_event, data);
  667. }
  668. #define IOAT_NUM_SRC_TEST 6 /* must be <= 8 */
  669. static int ioat_xor_val_self_test(struct ioatdma_device *ioat_dma)
  670. {
  671. int i, src_idx;
  672. struct page *dest;
  673. struct page *xor_srcs[IOAT_NUM_SRC_TEST];
  674. struct page *xor_val_srcs[IOAT_NUM_SRC_TEST + 1];
  675. dma_addr_t dma_srcs[IOAT_NUM_SRC_TEST + 1];
  676. dma_addr_t dest_dma;
  677. struct dma_async_tx_descriptor *tx;
  678. struct dma_chan *dma_chan;
  679. dma_cookie_t cookie;
  680. u8 cmp_byte = 0;
  681. u32 cmp_word;
  682. u32 xor_val_result;
  683. int err = 0;
  684. struct completion cmp;
  685. unsigned long tmo;
  686. struct device *dev = &ioat_dma->pdev->dev;
  687. struct dma_device *dma = &ioat_dma->dma_dev;
  688. u8 op = 0;
  689. dev_dbg(dev, "%s\n", __func__);
  690. if (!dma_has_cap(DMA_XOR, dma->cap_mask))
  691. return 0;
  692. for (src_idx = 0; src_idx < IOAT_NUM_SRC_TEST; src_idx++) {
  693. xor_srcs[src_idx] = alloc_page(GFP_KERNEL);
  694. if (!xor_srcs[src_idx]) {
  695. while (src_idx--)
  696. __free_page(xor_srcs[src_idx]);
  697. return -ENOMEM;
  698. }
  699. }
  700. dest = alloc_page(GFP_KERNEL);
  701. if (!dest) {
  702. while (src_idx--)
  703. __free_page(xor_srcs[src_idx]);
  704. return -ENOMEM;
  705. }
  706. /* Fill in src buffers */
  707. for (src_idx = 0; src_idx < IOAT_NUM_SRC_TEST; src_idx++) {
  708. u8 *ptr = page_address(xor_srcs[src_idx]);
  709. for (i = 0; i < PAGE_SIZE; i++)
  710. ptr[i] = (1 << src_idx);
  711. }
  712. for (src_idx = 0; src_idx < IOAT_NUM_SRC_TEST; src_idx++)
  713. cmp_byte ^= (u8) (1 << src_idx);
  714. cmp_word = (cmp_byte << 24) | (cmp_byte << 16) |
  715. (cmp_byte << 8) | cmp_byte;
  716. memset(page_address(dest), 0, PAGE_SIZE);
  717. dma_chan = container_of(dma->channels.next, struct dma_chan,
  718. device_node);
  719. if (dma->device_alloc_chan_resources(dma_chan) < 1) {
  720. err = -ENODEV;
  721. goto out;
  722. }
  723. /* test xor */
  724. op = IOAT_OP_XOR;
  725. dest_dma = dma_map_page(dev, dest, 0, PAGE_SIZE, DMA_FROM_DEVICE);
  726. if (dma_mapping_error(dev, dest_dma)) {
  727. err = -ENOMEM;
  728. goto free_resources;
  729. }
  730. for (i = 0; i < IOAT_NUM_SRC_TEST; i++) {
  731. dma_srcs[i] = dma_map_page(dev, xor_srcs[i], 0, PAGE_SIZE,
  732. DMA_TO_DEVICE);
  733. if (dma_mapping_error(dev, dma_srcs[i])) {
  734. err = -ENOMEM;
  735. goto dma_unmap;
  736. }
  737. }
  738. tx = dma->device_prep_dma_xor(dma_chan, dest_dma, dma_srcs,
  739. IOAT_NUM_SRC_TEST, PAGE_SIZE,
  740. DMA_PREP_INTERRUPT);
  741. if (!tx) {
  742. dev_err(dev, "Self-test xor prep failed\n");
  743. err = -ENODEV;
  744. goto dma_unmap;
  745. }
  746. async_tx_ack(tx);
  747. init_completion(&cmp);
  748. tx->callback = ioat_dma_test_callback;
  749. tx->callback_param = &cmp;
  750. cookie = tx->tx_submit(tx);
  751. if (cookie < 0) {
  752. dev_err(dev, "Self-test xor setup failed\n");
  753. err = -ENODEV;
  754. goto dma_unmap;
  755. }
  756. dma->device_issue_pending(dma_chan);
  757. tmo = wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000));
  758. if (tmo == 0 ||
  759. dma->device_tx_status(dma_chan, cookie, NULL) != DMA_COMPLETE) {
  760. dev_err(dev, "Self-test xor timed out\n");
  761. err = -ENODEV;
  762. goto dma_unmap;
  763. }
  764. for (i = 0; i < IOAT_NUM_SRC_TEST; i++)
  765. dma_unmap_page(dev, dma_srcs[i], PAGE_SIZE, DMA_TO_DEVICE);
  766. dma_sync_single_for_cpu(dev, dest_dma, PAGE_SIZE, DMA_FROM_DEVICE);
  767. for (i = 0; i < (PAGE_SIZE / sizeof(u32)); i++) {
  768. u32 *ptr = page_address(dest);
  769. if (ptr[i] != cmp_word) {
  770. dev_err(dev, "Self-test xor failed compare\n");
  771. err = -ENODEV;
  772. goto free_resources;
  773. }
  774. }
  775. dma_sync_single_for_device(dev, dest_dma, PAGE_SIZE, DMA_FROM_DEVICE);
  776. dma_unmap_page(dev, dest_dma, PAGE_SIZE, DMA_FROM_DEVICE);
  777. /* skip validate if the capability is not present */
  778. if (!dma_has_cap(DMA_XOR_VAL, dma_chan->device->cap_mask))
  779. goto free_resources;
  780. op = IOAT_OP_XOR_VAL;
  781. /* validate the sources with the destintation page */
  782. for (i = 0; i < IOAT_NUM_SRC_TEST; i++)
  783. xor_val_srcs[i] = xor_srcs[i];
  784. xor_val_srcs[i] = dest;
  785. xor_val_result = 1;
  786. for (i = 0; i < IOAT_NUM_SRC_TEST + 1; i++) {
  787. dma_srcs[i] = dma_map_page(dev, xor_val_srcs[i], 0, PAGE_SIZE,
  788. DMA_TO_DEVICE);
  789. if (dma_mapping_error(dev, dma_srcs[i])) {
  790. err = -ENOMEM;
  791. goto dma_unmap;
  792. }
  793. }
  794. tx = dma->device_prep_dma_xor_val(dma_chan, dma_srcs,
  795. IOAT_NUM_SRC_TEST + 1, PAGE_SIZE,
  796. &xor_val_result, DMA_PREP_INTERRUPT);
  797. if (!tx) {
  798. dev_err(dev, "Self-test zero prep failed\n");
  799. err = -ENODEV;
  800. goto dma_unmap;
  801. }
  802. async_tx_ack(tx);
  803. init_completion(&cmp);
  804. tx->callback = ioat_dma_test_callback;
  805. tx->callback_param = &cmp;
  806. cookie = tx->tx_submit(tx);
  807. if (cookie < 0) {
  808. dev_err(dev, "Self-test zero setup failed\n");
  809. err = -ENODEV;
  810. goto dma_unmap;
  811. }
  812. dma->device_issue_pending(dma_chan);
  813. tmo = wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000));
  814. if (tmo == 0 ||
  815. dma->device_tx_status(dma_chan, cookie, NULL) != DMA_COMPLETE) {
  816. dev_err(dev, "Self-test validate timed out\n");
  817. err = -ENODEV;
  818. goto dma_unmap;
  819. }
  820. for (i = 0; i < IOAT_NUM_SRC_TEST + 1; i++)
  821. dma_unmap_page(dev, dma_srcs[i], PAGE_SIZE, DMA_TO_DEVICE);
  822. if (xor_val_result != 0) {
  823. dev_err(dev, "Self-test validate failed compare\n");
  824. err = -ENODEV;
  825. goto free_resources;
  826. }
  827. memset(page_address(dest), 0, PAGE_SIZE);
  828. /* test for non-zero parity sum */
  829. op = IOAT_OP_XOR_VAL;
  830. xor_val_result = 0;
  831. for (i = 0; i < IOAT_NUM_SRC_TEST + 1; i++) {
  832. dma_srcs[i] = dma_map_page(dev, xor_val_srcs[i], 0, PAGE_SIZE,
  833. DMA_TO_DEVICE);
  834. if (dma_mapping_error(dev, dma_srcs[i])) {
  835. err = -ENOMEM;
  836. goto dma_unmap;
  837. }
  838. }
  839. tx = dma->device_prep_dma_xor_val(dma_chan, dma_srcs,
  840. IOAT_NUM_SRC_TEST + 1, PAGE_SIZE,
  841. &xor_val_result, DMA_PREP_INTERRUPT);
  842. if (!tx) {
  843. dev_err(dev, "Self-test 2nd zero prep failed\n");
  844. err = -ENODEV;
  845. goto dma_unmap;
  846. }
  847. async_tx_ack(tx);
  848. init_completion(&cmp);
  849. tx->callback = ioat_dma_test_callback;
  850. tx->callback_param = &cmp;
  851. cookie = tx->tx_submit(tx);
  852. if (cookie < 0) {
  853. dev_err(dev, "Self-test 2nd zero setup failed\n");
  854. err = -ENODEV;
  855. goto dma_unmap;
  856. }
  857. dma->device_issue_pending(dma_chan);
  858. tmo = wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000));
  859. if (tmo == 0 ||
  860. dma->device_tx_status(dma_chan, cookie, NULL) != DMA_COMPLETE) {
  861. dev_err(dev, "Self-test 2nd validate timed out\n");
  862. err = -ENODEV;
  863. goto dma_unmap;
  864. }
  865. if (xor_val_result != SUM_CHECK_P_RESULT) {
  866. dev_err(dev, "Self-test validate failed compare\n");
  867. err = -ENODEV;
  868. goto dma_unmap;
  869. }
  870. for (i = 0; i < IOAT_NUM_SRC_TEST + 1; i++)
  871. dma_unmap_page(dev, dma_srcs[i], PAGE_SIZE, DMA_TO_DEVICE);
  872. goto free_resources;
  873. dma_unmap:
  874. if (op == IOAT_OP_XOR) {
  875. while (--i >= 0)
  876. dma_unmap_page(dev, dma_srcs[i], PAGE_SIZE,
  877. DMA_TO_DEVICE);
  878. dma_unmap_page(dev, dest_dma, PAGE_SIZE, DMA_FROM_DEVICE);
  879. } else if (op == IOAT_OP_XOR_VAL) {
  880. while (--i >= 0)
  881. dma_unmap_page(dev, dma_srcs[i], PAGE_SIZE,
  882. DMA_TO_DEVICE);
  883. }
  884. free_resources:
  885. dma->device_free_chan_resources(dma_chan);
  886. out:
  887. src_idx = IOAT_NUM_SRC_TEST;
  888. while (src_idx--)
  889. __free_page(xor_srcs[src_idx]);
  890. __free_page(dest);
  891. return err;
  892. }
  893. static int ioat3_dma_self_test(struct ioatdma_device *ioat_dma)
  894. {
  895. int rc;
  896. rc = ioat_dma_self_test(ioat_dma);
  897. if (rc)
  898. return rc;
  899. rc = ioat_xor_val_self_test(ioat_dma);
  900. return rc;
  901. }
  902. static void ioat_intr_quirk(struct ioatdma_device *ioat_dma)
  903. {
  904. struct dma_device *dma;
  905. struct dma_chan *c;
  906. struct ioatdma_chan *ioat_chan;
  907. u32 errmask;
  908. dma = &ioat_dma->dma_dev;
  909. /*
  910. * if we have descriptor write back error status, we mask the
  911. * error interrupts
  912. */
  913. if (ioat_dma->cap & IOAT_CAP_DWBES) {
  914. list_for_each_entry(c, &dma->channels, device_node) {
  915. ioat_chan = to_ioat_chan(c);
  916. errmask = readl(ioat_chan->reg_base +
  917. IOAT_CHANERR_MASK_OFFSET);
  918. errmask |= IOAT_CHANERR_XOR_P_OR_CRC_ERR |
  919. IOAT_CHANERR_XOR_Q_ERR;
  920. writel(errmask, ioat_chan->reg_base +
  921. IOAT_CHANERR_MASK_OFFSET);
  922. }
  923. }
  924. }
  925. static int ioat3_dma_probe(struct ioatdma_device *ioat_dma, int dca)
  926. {
  927. struct pci_dev *pdev = ioat_dma->pdev;
  928. int dca_en = system_has_dca_enabled(pdev);
  929. struct dma_device *dma;
  930. struct dma_chan *c;
  931. struct ioatdma_chan *ioat_chan;
  932. int err;
  933. u16 val16;
  934. dma = &ioat_dma->dma_dev;
  935. dma->device_prep_dma_memcpy = ioat_dma_prep_memcpy_lock;
  936. dma->device_issue_pending = ioat_issue_pending;
  937. dma->device_alloc_chan_resources = ioat_alloc_chan_resources;
  938. dma->device_free_chan_resources = ioat_free_chan_resources;
  939. dma_cap_set(DMA_INTERRUPT, dma->cap_mask);
  940. dma->device_prep_dma_interrupt = ioat_prep_interrupt_lock;
  941. ioat_dma->cap = readl(ioat_dma->reg_base + IOAT_DMA_CAP_OFFSET);
  942. if (is_xeon_cb32(pdev) || is_bwd_noraid(pdev))
  943. ioat_dma->cap &=
  944. ~(IOAT_CAP_XOR | IOAT_CAP_PQ | IOAT_CAP_RAID16SS);
  945. /* dca is incompatible with raid operations */
  946. if (dca_en && (ioat_dma->cap & (IOAT_CAP_XOR|IOAT_CAP_PQ)))
  947. ioat_dma->cap &= ~(IOAT_CAP_XOR|IOAT_CAP_PQ);
  948. if (ioat_dma->cap & IOAT_CAP_XOR) {
  949. dma->max_xor = 8;
  950. dma_cap_set(DMA_XOR, dma->cap_mask);
  951. dma->device_prep_dma_xor = ioat_prep_xor;
  952. dma_cap_set(DMA_XOR_VAL, dma->cap_mask);
  953. dma->device_prep_dma_xor_val = ioat_prep_xor_val;
  954. }
  955. if (ioat_dma->cap & IOAT_CAP_PQ) {
  956. dma->device_prep_dma_pq = ioat_prep_pq;
  957. dma->device_prep_dma_pq_val = ioat_prep_pq_val;
  958. dma_cap_set(DMA_PQ, dma->cap_mask);
  959. dma_cap_set(DMA_PQ_VAL, dma->cap_mask);
  960. if (ioat_dma->cap & IOAT_CAP_RAID16SS)
  961. dma_set_maxpq(dma, 16, 0);
  962. else
  963. dma_set_maxpq(dma, 8, 0);
  964. if (!(ioat_dma->cap & IOAT_CAP_XOR)) {
  965. dma->device_prep_dma_xor = ioat_prep_pqxor;
  966. dma->device_prep_dma_xor_val = ioat_prep_pqxor_val;
  967. dma_cap_set(DMA_XOR, dma->cap_mask);
  968. dma_cap_set(DMA_XOR_VAL, dma->cap_mask);
  969. if (ioat_dma->cap & IOAT_CAP_RAID16SS)
  970. dma->max_xor = 16;
  971. else
  972. dma->max_xor = 8;
  973. }
  974. }
  975. dma->device_tx_status = ioat_tx_status;
  976. /* starting with CB3.3 super extended descriptors are supported */
  977. if (ioat_dma->cap & IOAT_CAP_RAID16SS) {
  978. char pool_name[14];
  979. int i;
  980. for (i = 0; i < MAX_SED_POOLS; i++) {
  981. snprintf(pool_name, 14, "ioat_hw%d_sed", i);
  982. /* allocate SED DMA pool */
  983. ioat_dma->sed_hw_pool[i] = dmam_pool_create(pool_name,
  984. &pdev->dev,
  985. SED_SIZE * (i + 1), 64, 0);
  986. if (!ioat_dma->sed_hw_pool[i])
  987. return -ENOMEM;
  988. }
  989. }
  990. if (!(ioat_dma->cap & (IOAT_CAP_XOR | IOAT_CAP_PQ)))
  991. dma_cap_set(DMA_PRIVATE, dma->cap_mask);
  992. err = ioat_probe(ioat_dma);
  993. if (err)
  994. return err;
  995. list_for_each_entry(c, &dma->channels, device_node) {
  996. ioat_chan = to_ioat_chan(c);
  997. writel(IOAT_DMA_DCA_ANY_CPU,
  998. ioat_chan->reg_base + IOAT_DCACTRL_OFFSET);
  999. }
  1000. err = ioat_register(ioat_dma);
  1001. if (err)
  1002. return err;
  1003. ioat_kobject_add(ioat_dma, &ioat_ktype);
  1004. if (dca)
  1005. ioat_dma->dca = ioat_dca_init(pdev, ioat_dma->reg_base);
  1006. /* disable relaxed ordering */
  1007. err = pcie_capability_read_word(pdev, IOAT_DEVCTRL_OFFSET, &val16);
  1008. if (err)
  1009. return err;
  1010. /* clear relaxed ordering enable */
  1011. val16 &= ~IOAT_DEVCTRL_ROE;
  1012. err = pcie_capability_write_word(pdev, IOAT_DEVCTRL_OFFSET, val16);
  1013. if (err)
  1014. return err;
  1015. return 0;
  1016. }
  1017. static void ioat_shutdown(struct pci_dev *pdev)
  1018. {
  1019. struct ioatdma_device *ioat_dma = pci_get_drvdata(pdev);
  1020. struct ioatdma_chan *ioat_chan;
  1021. int i;
  1022. if (!ioat_dma)
  1023. return;
  1024. for (i = 0; i < IOAT_MAX_CHANS; i++) {
  1025. ioat_chan = ioat_dma->idx[i];
  1026. if (!ioat_chan)
  1027. continue;
  1028. spin_lock_bh(&ioat_chan->prep_lock);
  1029. set_bit(IOAT_CHAN_DOWN, &ioat_chan->state);
  1030. spin_unlock_bh(&ioat_chan->prep_lock);
  1031. /*
  1032. * Synchronization rule for del_timer_sync():
  1033. * - The caller must not hold locks which would prevent
  1034. * completion of the timer's handler.
  1035. * So prep_lock cannot be held before calling it.
  1036. */
  1037. del_timer_sync(&ioat_chan->timer);
  1038. /* this should quiesce then reset */
  1039. ioat_reset_hw(ioat_chan);
  1040. }
  1041. ioat_disable_interrupts(ioat_dma);
  1042. }
  1043. static void ioat_resume(struct ioatdma_device *ioat_dma)
  1044. {
  1045. struct ioatdma_chan *ioat_chan;
  1046. u32 chanerr;
  1047. int i;
  1048. for (i = 0; i < IOAT_MAX_CHANS; i++) {
  1049. ioat_chan = ioat_dma->idx[i];
  1050. if (!ioat_chan)
  1051. continue;
  1052. spin_lock_bh(&ioat_chan->prep_lock);
  1053. clear_bit(IOAT_CHAN_DOWN, &ioat_chan->state);
  1054. spin_unlock_bh(&ioat_chan->prep_lock);
  1055. chanerr = readl(ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
  1056. writel(chanerr, ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
  1057. /* no need to reset as shutdown already did that */
  1058. }
  1059. }
  1060. #define DRV_NAME "ioatdma"
  1061. static pci_ers_result_t ioat_pcie_error_detected(struct pci_dev *pdev,
  1062. enum pci_channel_state error)
  1063. {
  1064. dev_dbg(&pdev->dev, "%s: PCIe AER error %d\n", DRV_NAME, error);
  1065. /* quiesce and block I/O */
  1066. ioat_shutdown(pdev);
  1067. return PCI_ERS_RESULT_NEED_RESET;
  1068. }
  1069. static pci_ers_result_t ioat_pcie_error_slot_reset(struct pci_dev *pdev)
  1070. {
  1071. pci_ers_result_t result = PCI_ERS_RESULT_RECOVERED;
  1072. int err;
  1073. dev_dbg(&pdev->dev, "%s post reset handling\n", DRV_NAME);
  1074. if (pci_enable_device_mem(pdev) < 0) {
  1075. dev_err(&pdev->dev,
  1076. "Failed to enable PCIe device after reset.\n");
  1077. result = PCI_ERS_RESULT_DISCONNECT;
  1078. } else {
  1079. pci_set_master(pdev);
  1080. pci_restore_state(pdev);
  1081. pci_save_state(pdev);
  1082. pci_wake_from_d3(pdev, false);
  1083. }
  1084. err = pci_cleanup_aer_uncorrect_error_status(pdev);
  1085. if (err) {
  1086. dev_err(&pdev->dev,
  1087. "AER uncorrect error status clear failed: %#x\n", err);
  1088. }
  1089. return result;
  1090. }
  1091. static void ioat_pcie_error_resume(struct pci_dev *pdev)
  1092. {
  1093. struct ioatdma_device *ioat_dma = pci_get_drvdata(pdev);
  1094. dev_dbg(&pdev->dev, "%s: AER handling resuming\n", DRV_NAME);
  1095. /* initialize and bring everything back */
  1096. ioat_resume(ioat_dma);
  1097. }
  1098. static const struct pci_error_handlers ioat_err_handler = {
  1099. .error_detected = ioat_pcie_error_detected,
  1100. .slot_reset = ioat_pcie_error_slot_reset,
  1101. .resume = ioat_pcie_error_resume,
  1102. };
  1103. static struct pci_driver ioat_pci_driver = {
  1104. .name = DRV_NAME,
  1105. .id_table = ioat_pci_tbl,
  1106. .probe = ioat_pci_probe,
  1107. .remove = ioat_remove,
  1108. .shutdown = ioat_shutdown,
  1109. .err_handler = &ioat_err_handler,
  1110. };
  1111. static struct ioatdma_device *
  1112. alloc_ioatdma(struct pci_dev *pdev, void __iomem *iobase)
  1113. {
  1114. struct device *dev = &pdev->dev;
  1115. struct ioatdma_device *d = devm_kzalloc(dev, sizeof(*d), GFP_KERNEL);
  1116. if (!d)
  1117. return NULL;
  1118. d->pdev = pdev;
  1119. d->reg_base = iobase;
  1120. return d;
  1121. }
  1122. static int ioat_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  1123. {
  1124. void __iomem * const *iomap;
  1125. struct device *dev = &pdev->dev;
  1126. struct ioatdma_device *device;
  1127. int err;
  1128. err = pcim_enable_device(pdev);
  1129. if (err)
  1130. return err;
  1131. err = pcim_iomap_regions(pdev, 1 << IOAT_MMIO_BAR, DRV_NAME);
  1132. if (err)
  1133. return err;
  1134. iomap = pcim_iomap_table(pdev);
  1135. if (!iomap)
  1136. return -ENOMEM;
  1137. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
  1138. if (err)
  1139. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  1140. if (err)
  1141. return err;
  1142. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
  1143. if (err)
  1144. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  1145. if (err)
  1146. return err;
  1147. device = alloc_ioatdma(pdev, iomap[IOAT_MMIO_BAR]);
  1148. if (!device)
  1149. return -ENOMEM;
  1150. pci_set_master(pdev);
  1151. pci_set_drvdata(pdev, device);
  1152. device->version = readb(device->reg_base + IOAT_VER_OFFSET);
  1153. if (device->version >= IOAT_VER_3_0) {
  1154. if (is_skx_ioat(pdev))
  1155. device->version = IOAT_VER_3_2;
  1156. err = ioat3_dma_probe(device, ioat_dca_enabled);
  1157. if (device->version >= IOAT_VER_3_3)
  1158. pci_enable_pcie_error_reporting(pdev);
  1159. } else
  1160. return -ENODEV;
  1161. if (err) {
  1162. dev_err(dev, "Intel(R) I/OAT DMA Engine init failed\n");
  1163. pci_disable_pcie_error_reporting(pdev);
  1164. return -ENODEV;
  1165. }
  1166. return 0;
  1167. }
  1168. static void ioat_remove(struct pci_dev *pdev)
  1169. {
  1170. struct ioatdma_device *device = pci_get_drvdata(pdev);
  1171. if (!device)
  1172. return;
  1173. dev_err(&pdev->dev, "Removing dma and dca services\n");
  1174. if (device->dca) {
  1175. unregister_dca_provider(device->dca, &pdev->dev);
  1176. free_dca_provider(device->dca);
  1177. device->dca = NULL;
  1178. }
  1179. pci_disable_pcie_error_reporting(pdev);
  1180. ioat_dma_remove(device);
  1181. }
  1182. static int __init ioat_init_module(void)
  1183. {
  1184. int err = -ENOMEM;
  1185. pr_info("%s: Intel(R) QuickData Technology Driver %s\n",
  1186. DRV_NAME, IOAT_DMA_VERSION);
  1187. ioat_cache = kmem_cache_create("ioat", sizeof(struct ioat_ring_ent),
  1188. 0, SLAB_HWCACHE_ALIGN, NULL);
  1189. if (!ioat_cache)
  1190. return -ENOMEM;
  1191. ioat_sed_cache = KMEM_CACHE(ioat_sed_ent, 0);
  1192. if (!ioat_sed_cache)
  1193. goto err_ioat_cache;
  1194. err = pci_register_driver(&ioat_pci_driver);
  1195. if (err)
  1196. goto err_ioat3_cache;
  1197. return 0;
  1198. err_ioat3_cache:
  1199. kmem_cache_destroy(ioat_sed_cache);
  1200. err_ioat_cache:
  1201. kmem_cache_destroy(ioat_cache);
  1202. return err;
  1203. }
  1204. module_init(ioat_init_module);
  1205. static void __exit ioat_exit_module(void)
  1206. {
  1207. pci_unregister_driver(&ioat_pci_driver);
  1208. kmem_cache_destroy(ioat_cache);
  1209. }
  1210. module_exit(ioat_exit_module);