ioat_dma.c 37 KB

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