init.c 38 KB

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