ioat_dma.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386
  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. int orig_ack;
  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_ack = first->async_tx.ack;
  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. new->async_tx.ack = 1;
  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.ack = orig_ack; /* 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. int orig_ack;
  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_ack = first->async_tx.ack;
  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. new->async_tx.ack = 1;
  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.ack = orig_ack; /* 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. return &new->async_tx;
  621. } else
  622. return NULL;
  623. }
  624. static struct dma_async_tx_descriptor *ioat2_dma_prep_memcpy(
  625. struct dma_chan *chan,
  626. dma_addr_t dma_dest,
  627. dma_addr_t dma_src,
  628. size_t len,
  629. unsigned long flags)
  630. {
  631. struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
  632. struct ioat_desc_sw *new;
  633. spin_lock_bh(&ioat_chan->desc_lock);
  634. new = ioat2_dma_get_next_descriptor(ioat_chan);
  635. /*
  636. * leave ioat_chan->desc_lock set in ioat 2 path
  637. * it will get unlocked at end of tx_submit
  638. */
  639. if (new) {
  640. new->len = len;
  641. new->dst = dma_dest;
  642. new->src = dma_src;
  643. return &new->async_tx;
  644. } else
  645. return NULL;
  646. }
  647. static void ioat_dma_cleanup_tasklet(unsigned long data)
  648. {
  649. struct ioat_dma_chan *chan = (void *)data;
  650. ioat_dma_memcpy_cleanup(chan);
  651. writew(IOAT_CHANCTRL_INT_DISABLE,
  652. chan->reg_base + IOAT_CHANCTRL_OFFSET);
  653. }
  654. /**
  655. * ioat_dma_memcpy_cleanup - cleanup up finished descriptors
  656. * @chan: ioat channel to be cleaned up
  657. */
  658. static void ioat_dma_memcpy_cleanup(struct ioat_dma_chan *ioat_chan)
  659. {
  660. unsigned long phys_complete;
  661. struct ioat_desc_sw *desc, *_desc;
  662. dma_cookie_t cookie = 0;
  663. unsigned long desc_phys;
  664. struct ioat_desc_sw *latest_desc;
  665. prefetch(ioat_chan->completion_virt);
  666. if (!spin_trylock_bh(&ioat_chan->cleanup_lock))
  667. return;
  668. /* The completion writeback can happen at any time,
  669. so reads by the driver need to be atomic operations
  670. The descriptor physical addresses are limited to 32-bits
  671. when the CPU can only do a 32-bit mov */
  672. #if (BITS_PER_LONG == 64)
  673. phys_complete =
  674. ioat_chan->completion_virt->full
  675. & IOAT_CHANSTS_COMPLETED_DESCRIPTOR_ADDR;
  676. #else
  677. phys_complete =
  678. ioat_chan->completion_virt->low & IOAT_LOW_COMPLETION_MASK;
  679. #endif
  680. if ((ioat_chan->completion_virt->full
  681. & IOAT_CHANSTS_DMA_TRANSFER_STATUS) ==
  682. IOAT_CHANSTS_DMA_TRANSFER_STATUS_HALTED) {
  683. dev_err(&ioat_chan->device->pdev->dev,
  684. "Channel halted, chanerr = %x\n",
  685. readl(ioat_chan->reg_base + IOAT_CHANERR_OFFSET));
  686. /* TODO do something to salvage the situation */
  687. }
  688. if (phys_complete == ioat_chan->last_completion) {
  689. spin_unlock_bh(&ioat_chan->cleanup_lock);
  690. return;
  691. }
  692. cookie = 0;
  693. spin_lock_bh(&ioat_chan->desc_lock);
  694. switch (ioat_chan->device->version) {
  695. case IOAT_VER_1_2:
  696. list_for_each_entry_safe(desc, _desc,
  697. &ioat_chan->used_desc, node) {
  698. /*
  699. * Incoming DMA requests may use multiple descriptors,
  700. * due to exceeding xfercap, perhaps. If so, only the
  701. * last one will have a cookie, and require unmapping.
  702. */
  703. if (desc->async_tx.cookie) {
  704. cookie = desc->async_tx.cookie;
  705. /*
  706. * yes we are unmapping both _page and _single
  707. * alloc'd regions with unmap_page. Is this
  708. * *really* that bad?
  709. */
  710. pci_unmap_page(ioat_chan->device->pdev,
  711. pci_unmap_addr(desc, dst),
  712. pci_unmap_len(desc, len),
  713. PCI_DMA_FROMDEVICE);
  714. pci_unmap_page(ioat_chan->device->pdev,
  715. pci_unmap_addr(desc, src),
  716. pci_unmap_len(desc, len),
  717. PCI_DMA_TODEVICE);
  718. if (desc->async_tx.callback) {
  719. desc->async_tx.callback(desc->async_tx.callback_param);
  720. desc->async_tx.callback = NULL;
  721. }
  722. }
  723. if (desc->async_tx.phys != phys_complete) {
  724. /*
  725. * a completed entry, but not the last, so clean
  726. * up if the client is done with the descriptor
  727. */
  728. if (desc->async_tx.ack) {
  729. list_del(&desc->node);
  730. list_add_tail(&desc->node,
  731. &ioat_chan->free_desc);
  732. } else
  733. desc->async_tx.cookie = 0;
  734. } else {
  735. /*
  736. * last used desc. Do not remove, so we can
  737. * append from it, but don't look at it next
  738. * time, either
  739. */
  740. desc->async_tx.cookie = 0;
  741. /* TODO check status bits? */
  742. break;
  743. }
  744. }
  745. break;
  746. case IOAT_VER_2_0:
  747. /* has some other thread has already cleaned up? */
  748. if (ioat_chan->used_desc.prev == NULL)
  749. break;
  750. /* work backwards to find latest finished desc */
  751. desc = to_ioat_desc(ioat_chan->used_desc.next);
  752. latest_desc = NULL;
  753. do {
  754. desc = to_ioat_desc(desc->node.prev);
  755. desc_phys = (unsigned long)desc->async_tx.phys
  756. & IOAT_CHANSTS_COMPLETED_DESCRIPTOR_ADDR;
  757. if (desc_phys == phys_complete) {
  758. latest_desc = desc;
  759. break;
  760. }
  761. } while (&desc->node != ioat_chan->used_desc.prev);
  762. if (latest_desc != NULL) {
  763. /* work forwards to clear finished descriptors */
  764. for (desc = to_ioat_desc(ioat_chan->used_desc.prev);
  765. &desc->node != latest_desc->node.next &&
  766. &desc->node != ioat_chan->used_desc.next;
  767. desc = to_ioat_desc(desc->node.next)) {
  768. if (desc->async_tx.cookie) {
  769. cookie = desc->async_tx.cookie;
  770. desc->async_tx.cookie = 0;
  771. pci_unmap_page(ioat_chan->device->pdev,
  772. pci_unmap_addr(desc, dst),
  773. pci_unmap_len(desc, len),
  774. PCI_DMA_FROMDEVICE);
  775. pci_unmap_page(ioat_chan->device->pdev,
  776. pci_unmap_addr(desc, src),
  777. pci_unmap_len(desc, len),
  778. PCI_DMA_TODEVICE);
  779. if (desc->async_tx.callback) {
  780. desc->async_tx.callback(desc->async_tx.callback_param);
  781. desc->async_tx.callback = NULL;
  782. }
  783. }
  784. }
  785. /* move used.prev up beyond those that are finished */
  786. if (&desc->node == ioat_chan->used_desc.next)
  787. ioat_chan->used_desc.prev = NULL;
  788. else
  789. ioat_chan->used_desc.prev = &desc->node;
  790. }
  791. break;
  792. }
  793. spin_unlock_bh(&ioat_chan->desc_lock);
  794. ioat_chan->last_completion = phys_complete;
  795. if (cookie != 0)
  796. ioat_chan->completed_cookie = cookie;
  797. spin_unlock_bh(&ioat_chan->cleanup_lock);
  798. }
  799. static void ioat_dma_dependency_added(struct dma_chan *chan)
  800. {
  801. struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
  802. spin_lock_bh(&ioat_chan->desc_lock);
  803. if (ioat_chan->pending == 0) {
  804. spin_unlock_bh(&ioat_chan->desc_lock);
  805. ioat_dma_memcpy_cleanup(ioat_chan);
  806. } else
  807. spin_unlock_bh(&ioat_chan->desc_lock);
  808. }
  809. /**
  810. * ioat_dma_is_complete - poll the status of a IOAT DMA transaction
  811. * @chan: IOAT DMA channel handle
  812. * @cookie: DMA transaction identifier
  813. * @done: if not %NULL, updated with last completed transaction
  814. * @used: if not %NULL, updated with last used transaction
  815. */
  816. static enum dma_status ioat_dma_is_complete(struct dma_chan *chan,
  817. dma_cookie_t cookie,
  818. dma_cookie_t *done,
  819. dma_cookie_t *used)
  820. {
  821. struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
  822. dma_cookie_t last_used;
  823. dma_cookie_t last_complete;
  824. enum dma_status ret;
  825. last_used = chan->cookie;
  826. last_complete = ioat_chan->completed_cookie;
  827. if (done)
  828. *done = last_complete;
  829. if (used)
  830. *used = last_used;
  831. ret = dma_async_is_complete(cookie, last_complete, last_used);
  832. if (ret == DMA_SUCCESS)
  833. return ret;
  834. ioat_dma_memcpy_cleanup(ioat_chan);
  835. last_used = chan->cookie;
  836. last_complete = ioat_chan->completed_cookie;
  837. if (done)
  838. *done = last_complete;
  839. if (used)
  840. *used = last_used;
  841. return dma_async_is_complete(cookie, last_complete, last_used);
  842. }
  843. static void ioat_dma_start_null_desc(struct ioat_dma_chan *ioat_chan)
  844. {
  845. struct ioat_desc_sw *desc;
  846. spin_lock_bh(&ioat_chan->desc_lock);
  847. desc = ioat_dma_get_next_descriptor(ioat_chan);
  848. desc->hw->ctl = IOAT_DMA_DESCRIPTOR_NUL
  849. | IOAT_DMA_DESCRIPTOR_CTL_INT_GN
  850. | IOAT_DMA_DESCRIPTOR_CTL_CP_STS;
  851. desc->hw->size = 0;
  852. desc->hw->src_addr = 0;
  853. desc->hw->dst_addr = 0;
  854. desc->async_tx.ack = 1;
  855. switch (ioat_chan->device->version) {
  856. case IOAT_VER_1_2:
  857. desc->hw->next = 0;
  858. list_add_tail(&desc->node, &ioat_chan->used_desc);
  859. writel(((u64) desc->async_tx.phys) & 0x00000000FFFFFFFF,
  860. ioat_chan->reg_base + IOAT1_CHAINADDR_OFFSET_LOW);
  861. writel(((u64) desc->async_tx.phys) >> 32,
  862. ioat_chan->reg_base + IOAT1_CHAINADDR_OFFSET_HIGH);
  863. writeb(IOAT_CHANCMD_START, ioat_chan->reg_base
  864. + IOAT_CHANCMD_OFFSET(ioat_chan->device->version));
  865. break;
  866. case IOAT_VER_2_0:
  867. writel(((u64) desc->async_tx.phys) & 0x00000000FFFFFFFF,
  868. ioat_chan->reg_base + IOAT2_CHAINADDR_OFFSET_LOW);
  869. writel(((u64) desc->async_tx.phys) >> 32,
  870. ioat_chan->reg_base + IOAT2_CHAINADDR_OFFSET_HIGH);
  871. ioat_chan->dmacount++;
  872. __ioat2_dma_memcpy_issue_pending(ioat_chan);
  873. break;
  874. }
  875. spin_unlock_bh(&ioat_chan->desc_lock);
  876. }
  877. /*
  878. * Perform a IOAT transaction to verify the HW works.
  879. */
  880. #define IOAT_TEST_SIZE 2000
  881. static void ioat_dma_test_callback(void *dma_async_param)
  882. {
  883. printk(KERN_ERR "ioatdma: ioat_dma_test_callback(%p)\n",
  884. dma_async_param);
  885. }
  886. /**
  887. * ioat_dma_self_test - Perform a IOAT transaction to verify the HW works.
  888. * @device: device to be tested
  889. */
  890. static int ioat_dma_self_test(struct ioatdma_device *device)
  891. {
  892. int i;
  893. u8 *src;
  894. u8 *dest;
  895. struct dma_chan *dma_chan;
  896. struct dma_async_tx_descriptor *tx;
  897. dma_addr_t dma_dest, dma_src;
  898. dma_cookie_t cookie;
  899. int err = 0;
  900. src = kzalloc(sizeof(u8) * IOAT_TEST_SIZE, GFP_KERNEL);
  901. if (!src)
  902. return -ENOMEM;
  903. dest = kzalloc(sizeof(u8) * IOAT_TEST_SIZE, GFP_KERNEL);
  904. if (!dest) {
  905. kfree(src);
  906. return -ENOMEM;
  907. }
  908. /* Fill in src buffer */
  909. for (i = 0; i < IOAT_TEST_SIZE; i++)
  910. src[i] = (u8)i;
  911. /* Start copy, using first DMA channel */
  912. dma_chan = container_of(device->common.channels.next,
  913. struct dma_chan,
  914. device_node);
  915. if (device->common.device_alloc_chan_resources(dma_chan) < 1) {
  916. dev_err(&device->pdev->dev,
  917. "selftest cannot allocate chan resource\n");
  918. err = -ENODEV;
  919. goto out;
  920. }
  921. dma_src = dma_map_single(dma_chan->device->dev, src, IOAT_TEST_SIZE,
  922. DMA_TO_DEVICE);
  923. dma_dest = dma_map_single(dma_chan->device->dev, dest, IOAT_TEST_SIZE,
  924. DMA_FROM_DEVICE);
  925. tx = device->common.device_prep_dma_memcpy(dma_chan, dma_dest, dma_src,
  926. IOAT_TEST_SIZE, 0);
  927. if (!tx) {
  928. dev_err(&device->pdev->dev,
  929. "Self-test prep failed, disabling\n");
  930. err = -ENODEV;
  931. goto free_resources;
  932. }
  933. async_tx_ack(tx);
  934. tx->callback = ioat_dma_test_callback;
  935. tx->callback_param = (void *)0x8086;
  936. cookie = tx->tx_submit(tx);
  937. if (cookie < 0) {
  938. dev_err(&device->pdev->dev,
  939. "Self-test setup failed, disabling\n");
  940. err = -ENODEV;
  941. goto free_resources;
  942. }
  943. device->common.device_issue_pending(dma_chan);
  944. msleep(1);
  945. if (device->common.device_is_tx_complete(dma_chan, cookie, NULL, NULL)
  946. != DMA_SUCCESS) {
  947. dev_err(&device->pdev->dev,
  948. "Self-test copy timed out, disabling\n");
  949. err = -ENODEV;
  950. goto free_resources;
  951. }
  952. if (memcmp(src, dest, IOAT_TEST_SIZE)) {
  953. dev_err(&device->pdev->dev,
  954. "Self-test copy failed compare, disabling\n");
  955. err = -ENODEV;
  956. goto free_resources;
  957. }
  958. free_resources:
  959. device->common.device_free_chan_resources(dma_chan);
  960. out:
  961. kfree(src);
  962. kfree(dest);
  963. return err;
  964. }
  965. static char ioat_interrupt_style[32] = "msix";
  966. module_param_string(ioat_interrupt_style, ioat_interrupt_style,
  967. sizeof(ioat_interrupt_style), 0644);
  968. MODULE_PARM_DESC(ioat_interrupt_style,
  969. "set ioat interrupt style: msix (default), "
  970. "msix-single-vector, msi, intx)");
  971. /**
  972. * ioat_dma_setup_interrupts - setup interrupt handler
  973. * @device: ioat device
  974. */
  975. static int ioat_dma_setup_interrupts(struct ioatdma_device *device)
  976. {
  977. struct ioat_dma_chan *ioat_chan;
  978. int err, i, j, msixcnt;
  979. u8 intrctrl = 0;
  980. if (!strcmp(ioat_interrupt_style, "msix"))
  981. goto msix;
  982. if (!strcmp(ioat_interrupt_style, "msix-single-vector"))
  983. goto msix_single_vector;
  984. if (!strcmp(ioat_interrupt_style, "msi"))
  985. goto msi;
  986. if (!strcmp(ioat_interrupt_style, "intx"))
  987. goto intx;
  988. dev_err(&device->pdev->dev, "invalid ioat_interrupt_style %s\n",
  989. ioat_interrupt_style);
  990. goto err_no_irq;
  991. msix:
  992. /* The number of MSI-X vectors should equal the number of channels */
  993. msixcnt = device->common.chancnt;
  994. for (i = 0; i < msixcnt; i++)
  995. device->msix_entries[i].entry = i;
  996. err = pci_enable_msix(device->pdev, device->msix_entries, msixcnt);
  997. if (err < 0)
  998. goto msi;
  999. if (err > 0)
  1000. goto msix_single_vector;
  1001. for (i = 0; i < msixcnt; i++) {
  1002. ioat_chan = ioat_lookup_chan_by_index(device, i);
  1003. err = request_irq(device->msix_entries[i].vector,
  1004. ioat_dma_do_interrupt_msix,
  1005. 0, "ioat-msix", ioat_chan);
  1006. if (err) {
  1007. for (j = 0; j < i; j++) {
  1008. ioat_chan =
  1009. ioat_lookup_chan_by_index(device, j);
  1010. free_irq(device->msix_entries[j].vector,
  1011. ioat_chan);
  1012. }
  1013. goto msix_single_vector;
  1014. }
  1015. }
  1016. intrctrl |= IOAT_INTRCTRL_MSIX_VECTOR_CONTROL;
  1017. device->irq_mode = msix_multi_vector;
  1018. goto done;
  1019. msix_single_vector:
  1020. device->msix_entries[0].entry = 0;
  1021. err = pci_enable_msix(device->pdev, device->msix_entries, 1);
  1022. if (err)
  1023. goto msi;
  1024. err = request_irq(device->msix_entries[0].vector, ioat_dma_do_interrupt,
  1025. 0, "ioat-msix", device);
  1026. if (err) {
  1027. pci_disable_msix(device->pdev);
  1028. goto msi;
  1029. }
  1030. device->irq_mode = msix_single_vector;
  1031. goto done;
  1032. msi:
  1033. err = pci_enable_msi(device->pdev);
  1034. if (err)
  1035. goto intx;
  1036. err = request_irq(device->pdev->irq, ioat_dma_do_interrupt,
  1037. 0, "ioat-msi", device);
  1038. if (err) {
  1039. pci_disable_msi(device->pdev);
  1040. goto intx;
  1041. }
  1042. /*
  1043. * CB 1.2 devices need a bit set in configuration space to enable MSI
  1044. */
  1045. if (device->version == IOAT_VER_1_2) {
  1046. u32 dmactrl;
  1047. pci_read_config_dword(device->pdev,
  1048. IOAT_PCI_DMACTRL_OFFSET, &dmactrl);
  1049. dmactrl |= IOAT_PCI_DMACTRL_MSI_EN;
  1050. pci_write_config_dword(device->pdev,
  1051. IOAT_PCI_DMACTRL_OFFSET, dmactrl);
  1052. }
  1053. device->irq_mode = msi;
  1054. goto done;
  1055. intx:
  1056. err = request_irq(device->pdev->irq, ioat_dma_do_interrupt,
  1057. IRQF_SHARED, "ioat-intx", device);
  1058. if (err)
  1059. goto err_no_irq;
  1060. device->irq_mode = intx;
  1061. done:
  1062. intrctrl |= IOAT_INTRCTRL_MASTER_INT_EN;
  1063. writeb(intrctrl, device->reg_base + IOAT_INTRCTRL_OFFSET);
  1064. return 0;
  1065. err_no_irq:
  1066. /* Disable all interrupt generation */
  1067. writeb(0, device->reg_base + IOAT_INTRCTRL_OFFSET);
  1068. dev_err(&device->pdev->dev, "no usable interrupts\n");
  1069. device->irq_mode = none;
  1070. return -1;
  1071. }
  1072. /**
  1073. * ioat_dma_remove_interrupts - remove whatever interrupts were set
  1074. * @device: ioat device
  1075. */
  1076. static void ioat_dma_remove_interrupts(struct ioatdma_device *device)
  1077. {
  1078. struct ioat_dma_chan *ioat_chan;
  1079. int i;
  1080. /* Disable all interrupt generation */
  1081. writeb(0, device->reg_base + IOAT_INTRCTRL_OFFSET);
  1082. switch (device->irq_mode) {
  1083. case msix_multi_vector:
  1084. for (i = 0; i < device->common.chancnt; i++) {
  1085. ioat_chan = ioat_lookup_chan_by_index(device, i);
  1086. free_irq(device->msix_entries[i].vector, ioat_chan);
  1087. }
  1088. pci_disable_msix(device->pdev);
  1089. break;
  1090. case msix_single_vector:
  1091. free_irq(device->msix_entries[0].vector, device);
  1092. pci_disable_msix(device->pdev);
  1093. break;
  1094. case msi:
  1095. free_irq(device->pdev->irq, device);
  1096. pci_disable_msi(device->pdev);
  1097. break;
  1098. case intx:
  1099. free_irq(device->pdev->irq, device);
  1100. break;
  1101. case none:
  1102. dev_warn(&device->pdev->dev,
  1103. "call to %s without interrupts setup\n", __func__);
  1104. }
  1105. device->irq_mode = none;
  1106. }
  1107. struct ioatdma_device *ioat_dma_probe(struct pci_dev *pdev,
  1108. void __iomem *iobase)
  1109. {
  1110. int err;
  1111. struct ioatdma_device *device;
  1112. device = kzalloc(sizeof(*device), GFP_KERNEL);
  1113. if (!device) {
  1114. err = -ENOMEM;
  1115. goto err_kzalloc;
  1116. }
  1117. device->pdev = pdev;
  1118. device->reg_base = iobase;
  1119. device->version = readb(device->reg_base + IOAT_VER_OFFSET);
  1120. /* DMA coherent memory pool for DMA descriptor allocations */
  1121. device->dma_pool = pci_pool_create("dma_desc_pool", pdev,
  1122. sizeof(struct ioat_dma_descriptor),
  1123. 64, 0);
  1124. if (!device->dma_pool) {
  1125. err = -ENOMEM;
  1126. goto err_dma_pool;
  1127. }
  1128. device->completion_pool = pci_pool_create("completion_pool", pdev,
  1129. sizeof(u64), SMP_CACHE_BYTES,
  1130. SMP_CACHE_BYTES);
  1131. if (!device->completion_pool) {
  1132. err = -ENOMEM;
  1133. goto err_completion_pool;
  1134. }
  1135. INIT_LIST_HEAD(&device->common.channels);
  1136. ioat_dma_enumerate_channels(device);
  1137. device->common.device_alloc_chan_resources =
  1138. ioat_dma_alloc_chan_resources;
  1139. device->common.device_free_chan_resources =
  1140. ioat_dma_free_chan_resources;
  1141. device->common.dev = &pdev->dev;
  1142. dma_cap_set(DMA_MEMCPY, device->common.cap_mask);
  1143. device->common.device_is_tx_complete = ioat_dma_is_complete;
  1144. device->common.device_dependency_added = ioat_dma_dependency_added;
  1145. switch (device->version) {
  1146. case IOAT_VER_1_2:
  1147. device->common.device_prep_dma_memcpy = ioat1_dma_prep_memcpy;
  1148. device->common.device_issue_pending =
  1149. ioat1_dma_memcpy_issue_pending;
  1150. break;
  1151. case IOAT_VER_2_0:
  1152. device->common.device_prep_dma_memcpy = ioat2_dma_prep_memcpy;
  1153. device->common.device_issue_pending =
  1154. ioat2_dma_memcpy_issue_pending;
  1155. break;
  1156. }
  1157. dev_err(&device->pdev->dev,
  1158. "Intel(R) I/OAT DMA Engine found,"
  1159. " %d channels, device version 0x%02x, driver version %s\n",
  1160. device->common.chancnt, device->version, IOAT_DMA_VERSION);
  1161. err = ioat_dma_setup_interrupts(device);
  1162. if (err)
  1163. goto err_setup_interrupts;
  1164. err = ioat_dma_self_test(device);
  1165. if (err)
  1166. goto err_self_test;
  1167. dma_async_device_register(&device->common);
  1168. return device;
  1169. err_self_test:
  1170. ioat_dma_remove_interrupts(device);
  1171. err_setup_interrupts:
  1172. pci_pool_destroy(device->completion_pool);
  1173. err_completion_pool:
  1174. pci_pool_destroy(device->dma_pool);
  1175. err_dma_pool:
  1176. kfree(device);
  1177. err_kzalloc:
  1178. dev_err(&pdev->dev,
  1179. "Intel(R) I/OAT DMA Engine initialization failed\n");
  1180. return NULL;
  1181. }
  1182. void ioat_dma_remove(struct ioatdma_device *device)
  1183. {
  1184. struct dma_chan *chan, *_chan;
  1185. struct ioat_dma_chan *ioat_chan;
  1186. ioat_dma_remove_interrupts(device);
  1187. dma_async_device_unregister(&device->common);
  1188. pci_pool_destroy(device->dma_pool);
  1189. pci_pool_destroy(device->completion_pool);
  1190. iounmap(device->reg_base);
  1191. pci_release_regions(device->pdev);
  1192. pci_disable_device(device->pdev);
  1193. list_for_each_entry_safe(chan, _chan,
  1194. &device->common.channels, device_node) {
  1195. ioat_chan = to_ioat_chan(chan);
  1196. list_del(&chan->device_node);
  1197. kfree(ioat_chan);
  1198. }
  1199. kfree(device);
  1200. }