init.c 38 KB

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