init.c 38 KB

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