ioat_dma.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383
  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. static void ioat_set_src(dma_addr_t addr,
  141. struct dma_async_tx_descriptor *tx,
  142. int index)
  143. {
  144. tx_to_ioat_desc(tx)->src = addr;
  145. }
  146. static void ioat_set_dest(dma_addr_t addr,
  147. struct dma_async_tx_descriptor *tx,
  148. int index)
  149. {
  150. tx_to_ioat_desc(tx)->dst = addr;
  151. }
  152. static inline void __ioat1_dma_memcpy_issue_pending(
  153. struct ioat_dma_chan *ioat_chan);
  154. static inline void __ioat2_dma_memcpy_issue_pending(
  155. struct ioat_dma_chan *ioat_chan);
  156. static dma_cookie_t ioat1_tx_submit(struct dma_async_tx_descriptor *tx)
  157. {
  158. struct ioat_dma_chan *ioat_chan = to_ioat_chan(tx->chan);
  159. struct ioat_desc_sw *first = tx_to_ioat_desc(tx);
  160. struct ioat_desc_sw *prev, *new;
  161. struct ioat_dma_descriptor *hw;
  162. dma_cookie_t cookie;
  163. LIST_HEAD(new_chain);
  164. u32 copy;
  165. size_t len;
  166. dma_addr_t src, dst;
  167. int orig_ack;
  168. unsigned int desc_count = 0;
  169. /* src and dest and len are stored in the initial descriptor */
  170. len = first->len;
  171. src = first->src;
  172. dst = first->dst;
  173. orig_ack = first->async_tx.ack;
  174. new = first;
  175. spin_lock_bh(&ioat_chan->desc_lock);
  176. prev = to_ioat_desc(ioat_chan->used_desc.prev);
  177. prefetch(prev->hw);
  178. do {
  179. copy = min((u32) len, ioat_chan->xfercap);
  180. new->async_tx.ack = 1;
  181. hw = new->hw;
  182. hw->size = copy;
  183. hw->ctl = 0;
  184. hw->src_addr = src;
  185. hw->dst_addr = dst;
  186. hw->next = 0;
  187. /* chain together the physical address list for the HW */
  188. wmb();
  189. prev->hw->next = (u64) new->async_tx.phys;
  190. len -= copy;
  191. dst += copy;
  192. src += copy;
  193. list_add_tail(&new->node, &new_chain);
  194. desc_count++;
  195. prev = new;
  196. } while (len && (new = ioat1_dma_get_next_descriptor(ioat_chan)));
  197. hw->ctl = IOAT_DMA_DESCRIPTOR_CTL_CP_STS;
  198. if (new->async_tx.callback) {
  199. hw->ctl |= IOAT_DMA_DESCRIPTOR_CTL_INT_GN;
  200. if (first != new) {
  201. /* move callback into to last desc */
  202. new->async_tx.callback = first->async_tx.callback;
  203. new->async_tx.callback_param
  204. = first->async_tx.callback_param;
  205. first->async_tx.callback = NULL;
  206. first->async_tx.callback_param = NULL;
  207. }
  208. }
  209. new->tx_cnt = desc_count;
  210. new->async_tx.ack = orig_ack; /* client is in control of this ack */
  211. /* store the original values for use in later cleanup */
  212. if (new != first) {
  213. new->src = first->src;
  214. new->dst = first->dst;
  215. new->len = first->len;
  216. }
  217. /* cookie incr and addition to used_list must be atomic */
  218. cookie = ioat_chan->common.cookie;
  219. cookie++;
  220. if (cookie < 0)
  221. cookie = 1;
  222. ioat_chan->common.cookie = new->async_tx.cookie = cookie;
  223. /* write address into NextDescriptor field of last desc in chain */
  224. to_ioat_desc(ioat_chan->used_desc.prev)->hw->next =
  225. first->async_tx.phys;
  226. __list_splice(&new_chain, ioat_chan->used_desc.prev);
  227. ioat_chan->dmacount += desc_count;
  228. ioat_chan->pending += desc_count;
  229. if (ioat_chan->pending >= ioat_pending_level)
  230. __ioat1_dma_memcpy_issue_pending(ioat_chan);
  231. spin_unlock_bh(&ioat_chan->desc_lock);
  232. return cookie;
  233. }
  234. static dma_cookie_t ioat2_tx_submit(struct dma_async_tx_descriptor *tx)
  235. {
  236. struct ioat_dma_chan *ioat_chan = to_ioat_chan(tx->chan);
  237. struct ioat_desc_sw *first = tx_to_ioat_desc(tx);
  238. struct ioat_desc_sw *new;
  239. struct ioat_dma_descriptor *hw;
  240. dma_cookie_t cookie;
  241. u32 copy;
  242. size_t len;
  243. dma_addr_t src, dst;
  244. int orig_ack;
  245. unsigned int desc_count = 0;
  246. /* src and dest and len are stored in the initial descriptor */
  247. len = first->len;
  248. src = first->src;
  249. dst = first->dst;
  250. orig_ack = first->async_tx.ack;
  251. new = first;
  252. /* ioat_chan->desc_lock is still in force in version 2 path */
  253. do {
  254. copy = min((u32) len, ioat_chan->xfercap);
  255. new->async_tx.ack = 1;
  256. hw = new->hw;
  257. hw->size = copy;
  258. hw->ctl = 0;
  259. hw->src_addr = src;
  260. hw->dst_addr = dst;
  261. len -= copy;
  262. dst += copy;
  263. src += copy;
  264. desc_count++;
  265. } while (len && (new = ioat2_dma_get_next_descriptor(ioat_chan)));
  266. hw->ctl = IOAT_DMA_DESCRIPTOR_CTL_CP_STS;
  267. if (new->async_tx.callback) {
  268. hw->ctl |= IOAT_DMA_DESCRIPTOR_CTL_INT_GN;
  269. if (first != new) {
  270. /* move callback into to last desc */
  271. new->async_tx.callback = first->async_tx.callback;
  272. new->async_tx.callback_param
  273. = first->async_tx.callback_param;
  274. first->async_tx.callback = NULL;
  275. first->async_tx.callback_param = NULL;
  276. }
  277. }
  278. new->tx_cnt = desc_count;
  279. new->async_tx.ack = orig_ack; /* client is in control of this ack */
  280. /* store the original values for use in later cleanup */
  281. if (new != first) {
  282. new->src = first->src;
  283. new->dst = first->dst;
  284. new->len = first->len;
  285. }
  286. /* cookie incr and addition to used_list must be atomic */
  287. cookie = ioat_chan->common.cookie;
  288. cookie++;
  289. if (cookie < 0)
  290. cookie = 1;
  291. ioat_chan->common.cookie = new->async_tx.cookie = cookie;
  292. ioat_chan->dmacount += desc_count;
  293. ioat_chan->pending += desc_count;
  294. if (ioat_chan->pending >= ioat_pending_level)
  295. __ioat2_dma_memcpy_issue_pending(ioat_chan);
  296. spin_unlock_bh(&ioat_chan->desc_lock);
  297. return cookie;
  298. }
  299. /**
  300. * ioat_dma_alloc_descriptor - allocate and return a sw and hw descriptor pair
  301. * @ioat_chan: the channel supplying the memory pool for the descriptors
  302. * @flags: allocation flags
  303. */
  304. static struct ioat_desc_sw *ioat_dma_alloc_descriptor(
  305. struct ioat_dma_chan *ioat_chan,
  306. gfp_t flags)
  307. {
  308. struct ioat_dma_descriptor *desc;
  309. struct ioat_desc_sw *desc_sw;
  310. struct ioatdma_device *ioatdma_device;
  311. dma_addr_t phys;
  312. ioatdma_device = to_ioatdma_device(ioat_chan->common.device);
  313. desc = pci_pool_alloc(ioatdma_device->dma_pool, flags, &phys);
  314. if (unlikely(!desc))
  315. return NULL;
  316. desc_sw = kzalloc(sizeof(*desc_sw), flags);
  317. if (unlikely(!desc_sw)) {
  318. pci_pool_free(ioatdma_device->dma_pool, desc, phys);
  319. return NULL;
  320. }
  321. memset(desc, 0, sizeof(*desc));
  322. dma_async_tx_descriptor_init(&desc_sw->async_tx, &ioat_chan->common);
  323. desc_sw->async_tx.tx_set_src = ioat_set_src;
  324. desc_sw->async_tx.tx_set_dest = ioat_set_dest;
  325. switch (ioat_chan->device->version) {
  326. case IOAT_VER_1_2:
  327. desc_sw->async_tx.tx_submit = ioat1_tx_submit;
  328. break;
  329. case IOAT_VER_2_0:
  330. desc_sw->async_tx.tx_submit = ioat2_tx_submit;
  331. break;
  332. }
  333. INIT_LIST_HEAD(&desc_sw->async_tx.tx_list);
  334. desc_sw->hw = desc;
  335. desc_sw->async_tx.phys = phys;
  336. return desc_sw;
  337. }
  338. static int ioat_initial_desc_count = 256;
  339. module_param(ioat_initial_desc_count, int, 0644);
  340. MODULE_PARM_DESC(ioat_initial_desc_count,
  341. "initial descriptors per channel (default: 256)");
  342. /**
  343. * ioat2_dma_massage_chan_desc - link the descriptors into a circle
  344. * @ioat_chan: the channel to be massaged
  345. */
  346. static void ioat2_dma_massage_chan_desc(struct ioat_dma_chan *ioat_chan)
  347. {
  348. struct ioat_desc_sw *desc, *_desc;
  349. /* setup used_desc */
  350. ioat_chan->used_desc.next = ioat_chan->free_desc.next;
  351. ioat_chan->used_desc.prev = NULL;
  352. /* pull free_desc out of the circle so that every node is a hw
  353. * descriptor, but leave it pointing to the list
  354. */
  355. ioat_chan->free_desc.prev->next = ioat_chan->free_desc.next;
  356. ioat_chan->free_desc.next->prev = ioat_chan->free_desc.prev;
  357. /* circle link the hw descriptors */
  358. desc = to_ioat_desc(ioat_chan->free_desc.next);
  359. desc->hw->next = to_ioat_desc(desc->node.next)->async_tx.phys;
  360. list_for_each_entry_safe(desc, _desc, ioat_chan->free_desc.next, node) {
  361. desc->hw->next = to_ioat_desc(desc->node.next)->async_tx.phys;
  362. }
  363. }
  364. /**
  365. * ioat_dma_alloc_chan_resources - returns the number of allocated descriptors
  366. * @chan: the channel to be filled out
  367. */
  368. static int ioat_dma_alloc_chan_resources(struct dma_chan *chan)
  369. {
  370. struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
  371. struct ioat_desc_sw *desc = NULL;
  372. u16 chanctrl;
  373. u32 chanerr;
  374. int i;
  375. LIST_HEAD(tmp_list);
  376. /* have we already been set up? */
  377. if (!list_empty(&ioat_chan->free_desc))
  378. return ioat_chan->desccount;
  379. /* Setup register to interrupt and write completion status on error */
  380. chanctrl = IOAT_CHANCTRL_ERR_INT_EN |
  381. IOAT_CHANCTRL_ANY_ERR_ABORT_EN |
  382. IOAT_CHANCTRL_ERR_COMPLETION_EN;
  383. writew(chanctrl, ioat_chan->reg_base + IOAT_CHANCTRL_OFFSET);
  384. chanerr = readl(ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
  385. if (chanerr) {
  386. dev_err(&ioat_chan->device->pdev->dev,
  387. "CHANERR = %x, clearing\n", chanerr);
  388. writel(chanerr, ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
  389. }
  390. /* Allocate descriptors */
  391. for (i = 0; i < ioat_initial_desc_count; i++) {
  392. desc = ioat_dma_alloc_descriptor(ioat_chan, GFP_KERNEL);
  393. if (!desc) {
  394. dev_err(&ioat_chan->device->pdev->dev,
  395. "Only %d initial descriptors\n", i);
  396. break;
  397. }
  398. list_add_tail(&desc->node, &tmp_list);
  399. }
  400. spin_lock_bh(&ioat_chan->desc_lock);
  401. ioat_chan->desccount = i;
  402. list_splice(&tmp_list, &ioat_chan->free_desc);
  403. if (ioat_chan->device->version != IOAT_VER_1_2)
  404. ioat2_dma_massage_chan_desc(ioat_chan);
  405. spin_unlock_bh(&ioat_chan->desc_lock);
  406. /* allocate a completion writeback area */
  407. /* doing 2 32bit writes to mmio since 1 64b write doesn't work */
  408. ioat_chan->completion_virt =
  409. pci_pool_alloc(ioat_chan->device->completion_pool,
  410. GFP_KERNEL,
  411. &ioat_chan->completion_addr);
  412. memset(ioat_chan->completion_virt, 0,
  413. sizeof(*ioat_chan->completion_virt));
  414. writel(((u64) ioat_chan->completion_addr) & 0x00000000FFFFFFFF,
  415. ioat_chan->reg_base + IOAT_CHANCMP_OFFSET_LOW);
  416. writel(((u64) ioat_chan->completion_addr) >> 32,
  417. ioat_chan->reg_base + IOAT_CHANCMP_OFFSET_HIGH);
  418. tasklet_enable(&ioat_chan->cleanup_task);
  419. ioat_dma_start_null_desc(ioat_chan); /* give chain to dma device */
  420. return ioat_chan->desccount;
  421. }
  422. /**
  423. * ioat_dma_free_chan_resources - release all the descriptors
  424. * @chan: the channel to be cleaned
  425. */
  426. static void ioat_dma_free_chan_resources(struct dma_chan *chan)
  427. {
  428. struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
  429. struct ioatdma_device *ioatdma_device = to_ioatdma_device(chan->device);
  430. struct ioat_desc_sw *desc, *_desc;
  431. int in_use_descs = 0;
  432. tasklet_disable(&ioat_chan->cleanup_task);
  433. ioat_dma_memcpy_cleanup(ioat_chan);
  434. /* Delay 100ms after reset to allow internal DMA logic to quiesce
  435. * before removing DMA descriptor resources.
  436. */
  437. writeb(IOAT_CHANCMD_RESET,
  438. ioat_chan->reg_base
  439. + IOAT_CHANCMD_OFFSET(ioat_chan->device->version));
  440. mdelay(100);
  441. spin_lock_bh(&ioat_chan->desc_lock);
  442. switch (ioat_chan->device->version) {
  443. case IOAT_VER_1_2:
  444. list_for_each_entry_safe(desc, _desc,
  445. &ioat_chan->used_desc, node) {
  446. in_use_descs++;
  447. list_del(&desc->node);
  448. pci_pool_free(ioatdma_device->dma_pool, desc->hw,
  449. desc->async_tx.phys);
  450. kfree(desc);
  451. }
  452. list_for_each_entry_safe(desc, _desc,
  453. &ioat_chan->free_desc, node) {
  454. list_del(&desc->node);
  455. pci_pool_free(ioatdma_device->dma_pool, desc->hw,
  456. desc->async_tx.phys);
  457. kfree(desc);
  458. }
  459. break;
  460. case IOAT_VER_2_0:
  461. list_for_each_entry_safe(desc, _desc,
  462. ioat_chan->free_desc.next, node) {
  463. list_del(&desc->node);
  464. pci_pool_free(ioatdma_device->dma_pool, desc->hw,
  465. desc->async_tx.phys);
  466. kfree(desc);
  467. }
  468. desc = to_ioat_desc(ioat_chan->free_desc.next);
  469. pci_pool_free(ioatdma_device->dma_pool, desc->hw,
  470. desc->async_tx.phys);
  471. kfree(desc);
  472. INIT_LIST_HEAD(&ioat_chan->free_desc);
  473. INIT_LIST_HEAD(&ioat_chan->used_desc);
  474. break;
  475. }
  476. spin_unlock_bh(&ioat_chan->desc_lock);
  477. pci_pool_free(ioatdma_device->completion_pool,
  478. ioat_chan->completion_virt,
  479. ioat_chan->completion_addr);
  480. /* one is ok since we left it on there on purpose */
  481. if (in_use_descs > 1)
  482. dev_err(&ioat_chan->device->pdev->dev,
  483. "Freeing %d in use descriptors!\n",
  484. in_use_descs - 1);
  485. ioat_chan->last_completion = ioat_chan->completion_addr = 0;
  486. ioat_chan->pending = 0;
  487. ioat_chan->dmacount = 0;
  488. }
  489. /**
  490. * ioat_dma_get_next_descriptor - return the next available descriptor
  491. * @ioat_chan: IOAT DMA channel handle
  492. *
  493. * Gets the next descriptor from the chain, and must be called with the
  494. * channel's desc_lock held. Allocates more descriptors if the channel
  495. * has run out.
  496. */
  497. static struct ioat_desc_sw *
  498. ioat1_dma_get_next_descriptor(struct ioat_dma_chan *ioat_chan)
  499. {
  500. struct ioat_desc_sw *new = NULL;
  501. if (!list_empty(&ioat_chan->free_desc)) {
  502. new = to_ioat_desc(ioat_chan->free_desc.next);
  503. list_del(&new->node);
  504. } else {
  505. /* try to get another desc */
  506. new = ioat_dma_alloc_descriptor(ioat_chan, GFP_ATOMIC);
  507. /* will this ever happen? */
  508. /* TODO add upper limit on these */
  509. BUG_ON(!new);
  510. }
  511. prefetch(new->hw);
  512. return new;
  513. }
  514. static struct ioat_desc_sw *
  515. ioat2_dma_get_next_descriptor(struct ioat_dma_chan *ioat_chan)
  516. {
  517. struct ioat_desc_sw *new = NULL;
  518. /*
  519. * used.prev points to where to start processing
  520. * used.next points to next free descriptor
  521. * if used.prev == NULL, there are none waiting to be processed
  522. * if used.next == used.prev.prev, there is only one free descriptor,
  523. * and we need to use it to as a noop descriptor before
  524. * linking in a new set of descriptors, since the device
  525. * has probably already read the pointer to it
  526. */
  527. if (ioat_chan->used_desc.prev &&
  528. ioat_chan->used_desc.next == ioat_chan->used_desc.prev->prev) {
  529. struct ioat_desc_sw *desc = NULL;
  530. struct ioat_desc_sw *noop_desc = NULL;
  531. int i;
  532. /* set up the noop descriptor */
  533. noop_desc = to_ioat_desc(ioat_chan->used_desc.next);
  534. noop_desc->hw->size = 0;
  535. noop_desc->hw->ctl = IOAT_DMA_DESCRIPTOR_NUL;
  536. noop_desc->hw->src_addr = 0;
  537. noop_desc->hw->dst_addr = 0;
  538. ioat_chan->used_desc.next = ioat_chan->used_desc.next->next;
  539. ioat_chan->pending++;
  540. ioat_chan->dmacount++;
  541. /* get a few more descriptors */
  542. for (i = 16; i; i--) {
  543. desc = ioat_dma_alloc_descriptor(ioat_chan, GFP_ATOMIC);
  544. BUG_ON(!desc);
  545. list_add_tail(&desc->node, ioat_chan->used_desc.next);
  546. desc->hw->next
  547. = to_ioat_desc(desc->node.next)->async_tx.phys;
  548. to_ioat_desc(desc->node.prev)->hw->next
  549. = desc->async_tx.phys;
  550. ioat_chan->desccount++;
  551. }
  552. ioat_chan->used_desc.next = noop_desc->node.next;
  553. }
  554. new = to_ioat_desc(ioat_chan->used_desc.next);
  555. prefetch(new);
  556. ioat_chan->used_desc.next = new->node.next;
  557. if (ioat_chan->used_desc.prev == NULL)
  558. ioat_chan->used_desc.prev = &new->node;
  559. prefetch(new->hw);
  560. return new;
  561. }
  562. static struct ioat_desc_sw *ioat_dma_get_next_descriptor(
  563. struct ioat_dma_chan *ioat_chan)
  564. {
  565. if (!ioat_chan)
  566. return NULL;
  567. switch (ioat_chan->device->version) {
  568. case IOAT_VER_1_2:
  569. return ioat1_dma_get_next_descriptor(ioat_chan);
  570. break;
  571. case IOAT_VER_2_0:
  572. return ioat2_dma_get_next_descriptor(ioat_chan);
  573. break;
  574. }
  575. return NULL;
  576. }
  577. static struct dma_async_tx_descriptor *ioat1_dma_prep_memcpy(
  578. struct dma_chan *chan,
  579. size_t len,
  580. int int_en)
  581. {
  582. struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
  583. struct ioat_desc_sw *new;
  584. spin_lock_bh(&ioat_chan->desc_lock);
  585. new = ioat_dma_get_next_descriptor(ioat_chan);
  586. new->len = len;
  587. spin_unlock_bh(&ioat_chan->desc_lock);
  588. return new ? &new->async_tx : NULL;
  589. }
  590. static struct dma_async_tx_descriptor *ioat2_dma_prep_memcpy(
  591. struct dma_chan *chan,
  592. size_t len,
  593. int int_en)
  594. {
  595. struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
  596. struct ioat_desc_sw *new;
  597. spin_lock_bh(&ioat_chan->desc_lock);
  598. new = ioat2_dma_get_next_descriptor(ioat_chan);
  599. new->len = len;
  600. /* leave ioat_chan->desc_lock set in version 2 path */
  601. return new ? &new->async_tx : NULL;
  602. }
  603. /**
  604. * ioat_dma_memcpy_issue_pending - push potentially unrecognized appended
  605. * descriptors to hw
  606. * @chan: DMA channel handle
  607. */
  608. static inline void __ioat1_dma_memcpy_issue_pending(
  609. struct ioat_dma_chan *ioat_chan)
  610. {
  611. ioat_chan->pending = 0;
  612. writeb(IOAT_CHANCMD_APPEND, ioat_chan->reg_base + IOAT1_CHANCMD_OFFSET);
  613. }
  614. static void ioat1_dma_memcpy_issue_pending(struct dma_chan *chan)
  615. {
  616. struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
  617. if (ioat_chan->pending != 0) {
  618. spin_lock_bh(&ioat_chan->desc_lock);
  619. __ioat1_dma_memcpy_issue_pending(ioat_chan);
  620. spin_unlock_bh(&ioat_chan->desc_lock);
  621. }
  622. }
  623. static inline void __ioat2_dma_memcpy_issue_pending(
  624. struct ioat_dma_chan *ioat_chan)
  625. {
  626. ioat_chan->pending = 0;
  627. writew(ioat_chan->dmacount,
  628. ioat_chan->reg_base + IOAT_CHAN_DMACOUNT_OFFSET);
  629. }
  630. static void ioat2_dma_memcpy_issue_pending(struct dma_chan *chan)
  631. {
  632. struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
  633. if (ioat_chan->pending != 0) {
  634. spin_lock_bh(&ioat_chan->desc_lock);
  635. __ioat2_dma_memcpy_issue_pending(ioat_chan);
  636. spin_unlock_bh(&ioat_chan->desc_lock);
  637. }
  638. }
  639. static void ioat_dma_cleanup_tasklet(unsigned long data)
  640. {
  641. struct ioat_dma_chan *chan = (void *)data;
  642. ioat_dma_memcpy_cleanup(chan);
  643. writew(IOAT_CHANCTRL_INT_DISABLE,
  644. chan->reg_base + IOAT_CHANCTRL_OFFSET);
  645. }
  646. /**
  647. * ioat_dma_memcpy_cleanup - cleanup up finished descriptors
  648. * @chan: ioat channel to be cleaned up
  649. */
  650. static void ioat_dma_memcpy_cleanup(struct ioat_dma_chan *ioat_chan)
  651. {
  652. unsigned long phys_complete;
  653. struct ioat_desc_sw *desc, *_desc;
  654. dma_cookie_t cookie = 0;
  655. unsigned long desc_phys;
  656. struct ioat_desc_sw *latest_desc;
  657. prefetch(ioat_chan->completion_virt);
  658. if (!spin_trylock_bh(&ioat_chan->cleanup_lock))
  659. return;
  660. /* The completion writeback can happen at any time,
  661. so reads by the driver need to be atomic operations
  662. The descriptor physical addresses are limited to 32-bits
  663. when the CPU can only do a 32-bit mov */
  664. #if (BITS_PER_LONG == 64)
  665. phys_complete =
  666. ioat_chan->completion_virt->full
  667. & IOAT_CHANSTS_COMPLETED_DESCRIPTOR_ADDR;
  668. #else
  669. phys_complete =
  670. ioat_chan->completion_virt->low & IOAT_LOW_COMPLETION_MASK;
  671. #endif
  672. if ((ioat_chan->completion_virt->full
  673. & IOAT_CHANSTS_DMA_TRANSFER_STATUS) ==
  674. IOAT_CHANSTS_DMA_TRANSFER_STATUS_HALTED) {
  675. dev_err(&ioat_chan->device->pdev->dev,
  676. "Channel halted, chanerr = %x\n",
  677. readl(ioat_chan->reg_base + IOAT_CHANERR_OFFSET));
  678. /* TODO do something to salvage the situation */
  679. }
  680. if (phys_complete == ioat_chan->last_completion) {
  681. spin_unlock_bh(&ioat_chan->cleanup_lock);
  682. return;
  683. }
  684. cookie = 0;
  685. spin_lock_bh(&ioat_chan->desc_lock);
  686. switch (ioat_chan->device->version) {
  687. case IOAT_VER_1_2:
  688. list_for_each_entry_safe(desc, _desc,
  689. &ioat_chan->used_desc, node) {
  690. /*
  691. * Incoming DMA requests may use multiple descriptors,
  692. * due to exceeding xfercap, perhaps. If so, only the
  693. * last one will have a cookie, and require unmapping.
  694. */
  695. if (desc->async_tx.cookie) {
  696. cookie = desc->async_tx.cookie;
  697. /*
  698. * yes we are unmapping both _page and _single
  699. * alloc'd regions with unmap_page. Is this
  700. * *really* that bad?
  701. */
  702. pci_unmap_page(ioat_chan->device->pdev,
  703. pci_unmap_addr(desc, dst),
  704. pci_unmap_len(desc, len),
  705. PCI_DMA_FROMDEVICE);
  706. pci_unmap_page(ioat_chan->device->pdev,
  707. pci_unmap_addr(desc, src),
  708. pci_unmap_len(desc, len),
  709. PCI_DMA_TODEVICE);
  710. if (desc->async_tx.callback) {
  711. desc->async_tx.callback(desc->async_tx.callback_param);
  712. desc->async_tx.callback = NULL;
  713. }
  714. }
  715. if (desc->async_tx.phys != phys_complete) {
  716. /*
  717. * a completed entry, but not the last, so clean
  718. * up if the client is done with the descriptor
  719. */
  720. if (desc->async_tx.ack) {
  721. list_del(&desc->node);
  722. list_add_tail(&desc->node,
  723. &ioat_chan->free_desc);
  724. } else
  725. desc->async_tx.cookie = 0;
  726. } else {
  727. /*
  728. * last used desc. Do not remove, so we can
  729. * append from it, but don't look at it next
  730. * time, either
  731. */
  732. desc->async_tx.cookie = 0;
  733. /* TODO check status bits? */
  734. break;
  735. }
  736. }
  737. break;
  738. case IOAT_VER_2_0:
  739. /* has some other thread has already cleaned up? */
  740. if (ioat_chan->used_desc.prev == NULL)
  741. break;
  742. /* work backwards to find latest finished desc */
  743. desc = to_ioat_desc(ioat_chan->used_desc.next);
  744. latest_desc = NULL;
  745. do {
  746. desc = to_ioat_desc(desc->node.prev);
  747. desc_phys = (unsigned long)desc->async_tx.phys
  748. & IOAT_CHANSTS_COMPLETED_DESCRIPTOR_ADDR;
  749. if (desc_phys == phys_complete) {
  750. latest_desc = desc;
  751. break;
  752. }
  753. } while (&desc->node != ioat_chan->used_desc.prev);
  754. if (latest_desc != NULL) {
  755. /* work forwards to clear finished descriptors */
  756. for (desc = to_ioat_desc(ioat_chan->used_desc.prev);
  757. &desc->node != latest_desc->node.next &&
  758. &desc->node != ioat_chan->used_desc.next;
  759. desc = to_ioat_desc(desc->node.next)) {
  760. if (desc->async_tx.cookie) {
  761. cookie = desc->async_tx.cookie;
  762. desc->async_tx.cookie = 0;
  763. pci_unmap_page(ioat_chan->device->pdev,
  764. pci_unmap_addr(desc, dst),
  765. pci_unmap_len(desc, len),
  766. PCI_DMA_FROMDEVICE);
  767. pci_unmap_page(ioat_chan->device->pdev,
  768. pci_unmap_addr(desc, src),
  769. pci_unmap_len(desc, len),
  770. PCI_DMA_TODEVICE);
  771. if (desc->async_tx.callback) {
  772. desc->async_tx.callback(desc->async_tx.callback_param);
  773. desc->async_tx.callback = NULL;
  774. }
  775. }
  776. }
  777. /* move used.prev up beyond those that are finished */
  778. if (&desc->node == ioat_chan->used_desc.next)
  779. ioat_chan->used_desc.prev = NULL;
  780. else
  781. ioat_chan->used_desc.prev = &desc->node;
  782. }
  783. break;
  784. }
  785. spin_unlock_bh(&ioat_chan->desc_lock);
  786. ioat_chan->last_completion = phys_complete;
  787. if (cookie != 0)
  788. ioat_chan->completed_cookie = cookie;
  789. spin_unlock_bh(&ioat_chan->cleanup_lock);
  790. }
  791. static void ioat_dma_dependency_added(struct dma_chan *chan)
  792. {
  793. struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
  794. spin_lock_bh(&ioat_chan->desc_lock);
  795. if (ioat_chan->pending == 0) {
  796. spin_unlock_bh(&ioat_chan->desc_lock);
  797. ioat_dma_memcpy_cleanup(ioat_chan);
  798. } else
  799. spin_unlock_bh(&ioat_chan->desc_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. desc->async_tx.ack = 1;
  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 = NULL;
  889. dma_addr_t addr;
  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. tx = device->common.device_prep_dma_memcpy(dma_chan, IOAT_TEST_SIZE, 0);
  914. if (!tx) {
  915. dev_err(&device->pdev->dev,
  916. "Self-test prep failed, disabling\n");
  917. err = -ENODEV;
  918. goto free_resources;
  919. }
  920. async_tx_ack(tx);
  921. addr = dma_map_single(dma_chan->device->dev, src, IOAT_TEST_SIZE,
  922. DMA_TO_DEVICE);
  923. tx->tx_set_src(addr, tx, 0);
  924. addr = dma_map_single(dma_chan->device->dev, dest, IOAT_TEST_SIZE,
  925. DMA_FROM_DEVICE);
  926. tx->tx_set_dest(addr, tx, 0);
  927. tx->callback = ioat_dma_test_callback;
  928. tx->callback_param = (void *)0x8086;
  929. cookie = tx->tx_submit(tx);
  930. if (cookie < 0) {
  931. dev_err(&device->pdev->dev,
  932. "Self-test setup failed, disabling\n");
  933. err = -ENODEV;
  934. goto free_resources;
  935. }
  936. device->common.device_issue_pending(dma_chan);
  937. msleep(1);
  938. if (device->common.device_is_tx_complete(dma_chan, cookie, NULL, NULL)
  939. != DMA_SUCCESS) {
  940. dev_err(&device->pdev->dev,
  941. "Self-test copy timed out, disabling\n");
  942. err = -ENODEV;
  943. goto free_resources;
  944. }
  945. if (memcmp(src, dest, IOAT_TEST_SIZE)) {
  946. dev_err(&device->pdev->dev,
  947. "Self-test copy failed compare, disabling\n");
  948. err = -ENODEV;
  949. goto free_resources;
  950. }
  951. free_resources:
  952. device->common.device_free_chan_resources(dma_chan);
  953. out:
  954. kfree(src);
  955. kfree(dest);
  956. return err;
  957. }
  958. static char ioat_interrupt_style[32] = "msix";
  959. module_param_string(ioat_interrupt_style, ioat_interrupt_style,
  960. sizeof(ioat_interrupt_style), 0644);
  961. MODULE_PARM_DESC(ioat_interrupt_style,
  962. "set ioat interrupt style: msix (default), "
  963. "msix-single-vector, msi, intx)");
  964. /**
  965. * ioat_dma_setup_interrupts - setup interrupt handler
  966. * @device: ioat device
  967. */
  968. static int ioat_dma_setup_interrupts(struct ioatdma_device *device)
  969. {
  970. struct ioat_dma_chan *ioat_chan;
  971. int err, i, j, msixcnt;
  972. u8 intrctrl = 0;
  973. if (!strcmp(ioat_interrupt_style, "msix"))
  974. goto msix;
  975. if (!strcmp(ioat_interrupt_style, "msix-single-vector"))
  976. goto msix_single_vector;
  977. if (!strcmp(ioat_interrupt_style, "msi"))
  978. goto msi;
  979. if (!strcmp(ioat_interrupt_style, "intx"))
  980. goto intx;
  981. dev_err(&device->pdev->dev, "invalid ioat_interrupt_style %s\n",
  982. ioat_interrupt_style);
  983. goto err_no_irq;
  984. msix:
  985. /* The number of MSI-X vectors should equal the number of channels */
  986. msixcnt = device->common.chancnt;
  987. for (i = 0; i < msixcnt; i++)
  988. device->msix_entries[i].entry = i;
  989. err = pci_enable_msix(device->pdev, device->msix_entries, msixcnt);
  990. if (err < 0)
  991. goto msi;
  992. if (err > 0)
  993. goto msix_single_vector;
  994. for (i = 0; i < msixcnt; i++) {
  995. ioat_chan = ioat_lookup_chan_by_index(device, i);
  996. err = request_irq(device->msix_entries[i].vector,
  997. ioat_dma_do_interrupt_msix,
  998. 0, "ioat-msix", ioat_chan);
  999. if (err) {
  1000. for (j = 0; j < i; j++) {
  1001. ioat_chan =
  1002. ioat_lookup_chan_by_index(device, j);
  1003. free_irq(device->msix_entries[j].vector,
  1004. ioat_chan);
  1005. }
  1006. goto msix_single_vector;
  1007. }
  1008. }
  1009. intrctrl |= IOAT_INTRCTRL_MSIX_VECTOR_CONTROL;
  1010. device->irq_mode = msix_multi_vector;
  1011. goto done;
  1012. msix_single_vector:
  1013. device->msix_entries[0].entry = 0;
  1014. err = pci_enable_msix(device->pdev, device->msix_entries, 1);
  1015. if (err)
  1016. goto msi;
  1017. err = request_irq(device->msix_entries[0].vector, ioat_dma_do_interrupt,
  1018. 0, "ioat-msix", device);
  1019. if (err) {
  1020. pci_disable_msix(device->pdev);
  1021. goto msi;
  1022. }
  1023. device->irq_mode = msix_single_vector;
  1024. goto done;
  1025. msi:
  1026. err = pci_enable_msi(device->pdev);
  1027. if (err)
  1028. goto intx;
  1029. err = request_irq(device->pdev->irq, ioat_dma_do_interrupt,
  1030. 0, "ioat-msi", device);
  1031. if (err) {
  1032. pci_disable_msi(device->pdev);
  1033. goto intx;
  1034. }
  1035. /*
  1036. * CB 1.2 devices need a bit set in configuration space to enable MSI
  1037. */
  1038. if (device->version == IOAT_VER_1_2) {
  1039. u32 dmactrl;
  1040. pci_read_config_dword(device->pdev,
  1041. IOAT_PCI_DMACTRL_OFFSET, &dmactrl);
  1042. dmactrl |= IOAT_PCI_DMACTRL_MSI_EN;
  1043. pci_write_config_dword(device->pdev,
  1044. IOAT_PCI_DMACTRL_OFFSET, dmactrl);
  1045. }
  1046. device->irq_mode = msi;
  1047. goto done;
  1048. intx:
  1049. err = request_irq(device->pdev->irq, ioat_dma_do_interrupt,
  1050. IRQF_SHARED, "ioat-intx", device);
  1051. if (err)
  1052. goto err_no_irq;
  1053. device->irq_mode = intx;
  1054. done:
  1055. intrctrl |= IOAT_INTRCTRL_MASTER_INT_EN;
  1056. writeb(intrctrl, device->reg_base + IOAT_INTRCTRL_OFFSET);
  1057. return 0;
  1058. err_no_irq:
  1059. /* Disable all interrupt generation */
  1060. writeb(0, device->reg_base + IOAT_INTRCTRL_OFFSET);
  1061. dev_err(&device->pdev->dev, "no usable interrupts\n");
  1062. device->irq_mode = none;
  1063. return -1;
  1064. }
  1065. /**
  1066. * ioat_dma_remove_interrupts - remove whatever interrupts were set
  1067. * @device: ioat device
  1068. */
  1069. static void ioat_dma_remove_interrupts(struct ioatdma_device *device)
  1070. {
  1071. struct ioat_dma_chan *ioat_chan;
  1072. int i;
  1073. /* Disable all interrupt generation */
  1074. writeb(0, device->reg_base + IOAT_INTRCTRL_OFFSET);
  1075. switch (device->irq_mode) {
  1076. case msix_multi_vector:
  1077. for (i = 0; i < device->common.chancnt; i++) {
  1078. ioat_chan = ioat_lookup_chan_by_index(device, i);
  1079. free_irq(device->msix_entries[i].vector, ioat_chan);
  1080. }
  1081. pci_disable_msix(device->pdev);
  1082. break;
  1083. case msix_single_vector:
  1084. free_irq(device->msix_entries[0].vector, device);
  1085. pci_disable_msix(device->pdev);
  1086. break;
  1087. case msi:
  1088. free_irq(device->pdev->irq, device);
  1089. pci_disable_msi(device->pdev);
  1090. break;
  1091. case intx:
  1092. free_irq(device->pdev->irq, device);
  1093. break;
  1094. case none:
  1095. dev_warn(&device->pdev->dev,
  1096. "call to %s without interrupts setup\n", __func__);
  1097. }
  1098. device->irq_mode = none;
  1099. }
  1100. struct ioatdma_device *ioat_dma_probe(struct pci_dev *pdev,
  1101. void __iomem *iobase)
  1102. {
  1103. int err;
  1104. struct ioatdma_device *device;
  1105. device = kzalloc(sizeof(*device), GFP_KERNEL);
  1106. if (!device) {
  1107. err = -ENOMEM;
  1108. goto err_kzalloc;
  1109. }
  1110. device->pdev = pdev;
  1111. device->reg_base = iobase;
  1112. device->version = readb(device->reg_base + IOAT_VER_OFFSET);
  1113. /* DMA coherent memory pool for DMA descriptor allocations */
  1114. device->dma_pool = pci_pool_create("dma_desc_pool", pdev,
  1115. sizeof(struct ioat_dma_descriptor),
  1116. 64, 0);
  1117. if (!device->dma_pool) {
  1118. err = -ENOMEM;
  1119. goto err_dma_pool;
  1120. }
  1121. device->completion_pool = pci_pool_create("completion_pool", pdev,
  1122. sizeof(u64), SMP_CACHE_BYTES,
  1123. SMP_CACHE_BYTES);
  1124. if (!device->completion_pool) {
  1125. err = -ENOMEM;
  1126. goto err_completion_pool;
  1127. }
  1128. INIT_LIST_HEAD(&device->common.channels);
  1129. ioat_dma_enumerate_channels(device);
  1130. device->common.device_alloc_chan_resources =
  1131. ioat_dma_alloc_chan_resources;
  1132. device->common.device_free_chan_resources =
  1133. ioat_dma_free_chan_resources;
  1134. device->common.dev = &pdev->dev;
  1135. dma_cap_set(DMA_MEMCPY, device->common.cap_mask);
  1136. device->common.device_is_tx_complete = ioat_dma_is_complete;
  1137. device->common.device_dependency_added = ioat_dma_dependency_added;
  1138. switch (device->version) {
  1139. case IOAT_VER_1_2:
  1140. device->common.device_prep_dma_memcpy = ioat1_dma_prep_memcpy;
  1141. device->common.device_issue_pending =
  1142. ioat1_dma_memcpy_issue_pending;
  1143. break;
  1144. case IOAT_VER_2_0:
  1145. device->common.device_prep_dma_memcpy = ioat2_dma_prep_memcpy;
  1146. device->common.device_issue_pending =
  1147. ioat2_dma_memcpy_issue_pending;
  1148. break;
  1149. }
  1150. dev_err(&device->pdev->dev,
  1151. "Intel(R) I/OAT DMA Engine found,"
  1152. " %d channels, device version 0x%02x, driver version %s\n",
  1153. device->common.chancnt, device->version, IOAT_DMA_VERSION);
  1154. err = ioat_dma_setup_interrupts(device);
  1155. if (err)
  1156. goto err_setup_interrupts;
  1157. err = ioat_dma_self_test(device);
  1158. if (err)
  1159. goto err_self_test;
  1160. dma_async_device_register(&device->common);
  1161. return device;
  1162. err_self_test:
  1163. ioat_dma_remove_interrupts(device);
  1164. err_setup_interrupts:
  1165. pci_pool_destroy(device->completion_pool);
  1166. err_completion_pool:
  1167. pci_pool_destroy(device->dma_pool);
  1168. err_dma_pool:
  1169. kfree(device);
  1170. err_kzalloc:
  1171. dev_err(&device->pdev->dev,
  1172. "Intel(R) I/OAT DMA Engine initialization failed\n");
  1173. return NULL;
  1174. }
  1175. void ioat_dma_remove(struct ioatdma_device *device)
  1176. {
  1177. struct dma_chan *chan, *_chan;
  1178. struct ioat_dma_chan *ioat_chan;
  1179. ioat_dma_remove_interrupts(device);
  1180. dma_async_device_unregister(&device->common);
  1181. pci_pool_destroy(device->dma_pool);
  1182. pci_pool_destroy(device->completion_pool);
  1183. iounmap(device->reg_base);
  1184. pci_release_regions(device->pdev);
  1185. pci_disable_device(device->pdev);
  1186. list_for_each_entry_safe(chan, _chan,
  1187. &device->common.channels, device_node) {
  1188. ioat_chan = to_ioat_chan(chan);
  1189. list_del(&chan->device_node);
  1190. kfree(ioat_chan);
  1191. }
  1192. kfree(device);
  1193. }