ioat_dma.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376
  1. /*
  2. * Intel I/OAT DMA Linux driver
  3. * Copyright(c) 2004 - 2007 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. * You should have received a copy of the GNU General Public License along with
  15. * this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  17. *
  18. * The full GNU General Public License is included in this distribution in
  19. * the file called "COPYING".
  20. *
  21. */
  22. /*
  23. * This driver supports an Intel I/OAT DMA engine, which does asynchronous
  24. * copy operations.
  25. */
  26. #include <linux/init.h>
  27. #include <linux/module.h>
  28. #include <linux/pci.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/dmaengine.h>
  31. #include <linux/delay.h>
  32. #include <linux/dma-mapping.h>
  33. #include "ioatdma.h"
  34. #include "ioatdma_registers.h"
  35. #include "ioatdma_hw.h"
  36. #define to_ioat_chan(chan) container_of(chan, struct ioat_dma_chan, common)
  37. #define to_ioatdma_device(dev) container_of(dev, struct ioatdma_device, common)
  38. #define to_ioat_desc(lh) container_of(lh, struct ioat_desc_sw, node)
  39. #define tx_to_ioat_desc(tx) container_of(tx, struct ioat_desc_sw, async_tx)
  40. static int ioat_pending_level = 4;
  41. module_param(ioat_pending_level, int, 0644);
  42. MODULE_PARM_DESC(ioat_pending_level,
  43. "high-water mark for pushing ioat descriptors (default: 4)");
  44. /* internal functions */
  45. static void ioat_dma_start_null_desc(struct ioat_dma_chan *ioat_chan);
  46. static void ioat_dma_memcpy_cleanup(struct ioat_dma_chan *ioat_chan);
  47. static struct ioat_desc_sw *
  48. ioat1_dma_get_next_descriptor(struct ioat_dma_chan *ioat_chan);
  49. static struct ioat_desc_sw *
  50. ioat2_dma_get_next_descriptor(struct ioat_dma_chan *ioat_chan);
  51. static inline struct ioat_dma_chan *ioat_lookup_chan_by_index(
  52. struct ioatdma_device *device,
  53. int index)
  54. {
  55. return device->idx[index];
  56. }
  57. /**
  58. * ioat_dma_do_interrupt - handler used for single vector interrupt mode
  59. * @irq: interrupt id
  60. * @data: interrupt data
  61. */
  62. static irqreturn_t ioat_dma_do_interrupt(int irq, void *data)
  63. {
  64. struct ioatdma_device *instance = data;
  65. struct ioat_dma_chan *ioat_chan;
  66. unsigned long attnstatus;
  67. int bit;
  68. u8 intrctrl;
  69. intrctrl = readb(instance->reg_base + IOAT_INTRCTRL_OFFSET);
  70. if (!(intrctrl & IOAT_INTRCTRL_MASTER_INT_EN))
  71. return IRQ_NONE;
  72. if (!(intrctrl & IOAT_INTRCTRL_INT_STATUS)) {
  73. writeb(intrctrl, instance->reg_base + IOAT_INTRCTRL_OFFSET);
  74. return IRQ_NONE;
  75. }
  76. attnstatus = readl(instance->reg_base + IOAT_ATTNSTATUS_OFFSET);
  77. for_each_bit(bit, &attnstatus, BITS_PER_LONG) {
  78. ioat_chan = ioat_lookup_chan_by_index(instance, bit);
  79. tasklet_schedule(&ioat_chan->cleanup_task);
  80. }
  81. writeb(intrctrl, instance->reg_base + IOAT_INTRCTRL_OFFSET);
  82. return IRQ_HANDLED;
  83. }
  84. /**
  85. * ioat_dma_do_interrupt_msix - handler used for vector-per-channel interrupt mode
  86. * @irq: interrupt id
  87. * @data: interrupt data
  88. */
  89. static irqreturn_t ioat_dma_do_interrupt_msix(int irq, void *data)
  90. {
  91. struct ioat_dma_chan *ioat_chan = data;
  92. tasklet_schedule(&ioat_chan->cleanup_task);
  93. return IRQ_HANDLED;
  94. }
  95. static void ioat_dma_cleanup_tasklet(unsigned long data);
  96. /**
  97. * ioat_dma_enumerate_channels - find and initialize the device's channels
  98. * @device: the device to be enumerated
  99. */
  100. static int ioat_dma_enumerate_channels(struct ioatdma_device *device)
  101. {
  102. u8 xfercap_scale;
  103. u32 xfercap;
  104. int i;
  105. struct ioat_dma_chan *ioat_chan;
  106. device->common.chancnt = readb(device->reg_base + IOAT_CHANCNT_OFFSET);
  107. xfercap_scale = readb(device->reg_base + IOAT_XFERCAP_OFFSET);
  108. xfercap = (xfercap_scale == 0 ? -1 : (1UL << xfercap_scale));
  109. for (i = 0; i < device->common.chancnt; i++) {
  110. ioat_chan = kzalloc(sizeof(*ioat_chan), GFP_KERNEL);
  111. if (!ioat_chan) {
  112. device->common.chancnt = i;
  113. break;
  114. }
  115. ioat_chan->device = device;
  116. ioat_chan->reg_base = device->reg_base + (0x80 * (i + 1));
  117. ioat_chan->xfercap = xfercap;
  118. ioat_chan->desccount = 0;
  119. if (ioat_chan->device->version != IOAT_VER_1_2) {
  120. writel(IOAT_DCACTRL_CMPL_WRITE_ENABLE
  121. | IOAT_DMA_DCA_ANY_CPU,
  122. ioat_chan->reg_base + IOAT_DCACTRL_OFFSET);
  123. }
  124. spin_lock_init(&ioat_chan->cleanup_lock);
  125. spin_lock_init(&ioat_chan->desc_lock);
  126. INIT_LIST_HEAD(&ioat_chan->free_desc);
  127. INIT_LIST_HEAD(&ioat_chan->used_desc);
  128. /* This should be made common somewhere in dmaengine.c */
  129. ioat_chan->common.device = &device->common;
  130. list_add_tail(&ioat_chan->common.device_node,
  131. &device->common.channels);
  132. device->idx[i] = ioat_chan;
  133. tasklet_init(&ioat_chan->cleanup_task,
  134. ioat_dma_cleanup_tasklet,
  135. (unsigned long) ioat_chan);
  136. tasklet_disable(&ioat_chan->cleanup_task);
  137. }
  138. return device->common.chancnt;
  139. }
  140. /**
  141. * ioat_dma_memcpy_issue_pending - push potentially unrecognized appended
  142. * descriptors to hw
  143. * @chan: DMA channel handle
  144. */
  145. static inline void __ioat1_dma_memcpy_issue_pending(
  146. struct ioat_dma_chan *ioat_chan)
  147. {
  148. ioat_chan->pending = 0;
  149. writeb(IOAT_CHANCMD_APPEND, ioat_chan->reg_base + IOAT1_CHANCMD_OFFSET);
  150. }
  151. static void ioat1_dma_memcpy_issue_pending(struct dma_chan *chan)
  152. {
  153. struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
  154. if (ioat_chan->pending != 0) {
  155. spin_lock_bh(&ioat_chan->desc_lock);
  156. __ioat1_dma_memcpy_issue_pending(ioat_chan);
  157. spin_unlock_bh(&ioat_chan->desc_lock);
  158. }
  159. }
  160. static inline void __ioat2_dma_memcpy_issue_pending(
  161. struct ioat_dma_chan *ioat_chan)
  162. {
  163. ioat_chan->pending = 0;
  164. writew(ioat_chan->dmacount,
  165. ioat_chan->reg_base + IOAT_CHAN_DMACOUNT_OFFSET);
  166. }
  167. static void ioat2_dma_memcpy_issue_pending(struct dma_chan *chan)
  168. {
  169. struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
  170. if (ioat_chan->pending != 0) {
  171. spin_lock_bh(&ioat_chan->desc_lock);
  172. __ioat2_dma_memcpy_issue_pending(ioat_chan);
  173. spin_unlock_bh(&ioat_chan->desc_lock);
  174. }
  175. }
  176. static dma_cookie_t ioat1_tx_submit(struct dma_async_tx_descriptor *tx)
  177. {
  178. struct ioat_dma_chan *ioat_chan = to_ioat_chan(tx->chan);
  179. struct ioat_desc_sw *first = tx_to_ioat_desc(tx);
  180. struct ioat_desc_sw *prev, *new;
  181. struct ioat_dma_descriptor *hw;
  182. dma_cookie_t cookie;
  183. LIST_HEAD(new_chain);
  184. u32 copy;
  185. size_t len;
  186. dma_addr_t src, dst;
  187. unsigned long orig_flags;
  188. unsigned int desc_count = 0;
  189. /* src and dest and len are stored in the initial descriptor */
  190. len = first->len;
  191. src = first->src;
  192. dst = first->dst;
  193. orig_flags = first->async_tx.flags;
  194. new = first;
  195. spin_lock_bh(&ioat_chan->desc_lock);
  196. prev = to_ioat_desc(ioat_chan->used_desc.prev);
  197. prefetch(prev->hw);
  198. do {
  199. copy = min_t(size_t, len, ioat_chan->xfercap);
  200. async_tx_ack(&new->async_tx);
  201. hw = new->hw;
  202. hw->size = copy;
  203. hw->ctl = 0;
  204. hw->src_addr = src;
  205. hw->dst_addr = dst;
  206. hw->next = 0;
  207. /* chain together the physical address list for the HW */
  208. wmb();
  209. prev->hw->next = (u64) new->async_tx.phys;
  210. len -= copy;
  211. dst += copy;
  212. src += copy;
  213. list_add_tail(&new->node, &new_chain);
  214. desc_count++;
  215. prev = new;
  216. } while (len && (new = ioat1_dma_get_next_descriptor(ioat_chan)));
  217. hw->ctl = IOAT_DMA_DESCRIPTOR_CTL_CP_STS;
  218. if (new->async_tx.callback) {
  219. hw->ctl |= IOAT_DMA_DESCRIPTOR_CTL_INT_GN;
  220. if (first != new) {
  221. /* move callback into to last desc */
  222. new->async_tx.callback = first->async_tx.callback;
  223. new->async_tx.callback_param
  224. = first->async_tx.callback_param;
  225. first->async_tx.callback = NULL;
  226. first->async_tx.callback_param = NULL;
  227. }
  228. }
  229. new->tx_cnt = desc_count;
  230. new->async_tx.flags = orig_flags; /* client is in control of this ack */
  231. /* store the original values for use in later cleanup */
  232. if (new != first) {
  233. new->src = first->src;
  234. new->dst = first->dst;
  235. new->len = first->len;
  236. }
  237. /* cookie incr and addition to used_list must be atomic */
  238. cookie = ioat_chan->common.cookie;
  239. cookie++;
  240. if (cookie < 0)
  241. cookie = 1;
  242. ioat_chan->common.cookie = new->async_tx.cookie = cookie;
  243. /* write address into NextDescriptor field of last desc in chain */
  244. to_ioat_desc(ioat_chan->used_desc.prev)->hw->next =
  245. first->async_tx.phys;
  246. __list_splice(&new_chain, ioat_chan->used_desc.prev);
  247. ioat_chan->dmacount += desc_count;
  248. ioat_chan->pending += desc_count;
  249. if (ioat_chan->pending >= ioat_pending_level)
  250. __ioat1_dma_memcpy_issue_pending(ioat_chan);
  251. spin_unlock_bh(&ioat_chan->desc_lock);
  252. return cookie;
  253. }
  254. static dma_cookie_t ioat2_tx_submit(struct dma_async_tx_descriptor *tx)
  255. {
  256. struct ioat_dma_chan *ioat_chan = to_ioat_chan(tx->chan);
  257. struct ioat_desc_sw *first = tx_to_ioat_desc(tx);
  258. struct ioat_desc_sw *new;
  259. struct ioat_dma_descriptor *hw;
  260. dma_cookie_t cookie;
  261. u32 copy;
  262. size_t len;
  263. dma_addr_t src, dst;
  264. unsigned long orig_flags;
  265. unsigned int desc_count = 0;
  266. /* src and dest and len are stored in the initial descriptor */
  267. len = first->len;
  268. src = first->src;
  269. dst = first->dst;
  270. orig_flags = first->async_tx.flags;
  271. new = first;
  272. /*
  273. * ioat_chan->desc_lock is still in force in version 2 path
  274. * it gets unlocked at end of this function
  275. */
  276. do {
  277. copy = min_t(size_t, len, ioat_chan->xfercap);
  278. async_tx_ack(&new->async_tx);
  279. hw = new->hw;
  280. hw->size = copy;
  281. hw->ctl = 0;
  282. hw->src_addr = src;
  283. hw->dst_addr = dst;
  284. len -= copy;
  285. dst += copy;
  286. src += copy;
  287. desc_count++;
  288. } while (len && (new = ioat2_dma_get_next_descriptor(ioat_chan)));
  289. hw->ctl = IOAT_DMA_DESCRIPTOR_CTL_CP_STS;
  290. if (new->async_tx.callback) {
  291. hw->ctl |= IOAT_DMA_DESCRIPTOR_CTL_INT_GN;
  292. if (first != new) {
  293. /* move callback into to last desc */
  294. new->async_tx.callback = first->async_tx.callback;
  295. new->async_tx.callback_param
  296. = first->async_tx.callback_param;
  297. first->async_tx.callback = NULL;
  298. first->async_tx.callback_param = NULL;
  299. }
  300. }
  301. new->tx_cnt = desc_count;
  302. new->async_tx.flags = orig_flags; /* client is in control of this ack */
  303. /* store the original values for use in later cleanup */
  304. if (new != first) {
  305. new->src = first->src;
  306. new->dst = first->dst;
  307. new->len = first->len;
  308. }
  309. /* cookie incr and addition to used_list must be atomic */
  310. cookie = ioat_chan->common.cookie;
  311. cookie++;
  312. if (cookie < 0)
  313. cookie = 1;
  314. ioat_chan->common.cookie = new->async_tx.cookie = cookie;
  315. ioat_chan->dmacount += desc_count;
  316. ioat_chan->pending += desc_count;
  317. if (ioat_chan->pending >= ioat_pending_level)
  318. __ioat2_dma_memcpy_issue_pending(ioat_chan);
  319. spin_unlock_bh(&ioat_chan->desc_lock);
  320. return cookie;
  321. }
  322. /**
  323. * ioat_dma_alloc_descriptor - allocate and return a sw and hw descriptor pair
  324. * @ioat_chan: the channel supplying the memory pool for the descriptors
  325. * @flags: allocation flags
  326. */
  327. static struct ioat_desc_sw *ioat_dma_alloc_descriptor(
  328. struct ioat_dma_chan *ioat_chan,
  329. gfp_t flags)
  330. {
  331. struct ioat_dma_descriptor *desc;
  332. struct ioat_desc_sw *desc_sw;
  333. struct ioatdma_device *ioatdma_device;
  334. dma_addr_t phys;
  335. ioatdma_device = to_ioatdma_device(ioat_chan->common.device);
  336. desc = pci_pool_alloc(ioatdma_device->dma_pool, flags, &phys);
  337. if (unlikely(!desc))
  338. return NULL;
  339. desc_sw = kzalloc(sizeof(*desc_sw), flags);
  340. if (unlikely(!desc_sw)) {
  341. pci_pool_free(ioatdma_device->dma_pool, desc, phys);
  342. return NULL;
  343. }
  344. memset(desc, 0, sizeof(*desc));
  345. dma_async_tx_descriptor_init(&desc_sw->async_tx, &ioat_chan->common);
  346. switch (ioat_chan->device->version) {
  347. case IOAT_VER_1_2:
  348. desc_sw->async_tx.tx_submit = ioat1_tx_submit;
  349. break;
  350. case IOAT_VER_2_0:
  351. desc_sw->async_tx.tx_submit = ioat2_tx_submit;
  352. break;
  353. }
  354. INIT_LIST_HEAD(&desc_sw->async_tx.tx_list);
  355. desc_sw->hw = desc;
  356. desc_sw->async_tx.phys = phys;
  357. return desc_sw;
  358. }
  359. static int ioat_initial_desc_count = 256;
  360. module_param(ioat_initial_desc_count, int, 0644);
  361. MODULE_PARM_DESC(ioat_initial_desc_count,
  362. "initial descriptors per channel (default: 256)");
  363. /**
  364. * ioat2_dma_massage_chan_desc - link the descriptors into a circle
  365. * @ioat_chan: the channel to be massaged
  366. */
  367. static void ioat2_dma_massage_chan_desc(struct ioat_dma_chan *ioat_chan)
  368. {
  369. struct ioat_desc_sw *desc, *_desc;
  370. /* setup used_desc */
  371. ioat_chan->used_desc.next = ioat_chan->free_desc.next;
  372. ioat_chan->used_desc.prev = NULL;
  373. /* pull free_desc out of the circle so that every node is a hw
  374. * descriptor, but leave it pointing to the list
  375. */
  376. ioat_chan->free_desc.prev->next = ioat_chan->free_desc.next;
  377. ioat_chan->free_desc.next->prev = ioat_chan->free_desc.prev;
  378. /* circle link the hw descriptors */
  379. desc = to_ioat_desc(ioat_chan->free_desc.next);
  380. desc->hw->next = to_ioat_desc(desc->node.next)->async_tx.phys;
  381. list_for_each_entry_safe(desc, _desc, ioat_chan->free_desc.next, node) {
  382. desc->hw->next = to_ioat_desc(desc->node.next)->async_tx.phys;
  383. }
  384. }
  385. /**
  386. * ioat_dma_alloc_chan_resources - returns the number of allocated descriptors
  387. * @chan: the channel to be filled out
  388. */
  389. static int ioat_dma_alloc_chan_resources(struct dma_chan *chan)
  390. {
  391. struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
  392. struct ioat_desc_sw *desc;
  393. u16 chanctrl;
  394. u32 chanerr;
  395. int i;
  396. LIST_HEAD(tmp_list);
  397. /* have we already been set up? */
  398. if (!list_empty(&ioat_chan->free_desc))
  399. return ioat_chan->desccount;
  400. /* Setup register to interrupt and write completion status on error */
  401. chanctrl = IOAT_CHANCTRL_ERR_INT_EN |
  402. IOAT_CHANCTRL_ANY_ERR_ABORT_EN |
  403. IOAT_CHANCTRL_ERR_COMPLETION_EN;
  404. writew(chanctrl, ioat_chan->reg_base + IOAT_CHANCTRL_OFFSET);
  405. chanerr = readl(ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
  406. if (chanerr) {
  407. dev_err(&ioat_chan->device->pdev->dev,
  408. "CHANERR = %x, clearing\n", chanerr);
  409. writel(chanerr, ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
  410. }
  411. /* Allocate descriptors */
  412. for (i = 0; i < ioat_initial_desc_count; i++) {
  413. desc = ioat_dma_alloc_descriptor(ioat_chan, GFP_KERNEL);
  414. if (!desc) {
  415. dev_err(&ioat_chan->device->pdev->dev,
  416. "Only %d initial descriptors\n", i);
  417. break;
  418. }
  419. list_add_tail(&desc->node, &tmp_list);
  420. }
  421. spin_lock_bh(&ioat_chan->desc_lock);
  422. ioat_chan->desccount = i;
  423. list_splice(&tmp_list, &ioat_chan->free_desc);
  424. if (ioat_chan->device->version != IOAT_VER_1_2)
  425. ioat2_dma_massage_chan_desc(ioat_chan);
  426. spin_unlock_bh(&ioat_chan->desc_lock);
  427. /* allocate a completion writeback area */
  428. /* doing 2 32bit writes to mmio since 1 64b write doesn't work */
  429. ioat_chan->completion_virt =
  430. pci_pool_alloc(ioat_chan->device->completion_pool,
  431. GFP_KERNEL,
  432. &ioat_chan->completion_addr);
  433. memset(ioat_chan->completion_virt, 0,
  434. sizeof(*ioat_chan->completion_virt));
  435. writel(((u64) ioat_chan->completion_addr) & 0x00000000FFFFFFFF,
  436. ioat_chan->reg_base + IOAT_CHANCMP_OFFSET_LOW);
  437. writel(((u64) ioat_chan->completion_addr) >> 32,
  438. ioat_chan->reg_base + IOAT_CHANCMP_OFFSET_HIGH);
  439. tasklet_enable(&ioat_chan->cleanup_task);
  440. ioat_dma_start_null_desc(ioat_chan); /* give chain to dma device */
  441. return ioat_chan->desccount;
  442. }
  443. /**
  444. * ioat_dma_free_chan_resources - release all the descriptors
  445. * @chan: the channel to be cleaned
  446. */
  447. static void ioat_dma_free_chan_resources(struct dma_chan *chan)
  448. {
  449. struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
  450. struct ioatdma_device *ioatdma_device = to_ioatdma_device(chan->device);
  451. struct ioat_desc_sw *desc, *_desc;
  452. int in_use_descs = 0;
  453. tasklet_disable(&ioat_chan->cleanup_task);
  454. ioat_dma_memcpy_cleanup(ioat_chan);
  455. /* Delay 100ms after reset to allow internal DMA logic to quiesce
  456. * before removing DMA descriptor resources.
  457. */
  458. writeb(IOAT_CHANCMD_RESET,
  459. ioat_chan->reg_base
  460. + IOAT_CHANCMD_OFFSET(ioat_chan->device->version));
  461. mdelay(100);
  462. spin_lock_bh(&ioat_chan->desc_lock);
  463. switch (ioat_chan->device->version) {
  464. case IOAT_VER_1_2:
  465. list_for_each_entry_safe(desc, _desc,
  466. &ioat_chan->used_desc, node) {
  467. in_use_descs++;
  468. list_del(&desc->node);
  469. pci_pool_free(ioatdma_device->dma_pool, desc->hw,
  470. desc->async_tx.phys);
  471. kfree(desc);
  472. }
  473. list_for_each_entry_safe(desc, _desc,
  474. &ioat_chan->free_desc, node) {
  475. list_del(&desc->node);
  476. pci_pool_free(ioatdma_device->dma_pool, desc->hw,
  477. desc->async_tx.phys);
  478. kfree(desc);
  479. }
  480. break;
  481. case IOAT_VER_2_0:
  482. list_for_each_entry_safe(desc, _desc,
  483. ioat_chan->free_desc.next, node) {
  484. list_del(&desc->node);
  485. pci_pool_free(ioatdma_device->dma_pool, desc->hw,
  486. desc->async_tx.phys);
  487. kfree(desc);
  488. }
  489. desc = to_ioat_desc(ioat_chan->free_desc.next);
  490. pci_pool_free(ioatdma_device->dma_pool, desc->hw,
  491. desc->async_tx.phys);
  492. kfree(desc);
  493. INIT_LIST_HEAD(&ioat_chan->free_desc);
  494. INIT_LIST_HEAD(&ioat_chan->used_desc);
  495. break;
  496. }
  497. spin_unlock_bh(&ioat_chan->desc_lock);
  498. pci_pool_free(ioatdma_device->completion_pool,
  499. ioat_chan->completion_virt,
  500. ioat_chan->completion_addr);
  501. /* one is ok since we left it on there on purpose */
  502. if (in_use_descs > 1)
  503. dev_err(&ioat_chan->device->pdev->dev,
  504. "Freeing %d in use descriptors!\n",
  505. in_use_descs - 1);
  506. ioat_chan->last_completion = ioat_chan->completion_addr = 0;
  507. ioat_chan->pending = 0;
  508. ioat_chan->dmacount = 0;
  509. }
  510. /**
  511. * ioat_dma_get_next_descriptor - return the next available descriptor
  512. * @ioat_chan: IOAT DMA channel handle
  513. *
  514. * Gets the next descriptor from the chain, and must be called with the
  515. * channel's desc_lock held. Allocates more descriptors if the channel
  516. * has run out.
  517. */
  518. static struct ioat_desc_sw *
  519. ioat1_dma_get_next_descriptor(struct ioat_dma_chan *ioat_chan)
  520. {
  521. struct ioat_desc_sw *new;
  522. if (!list_empty(&ioat_chan->free_desc)) {
  523. new = to_ioat_desc(ioat_chan->free_desc.next);
  524. list_del(&new->node);
  525. } else {
  526. /* try to get another desc */
  527. new = ioat_dma_alloc_descriptor(ioat_chan, GFP_ATOMIC);
  528. if (!new) {
  529. dev_err(&ioat_chan->device->pdev->dev,
  530. "alloc failed\n");
  531. return NULL;
  532. }
  533. }
  534. prefetch(new->hw);
  535. return new;
  536. }
  537. static struct ioat_desc_sw *
  538. ioat2_dma_get_next_descriptor(struct ioat_dma_chan *ioat_chan)
  539. {
  540. struct ioat_desc_sw *new;
  541. /*
  542. * used.prev points to where to start processing
  543. * used.next points to next free descriptor
  544. * if used.prev == NULL, there are none waiting to be processed
  545. * if used.next == used.prev.prev, there is only one free descriptor,
  546. * and we need to use it to as a noop descriptor before
  547. * linking in a new set of descriptors, since the device
  548. * has probably already read the pointer to it
  549. */
  550. if (ioat_chan->used_desc.prev &&
  551. ioat_chan->used_desc.next == ioat_chan->used_desc.prev->prev) {
  552. struct ioat_desc_sw *desc;
  553. struct ioat_desc_sw *noop_desc;
  554. int i;
  555. /* set up the noop descriptor */
  556. noop_desc = to_ioat_desc(ioat_chan->used_desc.next);
  557. noop_desc->hw->size = 0;
  558. noop_desc->hw->ctl = IOAT_DMA_DESCRIPTOR_NUL;
  559. noop_desc->hw->src_addr = 0;
  560. noop_desc->hw->dst_addr = 0;
  561. ioat_chan->used_desc.next = ioat_chan->used_desc.next->next;
  562. ioat_chan->pending++;
  563. ioat_chan->dmacount++;
  564. /* try to get a few more descriptors */
  565. for (i = 16; i; i--) {
  566. desc = ioat_dma_alloc_descriptor(ioat_chan, GFP_ATOMIC);
  567. if (!desc) {
  568. dev_err(&ioat_chan->device->pdev->dev,
  569. "alloc failed\n");
  570. break;
  571. }
  572. list_add_tail(&desc->node, ioat_chan->used_desc.next);
  573. desc->hw->next
  574. = to_ioat_desc(desc->node.next)->async_tx.phys;
  575. to_ioat_desc(desc->node.prev)->hw->next
  576. = desc->async_tx.phys;
  577. ioat_chan->desccount++;
  578. }
  579. ioat_chan->used_desc.next = noop_desc->node.next;
  580. }
  581. new = to_ioat_desc(ioat_chan->used_desc.next);
  582. prefetch(new);
  583. ioat_chan->used_desc.next = new->node.next;
  584. if (ioat_chan->used_desc.prev == NULL)
  585. ioat_chan->used_desc.prev = &new->node;
  586. prefetch(new->hw);
  587. return new;
  588. }
  589. static struct ioat_desc_sw *ioat_dma_get_next_descriptor(
  590. struct ioat_dma_chan *ioat_chan)
  591. {
  592. if (!ioat_chan)
  593. return NULL;
  594. switch (ioat_chan->device->version) {
  595. case IOAT_VER_1_2:
  596. return ioat1_dma_get_next_descriptor(ioat_chan);
  597. break;
  598. case IOAT_VER_2_0:
  599. return ioat2_dma_get_next_descriptor(ioat_chan);
  600. break;
  601. }
  602. return NULL;
  603. }
  604. static struct dma_async_tx_descriptor *ioat1_dma_prep_memcpy(
  605. struct dma_chan *chan,
  606. dma_addr_t dma_dest,
  607. dma_addr_t dma_src,
  608. size_t len,
  609. unsigned long flags)
  610. {
  611. struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
  612. struct ioat_desc_sw *new;
  613. spin_lock_bh(&ioat_chan->desc_lock);
  614. new = ioat_dma_get_next_descriptor(ioat_chan);
  615. spin_unlock_bh(&ioat_chan->desc_lock);
  616. if (new) {
  617. new->len = len;
  618. new->dst = dma_dest;
  619. new->src = dma_src;
  620. new->async_tx.flags = flags;
  621. return &new->async_tx;
  622. } else
  623. return NULL;
  624. }
  625. static struct dma_async_tx_descriptor *ioat2_dma_prep_memcpy(
  626. struct dma_chan *chan,
  627. dma_addr_t dma_dest,
  628. dma_addr_t dma_src,
  629. size_t len,
  630. unsigned long flags)
  631. {
  632. struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
  633. struct ioat_desc_sw *new;
  634. spin_lock_bh(&ioat_chan->desc_lock);
  635. new = ioat2_dma_get_next_descriptor(ioat_chan);
  636. /*
  637. * leave ioat_chan->desc_lock set in ioat 2 path
  638. * it will get unlocked at end of tx_submit
  639. */
  640. if (new) {
  641. new->len = len;
  642. new->dst = dma_dest;
  643. new->src = dma_src;
  644. new->async_tx.flags = flags;
  645. return &new->async_tx;
  646. } else
  647. return NULL;
  648. }
  649. static void ioat_dma_cleanup_tasklet(unsigned long data)
  650. {
  651. struct ioat_dma_chan *chan = (void *)data;
  652. ioat_dma_memcpy_cleanup(chan);
  653. writew(IOAT_CHANCTRL_INT_DISABLE,
  654. chan->reg_base + IOAT_CHANCTRL_OFFSET);
  655. }
  656. /**
  657. * ioat_dma_memcpy_cleanup - cleanup up finished descriptors
  658. * @chan: ioat channel to be cleaned up
  659. */
  660. static void ioat_dma_memcpy_cleanup(struct ioat_dma_chan *ioat_chan)
  661. {
  662. unsigned long phys_complete;
  663. struct ioat_desc_sw *desc, *_desc;
  664. dma_cookie_t cookie = 0;
  665. unsigned long desc_phys;
  666. struct ioat_desc_sw *latest_desc;
  667. prefetch(ioat_chan->completion_virt);
  668. if (!spin_trylock_bh(&ioat_chan->cleanup_lock))
  669. return;
  670. /* The completion writeback can happen at any time,
  671. so reads by the driver need to be atomic operations
  672. The descriptor physical addresses are limited to 32-bits
  673. when the CPU can only do a 32-bit mov */
  674. #if (BITS_PER_LONG == 64)
  675. phys_complete =
  676. ioat_chan->completion_virt->full
  677. & IOAT_CHANSTS_COMPLETED_DESCRIPTOR_ADDR;
  678. #else
  679. phys_complete =
  680. ioat_chan->completion_virt->low & IOAT_LOW_COMPLETION_MASK;
  681. #endif
  682. if ((ioat_chan->completion_virt->full
  683. & IOAT_CHANSTS_DMA_TRANSFER_STATUS) ==
  684. IOAT_CHANSTS_DMA_TRANSFER_STATUS_HALTED) {
  685. dev_err(&ioat_chan->device->pdev->dev,
  686. "Channel halted, chanerr = %x\n",
  687. readl(ioat_chan->reg_base + IOAT_CHANERR_OFFSET));
  688. /* TODO do something to salvage the situation */
  689. }
  690. if (phys_complete == ioat_chan->last_completion) {
  691. spin_unlock_bh(&ioat_chan->cleanup_lock);
  692. return;
  693. }
  694. cookie = 0;
  695. spin_lock_bh(&ioat_chan->desc_lock);
  696. switch (ioat_chan->device->version) {
  697. case IOAT_VER_1_2:
  698. list_for_each_entry_safe(desc, _desc,
  699. &ioat_chan->used_desc, node) {
  700. /*
  701. * Incoming DMA requests may use multiple descriptors,
  702. * due to exceeding xfercap, perhaps. If so, only the
  703. * last one will have a cookie, and require unmapping.
  704. */
  705. if (desc->async_tx.cookie) {
  706. cookie = desc->async_tx.cookie;
  707. /*
  708. * yes we are unmapping both _page and _single
  709. * alloc'd regions with unmap_page. Is this
  710. * *really* that bad?
  711. */
  712. pci_unmap_page(ioat_chan->device->pdev,
  713. pci_unmap_addr(desc, dst),
  714. pci_unmap_len(desc, len),
  715. PCI_DMA_FROMDEVICE);
  716. pci_unmap_page(ioat_chan->device->pdev,
  717. pci_unmap_addr(desc, src),
  718. pci_unmap_len(desc, len),
  719. PCI_DMA_TODEVICE);
  720. if (desc->async_tx.callback) {
  721. desc->async_tx.callback(desc->async_tx.callback_param);
  722. desc->async_tx.callback = NULL;
  723. }
  724. }
  725. if (desc->async_tx.phys != phys_complete) {
  726. /*
  727. * a completed entry, but not the last, so clean
  728. * up if the client is done with the descriptor
  729. */
  730. if (async_tx_test_ack(&desc->async_tx)) {
  731. list_del(&desc->node);
  732. list_add_tail(&desc->node,
  733. &ioat_chan->free_desc);
  734. } else
  735. desc->async_tx.cookie = 0;
  736. } else {
  737. /*
  738. * last used desc. Do not remove, so we can
  739. * append from it, but don't look at it next
  740. * time, either
  741. */
  742. desc->async_tx.cookie = 0;
  743. /* TODO check status bits? */
  744. break;
  745. }
  746. }
  747. break;
  748. case IOAT_VER_2_0:
  749. /* has some other thread has already cleaned up? */
  750. if (ioat_chan->used_desc.prev == NULL)
  751. break;
  752. /* work backwards to find latest finished desc */
  753. desc = to_ioat_desc(ioat_chan->used_desc.next);
  754. latest_desc = NULL;
  755. do {
  756. desc = to_ioat_desc(desc->node.prev);
  757. desc_phys = (unsigned long)desc->async_tx.phys
  758. & IOAT_CHANSTS_COMPLETED_DESCRIPTOR_ADDR;
  759. if (desc_phys == phys_complete) {
  760. latest_desc = desc;
  761. break;
  762. }
  763. } while (&desc->node != ioat_chan->used_desc.prev);
  764. if (latest_desc != NULL) {
  765. /* work forwards to clear finished descriptors */
  766. for (desc = to_ioat_desc(ioat_chan->used_desc.prev);
  767. &desc->node != latest_desc->node.next &&
  768. &desc->node != ioat_chan->used_desc.next;
  769. desc = to_ioat_desc(desc->node.next)) {
  770. if (desc->async_tx.cookie) {
  771. cookie = desc->async_tx.cookie;
  772. desc->async_tx.cookie = 0;
  773. pci_unmap_page(ioat_chan->device->pdev,
  774. pci_unmap_addr(desc, dst),
  775. pci_unmap_len(desc, len),
  776. PCI_DMA_FROMDEVICE);
  777. pci_unmap_page(ioat_chan->device->pdev,
  778. pci_unmap_addr(desc, src),
  779. pci_unmap_len(desc, len),
  780. PCI_DMA_TODEVICE);
  781. if (desc->async_tx.callback) {
  782. desc->async_tx.callback(desc->async_tx.callback_param);
  783. desc->async_tx.callback = NULL;
  784. }
  785. }
  786. }
  787. /* move used.prev up beyond those that are finished */
  788. if (&desc->node == ioat_chan->used_desc.next)
  789. ioat_chan->used_desc.prev = NULL;
  790. else
  791. ioat_chan->used_desc.prev = &desc->node;
  792. }
  793. break;
  794. }
  795. spin_unlock_bh(&ioat_chan->desc_lock);
  796. ioat_chan->last_completion = phys_complete;
  797. if (cookie != 0)
  798. ioat_chan->completed_cookie = cookie;
  799. spin_unlock_bh(&ioat_chan->cleanup_lock);
  800. }
  801. /**
  802. * ioat_dma_is_complete - poll the status of a IOAT DMA transaction
  803. * @chan: IOAT DMA channel handle
  804. * @cookie: DMA transaction identifier
  805. * @done: if not %NULL, updated with last completed transaction
  806. * @used: if not %NULL, updated with last used transaction
  807. */
  808. static enum dma_status ioat_dma_is_complete(struct dma_chan *chan,
  809. dma_cookie_t cookie,
  810. dma_cookie_t *done,
  811. dma_cookie_t *used)
  812. {
  813. struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
  814. dma_cookie_t last_used;
  815. dma_cookie_t last_complete;
  816. enum dma_status ret;
  817. last_used = chan->cookie;
  818. last_complete = ioat_chan->completed_cookie;
  819. if (done)
  820. *done = last_complete;
  821. if (used)
  822. *used = last_used;
  823. ret = dma_async_is_complete(cookie, last_complete, last_used);
  824. if (ret == DMA_SUCCESS)
  825. return ret;
  826. ioat_dma_memcpy_cleanup(ioat_chan);
  827. last_used = chan->cookie;
  828. last_complete = ioat_chan->completed_cookie;
  829. if (done)
  830. *done = last_complete;
  831. if (used)
  832. *used = last_used;
  833. return dma_async_is_complete(cookie, last_complete, last_used);
  834. }
  835. static void ioat_dma_start_null_desc(struct ioat_dma_chan *ioat_chan)
  836. {
  837. struct ioat_desc_sw *desc;
  838. spin_lock_bh(&ioat_chan->desc_lock);
  839. desc = ioat_dma_get_next_descriptor(ioat_chan);
  840. desc->hw->ctl = IOAT_DMA_DESCRIPTOR_NUL
  841. | IOAT_DMA_DESCRIPTOR_CTL_INT_GN
  842. | IOAT_DMA_DESCRIPTOR_CTL_CP_STS;
  843. desc->hw->size = 0;
  844. desc->hw->src_addr = 0;
  845. desc->hw->dst_addr = 0;
  846. async_tx_ack(&desc->async_tx);
  847. switch (ioat_chan->device->version) {
  848. case IOAT_VER_1_2:
  849. desc->hw->next = 0;
  850. list_add_tail(&desc->node, &ioat_chan->used_desc);
  851. writel(((u64) desc->async_tx.phys) & 0x00000000FFFFFFFF,
  852. ioat_chan->reg_base + IOAT1_CHAINADDR_OFFSET_LOW);
  853. writel(((u64) desc->async_tx.phys) >> 32,
  854. ioat_chan->reg_base + IOAT1_CHAINADDR_OFFSET_HIGH);
  855. writeb(IOAT_CHANCMD_START, ioat_chan->reg_base
  856. + IOAT_CHANCMD_OFFSET(ioat_chan->device->version));
  857. break;
  858. case IOAT_VER_2_0:
  859. writel(((u64) desc->async_tx.phys) & 0x00000000FFFFFFFF,
  860. ioat_chan->reg_base + IOAT2_CHAINADDR_OFFSET_LOW);
  861. writel(((u64) desc->async_tx.phys) >> 32,
  862. ioat_chan->reg_base + IOAT2_CHAINADDR_OFFSET_HIGH);
  863. ioat_chan->dmacount++;
  864. __ioat2_dma_memcpy_issue_pending(ioat_chan);
  865. break;
  866. }
  867. spin_unlock_bh(&ioat_chan->desc_lock);
  868. }
  869. /*
  870. * Perform a IOAT transaction to verify the HW works.
  871. */
  872. #define IOAT_TEST_SIZE 2000
  873. static void ioat_dma_test_callback(void *dma_async_param)
  874. {
  875. printk(KERN_ERR "ioatdma: ioat_dma_test_callback(%p)\n",
  876. dma_async_param);
  877. }
  878. /**
  879. * ioat_dma_self_test - Perform a IOAT transaction to verify the HW works.
  880. * @device: device to be tested
  881. */
  882. static int ioat_dma_self_test(struct ioatdma_device *device)
  883. {
  884. int i;
  885. u8 *src;
  886. u8 *dest;
  887. struct dma_chan *dma_chan;
  888. struct dma_async_tx_descriptor *tx;
  889. dma_addr_t dma_dest, dma_src;
  890. dma_cookie_t cookie;
  891. int err = 0;
  892. src = kzalloc(sizeof(u8) * IOAT_TEST_SIZE, GFP_KERNEL);
  893. if (!src)
  894. return -ENOMEM;
  895. dest = kzalloc(sizeof(u8) * IOAT_TEST_SIZE, GFP_KERNEL);
  896. if (!dest) {
  897. kfree(src);
  898. return -ENOMEM;
  899. }
  900. /* Fill in src buffer */
  901. for (i = 0; i < IOAT_TEST_SIZE; i++)
  902. src[i] = (u8)i;
  903. /* Start copy, using first DMA channel */
  904. dma_chan = container_of(device->common.channels.next,
  905. struct dma_chan,
  906. device_node);
  907. if (device->common.device_alloc_chan_resources(dma_chan) < 1) {
  908. dev_err(&device->pdev->dev,
  909. "selftest cannot allocate chan resource\n");
  910. err = -ENODEV;
  911. goto out;
  912. }
  913. dma_src = dma_map_single(dma_chan->device->dev, src, IOAT_TEST_SIZE,
  914. DMA_TO_DEVICE);
  915. dma_dest = dma_map_single(dma_chan->device->dev, dest, IOAT_TEST_SIZE,
  916. DMA_FROM_DEVICE);
  917. tx = device->common.device_prep_dma_memcpy(dma_chan, dma_dest, dma_src,
  918. IOAT_TEST_SIZE, 0);
  919. if (!tx) {
  920. dev_err(&device->pdev->dev,
  921. "Self-test prep failed, disabling\n");
  922. err = -ENODEV;
  923. goto free_resources;
  924. }
  925. async_tx_ack(tx);
  926. tx->callback = ioat_dma_test_callback;
  927. tx->callback_param = (void *)0x8086;
  928. cookie = tx->tx_submit(tx);
  929. if (cookie < 0) {
  930. dev_err(&device->pdev->dev,
  931. "Self-test setup failed, disabling\n");
  932. err = -ENODEV;
  933. goto free_resources;
  934. }
  935. device->common.device_issue_pending(dma_chan);
  936. msleep(1);
  937. if (device->common.device_is_tx_complete(dma_chan, cookie, NULL, NULL)
  938. != DMA_SUCCESS) {
  939. dev_err(&device->pdev->dev,
  940. "Self-test copy timed out, disabling\n");
  941. err = -ENODEV;
  942. goto free_resources;
  943. }
  944. if (memcmp(src, dest, IOAT_TEST_SIZE)) {
  945. dev_err(&device->pdev->dev,
  946. "Self-test copy failed compare, disabling\n");
  947. err = -ENODEV;
  948. goto free_resources;
  949. }
  950. free_resources:
  951. device->common.device_free_chan_resources(dma_chan);
  952. out:
  953. kfree(src);
  954. kfree(dest);
  955. return err;
  956. }
  957. static char ioat_interrupt_style[32] = "msix";
  958. module_param_string(ioat_interrupt_style, ioat_interrupt_style,
  959. sizeof(ioat_interrupt_style), 0644);
  960. MODULE_PARM_DESC(ioat_interrupt_style,
  961. "set ioat interrupt style: msix (default), "
  962. "msix-single-vector, msi, intx)");
  963. /**
  964. * ioat_dma_setup_interrupts - setup interrupt handler
  965. * @device: ioat device
  966. */
  967. static int ioat_dma_setup_interrupts(struct ioatdma_device *device)
  968. {
  969. struct ioat_dma_chan *ioat_chan;
  970. int err, i, j, msixcnt;
  971. u8 intrctrl = 0;
  972. if (!strcmp(ioat_interrupt_style, "msix"))
  973. goto msix;
  974. if (!strcmp(ioat_interrupt_style, "msix-single-vector"))
  975. goto msix_single_vector;
  976. if (!strcmp(ioat_interrupt_style, "msi"))
  977. goto msi;
  978. if (!strcmp(ioat_interrupt_style, "intx"))
  979. goto intx;
  980. dev_err(&device->pdev->dev, "invalid ioat_interrupt_style %s\n",
  981. ioat_interrupt_style);
  982. goto err_no_irq;
  983. msix:
  984. /* The number of MSI-X vectors should equal the number of channels */
  985. msixcnt = device->common.chancnt;
  986. for (i = 0; i < msixcnt; i++)
  987. device->msix_entries[i].entry = i;
  988. err = pci_enable_msix(device->pdev, device->msix_entries, msixcnt);
  989. if (err < 0)
  990. goto msi;
  991. if (err > 0)
  992. goto msix_single_vector;
  993. for (i = 0; i < msixcnt; i++) {
  994. ioat_chan = ioat_lookup_chan_by_index(device, i);
  995. err = request_irq(device->msix_entries[i].vector,
  996. ioat_dma_do_interrupt_msix,
  997. 0, "ioat-msix", ioat_chan);
  998. if (err) {
  999. for (j = 0; j < i; j++) {
  1000. ioat_chan =
  1001. ioat_lookup_chan_by_index(device, j);
  1002. free_irq(device->msix_entries[j].vector,
  1003. ioat_chan);
  1004. }
  1005. goto msix_single_vector;
  1006. }
  1007. }
  1008. intrctrl |= IOAT_INTRCTRL_MSIX_VECTOR_CONTROL;
  1009. device->irq_mode = msix_multi_vector;
  1010. goto done;
  1011. msix_single_vector:
  1012. device->msix_entries[0].entry = 0;
  1013. err = pci_enable_msix(device->pdev, device->msix_entries, 1);
  1014. if (err)
  1015. goto msi;
  1016. err = request_irq(device->msix_entries[0].vector, ioat_dma_do_interrupt,
  1017. 0, "ioat-msix", device);
  1018. if (err) {
  1019. pci_disable_msix(device->pdev);
  1020. goto msi;
  1021. }
  1022. device->irq_mode = msix_single_vector;
  1023. goto done;
  1024. msi:
  1025. err = pci_enable_msi(device->pdev);
  1026. if (err)
  1027. goto intx;
  1028. err = request_irq(device->pdev->irq, ioat_dma_do_interrupt,
  1029. 0, "ioat-msi", device);
  1030. if (err) {
  1031. pci_disable_msi(device->pdev);
  1032. goto intx;
  1033. }
  1034. /*
  1035. * CB 1.2 devices need a bit set in configuration space to enable MSI
  1036. */
  1037. if (device->version == IOAT_VER_1_2) {
  1038. u32 dmactrl;
  1039. pci_read_config_dword(device->pdev,
  1040. IOAT_PCI_DMACTRL_OFFSET, &dmactrl);
  1041. dmactrl |= IOAT_PCI_DMACTRL_MSI_EN;
  1042. pci_write_config_dword(device->pdev,
  1043. IOAT_PCI_DMACTRL_OFFSET, dmactrl);
  1044. }
  1045. device->irq_mode = msi;
  1046. goto done;
  1047. intx:
  1048. err = request_irq(device->pdev->irq, ioat_dma_do_interrupt,
  1049. IRQF_SHARED, "ioat-intx", device);
  1050. if (err)
  1051. goto err_no_irq;
  1052. device->irq_mode = intx;
  1053. done:
  1054. intrctrl |= IOAT_INTRCTRL_MASTER_INT_EN;
  1055. writeb(intrctrl, device->reg_base + IOAT_INTRCTRL_OFFSET);
  1056. return 0;
  1057. err_no_irq:
  1058. /* Disable all interrupt generation */
  1059. writeb(0, device->reg_base + IOAT_INTRCTRL_OFFSET);
  1060. dev_err(&device->pdev->dev, "no usable interrupts\n");
  1061. device->irq_mode = none;
  1062. return -1;
  1063. }
  1064. /**
  1065. * ioat_dma_remove_interrupts - remove whatever interrupts were set
  1066. * @device: ioat device
  1067. */
  1068. static void ioat_dma_remove_interrupts(struct ioatdma_device *device)
  1069. {
  1070. struct ioat_dma_chan *ioat_chan;
  1071. int i;
  1072. /* Disable all interrupt generation */
  1073. writeb(0, device->reg_base + IOAT_INTRCTRL_OFFSET);
  1074. switch (device->irq_mode) {
  1075. case msix_multi_vector:
  1076. for (i = 0; i < device->common.chancnt; i++) {
  1077. ioat_chan = ioat_lookup_chan_by_index(device, i);
  1078. free_irq(device->msix_entries[i].vector, ioat_chan);
  1079. }
  1080. pci_disable_msix(device->pdev);
  1081. break;
  1082. case msix_single_vector:
  1083. free_irq(device->msix_entries[0].vector, device);
  1084. pci_disable_msix(device->pdev);
  1085. break;
  1086. case msi:
  1087. free_irq(device->pdev->irq, device);
  1088. pci_disable_msi(device->pdev);
  1089. break;
  1090. case intx:
  1091. free_irq(device->pdev->irq, device);
  1092. break;
  1093. case none:
  1094. dev_warn(&device->pdev->dev,
  1095. "call to %s without interrupts setup\n", __func__);
  1096. }
  1097. device->irq_mode = none;
  1098. }
  1099. struct ioatdma_device *ioat_dma_probe(struct pci_dev *pdev,
  1100. void __iomem *iobase)
  1101. {
  1102. int err;
  1103. struct ioatdma_device *device;
  1104. device = kzalloc(sizeof(*device), GFP_KERNEL);
  1105. if (!device) {
  1106. err = -ENOMEM;
  1107. goto err_kzalloc;
  1108. }
  1109. device->pdev = pdev;
  1110. device->reg_base = iobase;
  1111. device->version = readb(device->reg_base + IOAT_VER_OFFSET);
  1112. /* DMA coherent memory pool for DMA descriptor allocations */
  1113. device->dma_pool = pci_pool_create("dma_desc_pool", pdev,
  1114. sizeof(struct ioat_dma_descriptor),
  1115. 64, 0);
  1116. if (!device->dma_pool) {
  1117. err = -ENOMEM;
  1118. goto err_dma_pool;
  1119. }
  1120. device->completion_pool = pci_pool_create("completion_pool", pdev,
  1121. sizeof(u64), SMP_CACHE_BYTES,
  1122. SMP_CACHE_BYTES);
  1123. if (!device->completion_pool) {
  1124. err = -ENOMEM;
  1125. goto err_completion_pool;
  1126. }
  1127. INIT_LIST_HEAD(&device->common.channels);
  1128. ioat_dma_enumerate_channels(device);
  1129. device->common.device_alloc_chan_resources =
  1130. ioat_dma_alloc_chan_resources;
  1131. device->common.device_free_chan_resources =
  1132. ioat_dma_free_chan_resources;
  1133. device->common.dev = &pdev->dev;
  1134. dma_cap_set(DMA_MEMCPY, device->common.cap_mask);
  1135. device->common.device_is_tx_complete = ioat_dma_is_complete;
  1136. switch (device->version) {
  1137. case IOAT_VER_1_2:
  1138. device->common.device_prep_dma_memcpy = ioat1_dma_prep_memcpy;
  1139. device->common.device_issue_pending =
  1140. ioat1_dma_memcpy_issue_pending;
  1141. break;
  1142. case IOAT_VER_2_0:
  1143. device->common.device_prep_dma_memcpy = ioat2_dma_prep_memcpy;
  1144. device->common.device_issue_pending =
  1145. ioat2_dma_memcpy_issue_pending;
  1146. break;
  1147. }
  1148. dev_err(&device->pdev->dev,
  1149. "Intel(R) I/OAT DMA Engine found,"
  1150. " %d channels, device version 0x%02x, driver version %s\n",
  1151. device->common.chancnt, device->version, IOAT_DMA_VERSION);
  1152. err = ioat_dma_setup_interrupts(device);
  1153. if (err)
  1154. goto err_setup_interrupts;
  1155. err = ioat_dma_self_test(device);
  1156. if (err)
  1157. goto err_self_test;
  1158. dma_async_device_register(&device->common);
  1159. return device;
  1160. err_self_test:
  1161. ioat_dma_remove_interrupts(device);
  1162. err_setup_interrupts:
  1163. pci_pool_destroy(device->completion_pool);
  1164. err_completion_pool:
  1165. pci_pool_destroy(device->dma_pool);
  1166. err_dma_pool:
  1167. kfree(device);
  1168. err_kzalloc:
  1169. dev_err(&pdev->dev,
  1170. "Intel(R) I/OAT DMA Engine initialization failed\n");
  1171. return NULL;
  1172. }
  1173. void ioat_dma_remove(struct ioatdma_device *device)
  1174. {
  1175. struct dma_chan *chan, *_chan;
  1176. struct ioat_dma_chan *ioat_chan;
  1177. ioat_dma_remove_interrupts(device);
  1178. dma_async_device_unregister(&device->common);
  1179. pci_pool_destroy(device->dma_pool);
  1180. pci_pool_destroy(device->completion_pool);
  1181. iounmap(device->reg_base);
  1182. pci_release_regions(device->pdev);
  1183. pci_disable_device(device->pdev);
  1184. list_for_each_entry_safe(chan, _chan,
  1185. &device->common.channels, device_node) {
  1186. ioat_chan = to_ioat_chan(chan);
  1187. list_del(&chan->device_node);
  1188. kfree(ioat_chan);
  1189. }
  1190. kfree(device);
  1191. }