ioatdma.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  1. /*
  2. * Copyright(c) 2004 - 2006 Intel Corporation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the Free
  6. * Software Foundation; either version 2 of the License, or (at your option)
  7. * any later version.
  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., 59
  16. * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. *
  18. * The full GNU General Public License is included in this distribution in the
  19. * file called COPYING.
  20. */
  21. /*
  22. * This driver supports an Intel I/OAT DMA engine, which does asynchronous
  23. * copy operations.
  24. */
  25. #include <linux/init.h>
  26. #include <linux/module.h>
  27. #include <linux/pci.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/dmaengine.h>
  30. #include <linux/delay.h>
  31. #include <linux/dma-mapping.h>
  32. #include "ioatdma.h"
  33. #include "ioatdma_registers.h"
  34. #include "ioatdma_hw.h"
  35. #define to_ioat_chan(chan) container_of(chan, struct ioat_dma_chan, common)
  36. #define to_ioat_device(dev) container_of(dev, struct ioat_device, common)
  37. #define to_ioat_desc(lh) container_of(lh, struct ioat_desc_sw, node)
  38. /* internal functions */
  39. static int __devinit ioat_probe(struct pci_dev *pdev, const struct pci_device_id *ent);
  40. static void __devexit ioat_remove(struct pci_dev *pdev);
  41. static int enumerate_dma_channels(struct ioat_device *device)
  42. {
  43. u8 xfercap_scale;
  44. u32 xfercap;
  45. int i;
  46. struct ioat_dma_chan *ioat_chan;
  47. device->common.chancnt = readb(device->reg_base + IOAT_CHANCNT_OFFSET);
  48. xfercap_scale = readb(device->reg_base + IOAT_XFERCAP_OFFSET);
  49. xfercap = (xfercap_scale == 0 ? -1 : (1UL << xfercap_scale));
  50. for (i = 0; i < device->common.chancnt; i++) {
  51. ioat_chan = kzalloc(sizeof(*ioat_chan), GFP_KERNEL);
  52. if (!ioat_chan) {
  53. device->common.chancnt = i;
  54. break;
  55. }
  56. ioat_chan->device = device;
  57. ioat_chan->reg_base = device->reg_base + (0x80 * (i + 1));
  58. ioat_chan->xfercap = xfercap;
  59. spin_lock_init(&ioat_chan->cleanup_lock);
  60. spin_lock_init(&ioat_chan->desc_lock);
  61. INIT_LIST_HEAD(&ioat_chan->free_desc);
  62. INIT_LIST_HEAD(&ioat_chan->used_desc);
  63. /* This should be made common somewhere in dmaengine.c */
  64. ioat_chan->common.device = &device->common;
  65. ioat_chan->common.client = NULL;
  66. list_add_tail(&ioat_chan->common.device_node,
  67. &device->common.channels);
  68. }
  69. return device->common.chancnt;
  70. }
  71. static struct ioat_desc_sw *ioat_dma_alloc_descriptor(
  72. struct ioat_dma_chan *ioat_chan,
  73. gfp_t flags)
  74. {
  75. struct ioat_dma_descriptor *desc;
  76. struct ioat_desc_sw *desc_sw;
  77. struct ioat_device *ioat_device;
  78. dma_addr_t phys;
  79. ioat_device = to_ioat_device(ioat_chan->common.device);
  80. desc = pci_pool_alloc(ioat_device->dma_pool, flags, &phys);
  81. if (unlikely(!desc))
  82. return NULL;
  83. desc_sw = kzalloc(sizeof(*desc_sw), flags);
  84. if (unlikely(!desc_sw)) {
  85. pci_pool_free(ioat_device->dma_pool, desc, phys);
  86. return NULL;
  87. }
  88. memset(desc, 0, sizeof(*desc));
  89. desc_sw->hw = desc;
  90. desc_sw->phys = phys;
  91. return desc_sw;
  92. }
  93. #define INITIAL_IOAT_DESC_COUNT 128
  94. static void ioat_start_null_desc(struct ioat_dma_chan *ioat_chan);
  95. /* returns the actual number of allocated descriptors */
  96. static int ioat_dma_alloc_chan_resources(struct dma_chan *chan)
  97. {
  98. struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
  99. struct ioat_desc_sw *desc = NULL;
  100. u16 chanctrl;
  101. u32 chanerr;
  102. int i;
  103. LIST_HEAD(tmp_list);
  104. /*
  105. * In-use bit automatically set by reading chanctrl
  106. * If 0, we got it, if 1, someone else did
  107. */
  108. chanctrl = readw(ioat_chan->reg_base + IOAT_CHANCTRL_OFFSET);
  109. if (chanctrl & IOAT_CHANCTRL_CHANNEL_IN_USE)
  110. return -EBUSY;
  111. /* Setup register to interrupt and write completion status on error */
  112. chanctrl = IOAT_CHANCTRL_CHANNEL_IN_USE |
  113. IOAT_CHANCTRL_ERR_INT_EN |
  114. IOAT_CHANCTRL_ANY_ERR_ABORT_EN |
  115. IOAT_CHANCTRL_ERR_COMPLETION_EN;
  116. writew(chanctrl, ioat_chan->reg_base + IOAT_CHANCTRL_OFFSET);
  117. chanerr = readl(ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
  118. if (chanerr) {
  119. printk("IOAT: CHANERR = %x, clearing\n", chanerr);
  120. writel(chanerr, ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
  121. }
  122. /* Allocate descriptors */
  123. for (i = 0; i < INITIAL_IOAT_DESC_COUNT; i++) {
  124. desc = ioat_dma_alloc_descriptor(ioat_chan, GFP_KERNEL);
  125. if (!desc) {
  126. printk(KERN_ERR "IOAT: Only %d initial descriptors\n", i);
  127. break;
  128. }
  129. list_add_tail(&desc->node, &tmp_list);
  130. }
  131. spin_lock_bh(&ioat_chan->desc_lock);
  132. list_splice(&tmp_list, &ioat_chan->free_desc);
  133. spin_unlock_bh(&ioat_chan->desc_lock);
  134. /* allocate a completion writeback area */
  135. /* doing 2 32bit writes to mmio since 1 64b write doesn't work */
  136. ioat_chan->completion_virt =
  137. pci_pool_alloc(ioat_chan->device->completion_pool,
  138. GFP_KERNEL,
  139. &ioat_chan->completion_addr);
  140. memset(ioat_chan->completion_virt, 0,
  141. sizeof(*ioat_chan->completion_virt));
  142. writel(((u64) ioat_chan->completion_addr) & 0x00000000FFFFFFFF,
  143. ioat_chan->reg_base + IOAT_CHANCMP_OFFSET_LOW);
  144. writel(((u64) ioat_chan->completion_addr) >> 32,
  145. ioat_chan->reg_base + IOAT_CHANCMP_OFFSET_HIGH);
  146. ioat_start_null_desc(ioat_chan);
  147. return i;
  148. }
  149. static void ioat_dma_memcpy_cleanup(struct ioat_dma_chan *ioat_chan);
  150. static void ioat_dma_free_chan_resources(struct dma_chan *chan)
  151. {
  152. struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
  153. struct ioat_device *ioat_device = to_ioat_device(chan->device);
  154. struct ioat_desc_sw *desc, *_desc;
  155. u16 chanctrl;
  156. int in_use_descs = 0;
  157. ioat_dma_memcpy_cleanup(ioat_chan);
  158. writeb(IOAT_CHANCMD_RESET, ioat_chan->reg_base + IOAT_CHANCMD_OFFSET);
  159. spin_lock_bh(&ioat_chan->desc_lock);
  160. list_for_each_entry_safe(desc, _desc, &ioat_chan->used_desc, node) {
  161. in_use_descs++;
  162. list_del(&desc->node);
  163. pci_pool_free(ioat_device->dma_pool, desc->hw, desc->phys);
  164. kfree(desc);
  165. }
  166. list_for_each_entry_safe(desc, _desc, &ioat_chan->free_desc, node) {
  167. list_del(&desc->node);
  168. pci_pool_free(ioat_device->dma_pool, desc->hw, desc->phys);
  169. kfree(desc);
  170. }
  171. spin_unlock_bh(&ioat_chan->desc_lock);
  172. pci_pool_free(ioat_device->completion_pool,
  173. ioat_chan->completion_virt,
  174. ioat_chan->completion_addr);
  175. /* one is ok since we left it on there on purpose */
  176. if (in_use_descs > 1)
  177. printk(KERN_ERR "IOAT: Freeing %d in use descriptors!\n",
  178. in_use_descs - 1);
  179. ioat_chan->last_completion = ioat_chan->completion_addr = 0;
  180. /* Tell hw the chan is free */
  181. chanctrl = readw(ioat_chan->reg_base + IOAT_CHANCTRL_OFFSET);
  182. chanctrl &= ~IOAT_CHANCTRL_CHANNEL_IN_USE;
  183. writew(chanctrl, ioat_chan->reg_base + IOAT_CHANCTRL_OFFSET);
  184. }
  185. /**
  186. * do_ioat_dma_memcpy - actual function that initiates a IOAT DMA transaction
  187. * @ioat_chan: IOAT DMA channel handle
  188. * @dest: DMA destination address
  189. * @src: DMA source address
  190. * @len: transaction length in bytes
  191. */
  192. static dma_cookie_t do_ioat_dma_memcpy(struct ioat_dma_chan *ioat_chan,
  193. dma_addr_t dest,
  194. dma_addr_t src,
  195. size_t len)
  196. {
  197. struct ioat_desc_sw *first;
  198. struct ioat_desc_sw *prev;
  199. struct ioat_desc_sw *new;
  200. dma_cookie_t cookie;
  201. LIST_HEAD(new_chain);
  202. u32 copy;
  203. size_t orig_len;
  204. dma_addr_t orig_src, orig_dst;
  205. unsigned int desc_count = 0;
  206. unsigned int append = 0;
  207. if (!ioat_chan || !dest || !src)
  208. return -EFAULT;
  209. if (!len)
  210. return ioat_chan->common.cookie;
  211. orig_len = len;
  212. orig_src = src;
  213. orig_dst = dest;
  214. first = NULL;
  215. prev = NULL;
  216. spin_lock_bh(&ioat_chan->desc_lock);
  217. while (len) {
  218. if (!list_empty(&ioat_chan->free_desc)) {
  219. new = to_ioat_desc(ioat_chan->free_desc.next);
  220. list_del(&new->node);
  221. } else {
  222. /* try to get another desc */
  223. new = ioat_dma_alloc_descriptor(ioat_chan, GFP_ATOMIC);
  224. /* will this ever happen? */
  225. /* TODO add upper limit on these */
  226. BUG_ON(!new);
  227. }
  228. copy = min((u32) len, ioat_chan->xfercap);
  229. new->hw->size = copy;
  230. new->hw->ctl = 0;
  231. new->hw->src_addr = src;
  232. new->hw->dst_addr = dest;
  233. new->cookie = 0;
  234. /* chain together the physical address list for the HW */
  235. if (!first)
  236. first = new;
  237. else
  238. prev->hw->next = (u64) new->phys;
  239. prev = new;
  240. len -= copy;
  241. dest += copy;
  242. src += copy;
  243. list_add_tail(&new->node, &new_chain);
  244. desc_count++;
  245. }
  246. new->hw->ctl = IOAT_DMA_DESCRIPTOR_CTL_CP_STS;
  247. new->hw->next = 0;
  248. /* cookie incr and addition to used_list must be atomic */
  249. cookie = ioat_chan->common.cookie;
  250. cookie++;
  251. if (cookie < 0)
  252. cookie = 1;
  253. ioat_chan->common.cookie = new->cookie = cookie;
  254. pci_unmap_addr_set(new, src, orig_src);
  255. pci_unmap_addr_set(new, dst, orig_dst);
  256. pci_unmap_len_set(new, src_len, orig_len);
  257. pci_unmap_len_set(new, dst_len, orig_len);
  258. /* write address into NextDescriptor field of last desc in chain */
  259. to_ioat_desc(ioat_chan->used_desc.prev)->hw->next = first->phys;
  260. list_splice_init(&new_chain, ioat_chan->used_desc.prev);
  261. ioat_chan->pending += desc_count;
  262. if (ioat_chan->pending >= 4) {
  263. append = 1;
  264. ioat_chan->pending = 0;
  265. }
  266. spin_unlock_bh(&ioat_chan->desc_lock);
  267. if (append)
  268. writeb(IOAT_CHANCMD_APPEND,
  269. ioat_chan->reg_base + IOAT_CHANCMD_OFFSET);
  270. return cookie;
  271. }
  272. /**
  273. * ioat_dma_memcpy_buf_to_buf - wrapper that takes src & dest bufs
  274. * @chan: IOAT DMA channel handle
  275. * @dest: DMA destination address
  276. * @src: DMA source address
  277. * @len: transaction length in bytes
  278. */
  279. static dma_cookie_t ioat_dma_memcpy_buf_to_buf(struct dma_chan *chan,
  280. void *dest,
  281. void *src,
  282. size_t len)
  283. {
  284. dma_addr_t dest_addr;
  285. dma_addr_t src_addr;
  286. struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
  287. dest_addr = pci_map_single(ioat_chan->device->pdev,
  288. dest, len, PCI_DMA_FROMDEVICE);
  289. src_addr = pci_map_single(ioat_chan->device->pdev,
  290. src, len, PCI_DMA_TODEVICE);
  291. return do_ioat_dma_memcpy(ioat_chan, dest_addr, src_addr, len);
  292. }
  293. /**
  294. * ioat_dma_memcpy_buf_to_pg - wrapper, copying from a buf to a page
  295. * @chan: IOAT DMA channel handle
  296. * @page: pointer to the page to copy to
  297. * @offset: offset into that page
  298. * @src: DMA source address
  299. * @len: transaction length in bytes
  300. */
  301. static dma_cookie_t ioat_dma_memcpy_buf_to_pg(struct dma_chan *chan,
  302. struct page *page,
  303. unsigned int offset,
  304. void *src,
  305. size_t len)
  306. {
  307. dma_addr_t dest_addr;
  308. dma_addr_t src_addr;
  309. struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
  310. dest_addr = pci_map_page(ioat_chan->device->pdev,
  311. page, offset, len, PCI_DMA_FROMDEVICE);
  312. src_addr = pci_map_single(ioat_chan->device->pdev,
  313. src, len, PCI_DMA_TODEVICE);
  314. return do_ioat_dma_memcpy(ioat_chan, dest_addr, src_addr, len);
  315. }
  316. /**
  317. * ioat_dma_memcpy_pg_to_pg - wrapper, copying between two pages
  318. * @chan: IOAT DMA channel handle
  319. * @dest_pg: pointer to the page to copy to
  320. * @dest_off: offset into that page
  321. * @src_pg: pointer to the page to copy from
  322. * @src_off: offset into that page
  323. * @len: transaction length in bytes. This is guaranteed not to make a copy
  324. * across a page boundary.
  325. */
  326. static dma_cookie_t ioat_dma_memcpy_pg_to_pg(struct dma_chan *chan,
  327. struct page *dest_pg,
  328. unsigned int dest_off,
  329. struct page *src_pg,
  330. unsigned int src_off,
  331. size_t len)
  332. {
  333. dma_addr_t dest_addr;
  334. dma_addr_t src_addr;
  335. struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
  336. dest_addr = pci_map_page(ioat_chan->device->pdev,
  337. dest_pg, dest_off, len, PCI_DMA_FROMDEVICE);
  338. src_addr = pci_map_page(ioat_chan->device->pdev,
  339. src_pg, src_off, len, PCI_DMA_TODEVICE);
  340. return do_ioat_dma_memcpy(ioat_chan, dest_addr, src_addr, len);
  341. }
  342. /**
  343. * ioat_dma_memcpy_issue_pending - push potentially unrecognized appended descriptors to hw
  344. * @chan: DMA channel handle
  345. */
  346. static void ioat_dma_memcpy_issue_pending(struct dma_chan *chan)
  347. {
  348. struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
  349. if (ioat_chan->pending != 0) {
  350. ioat_chan->pending = 0;
  351. writeb(IOAT_CHANCMD_APPEND,
  352. ioat_chan->reg_base + IOAT_CHANCMD_OFFSET);
  353. }
  354. }
  355. static void ioat_dma_memcpy_cleanup(struct ioat_dma_chan *chan)
  356. {
  357. unsigned long phys_complete;
  358. struct ioat_desc_sw *desc, *_desc;
  359. dma_cookie_t cookie = 0;
  360. prefetch(chan->completion_virt);
  361. if (!spin_trylock(&chan->cleanup_lock))
  362. return;
  363. /* The completion writeback can happen at any time,
  364. so reads by the driver need to be atomic operations
  365. The descriptor physical addresses are limited to 32-bits
  366. when the CPU can only do a 32-bit mov */
  367. #if (BITS_PER_LONG == 64)
  368. phys_complete =
  369. chan->completion_virt->full & IOAT_CHANSTS_COMPLETED_DESCRIPTOR_ADDR;
  370. #else
  371. phys_complete = chan->completion_virt->low & IOAT_LOW_COMPLETION_MASK;
  372. #endif
  373. if ((chan->completion_virt->full & IOAT_CHANSTS_DMA_TRANSFER_STATUS) ==
  374. IOAT_CHANSTS_DMA_TRANSFER_STATUS_HALTED) {
  375. printk("IOAT: Channel halted, chanerr = %x\n",
  376. readl(chan->reg_base + IOAT_CHANERR_OFFSET));
  377. /* TODO do something to salvage the situation */
  378. }
  379. if (phys_complete == chan->last_completion) {
  380. spin_unlock(&chan->cleanup_lock);
  381. return;
  382. }
  383. spin_lock_bh(&chan->desc_lock);
  384. list_for_each_entry_safe(desc, _desc, &chan->used_desc, node) {
  385. /*
  386. * Incoming DMA requests may use multiple descriptors, due to
  387. * exceeding xfercap, perhaps. If so, only the last one will
  388. * have a cookie, and require unmapping.
  389. */
  390. if (desc->cookie) {
  391. cookie = desc->cookie;
  392. /* yes we are unmapping both _page and _single alloc'd
  393. regions with unmap_page. Is this *really* that bad?
  394. */
  395. pci_unmap_page(chan->device->pdev,
  396. pci_unmap_addr(desc, dst),
  397. pci_unmap_len(desc, dst_len),
  398. PCI_DMA_FROMDEVICE);
  399. pci_unmap_page(chan->device->pdev,
  400. pci_unmap_addr(desc, src),
  401. pci_unmap_len(desc, src_len),
  402. PCI_DMA_TODEVICE);
  403. }
  404. if (desc->phys != phys_complete) {
  405. /* a completed entry, but not the last, so cleanup */
  406. list_del(&desc->node);
  407. list_add_tail(&desc->node, &chan->free_desc);
  408. } else {
  409. /* last used desc. Do not remove, so we can append from
  410. it, but don't look at it next time, either */
  411. desc->cookie = 0;
  412. /* TODO check status bits? */
  413. break;
  414. }
  415. }
  416. spin_unlock_bh(&chan->desc_lock);
  417. chan->last_completion = phys_complete;
  418. if (cookie != 0)
  419. chan->completed_cookie = cookie;
  420. spin_unlock(&chan->cleanup_lock);
  421. }
  422. /**
  423. * ioat_dma_is_complete - poll the status of a IOAT DMA transaction
  424. * @chan: IOAT DMA channel handle
  425. * @cookie: DMA transaction identifier
  426. * @done: if not %NULL, updated with last completed transaction
  427. * @used: if not %NULL, updated with last used transaction
  428. */
  429. static enum dma_status ioat_dma_is_complete(struct dma_chan *chan,
  430. dma_cookie_t cookie,
  431. dma_cookie_t *done,
  432. dma_cookie_t *used)
  433. {
  434. struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
  435. dma_cookie_t last_used;
  436. dma_cookie_t last_complete;
  437. enum dma_status ret;
  438. last_used = chan->cookie;
  439. last_complete = ioat_chan->completed_cookie;
  440. if (done)
  441. *done= last_complete;
  442. if (used)
  443. *used = last_used;
  444. ret = dma_async_is_complete(cookie, last_complete, last_used);
  445. if (ret == DMA_SUCCESS)
  446. return ret;
  447. ioat_dma_memcpy_cleanup(ioat_chan);
  448. last_used = chan->cookie;
  449. last_complete = ioat_chan->completed_cookie;
  450. if (done)
  451. *done= last_complete;
  452. if (used)
  453. *used = last_used;
  454. return dma_async_is_complete(cookie, last_complete, last_used);
  455. }
  456. /* PCI API */
  457. static struct pci_device_id ioat_pci_tbl[] = {
  458. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_IOAT) },
  459. { 0, }
  460. };
  461. static struct pci_driver ioat_pci_driver = {
  462. .name = "ioatdma",
  463. .id_table = ioat_pci_tbl,
  464. .probe = ioat_probe,
  465. .remove = __devexit_p(ioat_remove),
  466. };
  467. static irqreturn_t ioat_do_interrupt(int irq, void *data)
  468. {
  469. struct ioat_device *instance = data;
  470. unsigned long attnstatus;
  471. u8 intrctrl;
  472. intrctrl = readb(instance->reg_base + IOAT_INTRCTRL_OFFSET);
  473. if (!(intrctrl & IOAT_INTRCTRL_MASTER_INT_EN))
  474. return IRQ_NONE;
  475. if (!(intrctrl & IOAT_INTRCTRL_INT_STATUS)) {
  476. writeb(intrctrl, instance->reg_base + IOAT_INTRCTRL_OFFSET);
  477. return IRQ_NONE;
  478. }
  479. attnstatus = readl(instance->reg_base + IOAT_ATTNSTATUS_OFFSET);
  480. printk(KERN_ERR "ioatdma error: interrupt! status %lx\n", attnstatus);
  481. writeb(intrctrl, instance->reg_base + IOAT_INTRCTRL_OFFSET);
  482. return IRQ_HANDLED;
  483. }
  484. static void ioat_start_null_desc(struct ioat_dma_chan *ioat_chan)
  485. {
  486. struct ioat_desc_sw *desc;
  487. spin_lock_bh(&ioat_chan->desc_lock);
  488. if (!list_empty(&ioat_chan->free_desc)) {
  489. desc = to_ioat_desc(ioat_chan->free_desc.next);
  490. list_del(&desc->node);
  491. } else {
  492. /* try to get another desc */
  493. spin_unlock_bh(&ioat_chan->desc_lock);
  494. desc = ioat_dma_alloc_descriptor(ioat_chan, GFP_KERNEL);
  495. spin_lock_bh(&ioat_chan->desc_lock);
  496. /* will this ever happen? */
  497. BUG_ON(!desc);
  498. }
  499. desc->hw->ctl = IOAT_DMA_DESCRIPTOR_NUL;
  500. desc->hw->next = 0;
  501. list_add_tail(&desc->node, &ioat_chan->used_desc);
  502. spin_unlock_bh(&ioat_chan->desc_lock);
  503. #if (BITS_PER_LONG == 64)
  504. writeq(desc->phys, ioat_chan->reg_base + IOAT_CHAINADDR_OFFSET);
  505. #else
  506. writel((u32) desc->phys,
  507. ioat_chan->reg_base + IOAT_CHAINADDR_OFFSET_LOW);
  508. writel(0, ioat_chan->reg_base + IOAT_CHAINADDR_OFFSET_HIGH);
  509. #endif
  510. writeb(IOAT_CHANCMD_START, ioat_chan->reg_base + IOAT_CHANCMD_OFFSET);
  511. }
  512. /*
  513. * Perform a IOAT transaction to verify the HW works.
  514. */
  515. #define IOAT_TEST_SIZE 2000
  516. static int ioat_self_test(struct ioat_device *device)
  517. {
  518. int i;
  519. u8 *src;
  520. u8 *dest;
  521. struct dma_chan *dma_chan;
  522. dma_cookie_t cookie;
  523. int err = 0;
  524. src = kzalloc(sizeof(u8) * IOAT_TEST_SIZE, GFP_KERNEL);
  525. if (!src)
  526. return -ENOMEM;
  527. dest = kzalloc(sizeof(u8) * IOAT_TEST_SIZE, GFP_KERNEL);
  528. if (!dest) {
  529. kfree(src);
  530. return -ENOMEM;
  531. }
  532. /* Fill in src buffer */
  533. for (i = 0; i < IOAT_TEST_SIZE; i++)
  534. src[i] = (u8)i;
  535. /* Start copy, using first DMA channel */
  536. dma_chan = container_of(device->common.channels.next,
  537. struct dma_chan,
  538. device_node);
  539. if (ioat_dma_alloc_chan_resources(dma_chan) < 1) {
  540. err = -ENODEV;
  541. goto out;
  542. }
  543. cookie = ioat_dma_memcpy_buf_to_buf(dma_chan, dest, src, IOAT_TEST_SIZE);
  544. ioat_dma_memcpy_issue_pending(dma_chan);
  545. msleep(1);
  546. if (ioat_dma_is_complete(dma_chan, cookie, NULL, NULL) != DMA_SUCCESS) {
  547. printk(KERN_ERR "ioatdma: Self-test copy timed out, disabling\n");
  548. err = -ENODEV;
  549. goto free_resources;
  550. }
  551. if (memcmp(src, dest, IOAT_TEST_SIZE)) {
  552. printk(KERN_ERR "ioatdma: Self-test copy failed compare, disabling\n");
  553. err = -ENODEV;
  554. goto free_resources;
  555. }
  556. free_resources:
  557. ioat_dma_free_chan_resources(dma_chan);
  558. out:
  559. kfree(src);
  560. kfree(dest);
  561. return err;
  562. }
  563. static int __devinit ioat_probe(struct pci_dev *pdev,
  564. const struct pci_device_id *ent)
  565. {
  566. int err;
  567. unsigned long mmio_start, mmio_len;
  568. void __iomem *reg_base;
  569. struct ioat_device *device;
  570. err = pci_enable_device(pdev);
  571. if (err)
  572. goto err_enable_device;
  573. err = pci_set_dma_mask(pdev, DMA_64BIT_MASK);
  574. if (err)
  575. err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
  576. if (err)
  577. goto err_set_dma_mask;
  578. err = pci_request_regions(pdev, ioat_pci_driver.name);
  579. if (err)
  580. goto err_request_regions;
  581. mmio_start = pci_resource_start(pdev, 0);
  582. mmio_len = pci_resource_len(pdev, 0);
  583. reg_base = ioremap(mmio_start, mmio_len);
  584. if (!reg_base) {
  585. err = -ENOMEM;
  586. goto err_ioremap;
  587. }
  588. device = kzalloc(sizeof(*device), GFP_KERNEL);
  589. if (!device) {
  590. err = -ENOMEM;
  591. goto err_kzalloc;
  592. }
  593. /* DMA coherent memory pool for DMA descriptor allocations */
  594. device->dma_pool = pci_pool_create("dma_desc_pool", pdev,
  595. sizeof(struct ioat_dma_descriptor), 64, 0);
  596. if (!device->dma_pool) {
  597. err = -ENOMEM;
  598. goto err_dma_pool;
  599. }
  600. device->completion_pool = pci_pool_create("completion_pool", pdev, sizeof(u64), SMP_CACHE_BYTES, SMP_CACHE_BYTES);
  601. if (!device->completion_pool) {
  602. err = -ENOMEM;
  603. goto err_completion_pool;
  604. }
  605. device->pdev = pdev;
  606. pci_set_drvdata(pdev, device);
  607. #ifdef CONFIG_PCI_MSI
  608. if (pci_enable_msi(pdev) == 0) {
  609. device->msi = 1;
  610. } else {
  611. device->msi = 0;
  612. }
  613. #endif
  614. err = request_irq(pdev->irq, &ioat_do_interrupt, IRQF_SHARED, "ioat",
  615. device);
  616. if (err)
  617. goto err_irq;
  618. device->reg_base = reg_base;
  619. writeb(IOAT_INTRCTRL_MASTER_INT_EN, device->reg_base + IOAT_INTRCTRL_OFFSET);
  620. pci_set_master(pdev);
  621. INIT_LIST_HEAD(&device->common.channels);
  622. enumerate_dma_channels(device);
  623. device->common.device_alloc_chan_resources = ioat_dma_alloc_chan_resources;
  624. device->common.device_free_chan_resources = ioat_dma_free_chan_resources;
  625. device->common.device_memcpy_buf_to_buf = ioat_dma_memcpy_buf_to_buf;
  626. device->common.device_memcpy_buf_to_pg = ioat_dma_memcpy_buf_to_pg;
  627. device->common.device_memcpy_pg_to_pg = ioat_dma_memcpy_pg_to_pg;
  628. device->common.device_memcpy_complete = ioat_dma_is_complete;
  629. device->common.device_memcpy_issue_pending = ioat_dma_memcpy_issue_pending;
  630. printk(KERN_INFO "Intel(R) I/OAT DMA Engine found, %d channels\n",
  631. device->common.chancnt);
  632. err = ioat_self_test(device);
  633. if (err)
  634. goto err_self_test;
  635. dma_async_device_register(&device->common);
  636. return 0;
  637. err_self_test:
  638. err_irq:
  639. pci_pool_destroy(device->completion_pool);
  640. err_completion_pool:
  641. pci_pool_destroy(device->dma_pool);
  642. err_dma_pool:
  643. kfree(device);
  644. err_kzalloc:
  645. iounmap(reg_base);
  646. err_ioremap:
  647. pci_release_regions(pdev);
  648. err_request_regions:
  649. err_set_dma_mask:
  650. pci_disable_device(pdev);
  651. err_enable_device:
  652. return err;
  653. }
  654. static void __devexit ioat_remove(struct pci_dev *pdev)
  655. {
  656. struct ioat_device *device;
  657. struct dma_chan *chan, *_chan;
  658. struct ioat_dma_chan *ioat_chan;
  659. device = pci_get_drvdata(pdev);
  660. dma_async_device_unregister(&device->common);
  661. free_irq(device->pdev->irq, device);
  662. #ifdef CONFIG_PCI_MSI
  663. if (device->msi)
  664. pci_disable_msi(device->pdev);
  665. #endif
  666. pci_pool_destroy(device->dma_pool);
  667. pci_pool_destroy(device->completion_pool);
  668. iounmap(device->reg_base);
  669. pci_release_regions(pdev);
  670. pci_disable_device(pdev);
  671. list_for_each_entry_safe(chan, _chan, &device->common.channels, device_node) {
  672. ioat_chan = to_ioat_chan(chan);
  673. list_del(&chan->device_node);
  674. kfree(ioat_chan);
  675. }
  676. kfree(device);
  677. }
  678. /* MODULE API */
  679. MODULE_VERSION("1.9");
  680. MODULE_LICENSE("GPL");
  681. MODULE_AUTHOR("Intel Corporation");
  682. static int __init ioat_init_module(void)
  683. {
  684. /* it's currently unsafe to unload this module */
  685. /* if forced, worst case is that rmmod hangs */
  686. __unsafe(THIS_MODULE);
  687. return pci_register_driver(&ioat_pci_driver);
  688. }
  689. module_init(ioat_init_module);
  690. static void __exit ioat_exit_module(void)
  691. {
  692. pci_unregister_driver(&ioat_pci_driver);
  693. }
  694. module_exit(ioat_exit_module);