dma.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659
  1. /*
  2. * Intel I/OAT DMA Linux driver
  3. * Copyright(c) 2004 - 2009 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 <linux/workqueue.h>
  34. #include <linux/i7300_idle.h>
  35. #include "dma.h"
  36. #include "registers.h"
  37. #include "hw.h"
  38. static int ioat_pending_level = 4;
  39. module_param(ioat_pending_level, int, 0644);
  40. MODULE_PARM_DESC(ioat_pending_level,
  41. "high-water mark for pushing ioat descriptors (default: 4)");
  42. static void ioat_dma_chan_reset_part2(struct work_struct *work);
  43. static void ioat_dma_chan_watchdog(struct work_struct *work);
  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 *
  52. ioat_chan_by_index(struct ioatdma_device *device, int index)
  53. {
  54. return device->idx[index];
  55. }
  56. /**
  57. * ioat_dma_do_interrupt - handler used for single vector interrupt mode
  58. * @irq: interrupt id
  59. * @data: interrupt data
  60. */
  61. static irqreturn_t ioat_dma_do_interrupt(int irq, void *data)
  62. {
  63. struct ioatdma_device *instance = data;
  64. struct ioat_dma_chan *ioat_chan;
  65. unsigned long attnstatus;
  66. int bit;
  67. u8 intrctrl;
  68. intrctrl = readb(instance->reg_base + IOAT_INTRCTRL_OFFSET);
  69. if (!(intrctrl & IOAT_INTRCTRL_MASTER_INT_EN))
  70. return IRQ_NONE;
  71. if (!(intrctrl & IOAT_INTRCTRL_INT_STATUS)) {
  72. writeb(intrctrl, instance->reg_base + IOAT_INTRCTRL_OFFSET);
  73. return IRQ_NONE;
  74. }
  75. attnstatus = readl(instance->reg_base + IOAT_ATTNSTATUS_OFFSET);
  76. for_each_bit(bit, &attnstatus, BITS_PER_LONG) {
  77. ioat_chan = ioat_chan_by_index(instance, bit);
  78. tasklet_schedule(&ioat_chan->cleanup_task);
  79. }
  80. writeb(intrctrl, instance->reg_base + IOAT_INTRCTRL_OFFSET);
  81. return IRQ_HANDLED;
  82. }
  83. /**
  84. * ioat_dma_do_interrupt_msix - handler used for vector-per-channel interrupt mode
  85. * @irq: interrupt id
  86. * @data: interrupt data
  87. */
  88. static irqreturn_t ioat_dma_do_interrupt_msix(int irq, void *data)
  89. {
  90. struct ioat_dma_chan *ioat_chan = data;
  91. tasklet_schedule(&ioat_chan->cleanup_task);
  92. return IRQ_HANDLED;
  93. }
  94. static void ioat_dma_cleanup_tasklet(unsigned long data);
  95. /**
  96. * ioat_dma_enumerate_channels - find and initialize the device's channels
  97. * @device: the device to be enumerated
  98. */
  99. static int ioat_dma_enumerate_channels(struct ioatdma_device *device)
  100. {
  101. u8 xfercap_scale;
  102. u32 xfercap;
  103. int i;
  104. struct ioat_dma_chan *ioat_chan;
  105. struct device *dev = &device->pdev->dev;
  106. /*
  107. * IOAT ver.3 workarounds
  108. */
  109. if (device->version == IOAT_VER_3_0) {
  110. u32 chan_err_mask;
  111. u16 dev_id;
  112. u32 dmauncerrsts;
  113. /*
  114. * Write CHANERRMSK_INT with 3E07h to mask out the errors
  115. * that can cause stability issues for IOAT ver.3
  116. */
  117. chan_err_mask = 0x3E07;
  118. pci_write_config_dword(device->pdev,
  119. IOAT_PCI_CHANERRMASK_INT_OFFSET,
  120. chan_err_mask);
  121. /*
  122. * Clear DMAUNCERRSTS Cfg-Reg Parity Error status bit
  123. * (workaround for spurious config parity error after restart)
  124. */
  125. pci_read_config_word(device->pdev,
  126. IOAT_PCI_DEVICE_ID_OFFSET,
  127. &dev_id);
  128. if (dev_id == PCI_DEVICE_ID_INTEL_IOAT_TBG0) {
  129. dmauncerrsts = 0x10;
  130. pci_write_config_dword(device->pdev,
  131. IOAT_PCI_DMAUNCERRSTS_OFFSET,
  132. dmauncerrsts);
  133. }
  134. }
  135. device->common.chancnt = readb(device->reg_base + IOAT_CHANCNT_OFFSET);
  136. xfercap_scale = readb(device->reg_base + IOAT_XFERCAP_OFFSET);
  137. xfercap = (xfercap_scale == 0 ? -1 : (1UL << xfercap_scale));
  138. #ifdef CONFIG_I7300_IDLE_IOAT_CHANNEL
  139. if (i7300_idle_platform_probe(NULL, NULL, 1) == 0) {
  140. device->common.chancnt--;
  141. }
  142. #endif
  143. for (i = 0; i < device->common.chancnt; i++) {
  144. ioat_chan = devm_kzalloc(dev, sizeof(*ioat_chan), GFP_KERNEL);
  145. if (!ioat_chan) {
  146. device->common.chancnt = i;
  147. break;
  148. }
  149. ioat_chan->device = device;
  150. ioat_chan->reg_base = device->reg_base + (0x80 * (i + 1));
  151. ioat_chan->xfercap = xfercap;
  152. ioat_chan->desccount = 0;
  153. INIT_DELAYED_WORK(&ioat_chan->work, ioat_dma_chan_reset_part2);
  154. if (ioat_chan->device->version == IOAT_VER_2_0)
  155. writel(IOAT_DCACTRL_CMPL_WRITE_ENABLE |
  156. IOAT_DMA_DCA_ANY_CPU,
  157. ioat_chan->reg_base + IOAT_DCACTRL_OFFSET);
  158. else if (ioat_chan->device->version == IOAT_VER_3_0)
  159. writel(IOAT_DMA_DCA_ANY_CPU,
  160. ioat_chan->reg_base + IOAT_DCACTRL_OFFSET);
  161. spin_lock_init(&ioat_chan->cleanup_lock);
  162. spin_lock_init(&ioat_chan->desc_lock);
  163. INIT_LIST_HEAD(&ioat_chan->free_desc);
  164. INIT_LIST_HEAD(&ioat_chan->used_desc);
  165. /* This should be made common somewhere in dmaengine.c */
  166. ioat_chan->common.device = &device->common;
  167. list_add_tail(&ioat_chan->common.device_node,
  168. &device->common.channels);
  169. device->idx[i] = ioat_chan;
  170. tasklet_init(&ioat_chan->cleanup_task,
  171. ioat_dma_cleanup_tasklet,
  172. (unsigned long) ioat_chan);
  173. tasklet_disable(&ioat_chan->cleanup_task);
  174. }
  175. return device->common.chancnt;
  176. }
  177. /**
  178. * ioat_dma_memcpy_issue_pending - push potentially unrecognized appended
  179. * descriptors to hw
  180. * @chan: DMA channel handle
  181. */
  182. static inline void
  183. __ioat1_dma_memcpy_issue_pending(struct ioat_dma_chan *ioat_chan)
  184. {
  185. ioat_chan->pending = 0;
  186. writeb(IOAT_CHANCMD_APPEND, ioat_chan->reg_base + IOAT1_CHANCMD_OFFSET);
  187. }
  188. static void ioat1_dma_memcpy_issue_pending(struct dma_chan *chan)
  189. {
  190. struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
  191. if (ioat_chan->pending > 0) {
  192. spin_lock_bh(&ioat_chan->desc_lock);
  193. __ioat1_dma_memcpy_issue_pending(ioat_chan);
  194. spin_unlock_bh(&ioat_chan->desc_lock);
  195. }
  196. }
  197. static inline void
  198. __ioat2_dma_memcpy_issue_pending(struct ioat_dma_chan *ioat_chan)
  199. {
  200. ioat_chan->pending = 0;
  201. writew(ioat_chan->dmacount,
  202. ioat_chan->reg_base + IOAT_CHAN_DMACOUNT_OFFSET);
  203. }
  204. static void ioat2_dma_memcpy_issue_pending(struct dma_chan *chan)
  205. {
  206. struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
  207. if (ioat_chan->pending > 0) {
  208. spin_lock_bh(&ioat_chan->desc_lock);
  209. __ioat2_dma_memcpy_issue_pending(ioat_chan);
  210. spin_unlock_bh(&ioat_chan->desc_lock);
  211. }
  212. }
  213. /**
  214. * ioat_dma_chan_reset_part2 - reinit the channel after a reset
  215. */
  216. static void ioat_dma_chan_reset_part2(struct work_struct *work)
  217. {
  218. struct ioat_dma_chan *ioat_chan =
  219. container_of(work, struct ioat_dma_chan, work.work);
  220. struct ioat_desc_sw *desc;
  221. spin_lock_bh(&ioat_chan->cleanup_lock);
  222. spin_lock_bh(&ioat_chan->desc_lock);
  223. ioat_chan->completion_virt->low = 0;
  224. ioat_chan->completion_virt->high = 0;
  225. ioat_chan->pending = 0;
  226. /*
  227. * count the descriptors waiting, and be sure to do it
  228. * right for both the CB1 line and the CB2 ring
  229. */
  230. ioat_chan->dmacount = 0;
  231. if (ioat_chan->used_desc.prev) {
  232. desc = to_ioat_desc(ioat_chan->used_desc.prev);
  233. do {
  234. ioat_chan->dmacount++;
  235. desc = to_ioat_desc(desc->node.next);
  236. } while (&desc->node != ioat_chan->used_desc.next);
  237. }
  238. /*
  239. * write the new starting descriptor address
  240. * this puts channel engine into ARMED state
  241. */
  242. desc = to_ioat_desc(ioat_chan->used_desc.prev);
  243. switch (ioat_chan->device->version) {
  244. case IOAT_VER_1_2:
  245. writel(((u64) desc->txd.phys) & 0x00000000FFFFFFFF,
  246. ioat_chan->reg_base + IOAT1_CHAINADDR_OFFSET_LOW);
  247. writel(((u64) desc->txd.phys) >> 32,
  248. ioat_chan->reg_base + IOAT1_CHAINADDR_OFFSET_HIGH);
  249. writeb(IOAT_CHANCMD_START, ioat_chan->reg_base
  250. + IOAT_CHANCMD_OFFSET(ioat_chan->device->version));
  251. break;
  252. case IOAT_VER_2_0:
  253. writel(((u64) desc->txd.phys) & 0x00000000FFFFFFFF,
  254. ioat_chan->reg_base + IOAT2_CHAINADDR_OFFSET_LOW);
  255. writel(((u64) desc->txd.phys) >> 32,
  256. ioat_chan->reg_base + IOAT2_CHAINADDR_OFFSET_HIGH);
  257. /* tell the engine to go with what's left to be done */
  258. writew(ioat_chan->dmacount,
  259. ioat_chan->reg_base + IOAT_CHAN_DMACOUNT_OFFSET);
  260. break;
  261. }
  262. dev_err(to_dev(ioat_chan),
  263. "chan%d reset - %d descs waiting, %d total desc\n",
  264. chan_num(ioat_chan), ioat_chan->dmacount, ioat_chan->desccount);
  265. spin_unlock_bh(&ioat_chan->desc_lock);
  266. spin_unlock_bh(&ioat_chan->cleanup_lock);
  267. }
  268. /**
  269. * ioat_dma_reset_channel - restart a channel
  270. * @ioat_chan: IOAT DMA channel handle
  271. */
  272. static void ioat_dma_reset_channel(struct ioat_dma_chan *ioat_chan)
  273. {
  274. u32 chansts, chanerr;
  275. if (!ioat_chan->used_desc.prev)
  276. return;
  277. chanerr = readl(ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
  278. chansts = (ioat_chan->completion_virt->low
  279. & IOAT_CHANSTS_DMA_TRANSFER_STATUS);
  280. if (chanerr) {
  281. dev_err(to_dev(ioat_chan),
  282. "chan%d, CHANSTS = 0x%08x CHANERR = 0x%04x, clearing\n",
  283. chan_num(ioat_chan), chansts, chanerr);
  284. writel(chanerr, ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
  285. }
  286. /*
  287. * whack it upside the head with a reset
  288. * and wait for things to settle out.
  289. * force the pending count to a really big negative
  290. * to make sure no one forces an issue_pending
  291. * while we're waiting.
  292. */
  293. spin_lock_bh(&ioat_chan->desc_lock);
  294. ioat_chan->pending = INT_MIN;
  295. writeb(IOAT_CHANCMD_RESET,
  296. ioat_chan->reg_base
  297. + IOAT_CHANCMD_OFFSET(ioat_chan->device->version));
  298. spin_unlock_bh(&ioat_chan->desc_lock);
  299. /* schedule the 2nd half instead of sleeping a long time */
  300. schedule_delayed_work(&ioat_chan->work, RESET_DELAY);
  301. }
  302. /**
  303. * ioat_dma_chan_watchdog - watch for stuck channels
  304. */
  305. static void ioat_dma_chan_watchdog(struct work_struct *work)
  306. {
  307. struct ioatdma_device *device =
  308. container_of(work, struct ioatdma_device, work.work);
  309. struct ioat_dma_chan *ioat_chan;
  310. int i;
  311. union {
  312. u64 full;
  313. struct {
  314. u32 low;
  315. u32 high;
  316. };
  317. } completion_hw;
  318. unsigned long compl_desc_addr_hw;
  319. for (i = 0; i < device->common.chancnt; i++) {
  320. ioat_chan = ioat_chan_by_index(device, i);
  321. if (ioat_chan->device->version == IOAT_VER_1_2
  322. /* have we started processing anything yet */
  323. && ioat_chan->last_completion
  324. /* have we completed any since last watchdog cycle? */
  325. && (ioat_chan->last_completion ==
  326. ioat_chan->watchdog_completion)
  327. /* has TCP stuck on one cookie since last watchdog? */
  328. && (ioat_chan->watchdog_tcp_cookie ==
  329. ioat_chan->watchdog_last_tcp_cookie)
  330. && (ioat_chan->watchdog_tcp_cookie !=
  331. ioat_chan->completed_cookie)
  332. /* is there something in the chain to be processed? */
  333. /* CB1 chain always has at least the last one processed */
  334. && (ioat_chan->used_desc.prev != ioat_chan->used_desc.next)
  335. && ioat_chan->pending == 0) {
  336. /*
  337. * check CHANSTS register for completed
  338. * descriptor address.
  339. * if it is different than completion writeback,
  340. * it is not zero
  341. * and it has changed since the last watchdog
  342. * we can assume that channel
  343. * is still working correctly
  344. * and the problem is in completion writeback.
  345. * update completion writeback
  346. * with actual CHANSTS value
  347. * else
  348. * try resetting the channel
  349. */
  350. completion_hw.low = readl(ioat_chan->reg_base +
  351. IOAT_CHANSTS_OFFSET_LOW(ioat_chan->device->version));
  352. completion_hw.high = readl(ioat_chan->reg_base +
  353. IOAT_CHANSTS_OFFSET_HIGH(ioat_chan->device->version));
  354. #if (BITS_PER_LONG == 64)
  355. compl_desc_addr_hw =
  356. completion_hw.full
  357. & IOAT_CHANSTS_COMPLETED_DESCRIPTOR_ADDR;
  358. #else
  359. compl_desc_addr_hw =
  360. completion_hw.low & IOAT_LOW_COMPLETION_MASK;
  361. #endif
  362. if ((compl_desc_addr_hw != 0)
  363. && (compl_desc_addr_hw != ioat_chan->watchdog_completion)
  364. && (compl_desc_addr_hw != ioat_chan->last_compl_desc_addr_hw)) {
  365. ioat_chan->last_compl_desc_addr_hw = compl_desc_addr_hw;
  366. ioat_chan->completion_virt->low = completion_hw.low;
  367. ioat_chan->completion_virt->high = completion_hw.high;
  368. } else {
  369. ioat_dma_reset_channel(ioat_chan);
  370. ioat_chan->watchdog_completion = 0;
  371. ioat_chan->last_compl_desc_addr_hw = 0;
  372. }
  373. /*
  374. * for version 2.0 if there are descriptors yet to be processed
  375. * and the last completed hasn't changed since the last watchdog
  376. * if they haven't hit the pending level
  377. * issue the pending to push them through
  378. * else
  379. * try resetting the channel
  380. */
  381. } else if (ioat_chan->device->version == IOAT_VER_2_0
  382. && ioat_chan->used_desc.prev
  383. && ioat_chan->last_completion
  384. && ioat_chan->last_completion == ioat_chan->watchdog_completion) {
  385. if (ioat_chan->pending < ioat_pending_level)
  386. ioat2_dma_memcpy_issue_pending(&ioat_chan->common);
  387. else {
  388. ioat_dma_reset_channel(ioat_chan);
  389. ioat_chan->watchdog_completion = 0;
  390. }
  391. } else {
  392. ioat_chan->last_compl_desc_addr_hw = 0;
  393. ioat_chan->watchdog_completion
  394. = ioat_chan->last_completion;
  395. }
  396. ioat_chan->watchdog_last_tcp_cookie =
  397. ioat_chan->watchdog_tcp_cookie;
  398. }
  399. schedule_delayed_work(&device->work, WATCHDOG_DELAY);
  400. }
  401. static dma_cookie_t ioat1_tx_submit(struct dma_async_tx_descriptor *tx)
  402. {
  403. struct ioat_dma_chan *ioat_chan = to_ioat_chan(tx->chan);
  404. struct ioat_desc_sw *first = tx_to_ioat_desc(tx);
  405. struct ioat_desc_sw *prev, *new;
  406. struct ioat_dma_descriptor *hw;
  407. dma_cookie_t cookie;
  408. LIST_HEAD(new_chain);
  409. u32 copy;
  410. size_t len;
  411. dma_addr_t src, dst;
  412. unsigned long orig_flags;
  413. unsigned int desc_count = 0;
  414. /* src and dest and len are stored in the initial descriptor */
  415. len = first->len;
  416. src = first->src;
  417. dst = first->dst;
  418. orig_flags = first->txd.flags;
  419. new = first;
  420. spin_lock_bh(&ioat_chan->desc_lock);
  421. prev = to_ioat_desc(ioat_chan->used_desc.prev);
  422. prefetch(prev->hw);
  423. do {
  424. copy = min_t(size_t, len, ioat_chan->xfercap);
  425. async_tx_ack(&new->txd);
  426. hw = new->hw;
  427. hw->size = copy;
  428. hw->ctl = 0;
  429. hw->src_addr = src;
  430. hw->dst_addr = dst;
  431. hw->next = 0;
  432. /* chain together the physical address list for the HW */
  433. wmb();
  434. prev->hw->next = (u64) new->txd.phys;
  435. len -= copy;
  436. dst += copy;
  437. src += copy;
  438. list_add_tail(&new->node, &new_chain);
  439. desc_count++;
  440. prev = new;
  441. } while (len && (new = ioat1_dma_get_next_descriptor(ioat_chan)));
  442. if (!new) {
  443. dev_err(to_dev(ioat_chan), "tx submit failed\n");
  444. spin_unlock_bh(&ioat_chan->desc_lock);
  445. return -ENOMEM;
  446. }
  447. hw->ctl = IOAT_DMA_DESCRIPTOR_CTL_CP_STS;
  448. if (first->txd.callback) {
  449. hw->ctl |= IOAT_DMA_DESCRIPTOR_CTL_INT_GN;
  450. if (first != new) {
  451. /* move callback into to last desc */
  452. new->txd.callback = first->txd.callback;
  453. new->txd.callback_param
  454. = first->txd.callback_param;
  455. first->txd.callback = NULL;
  456. first->txd.callback_param = NULL;
  457. }
  458. }
  459. new->tx_cnt = desc_count;
  460. new->txd.flags = orig_flags; /* client is in control of this ack */
  461. /* store the original values for use in later cleanup */
  462. if (new != first) {
  463. new->src = first->src;
  464. new->dst = first->dst;
  465. new->len = first->len;
  466. }
  467. /* cookie incr and addition to used_list must be atomic */
  468. cookie = ioat_chan->common.cookie;
  469. cookie++;
  470. if (cookie < 0)
  471. cookie = 1;
  472. ioat_chan->common.cookie = new->txd.cookie = cookie;
  473. /* write address into NextDescriptor field of last desc in chain */
  474. to_ioat_desc(ioat_chan->used_desc.prev)->hw->next =
  475. first->txd.phys;
  476. list_splice_tail(&new_chain, &ioat_chan->used_desc);
  477. ioat_chan->dmacount += desc_count;
  478. ioat_chan->pending += desc_count;
  479. if (ioat_chan->pending >= ioat_pending_level)
  480. __ioat1_dma_memcpy_issue_pending(ioat_chan);
  481. spin_unlock_bh(&ioat_chan->desc_lock);
  482. return cookie;
  483. }
  484. static dma_cookie_t ioat2_tx_submit(struct dma_async_tx_descriptor *tx)
  485. {
  486. struct ioat_dma_chan *ioat_chan = to_ioat_chan(tx->chan);
  487. struct ioat_desc_sw *first = tx_to_ioat_desc(tx);
  488. struct ioat_desc_sw *new;
  489. struct ioat_dma_descriptor *hw;
  490. dma_cookie_t cookie;
  491. u32 copy;
  492. size_t len;
  493. dma_addr_t src, dst;
  494. unsigned long orig_flags;
  495. unsigned int desc_count = 0;
  496. /* src and dest and len are stored in the initial descriptor */
  497. len = first->len;
  498. src = first->src;
  499. dst = first->dst;
  500. orig_flags = first->txd.flags;
  501. new = first;
  502. /*
  503. * ioat_chan->desc_lock is still in force in version 2 path
  504. * it gets unlocked at end of this function
  505. */
  506. do {
  507. copy = min_t(size_t, len, ioat_chan->xfercap);
  508. async_tx_ack(&new->txd);
  509. hw = new->hw;
  510. hw->size = copy;
  511. hw->ctl = 0;
  512. hw->src_addr = src;
  513. hw->dst_addr = dst;
  514. len -= copy;
  515. dst += copy;
  516. src += copy;
  517. desc_count++;
  518. } while (len && (new = ioat2_dma_get_next_descriptor(ioat_chan)));
  519. if (!new) {
  520. dev_err(to_dev(ioat_chan), "tx submit failed\n");
  521. spin_unlock_bh(&ioat_chan->desc_lock);
  522. return -ENOMEM;
  523. }
  524. hw->ctl |= IOAT_DMA_DESCRIPTOR_CTL_CP_STS;
  525. if (first->txd.callback) {
  526. hw->ctl |= IOAT_DMA_DESCRIPTOR_CTL_INT_GN;
  527. if (first != new) {
  528. /* move callback into to last desc */
  529. new->txd.callback = first->txd.callback;
  530. new->txd.callback_param
  531. = first->txd.callback_param;
  532. first->txd.callback = NULL;
  533. first->txd.callback_param = NULL;
  534. }
  535. }
  536. new->tx_cnt = desc_count;
  537. new->txd.flags = orig_flags; /* client is in control of this ack */
  538. /* store the original values for use in later cleanup */
  539. if (new != first) {
  540. new->src = first->src;
  541. new->dst = first->dst;
  542. new->len = first->len;
  543. }
  544. /* cookie incr and addition to used_list must be atomic */
  545. cookie = ioat_chan->common.cookie;
  546. cookie++;
  547. if (cookie < 0)
  548. cookie = 1;
  549. ioat_chan->common.cookie = new->txd.cookie = cookie;
  550. ioat_chan->dmacount += desc_count;
  551. ioat_chan->pending += desc_count;
  552. if (ioat_chan->pending >= ioat_pending_level)
  553. __ioat2_dma_memcpy_issue_pending(ioat_chan);
  554. spin_unlock_bh(&ioat_chan->desc_lock);
  555. return cookie;
  556. }
  557. /**
  558. * ioat_dma_alloc_descriptor - allocate and return a sw and hw descriptor pair
  559. * @ioat_chan: the channel supplying the memory pool for the descriptors
  560. * @flags: allocation flags
  561. */
  562. static struct ioat_desc_sw *
  563. ioat_dma_alloc_descriptor(struct ioat_dma_chan *ioat_chan, gfp_t flags)
  564. {
  565. struct ioat_dma_descriptor *desc;
  566. struct ioat_desc_sw *desc_sw;
  567. struct ioatdma_device *ioatdma_device;
  568. dma_addr_t phys;
  569. ioatdma_device = to_ioatdma_device(ioat_chan->common.device);
  570. desc = pci_pool_alloc(ioatdma_device->dma_pool, flags, &phys);
  571. if (unlikely(!desc))
  572. return NULL;
  573. desc_sw = kzalloc(sizeof(*desc_sw), flags);
  574. if (unlikely(!desc_sw)) {
  575. pci_pool_free(ioatdma_device->dma_pool, desc, phys);
  576. return NULL;
  577. }
  578. memset(desc, 0, sizeof(*desc));
  579. dma_async_tx_descriptor_init(&desc_sw->txd, &ioat_chan->common);
  580. switch (ioat_chan->device->version) {
  581. case IOAT_VER_1_2:
  582. desc_sw->txd.tx_submit = ioat1_tx_submit;
  583. break;
  584. case IOAT_VER_2_0:
  585. case IOAT_VER_3_0:
  586. desc_sw->txd.tx_submit = ioat2_tx_submit;
  587. break;
  588. }
  589. desc_sw->hw = desc;
  590. desc_sw->txd.phys = phys;
  591. return desc_sw;
  592. }
  593. static int ioat_initial_desc_count = 256;
  594. module_param(ioat_initial_desc_count, int, 0644);
  595. MODULE_PARM_DESC(ioat_initial_desc_count,
  596. "initial descriptors per channel (default: 256)");
  597. /**
  598. * ioat2_dma_massage_chan_desc - link the descriptors into a circle
  599. * @ioat_chan: the channel to be massaged
  600. */
  601. static void ioat2_dma_massage_chan_desc(struct ioat_dma_chan *ioat_chan)
  602. {
  603. struct ioat_desc_sw *desc, *_desc;
  604. /* setup used_desc */
  605. ioat_chan->used_desc.next = ioat_chan->free_desc.next;
  606. ioat_chan->used_desc.prev = NULL;
  607. /* pull free_desc out of the circle so that every node is a hw
  608. * descriptor, but leave it pointing to the list
  609. */
  610. ioat_chan->free_desc.prev->next = ioat_chan->free_desc.next;
  611. ioat_chan->free_desc.next->prev = ioat_chan->free_desc.prev;
  612. /* circle link the hw descriptors */
  613. desc = to_ioat_desc(ioat_chan->free_desc.next);
  614. desc->hw->next = to_ioat_desc(desc->node.next)->txd.phys;
  615. list_for_each_entry_safe(desc, _desc, ioat_chan->free_desc.next, node) {
  616. desc->hw->next = to_ioat_desc(desc->node.next)->txd.phys;
  617. }
  618. }
  619. /**
  620. * ioat_dma_alloc_chan_resources - returns the number of allocated descriptors
  621. * @chan: the channel to be filled out
  622. */
  623. static int ioat_dma_alloc_chan_resources(struct dma_chan *chan)
  624. {
  625. struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
  626. struct ioat_desc_sw *desc;
  627. u16 chanctrl;
  628. u32 chanerr;
  629. int i;
  630. LIST_HEAD(tmp_list);
  631. /* have we already been set up? */
  632. if (!list_empty(&ioat_chan->free_desc))
  633. return ioat_chan->desccount;
  634. /* Setup register to interrupt and write completion status on error */
  635. chanctrl = IOAT_CHANCTRL_ERR_INT_EN |
  636. IOAT_CHANCTRL_ANY_ERR_ABORT_EN |
  637. IOAT_CHANCTRL_ERR_COMPLETION_EN;
  638. writew(chanctrl, ioat_chan->reg_base + IOAT_CHANCTRL_OFFSET);
  639. chanerr = readl(ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
  640. if (chanerr) {
  641. dev_err(to_dev(ioat_chan), "CHANERR = %x, clearing\n", chanerr);
  642. writel(chanerr, ioat_chan->reg_base + IOAT_CHANERR_OFFSET);
  643. }
  644. /* Allocate descriptors */
  645. for (i = 0; i < ioat_initial_desc_count; i++) {
  646. desc = ioat_dma_alloc_descriptor(ioat_chan, GFP_KERNEL);
  647. if (!desc) {
  648. dev_err(to_dev(ioat_chan),
  649. "Only %d initial descriptors\n", i);
  650. break;
  651. }
  652. list_add_tail(&desc->node, &tmp_list);
  653. }
  654. spin_lock_bh(&ioat_chan->desc_lock);
  655. ioat_chan->desccount = i;
  656. list_splice(&tmp_list, &ioat_chan->free_desc);
  657. if (ioat_chan->device->version != IOAT_VER_1_2)
  658. ioat2_dma_massage_chan_desc(ioat_chan);
  659. spin_unlock_bh(&ioat_chan->desc_lock);
  660. /* allocate a completion writeback area */
  661. /* doing 2 32bit writes to mmio since 1 64b write doesn't work */
  662. ioat_chan->completion_virt =
  663. pci_pool_alloc(ioat_chan->device->completion_pool,
  664. GFP_KERNEL,
  665. &ioat_chan->completion_addr);
  666. memset(ioat_chan->completion_virt, 0,
  667. sizeof(*ioat_chan->completion_virt));
  668. writel(((u64) ioat_chan->completion_addr) & 0x00000000FFFFFFFF,
  669. ioat_chan->reg_base + IOAT_CHANCMP_OFFSET_LOW);
  670. writel(((u64) ioat_chan->completion_addr) >> 32,
  671. ioat_chan->reg_base + IOAT_CHANCMP_OFFSET_HIGH);
  672. tasklet_enable(&ioat_chan->cleanup_task);
  673. ioat_dma_start_null_desc(ioat_chan); /* give chain to dma device */
  674. return ioat_chan->desccount;
  675. }
  676. /**
  677. * ioat_dma_free_chan_resources - release all the descriptors
  678. * @chan: the channel to be cleaned
  679. */
  680. static void ioat_dma_free_chan_resources(struct dma_chan *chan)
  681. {
  682. struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
  683. struct ioatdma_device *ioatdma_device = to_ioatdma_device(chan->device);
  684. struct ioat_desc_sw *desc, *_desc;
  685. int in_use_descs = 0;
  686. /* Before freeing channel resources first check
  687. * if they have been previously allocated for this channel.
  688. */
  689. if (ioat_chan->desccount == 0)
  690. return;
  691. tasklet_disable(&ioat_chan->cleanup_task);
  692. ioat_dma_memcpy_cleanup(ioat_chan);
  693. /* Delay 100ms after reset to allow internal DMA logic to quiesce
  694. * before removing DMA descriptor resources.
  695. */
  696. writeb(IOAT_CHANCMD_RESET,
  697. ioat_chan->reg_base
  698. + IOAT_CHANCMD_OFFSET(ioat_chan->device->version));
  699. mdelay(100);
  700. spin_lock_bh(&ioat_chan->desc_lock);
  701. switch (ioat_chan->device->version) {
  702. case IOAT_VER_1_2:
  703. list_for_each_entry_safe(desc, _desc,
  704. &ioat_chan->used_desc, node) {
  705. in_use_descs++;
  706. list_del(&desc->node);
  707. pci_pool_free(ioatdma_device->dma_pool, desc->hw,
  708. desc->txd.phys);
  709. kfree(desc);
  710. }
  711. list_for_each_entry_safe(desc, _desc,
  712. &ioat_chan->free_desc, node) {
  713. list_del(&desc->node);
  714. pci_pool_free(ioatdma_device->dma_pool, desc->hw,
  715. desc->txd.phys);
  716. kfree(desc);
  717. }
  718. break;
  719. case IOAT_VER_2_0:
  720. case IOAT_VER_3_0:
  721. list_for_each_entry_safe(desc, _desc,
  722. ioat_chan->free_desc.next, node) {
  723. list_del(&desc->node);
  724. pci_pool_free(ioatdma_device->dma_pool, desc->hw,
  725. desc->txd.phys);
  726. kfree(desc);
  727. }
  728. desc = to_ioat_desc(ioat_chan->free_desc.next);
  729. pci_pool_free(ioatdma_device->dma_pool, desc->hw,
  730. desc->txd.phys);
  731. kfree(desc);
  732. INIT_LIST_HEAD(&ioat_chan->free_desc);
  733. INIT_LIST_HEAD(&ioat_chan->used_desc);
  734. break;
  735. }
  736. spin_unlock_bh(&ioat_chan->desc_lock);
  737. pci_pool_free(ioatdma_device->completion_pool,
  738. ioat_chan->completion_virt,
  739. ioat_chan->completion_addr);
  740. /* one is ok since we left it on there on purpose */
  741. if (in_use_descs > 1)
  742. dev_err(to_dev(ioat_chan), "Freeing %d in use descriptors!\n",
  743. in_use_descs - 1);
  744. ioat_chan->last_completion = ioat_chan->completion_addr = 0;
  745. ioat_chan->pending = 0;
  746. ioat_chan->dmacount = 0;
  747. ioat_chan->desccount = 0;
  748. ioat_chan->watchdog_completion = 0;
  749. ioat_chan->last_compl_desc_addr_hw = 0;
  750. ioat_chan->watchdog_tcp_cookie =
  751. ioat_chan->watchdog_last_tcp_cookie = 0;
  752. }
  753. /**
  754. * ioat_dma_get_next_descriptor - return the next available descriptor
  755. * @ioat_chan: IOAT DMA channel handle
  756. *
  757. * Gets the next descriptor from the chain, and must be called with the
  758. * channel's desc_lock held. Allocates more descriptors if the channel
  759. * has run out.
  760. */
  761. static struct ioat_desc_sw *
  762. ioat1_dma_get_next_descriptor(struct ioat_dma_chan *ioat_chan)
  763. {
  764. struct ioat_desc_sw *new;
  765. if (!list_empty(&ioat_chan->free_desc)) {
  766. new = to_ioat_desc(ioat_chan->free_desc.next);
  767. list_del(&new->node);
  768. } else {
  769. /* try to get another desc */
  770. new = ioat_dma_alloc_descriptor(ioat_chan, GFP_ATOMIC);
  771. if (!new) {
  772. dev_err(to_dev(ioat_chan), "alloc failed\n");
  773. return NULL;
  774. }
  775. }
  776. prefetch(new->hw);
  777. return new;
  778. }
  779. static struct ioat_desc_sw *
  780. ioat2_dma_get_next_descriptor(struct ioat_dma_chan *ioat_chan)
  781. {
  782. struct ioat_desc_sw *new;
  783. /*
  784. * used.prev points to where to start processing
  785. * used.next points to next free descriptor
  786. * if used.prev == NULL, there are none waiting to be processed
  787. * if used.next == used.prev.prev, there is only one free descriptor,
  788. * and we need to use it to as a noop descriptor before
  789. * linking in a new set of descriptors, since the device
  790. * has probably already read the pointer to it
  791. */
  792. if (ioat_chan->used_desc.prev &&
  793. ioat_chan->used_desc.next == ioat_chan->used_desc.prev->prev) {
  794. struct ioat_desc_sw *desc;
  795. struct ioat_desc_sw *noop_desc;
  796. int i;
  797. /* set up the noop descriptor */
  798. noop_desc = to_ioat_desc(ioat_chan->used_desc.next);
  799. /* set size to non-zero value (channel returns error when size is 0) */
  800. noop_desc->hw->size = NULL_DESC_BUFFER_SIZE;
  801. noop_desc->hw->ctl = IOAT_DMA_DESCRIPTOR_NUL;
  802. noop_desc->hw->src_addr = 0;
  803. noop_desc->hw->dst_addr = 0;
  804. ioat_chan->used_desc.next = ioat_chan->used_desc.next->next;
  805. ioat_chan->pending++;
  806. ioat_chan->dmacount++;
  807. /* try to get a few more descriptors */
  808. for (i = 16; i; i--) {
  809. desc = ioat_dma_alloc_descriptor(ioat_chan, GFP_ATOMIC);
  810. if (!desc) {
  811. dev_err(to_dev(ioat_chan), "alloc failed\n");
  812. break;
  813. }
  814. list_add_tail(&desc->node, ioat_chan->used_desc.next);
  815. desc->hw->next
  816. = to_ioat_desc(desc->node.next)->txd.phys;
  817. to_ioat_desc(desc->node.prev)->hw->next
  818. = desc->txd.phys;
  819. ioat_chan->desccount++;
  820. }
  821. ioat_chan->used_desc.next = noop_desc->node.next;
  822. }
  823. new = to_ioat_desc(ioat_chan->used_desc.next);
  824. prefetch(new);
  825. ioat_chan->used_desc.next = new->node.next;
  826. if (ioat_chan->used_desc.prev == NULL)
  827. ioat_chan->used_desc.prev = &new->node;
  828. prefetch(new->hw);
  829. return new;
  830. }
  831. static struct ioat_desc_sw *
  832. ioat_dma_get_next_descriptor(struct ioat_dma_chan *ioat_chan)
  833. {
  834. if (!ioat_chan)
  835. return NULL;
  836. switch (ioat_chan->device->version) {
  837. case IOAT_VER_1_2:
  838. return ioat1_dma_get_next_descriptor(ioat_chan);
  839. case IOAT_VER_2_0:
  840. case IOAT_VER_3_0:
  841. return ioat2_dma_get_next_descriptor(ioat_chan);
  842. }
  843. return NULL;
  844. }
  845. static struct dma_async_tx_descriptor *
  846. ioat1_dma_prep_memcpy(struct dma_chan *chan, dma_addr_t dma_dest,
  847. dma_addr_t dma_src, size_t len, unsigned long flags)
  848. {
  849. struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
  850. struct ioat_desc_sw *new;
  851. spin_lock_bh(&ioat_chan->desc_lock);
  852. new = ioat_dma_get_next_descriptor(ioat_chan);
  853. spin_unlock_bh(&ioat_chan->desc_lock);
  854. if (new) {
  855. new->len = len;
  856. new->dst = dma_dest;
  857. new->src = dma_src;
  858. new->txd.flags = flags;
  859. return &new->txd;
  860. } else {
  861. dev_err(to_dev(ioat_chan),
  862. "chan%d - get_next_desc failed: %d descs waiting, %d total desc\n",
  863. chan_num(ioat_chan), ioat_chan->dmacount, ioat_chan->desccount);
  864. return NULL;
  865. }
  866. }
  867. static struct dma_async_tx_descriptor *
  868. ioat2_dma_prep_memcpy(struct dma_chan *chan, dma_addr_t dma_dest,
  869. dma_addr_t dma_src, size_t len, unsigned long flags)
  870. {
  871. struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
  872. struct ioat_desc_sw *new;
  873. spin_lock_bh(&ioat_chan->desc_lock);
  874. new = ioat2_dma_get_next_descriptor(ioat_chan);
  875. /*
  876. * leave ioat_chan->desc_lock set in ioat 2 path
  877. * it will get unlocked at end of tx_submit
  878. */
  879. if (new) {
  880. new->len = len;
  881. new->dst = dma_dest;
  882. new->src = dma_src;
  883. new->txd.flags = flags;
  884. return &new->txd;
  885. } else {
  886. spin_unlock_bh(&ioat_chan->desc_lock);
  887. dev_err(to_dev(ioat_chan),
  888. "chan%d - get_next_desc failed: %d descs waiting, %d total desc\n",
  889. chan_num(ioat_chan), ioat_chan->dmacount, ioat_chan->desccount);
  890. return NULL;
  891. }
  892. }
  893. static void ioat_dma_cleanup_tasklet(unsigned long data)
  894. {
  895. struct ioat_dma_chan *chan = (void *)data;
  896. ioat_dma_memcpy_cleanup(chan);
  897. writew(IOAT_CHANCTRL_INT_DISABLE,
  898. chan->reg_base + IOAT_CHANCTRL_OFFSET);
  899. }
  900. static void
  901. ioat_dma_unmap(struct ioat_dma_chan *ioat_chan, struct ioat_desc_sw *desc)
  902. {
  903. if (!(desc->txd.flags & DMA_COMPL_SKIP_DEST_UNMAP)) {
  904. if (desc->txd.flags & DMA_COMPL_DEST_UNMAP_SINGLE)
  905. pci_unmap_single(ioat_chan->device->pdev,
  906. pci_unmap_addr(desc, dst),
  907. pci_unmap_len(desc, len),
  908. PCI_DMA_FROMDEVICE);
  909. else
  910. pci_unmap_page(ioat_chan->device->pdev,
  911. pci_unmap_addr(desc, dst),
  912. pci_unmap_len(desc, len),
  913. PCI_DMA_FROMDEVICE);
  914. }
  915. if (!(desc->txd.flags & DMA_COMPL_SKIP_SRC_UNMAP)) {
  916. if (desc->txd.flags & DMA_COMPL_SRC_UNMAP_SINGLE)
  917. pci_unmap_single(ioat_chan->device->pdev,
  918. pci_unmap_addr(desc, src),
  919. pci_unmap_len(desc, len),
  920. PCI_DMA_TODEVICE);
  921. else
  922. pci_unmap_page(ioat_chan->device->pdev,
  923. pci_unmap_addr(desc, src),
  924. pci_unmap_len(desc, len),
  925. PCI_DMA_TODEVICE);
  926. }
  927. }
  928. /**
  929. * ioat_dma_memcpy_cleanup - cleanup up finished descriptors
  930. * @chan: ioat channel to be cleaned up
  931. */
  932. static void ioat_dma_memcpy_cleanup(struct ioat_dma_chan *ioat_chan)
  933. {
  934. unsigned long phys_complete;
  935. struct ioat_desc_sw *desc, *_desc;
  936. dma_cookie_t cookie = 0;
  937. unsigned long desc_phys;
  938. struct ioat_desc_sw *latest_desc;
  939. struct dma_async_tx_descriptor *tx;
  940. prefetch(ioat_chan->completion_virt);
  941. if (!spin_trylock_bh(&ioat_chan->cleanup_lock))
  942. return;
  943. /* The completion writeback can happen at any time,
  944. so reads by the driver need to be atomic operations
  945. The descriptor physical addresses are limited to 32-bits
  946. when the CPU can only do a 32-bit mov */
  947. #if (BITS_PER_LONG == 64)
  948. phys_complete =
  949. ioat_chan->completion_virt->full
  950. & IOAT_CHANSTS_COMPLETED_DESCRIPTOR_ADDR;
  951. #else
  952. phys_complete =
  953. ioat_chan->completion_virt->low & IOAT_LOW_COMPLETION_MASK;
  954. #endif
  955. if ((ioat_chan->completion_virt->full
  956. & IOAT_CHANSTS_DMA_TRANSFER_STATUS) ==
  957. IOAT_CHANSTS_DMA_TRANSFER_STATUS_HALTED) {
  958. dev_err(to_dev(ioat_chan), "Channel halted, chanerr = %x\n",
  959. readl(ioat_chan->reg_base + IOAT_CHANERR_OFFSET));
  960. /* TODO do something to salvage the situation */
  961. }
  962. if (phys_complete == ioat_chan->last_completion) {
  963. spin_unlock_bh(&ioat_chan->cleanup_lock);
  964. /*
  965. * perhaps we're stuck so hard that the watchdog can't go off?
  966. * try to catch it after 2 seconds
  967. */
  968. if (ioat_chan->device->version != IOAT_VER_3_0) {
  969. if (time_after(jiffies,
  970. ioat_chan->last_completion_time + HZ*WATCHDOG_DELAY)) {
  971. ioat_dma_chan_watchdog(&(ioat_chan->device->work.work));
  972. ioat_chan->last_completion_time = jiffies;
  973. }
  974. }
  975. return;
  976. }
  977. ioat_chan->last_completion_time = jiffies;
  978. cookie = 0;
  979. if (!spin_trylock_bh(&ioat_chan->desc_lock)) {
  980. spin_unlock_bh(&ioat_chan->cleanup_lock);
  981. return;
  982. }
  983. switch (ioat_chan->device->version) {
  984. case IOAT_VER_1_2:
  985. list_for_each_entry_safe(desc, _desc,
  986. &ioat_chan->used_desc, node) {
  987. tx = &desc->txd;
  988. /*
  989. * Incoming DMA requests may use multiple descriptors,
  990. * due to exceeding xfercap, perhaps. If so, only the
  991. * last one will have a cookie, and require unmapping.
  992. */
  993. if (tx->cookie) {
  994. cookie = tx->cookie;
  995. ioat_dma_unmap(ioat_chan, desc);
  996. if (tx->callback) {
  997. tx->callback(tx->callback_param);
  998. tx->callback = NULL;
  999. }
  1000. }
  1001. if (tx->phys != phys_complete) {
  1002. /*
  1003. * a completed entry, but not the last, so clean
  1004. * up if the client is done with the descriptor
  1005. */
  1006. if (async_tx_test_ack(tx)) {
  1007. list_move_tail(&desc->node,
  1008. &ioat_chan->free_desc);
  1009. } else
  1010. tx->cookie = 0;
  1011. } else {
  1012. /*
  1013. * last used desc. Do not remove, so we can
  1014. * append from it, but don't look at it next
  1015. * time, either
  1016. */
  1017. tx->cookie = 0;
  1018. /* TODO check status bits? */
  1019. break;
  1020. }
  1021. }
  1022. break;
  1023. case IOAT_VER_2_0:
  1024. case IOAT_VER_3_0:
  1025. /* has some other thread has already cleaned up? */
  1026. if (ioat_chan->used_desc.prev == NULL)
  1027. break;
  1028. /* work backwards to find latest finished desc */
  1029. desc = to_ioat_desc(ioat_chan->used_desc.next);
  1030. tx = &desc->txd;
  1031. latest_desc = NULL;
  1032. do {
  1033. desc = to_ioat_desc(desc->node.prev);
  1034. desc_phys = (unsigned long)tx->phys
  1035. & IOAT_CHANSTS_COMPLETED_DESCRIPTOR_ADDR;
  1036. if (desc_phys == phys_complete) {
  1037. latest_desc = desc;
  1038. break;
  1039. }
  1040. } while (&desc->node != ioat_chan->used_desc.prev);
  1041. if (latest_desc != NULL) {
  1042. /* work forwards to clear finished descriptors */
  1043. for (desc = to_ioat_desc(ioat_chan->used_desc.prev);
  1044. &desc->node != latest_desc->node.next &&
  1045. &desc->node != ioat_chan->used_desc.next;
  1046. desc = to_ioat_desc(desc->node.next)) {
  1047. if (tx->cookie) {
  1048. cookie = tx->cookie;
  1049. tx->cookie = 0;
  1050. ioat_dma_unmap(ioat_chan, desc);
  1051. if (tx->callback) {
  1052. tx->callback(tx->callback_param);
  1053. tx->callback = NULL;
  1054. }
  1055. }
  1056. }
  1057. /* move used.prev up beyond those that are finished */
  1058. if (&desc->node == ioat_chan->used_desc.next)
  1059. ioat_chan->used_desc.prev = NULL;
  1060. else
  1061. ioat_chan->used_desc.prev = &desc->node;
  1062. }
  1063. break;
  1064. }
  1065. spin_unlock_bh(&ioat_chan->desc_lock);
  1066. ioat_chan->last_completion = phys_complete;
  1067. if (cookie != 0)
  1068. ioat_chan->completed_cookie = cookie;
  1069. spin_unlock_bh(&ioat_chan->cleanup_lock);
  1070. }
  1071. /**
  1072. * ioat_dma_is_complete - poll the status of a IOAT DMA transaction
  1073. * @chan: IOAT DMA channel handle
  1074. * @cookie: DMA transaction identifier
  1075. * @done: if not %NULL, updated with last completed transaction
  1076. * @used: if not %NULL, updated with last used transaction
  1077. */
  1078. static enum dma_status
  1079. ioat_dma_is_complete(struct dma_chan *chan, dma_cookie_t cookie,
  1080. dma_cookie_t *done, dma_cookie_t *used)
  1081. {
  1082. struct ioat_dma_chan *ioat_chan = to_ioat_chan(chan);
  1083. dma_cookie_t last_used;
  1084. dma_cookie_t last_complete;
  1085. enum dma_status ret;
  1086. last_used = chan->cookie;
  1087. last_complete = ioat_chan->completed_cookie;
  1088. ioat_chan->watchdog_tcp_cookie = cookie;
  1089. if (done)
  1090. *done = last_complete;
  1091. if (used)
  1092. *used = last_used;
  1093. ret = dma_async_is_complete(cookie, last_complete, last_used);
  1094. if (ret == DMA_SUCCESS)
  1095. return ret;
  1096. ioat_dma_memcpy_cleanup(ioat_chan);
  1097. last_used = chan->cookie;
  1098. last_complete = ioat_chan->completed_cookie;
  1099. if (done)
  1100. *done = last_complete;
  1101. if (used)
  1102. *used = last_used;
  1103. return dma_async_is_complete(cookie, last_complete, last_used);
  1104. }
  1105. static void ioat_dma_start_null_desc(struct ioat_dma_chan *ioat_chan)
  1106. {
  1107. struct ioat_desc_sw *desc;
  1108. spin_lock_bh(&ioat_chan->desc_lock);
  1109. desc = ioat_dma_get_next_descriptor(ioat_chan);
  1110. if (!desc) {
  1111. dev_err(to_dev(ioat_chan),
  1112. "Unable to start null desc - get next desc failed\n");
  1113. spin_unlock_bh(&ioat_chan->desc_lock);
  1114. return;
  1115. }
  1116. desc->hw->ctl = IOAT_DMA_DESCRIPTOR_NUL
  1117. | IOAT_DMA_DESCRIPTOR_CTL_INT_GN
  1118. | IOAT_DMA_DESCRIPTOR_CTL_CP_STS;
  1119. /* set size to non-zero value (channel returns error when size is 0) */
  1120. desc->hw->size = NULL_DESC_BUFFER_SIZE;
  1121. desc->hw->src_addr = 0;
  1122. desc->hw->dst_addr = 0;
  1123. async_tx_ack(&desc->txd);
  1124. switch (ioat_chan->device->version) {
  1125. case IOAT_VER_1_2:
  1126. desc->hw->next = 0;
  1127. list_add_tail(&desc->node, &ioat_chan->used_desc);
  1128. writel(((u64) desc->txd.phys) & 0x00000000FFFFFFFF,
  1129. ioat_chan->reg_base + IOAT1_CHAINADDR_OFFSET_LOW);
  1130. writel(((u64) desc->txd.phys) >> 32,
  1131. ioat_chan->reg_base + IOAT1_CHAINADDR_OFFSET_HIGH);
  1132. writeb(IOAT_CHANCMD_START, ioat_chan->reg_base
  1133. + IOAT_CHANCMD_OFFSET(ioat_chan->device->version));
  1134. break;
  1135. case IOAT_VER_2_0:
  1136. case IOAT_VER_3_0:
  1137. writel(((u64) desc->txd.phys) & 0x00000000FFFFFFFF,
  1138. ioat_chan->reg_base + IOAT2_CHAINADDR_OFFSET_LOW);
  1139. writel(((u64) desc->txd.phys) >> 32,
  1140. ioat_chan->reg_base + IOAT2_CHAINADDR_OFFSET_HIGH);
  1141. ioat_chan->dmacount++;
  1142. __ioat2_dma_memcpy_issue_pending(ioat_chan);
  1143. break;
  1144. }
  1145. spin_unlock_bh(&ioat_chan->desc_lock);
  1146. }
  1147. /*
  1148. * Perform a IOAT transaction to verify the HW works.
  1149. */
  1150. #define IOAT_TEST_SIZE 2000
  1151. static void ioat_dma_test_callback(void *dma_async_param)
  1152. {
  1153. struct completion *cmp = dma_async_param;
  1154. complete(cmp);
  1155. }
  1156. /**
  1157. * ioat_dma_self_test - Perform a IOAT transaction to verify the HW works.
  1158. * @device: device to be tested
  1159. */
  1160. static int ioat_dma_self_test(struct ioatdma_device *device)
  1161. {
  1162. int i;
  1163. u8 *src;
  1164. u8 *dest;
  1165. struct dma_device *dma = &device->common;
  1166. struct device *dev = &device->pdev->dev;
  1167. struct dma_chan *dma_chan;
  1168. struct dma_async_tx_descriptor *tx;
  1169. dma_addr_t dma_dest, dma_src;
  1170. dma_cookie_t cookie;
  1171. int err = 0;
  1172. struct completion cmp;
  1173. unsigned long tmo;
  1174. unsigned long flags;
  1175. src = kzalloc(sizeof(u8) * IOAT_TEST_SIZE, GFP_KERNEL);
  1176. if (!src)
  1177. return -ENOMEM;
  1178. dest = kzalloc(sizeof(u8) * IOAT_TEST_SIZE, GFP_KERNEL);
  1179. if (!dest) {
  1180. kfree(src);
  1181. return -ENOMEM;
  1182. }
  1183. /* Fill in src buffer */
  1184. for (i = 0; i < IOAT_TEST_SIZE; i++)
  1185. src[i] = (u8)i;
  1186. /* Start copy, using first DMA channel */
  1187. dma_chan = container_of(dma->channels.next, struct dma_chan,
  1188. device_node);
  1189. if (dma->device_alloc_chan_resources(dma_chan) < 1) {
  1190. dev_err(dev, "selftest cannot allocate chan resource\n");
  1191. err = -ENODEV;
  1192. goto out;
  1193. }
  1194. dma_src = dma_map_single(dev, src, IOAT_TEST_SIZE, DMA_TO_DEVICE);
  1195. dma_dest = dma_map_single(dev, dest, IOAT_TEST_SIZE, DMA_FROM_DEVICE);
  1196. flags = DMA_COMPL_SRC_UNMAP_SINGLE | DMA_COMPL_DEST_UNMAP_SINGLE;
  1197. tx = device->common.device_prep_dma_memcpy(dma_chan, dma_dest, dma_src,
  1198. IOAT_TEST_SIZE, flags);
  1199. if (!tx) {
  1200. dev_err(dev, "Self-test prep failed, disabling\n");
  1201. err = -ENODEV;
  1202. goto free_resources;
  1203. }
  1204. async_tx_ack(tx);
  1205. init_completion(&cmp);
  1206. tx->callback = ioat_dma_test_callback;
  1207. tx->callback_param = &cmp;
  1208. cookie = tx->tx_submit(tx);
  1209. if (cookie < 0) {
  1210. dev_err(dev, "Self-test setup failed, disabling\n");
  1211. err = -ENODEV;
  1212. goto free_resources;
  1213. }
  1214. dma->device_issue_pending(dma_chan);
  1215. tmo = wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000));
  1216. if (tmo == 0 ||
  1217. dma->device_is_tx_complete(dma_chan, cookie, NULL, NULL)
  1218. != DMA_SUCCESS) {
  1219. dev_err(dev, "Self-test copy timed out, disabling\n");
  1220. err = -ENODEV;
  1221. goto free_resources;
  1222. }
  1223. if (memcmp(src, dest, IOAT_TEST_SIZE)) {
  1224. dev_err(dev, "Self-test copy failed compare, disabling\n");
  1225. err = -ENODEV;
  1226. goto free_resources;
  1227. }
  1228. free_resources:
  1229. dma->device_free_chan_resources(dma_chan);
  1230. out:
  1231. kfree(src);
  1232. kfree(dest);
  1233. return err;
  1234. }
  1235. static char ioat_interrupt_style[32] = "msix";
  1236. module_param_string(ioat_interrupt_style, ioat_interrupt_style,
  1237. sizeof(ioat_interrupt_style), 0644);
  1238. MODULE_PARM_DESC(ioat_interrupt_style,
  1239. "set ioat interrupt style: msix (default), "
  1240. "msix-single-vector, msi, intx)");
  1241. /**
  1242. * ioat_dma_setup_interrupts - setup interrupt handler
  1243. * @device: ioat device
  1244. */
  1245. static int ioat_dma_setup_interrupts(struct ioatdma_device *device)
  1246. {
  1247. struct ioat_dma_chan *ioat_chan;
  1248. struct pci_dev *pdev = device->pdev;
  1249. struct device *dev = &pdev->dev;
  1250. struct msix_entry *msix;
  1251. int i, j, msixcnt;
  1252. int err = -EINVAL;
  1253. u8 intrctrl = 0;
  1254. if (!strcmp(ioat_interrupt_style, "msix"))
  1255. goto msix;
  1256. if (!strcmp(ioat_interrupt_style, "msix-single-vector"))
  1257. goto msix_single_vector;
  1258. if (!strcmp(ioat_interrupt_style, "msi"))
  1259. goto msi;
  1260. if (!strcmp(ioat_interrupt_style, "intx"))
  1261. goto intx;
  1262. dev_err(dev, "invalid ioat_interrupt_style %s\n", ioat_interrupt_style);
  1263. goto err_no_irq;
  1264. msix:
  1265. /* The number of MSI-X vectors should equal the number of channels */
  1266. msixcnt = device->common.chancnt;
  1267. for (i = 0; i < msixcnt; i++)
  1268. device->msix_entries[i].entry = i;
  1269. err = pci_enable_msix(pdev, device->msix_entries, msixcnt);
  1270. if (err < 0)
  1271. goto msi;
  1272. if (err > 0)
  1273. goto msix_single_vector;
  1274. for (i = 0; i < msixcnt; i++) {
  1275. msix = &device->msix_entries[i];
  1276. ioat_chan = ioat_chan_by_index(device, i);
  1277. err = devm_request_irq(dev, msix->vector,
  1278. ioat_dma_do_interrupt_msix, 0,
  1279. "ioat-msix", ioat_chan);
  1280. if (err) {
  1281. for (j = 0; j < i; j++) {
  1282. msix = &device->msix_entries[j];
  1283. ioat_chan = ioat_chan_by_index(device, j);
  1284. devm_free_irq(dev, msix->vector, ioat_chan);
  1285. }
  1286. goto msix_single_vector;
  1287. }
  1288. }
  1289. intrctrl |= IOAT_INTRCTRL_MSIX_VECTOR_CONTROL;
  1290. goto done;
  1291. msix_single_vector:
  1292. msix = &device->msix_entries[0];
  1293. msix->entry = 0;
  1294. err = pci_enable_msix(pdev, device->msix_entries, 1);
  1295. if (err)
  1296. goto msi;
  1297. err = devm_request_irq(dev, msix->vector, ioat_dma_do_interrupt, 0,
  1298. "ioat-msix", device);
  1299. if (err) {
  1300. pci_disable_msix(pdev);
  1301. goto msi;
  1302. }
  1303. goto done;
  1304. msi:
  1305. err = pci_enable_msi(pdev);
  1306. if (err)
  1307. goto intx;
  1308. err = devm_request_irq(dev, pdev->irq, ioat_dma_do_interrupt, 0,
  1309. "ioat-msi", device);
  1310. if (err) {
  1311. pci_disable_msi(pdev);
  1312. goto intx;
  1313. }
  1314. /*
  1315. * CB 1.2 devices need a bit set in configuration space to enable MSI
  1316. */
  1317. if (device->version == IOAT_VER_1_2) {
  1318. u32 dmactrl;
  1319. pci_read_config_dword(pdev, IOAT_PCI_DMACTRL_OFFSET, &dmactrl);
  1320. dmactrl |= IOAT_PCI_DMACTRL_MSI_EN;
  1321. pci_write_config_dword(pdev, IOAT_PCI_DMACTRL_OFFSET, dmactrl);
  1322. }
  1323. goto done;
  1324. intx:
  1325. err = devm_request_irq(dev, pdev->irq, ioat_dma_do_interrupt,
  1326. IRQF_SHARED, "ioat-intx", device);
  1327. if (err)
  1328. goto err_no_irq;
  1329. done:
  1330. intrctrl |= IOAT_INTRCTRL_MASTER_INT_EN;
  1331. writeb(intrctrl, device->reg_base + IOAT_INTRCTRL_OFFSET);
  1332. return 0;
  1333. err_no_irq:
  1334. /* Disable all interrupt generation */
  1335. writeb(0, device->reg_base + IOAT_INTRCTRL_OFFSET);
  1336. dev_err(dev, "no usable interrupts\n");
  1337. return err;
  1338. }
  1339. static void ioat_disable_interrupts(struct ioatdma_device *device)
  1340. {
  1341. /* Disable all interrupt generation */
  1342. writeb(0, device->reg_base + IOAT_INTRCTRL_OFFSET);
  1343. }
  1344. struct ioatdma_device *
  1345. ioat_dma_probe(struct pci_dev *pdev, void __iomem *iobase)
  1346. {
  1347. int err;
  1348. struct device *dev = &pdev->dev;
  1349. struct ioatdma_device *device;
  1350. struct dma_device *dma;
  1351. device = devm_kzalloc(dev, sizeof(*device), GFP_KERNEL);
  1352. if (!device)
  1353. err = -ENOMEM;
  1354. device->pdev = pdev;
  1355. device->reg_base = iobase;
  1356. device->version = readb(device->reg_base + IOAT_VER_OFFSET);
  1357. dma = &device->common;
  1358. /* DMA coherent memory pool for DMA descriptor allocations */
  1359. device->dma_pool = pci_pool_create("dma_desc_pool", pdev,
  1360. sizeof(struct ioat_dma_descriptor),
  1361. 64, 0);
  1362. if (!device->dma_pool) {
  1363. err = -ENOMEM;
  1364. goto err_dma_pool;
  1365. }
  1366. device->completion_pool = pci_pool_create("completion_pool", pdev,
  1367. sizeof(u64), SMP_CACHE_BYTES,
  1368. SMP_CACHE_BYTES);
  1369. if (!device->completion_pool) {
  1370. err = -ENOMEM;
  1371. goto err_completion_pool;
  1372. }
  1373. INIT_LIST_HEAD(&dma->channels);
  1374. ioat_dma_enumerate_channels(device);
  1375. dma->device_alloc_chan_resources = ioat_dma_alloc_chan_resources;
  1376. dma->device_free_chan_resources = ioat_dma_free_chan_resources;
  1377. dma->dev = &pdev->dev;
  1378. dma_cap_set(DMA_MEMCPY, dma->cap_mask);
  1379. dma->device_is_tx_complete = ioat_dma_is_complete;
  1380. switch (device->version) {
  1381. case IOAT_VER_1_2:
  1382. dma->device_prep_dma_memcpy = ioat1_dma_prep_memcpy;
  1383. dma->device_issue_pending = ioat1_dma_memcpy_issue_pending;
  1384. break;
  1385. case IOAT_VER_2_0:
  1386. case IOAT_VER_3_0:
  1387. dma->device_prep_dma_memcpy = ioat2_dma_prep_memcpy;
  1388. dma->device_issue_pending = ioat2_dma_memcpy_issue_pending;
  1389. break;
  1390. }
  1391. dev_err(dev, "Intel(R) I/OAT DMA Engine found,"
  1392. " %d channels, device version 0x%02x, driver version %s\n",
  1393. dma->chancnt, device->version, IOAT_DMA_VERSION);
  1394. if (!dma->chancnt) {
  1395. dev_err(dev, "Intel(R) I/OAT DMA Engine problem found: "
  1396. "zero channels detected\n");
  1397. goto err_setup_interrupts;
  1398. }
  1399. err = ioat_dma_setup_interrupts(device);
  1400. if (err)
  1401. goto err_setup_interrupts;
  1402. err = ioat_dma_self_test(device);
  1403. if (err)
  1404. goto err_self_test;
  1405. err = dma_async_device_register(dma);
  1406. if (err)
  1407. goto err_self_test;
  1408. ioat_set_tcp_copy_break(device);
  1409. if (device->version != IOAT_VER_3_0) {
  1410. INIT_DELAYED_WORK(&device->work, ioat_dma_chan_watchdog);
  1411. schedule_delayed_work(&device->work,
  1412. WATCHDOG_DELAY);
  1413. }
  1414. return device;
  1415. err_self_test:
  1416. ioat_disable_interrupts(device);
  1417. err_setup_interrupts:
  1418. pci_pool_destroy(device->completion_pool);
  1419. err_completion_pool:
  1420. pci_pool_destroy(device->dma_pool);
  1421. err_dma_pool:
  1422. return NULL;
  1423. }
  1424. void ioat_dma_remove(struct ioatdma_device *device)
  1425. {
  1426. struct dma_chan *chan, *_chan;
  1427. struct ioat_dma_chan *ioat_chan;
  1428. struct dma_device *dma = &device->common;
  1429. if (device->version != IOAT_VER_3_0)
  1430. cancel_delayed_work(&device->work);
  1431. ioat_disable_interrupts(device);
  1432. dma_async_device_unregister(dma);
  1433. pci_pool_destroy(device->dma_pool);
  1434. pci_pool_destroy(device->completion_pool);
  1435. list_for_each_entry_safe(chan, _chan, &dma->channels, device_node) {
  1436. ioat_chan = to_ioat_chan(chan);
  1437. list_del(&chan->device_node);
  1438. }
  1439. }