init.c 35 KB

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