init.c 38 KB

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