dma_v3.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301
  1. /*
  2. * This file is provided under a dual BSD/GPLv2 license. When using or
  3. * redistributing this file, you may do so under either license.
  4. *
  5. * GPL LICENSE SUMMARY
  6. *
  7. * Copyright(c) 2004 - 2009 Intel Corporation. All rights reserved.
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms and conditions of the GNU General Public License,
  11. * version 2, as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but WITHOUT
  14. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  16. * more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along with
  19. * this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  21. *
  22. * The full GNU General Public License is included in this distribution in
  23. * the file called "COPYING".
  24. *
  25. * BSD LICENSE
  26. *
  27. * Copyright(c) 2004-2009 Intel Corporation. All rights reserved.
  28. *
  29. * Redistribution and use in source and binary forms, with or without
  30. * modification, are permitted provided that the following conditions are met:
  31. *
  32. * * Redistributions of source code must retain the above copyright
  33. * notice, this list of conditions and the following disclaimer.
  34. * * Redistributions in binary form must reproduce the above copyright
  35. * notice, this list of conditions and the following disclaimer in
  36. * the documentation and/or other materials provided with the
  37. * distribution.
  38. * * Neither the name of Intel Corporation nor the names of its
  39. * contributors may be used to endorse or promote products derived
  40. * from this software without specific prior written permission.
  41. *
  42. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  43. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  44. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  45. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  46. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  47. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  48. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  49. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  50. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  51. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  52. * POSSIBILITY OF SUCH DAMAGE.
  53. */
  54. /*
  55. * Support routines for v3+ hardware
  56. */
  57. #include <linux/pci.h>
  58. #include <linux/dmaengine.h>
  59. #include <linux/dma-mapping.h>
  60. #include "registers.h"
  61. #include "hw.h"
  62. #include "dma.h"
  63. #include "dma_v2.h"
  64. /* ioat hardware assumes at least two sources for raid operations */
  65. #define src_cnt_to_sw(x) ((x) + 2)
  66. #define src_cnt_to_hw(x) ((x) - 2)
  67. /* provide a lookup table for setting the source address in the base or
  68. * extended descriptor of an xor or pq descriptor
  69. */
  70. static const u8 xor_idx_to_desc __read_mostly = 0xd0;
  71. static const u8 xor_idx_to_field[] __read_mostly = { 1, 4, 5, 6, 7, 0, 1, 2 };
  72. static const u8 pq_idx_to_desc __read_mostly = 0xf8;
  73. static const u8 pq_idx_to_field[] __read_mostly = { 1, 4, 5, 0, 1, 2, 4, 5 };
  74. static dma_addr_t xor_get_src(struct ioat_raw_descriptor *descs[2], int idx)
  75. {
  76. struct ioat_raw_descriptor *raw = descs[xor_idx_to_desc >> idx & 1];
  77. return raw->field[xor_idx_to_field[idx]];
  78. }
  79. static void xor_set_src(struct ioat_raw_descriptor *descs[2],
  80. dma_addr_t addr, u32 offset, int idx)
  81. {
  82. struct ioat_raw_descriptor *raw = descs[xor_idx_to_desc >> idx & 1];
  83. raw->field[xor_idx_to_field[idx]] = addr + offset;
  84. }
  85. static dma_addr_t pq_get_src(struct ioat_raw_descriptor *descs[2], int idx)
  86. {
  87. struct ioat_raw_descriptor *raw = descs[pq_idx_to_desc >> idx & 1];
  88. return raw->field[pq_idx_to_field[idx]];
  89. }
  90. static void pq_set_src(struct ioat_raw_descriptor *descs[2],
  91. dma_addr_t addr, u32 offset, u8 coef, int idx)
  92. {
  93. struct ioat_pq_descriptor *pq = (struct ioat_pq_descriptor *) descs[0];
  94. struct ioat_raw_descriptor *raw = descs[pq_idx_to_desc >> idx & 1];
  95. raw->field[pq_idx_to_field[idx]] = addr + offset;
  96. pq->coef[idx] = coef;
  97. }
  98. static void ioat3_dma_unmap(struct ioat2_dma_chan *ioat,
  99. struct ioat_ring_ent *desc, int idx)
  100. {
  101. struct ioat_chan_common *chan = &ioat->base;
  102. struct pci_dev *pdev = chan->device->pdev;
  103. size_t len = desc->len;
  104. size_t offset = len - desc->hw->size;
  105. struct dma_async_tx_descriptor *tx = &desc->txd;
  106. enum dma_ctrl_flags flags = tx->flags;
  107. switch (desc->hw->ctl_f.op) {
  108. case IOAT_OP_COPY:
  109. if (!desc->hw->ctl_f.null) /* skip 'interrupt' ops */
  110. ioat_dma_unmap(chan, flags, len, desc->hw);
  111. break;
  112. case IOAT_OP_FILL: {
  113. struct ioat_fill_descriptor *hw = desc->fill;
  114. if (!(flags & DMA_COMPL_SKIP_DEST_UNMAP))
  115. ioat_unmap(pdev, hw->dst_addr - offset, len,
  116. PCI_DMA_FROMDEVICE, flags, 1);
  117. break;
  118. }
  119. case IOAT_OP_XOR_VAL:
  120. case IOAT_OP_XOR: {
  121. struct ioat_xor_descriptor *xor = desc->xor;
  122. struct ioat_ring_ent *ext;
  123. struct ioat_xor_ext_descriptor *xor_ex = NULL;
  124. int src_cnt = src_cnt_to_sw(xor->ctl_f.src_cnt);
  125. struct ioat_raw_descriptor *descs[2];
  126. int i;
  127. if (src_cnt > 5) {
  128. ext = ioat2_get_ring_ent(ioat, idx + 1);
  129. xor_ex = ext->xor_ex;
  130. }
  131. if (!(flags & DMA_COMPL_SKIP_SRC_UNMAP)) {
  132. descs[0] = (struct ioat_raw_descriptor *) xor;
  133. descs[1] = (struct ioat_raw_descriptor *) xor_ex;
  134. for (i = 0; i < src_cnt; i++) {
  135. dma_addr_t src = xor_get_src(descs, i);
  136. ioat_unmap(pdev, src - offset, len,
  137. PCI_DMA_TODEVICE, flags, 0);
  138. }
  139. /* dest is a source in xor validate operations */
  140. if (xor->ctl_f.op == IOAT_OP_XOR_VAL) {
  141. ioat_unmap(pdev, xor->dst_addr - offset, len,
  142. PCI_DMA_TODEVICE, flags, 1);
  143. break;
  144. }
  145. }
  146. if (!(flags & DMA_COMPL_SKIP_DEST_UNMAP))
  147. ioat_unmap(pdev, xor->dst_addr - offset, len,
  148. PCI_DMA_FROMDEVICE, flags, 1);
  149. break;
  150. }
  151. case IOAT_OP_PQ_VAL:
  152. case IOAT_OP_PQ: {
  153. struct ioat_pq_descriptor *pq = desc->pq;
  154. struct ioat_ring_ent *ext;
  155. struct ioat_pq_ext_descriptor *pq_ex = NULL;
  156. int src_cnt = src_cnt_to_sw(pq->ctl_f.src_cnt);
  157. struct ioat_raw_descriptor *descs[2];
  158. int i;
  159. if (src_cnt > 3) {
  160. ext = ioat2_get_ring_ent(ioat, idx + 1);
  161. pq_ex = ext->pq_ex;
  162. }
  163. /* in the 'continue' case don't unmap the dests as sources */
  164. if (dmaf_p_disabled_continue(flags))
  165. src_cnt--;
  166. else if (dmaf_continue(flags))
  167. src_cnt -= 3;
  168. if (!(flags & DMA_COMPL_SKIP_SRC_UNMAP)) {
  169. descs[0] = (struct ioat_raw_descriptor *) pq;
  170. descs[1] = (struct ioat_raw_descriptor *) pq_ex;
  171. for (i = 0; i < src_cnt; i++) {
  172. dma_addr_t src = pq_get_src(descs, i);
  173. ioat_unmap(pdev, src - offset, len,
  174. PCI_DMA_TODEVICE, flags, 0);
  175. }
  176. /* the dests are sources in pq validate operations */
  177. if (pq->ctl_f.op == IOAT_OP_XOR_VAL) {
  178. if (!(flags & DMA_PREP_PQ_DISABLE_P))
  179. ioat_unmap(pdev, pq->p_addr - offset,
  180. len, PCI_DMA_TODEVICE, flags, 0);
  181. if (!(flags & DMA_PREP_PQ_DISABLE_Q))
  182. ioat_unmap(pdev, pq->q_addr - offset,
  183. len, PCI_DMA_TODEVICE, flags, 0);
  184. break;
  185. }
  186. }
  187. if (!(flags & DMA_COMPL_SKIP_DEST_UNMAP)) {
  188. if (!(flags & DMA_PREP_PQ_DISABLE_P))
  189. ioat_unmap(pdev, pq->p_addr - offset, len,
  190. PCI_DMA_BIDIRECTIONAL, flags, 1);
  191. if (!(flags & DMA_PREP_PQ_DISABLE_Q))
  192. ioat_unmap(pdev, pq->q_addr - offset, len,
  193. PCI_DMA_BIDIRECTIONAL, flags, 1);
  194. }
  195. break;
  196. }
  197. default:
  198. dev_err(&pdev->dev, "%s: unknown op type: %#x\n",
  199. __func__, desc->hw->ctl_f.op);
  200. }
  201. }
  202. static bool desc_has_ext(struct ioat_ring_ent *desc)
  203. {
  204. struct ioat_dma_descriptor *hw = desc->hw;
  205. if (hw->ctl_f.op == IOAT_OP_XOR ||
  206. hw->ctl_f.op == IOAT_OP_XOR_VAL) {
  207. struct ioat_xor_descriptor *xor = desc->xor;
  208. if (src_cnt_to_sw(xor->ctl_f.src_cnt) > 5)
  209. return true;
  210. } else if (hw->ctl_f.op == IOAT_OP_PQ ||
  211. hw->ctl_f.op == IOAT_OP_PQ_VAL) {
  212. struct ioat_pq_descriptor *pq = desc->pq;
  213. if (src_cnt_to_sw(pq->ctl_f.src_cnt) > 3)
  214. return true;
  215. }
  216. return false;
  217. }
  218. /**
  219. * __cleanup - reclaim used descriptors
  220. * @ioat: channel (ring) to clean
  221. *
  222. * The difference from the dma_v2.c __cleanup() is that this routine
  223. * handles extended descriptors and dma-unmapping raid operations.
  224. */
  225. static void __cleanup(struct ioat2_dma_chan *ioat, unsigned long phys_complete)
  226. {
  227. struct ioat_chan_common *chan = &ioat->base;
  228. struct ioat_ring_ent *desc;
  229. bool seen_current = false;
  230. u16 active;
  231. int i;
  232. dev_dbg(to_dev(chan), "%s: head: %#x tail: %#x issued: %#x\n",
  233. __func__, ioat->head, ioat->tail, ioat->issued);
  234. active = ioat2_ring_active(ioat);
  235. for (i = 0; i < active && !seen_current; i++) {
  236. struct dma_async_tx_descriptor *tx;
  237. prefetch(ioat2_get_ring_ent(ioat, ioat->tail + i + 1));
  238. desc = ioat2_get_ring_ent(ioat, ioat->tail + i);
  239. dump_desc_dbg(ioat, desc);
  240. tx = &desc->txd;
  241. if (tx->cookie) {
  242. chan->completed_cookie = tx->cookie;
  243. ioat3_dma_unmap(ioat, desc, ioat->tail + i);
  244. tx->cookie = 0;
  245. if (tx->callback) {
  246. tx->callback(tx->callback_param);
  247. tx->callback = NULL;
  248. }
  249. }
  250. if (tx->phys == phys_complete)
  251. seen_current = true;
  252. /* skip extended descriptors */
  253. if (desc_has_ext(desc)) {
  254. BUG_ON(i + 1 >= active);
  255. i++;
  256. }
  257. }
  258. ioat->tail += i;
  259. BUG_ON(active && !seen_current); /* no active descs have written a completion? */
  260. chan->last_completion = phys_complete;
  261. active = ioat2_ring_active(ioat);
  262. if (active == 0) {
  263. dev_dbg(to_dev(chan), "%s: cancel completion timeout\n",
  264. __func__);
  265. clear_bit(IOAT_COMPLETION_PENDING, &chan->state);
  266. mod_timer(&chan->timer, jiffies + IDLE_TIMEOUT);
  267. }
  268. /* 5 microsecond delay per pending descriptor */
  269. writew(min((5 * active), IOAT_INTRDELAY_MASK),
  270. chan->device->reg_base + IOAT_INTRDELAY_OFFSET);
  271. }
  272. /* try to cleanup, but yield (via spin_trylock) to incoming submissions
  273. * with the expectation that we will immediately poll again shortly
  274. */
  275. static void ioat3_cleanup_poll(struct ioat2_dma_chan *ioat)
  276. {
  277. struct ioat_chan_common *chan = &ioat->base;
  278. unsigned long phys_complete;
  279. prefetch(chan->completion);
  280. if (!spin_trylock_bh(&chan->cleanup_lock))
  281. return;
  282. if (!ioat_cleanup_preamble(chan, &phys_complete)) {
  283. spin_unlock_bh(&chan->cleanup_lock);
  284. return;
  285. }
  286. if (!spin_trylock_bh(&ioat->ring_lock)) {
  287. spin_unlock_bh(&chan->cleanup_lock);
  288. return;
  289. }
  290. __cleanup(ioat, phys_complete);
  291. spin_unlock_bh(&ioat->ring_lock);
  292. spin_unlock_bh(&chan->cleanup_lock);
  293. }
  294. /* run cleanup now because we already delayed the interrupt via INTRDELAY */
  295. static void ioat3_cleanup_sync(struct ioat2_dma_chan *ioat)
  296. {
  297. struct ioat_chan_common *chan = &ioat->base;
  298. unsigned long phys_complete;
  299. prefetch(chan->completion);
  300. spin_lock_bh(&chan->cleanup_lock);
  301. if (!ioat_cleanup_preamble(chan, &phys_complete)) {
  302. spin_unlock_bh(&chan->cleanup_lock);
  303. return;
  304. }
  305. spin_lock_bh(&ioat->ring_lock);
  306. __cleanup(ioat, phys_complete);
  307. spin_unlock_bh(&ioat->ring_lock);
  308. spin_unlock_bh(&chan->cleanup_lock);
  309. }
  310. static void ioat3_cleanup_event(unsigned long data)
  311. {
  312. struct ioat2_dma_chan *ioat = to_ioat2_chan((void *) data);
  313. ioat3_cleanup_sync(ioat);
  314. writew(IOAT_CHANCTRL_RUN, ioat->base.reg_base + IOAT_CHANCTRL_OFFSET);
  315. }
  316. static void ioat3_restart_channel(struct ioat2_dma_chan *ioat)
  317. {
  318. struct ioat_chan_common *chan = &ioat->base;
  319. unsigned long phys_complete;
  320. ioat2_quiesce(chan, 0);
  321. if (ioat_cleanup_preamble(chan, &phys_complete))
  322. __cleanup(ioat, phys_complete);
  323. __ioat2_restart_chan(ioat);
  324. }
  325. static void ioat3_timer_event(unsigned long data)
  326. {
  327. struct ioat2_dma_chan *ioat = to_ioat2_chan((void *) data);
  328. struct ioat_chan_common *chan = &ioat->base;
  329. spin_lock_bh(&chan->cleanup_lock);
  330. if (test_bit(IOAT_COMPLETION_PENDING, &chan->state)) {
  331. unsigned long phys_complete;
  332. u64 status;
  333. spin_lock_bh(&ioat->ring_lock);
  334. status = ioat_chansts(chan);
  335. /* when halted due to errors check for channel
  336. * programming errors before advancing the completion state
  337. */
  338. if (is_ioat_halted(status)) {
  339. u32 chanerr;
  340. chanerr = readl(chan->reg_base + IOAT_CHANERR_OFFSET);
  341. dev_err(to_dev(chan), "%s: Channel halted (%x)\n",
  342. __func__, chanerr);
  343. BUG_ON(is_ioat_bug(chanerr));
  344. }
  345. /* if we haven't made progress and we have already
  346. * acknowledged a pending completion once, then be more
  347. * forceful with a restart
  348. */
  349. if (ioat_cleanup_preamble(chan, &phys_complete))
  350. __cleanup(ioat, phys_complete);
  351. else if (test_bit(IOAT_COMPLETION_ACK, &chan->state))
  352. ioat3_restart_channel(ioat);
  353. else {
  354. set_bit(IOAT_COMPLETION_ACK, &chan->state);
  355. mod_timer(&chan->timer, jiffies + COMPLETION_TIMEOUT);
  356. }
  357. spin_unlock_bh(&ioat->ring_lock);
  358. } else {
  359. u16 active;
  360. /* if the ring is idle, empty, and oversized try to step
  361. * down the size
  362. */
  363. spin_lock_bh(&ioat->ring_lock);
  364. active = ioat2_ring_active(ioat);
  365. if (active == 0 && ioat->alloc_order > ioat_get_alloc_order())
  366. reshape_ring(ioat, ioat->alloc_order-1);
  367. spin_unlock_bh(&ioat->ring_lock);
  368. /* keep shrinking until we get back to our minimum
  369. * default size
  370. */
  371. if (ioat->alloc_order > ioat_get_alloc_order())
  372. mod_timer(&chan->timer, jiffies + IDLE_TIMEOUT);
  373. }
  374. spin_unlock_bh(&chan->cleanup_lock);
  375. }
  376. static enum dma_status
  377. ioat3_is_complete(struct dma_chan *c, dma_cookie_t cookie,
  378. dma_cookie_t *done, dma_cookie_t *used)
  379. {
  380. struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
  381. if (ioat_is_complete(c, cookie, done, used) == DMA_SUCCESS)
  382. return DMA_SUCCESS;
  383. ioat3_cleanup_poll(ioat);
  384. return ioat_is_complete(c, cookie, done, used);
  385. }
  386. static struct dma_async_tx_descriptor *
  387. ioat3_prep_memset_lock(struct dma_chan *c, dma_addr_t dest, int value,
  388. size_t len, unsigned long flags)
  389. {
  390. struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
  391. struct ioat_ring_ent *desc;
  392. size_t total_len = len;
  393. struct ioat_fill_descriptor *fill;
  394. int num_descs;
  395. u64 src_data = (0x0101010101010101ULL) * (value & 0xff);
  396. u16 idx;
  397. int i;
  398. num_descs = ioat2_xferlen_to_descs(ioat, len);
  399. if (likely(num_descs) &&
  400. ioat2_alloc_and_lock(&idx, ioat, num_descs) == 0)
  401. /* pass */;
  402. else
  403. return NULL;
  404. i = 0;
  405. do {
  406. size_t xfer_size = min_t(size_t, len, 1 << ioat->xfercap_log);
  407. desc = ioat2_get_ring_ent(ioat, idx + i);
  408. fill = desc->fill;
  409. fill->size = xfer_size;
  410. fill->src_data = src_data;
  411. fill->dst_addr = dest;
  412. fill->ctl = 0;
  413. fill->ctl_f.op = IOAT_OP_FILL;
  414. len -= xfer_size;
  415. dest += xfer_size;
  416. dump_desc_dbg(ioat, desc);
  417. } while (++i < num_descs);
  418. desc->txd.flags = flags;
  419. desc->len = total_len;
  420. fill->ctl_f.int_en = !!(flags & DMA_PREP_INTERRUPT);
  421. fill->ctl_f.fence = !!(flags & DMA_PREP_FENCE);
  422. fill->ctl_f.compl_write = 1;
  423. dump_desc_dbg(ioat, desc);
  424. /* we leave the channel locked to ensure in order submission */
  425. return &desc->txd;
  426. }
  427. static struct dma_async_tx_descriptor *
  428. __ioat3_prep_xor_lock(struct dma_chan *c, enum sum_check_flags *result,
  429. dma_addr_t dest, dma_addr_t *src, unsigned int src_cnt,
  430. size_t len, unsigned long flags)
  431. {
  432. struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
  433. struct ioat_ring_ent *compl_desc;
  434. struct ioat_ring_ent *desc;
  435. struct ioat_ring_ent *ext;
  436. size_t total_len = len;
  437. struct ioat_xor_descriptor *xor;
  438. struct ioat_xor_ext_descriptor *xor_ex = NULL;
  439. struct ioat_dma_descriptor *hw;
  440. u32 offset = 0;
  441. int num_descs;
  442. int with_ext;
  443. int i;
  444. u16 idx;
  445. u8 op = result ? IOAT_OP_XOR_VAL : IOAT_OP_XOR;
  446. BUG_ON(src_cnt < 2);
  447. num_descs = ioat2_xferlen_to_descs(ioat, len);
  448. /* we need 2x the number of descriptors to cover greater than 5
  449. * sources
  450. */
  451. if (src_cnt > 5) {
  452. with_ext = 1;
  453. num_descs *= 2;
  454. } else
  455. with_ext = 0;
  456. /* completion writes from the raid engine may pass completion
  457. * writes from the legacy engine, so we need one extra null
  458. * (legacy) descriptor to ensure all completion writes arrive in
  459. * order.
  460. */
  461. if (likely(num_descs) &&
  462. ioat2_alloc_and_lock(&idx, ioat, num_descs+1) == 0)
  463. /* pass */;
  464. else
  465. return NULL;
  466. i = 0;
  467. do {
  468. struct ioat_raw_descriptor *descs[2];
  469. size_t xfer_size = min_t(size_t, len, 1 << ioat->xfercap_log);
  470. int s;
  471. desc = ioat2_get_ring_ent(ioat, idx + i);
  472. xor = desc->xor;
  473. /* save a branch by unconditionally retrieving the
  474. * extended descriptor xor_set_src() knows to not write
  475. * to it in the single descriptor case
  476. */
  477. ext = ioat2_get_ring_ent(ioat, idx + i + 1);
  478. xor_ex = ext->xor_ex;
  479. descs[0] = (struct ioat_raw_descriptor *) xor;
  480. descs[1] = (struct ioat_raw_descriptor *) xor_ex;
  481. for (s = 0; s < src_cnt; s++)
  482. xor_set_src(descs, src[s], offset, s);
  483. xor->size = xfer_size;
  484. xor->dst_addr = dest + offset;
  485. xor->ctl = 0;
  486. xor->ctl_f.op = op;
  487. xor->ctl_f.src_cnt = src_cnt_to_hw(src_cnt);
  488. len -= xfer_size;
  489. offset += xfer_size;
  490. dump_desc_dbg(ioat, desc);
  491. } while ((i += 1 + with_ext) < num_descs);
  492. /* last xor descriptor carries the unmap parameters and fence bit */
  493. desc->txd.flags = flags;
  494. desc->len = total_len;
  495. if (result)
  496. desc->result = result;
  497. xor->ctl_f.fence = !!(flags & DMA_PREP_FENCE);
  498. /* completion descriptor carries interrupt bit */
  499. compl_desc = ioat2_get_ring_ent(ioat, idx + i);
  500. compl_desc->txd.flags = flags & DMA_PREP_INTERRUPT;
  501. hw = compl_desc->hw;
  502. hw->ctl = 0;
  503. hw->ctl_f.null = 1;
  504. hw->ctl_f.int_en = !!(flags & DMA_PREP_INTERRUPT);
  505. hw->ctl_f.compl_write = 1;
  506. hw->size = NULL_DESC_BUFFER_SIZE;
  507. dump_desc_dbg(ioat, compl_desc);
  508. /* we leave the channel locked to ensure in order submission */
  509. return &compl_desc->txd;
  510. }
  511. static struct dma_async_tx_descriptor *
  512. ioat3_prep_xor(struct dma_chan *chan, dma_addr_t dest, dma_addr_t *src,
  513. unsigned int src_cnt, size_t len, unsigned long flags)
  514. {
  515. return __ioat3_prep_xor_lock(chan, NULL, dest, src, src_cnt, len, flags);
  516. }
  517. struct dma_async_tx_descriptor *
  518. ioat3_prep_xor_val(struct dma_chan *chan, dma_addr_t *src,
  519. unsigned int src_cnt, size_t len,
  520. enum sum_check_flags *result, unsigned long flags)
  521. {
  522. /* the cleanup routine only sets bits on validate failure, it
  523. * does not clear bits on validate success... so clear it here
  524. */
  525. *result = 0;
  526. return __ioat3_prep_xor_lock(chan, result, src[0], &src[1],
  527. src_cnt - 1, len, flags);
  528. }
  529. static void
  530. dump_pq_desc_dbg(struct ioat2_dma_chan *ioat, struct ioat_ring_ent *desc, struct ioat_ring_ent *ext)
  531. {
  532. struct device *dev = to_dev(&ioat->base);
  533. struct ioat_pq_descriptor *pq = desc->pq;
  534. struct ioat_pq_ext_descriptor *pq_ex = ext ? ext->pq_ex : NULL;
  535. struct ioat_raw_descriptor *descs[] = { (void *) pq, (void *) pq_ex };
  536. int src_cnt = src_cnt_to_sw(pq->ctl_f.src_cnt);
  537. int i;
  538. dev_dbg(dev, "desc[%d]: (%#llx->%#llx) flags: %#x"
  539. " sz: %#x ctl: %#x (op: %d int: %d compl: %d pq: '%s%s' src_cnt: %d)\n",
  540. desc_id(desc), (unsigned long long) desc->txd.phys,
  541. (unsigned long long) (pq_ex ? pq_ex->next : pq->next),
  542. desc->txd.flags, pq->size, pq->ctl, pq->ctl_f.op, pq->ctl_f.int_en,
  543. pq->ctl_f.compl_write,
  544. pq->ctl_f.p_disable ? "" : "p", pq->ctl_f.q_disable ? "" : "q",
  545. pq->ctl_f.src_cnt);
  546. for (i = 0; i < src_cnt; i++)
  547. dev_dbg(dev, "\tsrc[%d]: %#llx coef: %#x\n", i,
  548. (unsigned long long) pq_get_src(descs, i), pq->coef[i]);
  549. dev_dbg(dev, "\tP: %#llx\n", pq->p_addr);
  550. dev_dbg(dev, "\tQ: %#llx\n", pq->q_addr);
  551. }
  552. static struct dma_async_tx_descriptor *
  553. __ioat3_prep_pq_lock(struct dma_chan *c, enum sum_check_flags *result,
  554. const dma_addr_t *dst, const dma_addr_t *src,
  555. unsigned int src_cnt, const unsigned char *scf,
  556. size_t len, unsigned long flags)
  557. {
  558. struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
  559. struct ioat_chan_common *chan = &ioat->base;
  560. struct ioat_ring_ent *compl_desc;
  561. struct ioat_ring_ent *desc;
  562. struct ioat_ring_ent *ext;
  563. size_t total_len = len;
  564. struct ioat_pq_descriptor *pq;
  565. struct ioat_pq_ext_descriptor *pq_ex = NULL;
  566. struct ioat_dma_descriptor *hw;
  567. u32 offset = 0;
  568. int num_descs;
  569. int with_ext;
  570. int i, s;
  571. u16 idx;
  572. u8 op = result ? IOAT_OP_PQ_VAL : IOAT_OP_PQ;
  573. dev_dbg(to_dev(chan), "%s\n", __func__);
  574. /* the engine requires at least two sources (we provide
  575. * at least 1 implied source in the DMA_PREP_CONTINUE case)
  576. */
  577. BUG_ON(src_cnt + dmaf_continue(flags) < 2);
  578. num_descs = ioat2_xferlen_to_descs(ioat, len);
  579. /* we need 2x the number of descriptors to cover greater than 3
  580. * sources (we need 1 extra source in the q-only continuation
  581. * case and 3 extra sources in the p+q continuation case.
  582. */
  583. if (src_cnt + dmaf_p_disabled_continue(flags) > 3 ||
  584. (dmaf_continue(flags) && !dmaf_p_disabled_continue(flags))) {
  585. with_ext = 1;
  586. num_descs *= 2;
  587. } else
  588. with_ext = 0;
  589. /* completion writes from the raid engine may pass completion
  590. * writes from the legacy engine, so we need one extra null
  591. * (legacy) descriptor to ensure all completion writes arrive in
  592. * order.
  593. */
  594. if (likely(num_descs) &&
  595. ioat2_alloc_and_lock(&idx, ioat, num_descs+1) == 0)
  596. /* pass */;
  597. else
  598. return NULL;
  599. i = 0;
  600. do {
  601. struct ioat_raw_descriptor *descs[2];
  602. size_t xfer_size = min_t(size_t, len, 1 << ioat->xfercap_log);
  603. desc = ioat2_get_ring_ent(ioat, idx + i);
  604. pq = desc->pq;
  605. /* save a branch by unconditionally retrieving the
  606. * extended descriptor pq_set_src() knows to not write
  607. * to it in the single descriptor case
  608. */
  609. ext = ioat2_get_ring_ent(ioat, idx + i + with_ext);
  610. pq_ex = ext->pq_ex;
  611. descs[0] = (struct ioat_raw_descriptor *) pq;
  612. descs[1] = (struct ioat_raw_descriptor *) pq_ex;
  613. for (s = 0; s < src_cnt; s++)
  614. pq_set_src(descs, src[s], offset, scf[s], s);
  615. /* see the comment for dma_maxpq in include/linux/dmaengine.h */
  616. if (dmaf_p_disabled_continue(flags))
  617. pq_set_src(descs, dst[1], offset, 1, s++);
  618. else if (dmaf_continue(flags)) {
  619. pq_set_src(descs, dst[0], offset, 0, s++);
  620. pq_set_src(descs, dst[1], offset, 1, s++);
  621. pq_set_src(descs, dst[1], offset, 0, s++);
  622. }
  623. pq->size = xfer_size;
  624. pq->p_addr = dst[0] + offset;
  625. pq->q_addr = dst[1] + offset;
  626. pq->ctl = 0;
  627. pq->ctl_f.op = op;
  628. pq->ctl_f.src_cnt = src_cnt_to_hw(s);
  629. pq->ctl_f.p_disable = !!(flags & DMA_PREP_PQ_DISABLE_P);
  630. pq->ctl_f.q_disable = !!(flags & DMA_PREP_PQ_DISABLE_Q);
  631. len -= xfer_size;
  632. offset += xfer_size;
  633. } while ((i += 1 + with_ext) < num_descs);
  634. /* last pq descriptor carries the unmap parameters and fence bit */
  635. desc->txd.flags = flags;
  636. desc->len = total_len;
  637. if (result)
  638. desc->result = result;
  639. pq->ctl_f.fence = !!(flags & DMA_PREP_FENCE);
  640. dump_pq_desc_dbg(ioat, desc, ext);
  641. /* completion descriptor carries interrupt bit */
  642. compl_desc = ioat2_get_ring_ent(ioat, idx + i);
  643. compl_desc->txd.flags = flags & DMA_PREP_INTERRUPT;
  644. hw = compl_desc->hw;
  645. hw->ctl = 0;
  646. hw->ctl_f.null = 1;
  647. hw->ctl_f.int_en = !!(flags & DMA_PREP_INTERRUPT);
  648. hw->ctl_f.compl_write = 1;
  649. hw->size = NULL_DESC_BUFFER_SIZE;
  650. dump_desc_dbg(ioat, compl_desc);
  651. /* we leave the channel locked to ensure in order submission */
  652. return &compl_desc->txd;
  653. }
  654. static struct dma_async_tx_descriptor *
  655. ioat3_prep_pq(struct dma_chan *chan, dma_addr_t *dst, dma_addr_t *src,
  656. unsigned int src_cnt, const unsigned char *scf, size_t len,
  657. unsigned long flags)
  658. {
  659. /* specify valid address for disabled result */
  660. if (flags & DMA_PREP_PQ_DISABLE_P)
  661. dst[0] = dst[1];
  662. if (flags & DMA_PREP_PQ_DISABLE_Q)
  663. dst[1] = dst[0];
  664. /* handle the single source multiply case from the raid6
  665. * recovery path
  666. */
  667. if ((flags & DMA_PREP_PQ_DISABLE_P) && src_cnt == 1) {
  668. dma_addr_t single_source[2];
  669. unsigned char single_source_coef[2];
  670. BUG_ON(flags & DMA_PREP_PQ_DISABLE_Q);
  671. single_source[0] = src[0];
  672. single_source[1] = src[0];
  673. single_source_coef[0] = scf[0];
  674. single_source_coef[1] = 0;
  675. return __ioat3_prep_pq_lock(chan, NULL, dst, single_source, 2,
  676. single_source_coef, len, flags);
  677. } else
  678. return __ioat3_prep_pq_lock(chan, NULL, dst, src, src_cnt, scf,
  679. len, flags);
  680. }
  681. struct dma_async_tx_descriptor *
  682. ioat3_prep_pq_val(struct dma_chan *chan, dma_addr_t *pq, dma_addr_t *src,
  683. unsigned int src_cnt, const unsigned char *scf, size_t len,
  684. enum sum_check_flags *pqres, unsigned long flags)
  685. {
  686. /* specify valid address for disabled result */
  687. if (flags & DMA_PREP_PQ_DISABLE_P)
  688. pq[0] = pq[1];
  689. if (flags & DMA_PREP_PQ_DISABLE_Q)
  690. pq[1] = pq[0];
  691. /* the cleanup routine only sets bits on validate failure, it
  692. * does not clear bits on validate success... so clear it here
  693. */
  694. *pqres = 0;
  695. return __ioat3_prep_pq_lock(chan, pqres, pq, src, src_cnt, scf, len,
  696. flags);
  697. }
  698. static struct dma_async_tx_descriptor *
  699. ioat3_prep_pqxor(struct dma_chan *chan, dma_addr_t dst, dma_addr_t *src,
  700. unsigned int src_cnt, size_t len, unsigned long flags)
  701. {
  702. unsigned char scf[src_cnt];
  703. dma_addr_t pq[2];
  704. memset(scf, 0, src_cnt);
  705. pq[0] = dst;
  706. flags |= DMA_PREP_PQ_DISABLE_Q;
  707. pq[1] = dst; /* specify valid address for disabled result */
  708. return __ioat3_prep_pq_lock(chan, NULL, pq, src, src_cnt, scf, len,
  709. flags);
  710. }
  711. struct dma_async_tx_descriptor *
  712. ioat3_prep_pqxor_val(struct dma_chan *chan, dma_addr_t *src,
  713. unsigned int src_cnt, size_t len,
  714. enum sum_check_flags *result, unsigned long flags)
  715. {
  716. unsigned char scf[src_cnt];
  717. dma_addr_t pq[2];
  718. /* the cleanup routine only sets bits on validate failure, it
  719. * does not clear bits on validate success... so clear it here
  720. */
  721. *result = 0;
  722. memset(scf, 0, src_cnt);
  723. pq[0] = src[0];
  724. flags |= DMA_PREP_PQ_DISABLE_Q;
  725. pq[1] = pq[0]; /* specify valid address for disabled result */
  726. return __ioat3_prep_pq_lock(chan, result, pq, &src[1], src_cnt - 1, scf,
  727. len, flags);
  728. }
  729. static struct dma_async_tx_descriptor *
  730. ioat3_prep_interrupt_lock(struct dma_chan *c, unsigned long flags)
  731. {
  732. struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
  733. struct ioat_ring_ent *desc;
  734. struct ioat_dma_descriptor *hw;
  735. u16 idx;
  736. if (ioat2_alloc_and_lock(&idx, ioat, 1) == 0)
  737. desc = ioat2_get_ring_ent(ioat, idx);
  738. else
  739. return NULL;
  740. hw = desc->hw;
  741. hw->ctl = 0;
  742. hw->ctl_f.null = 1;
  743. hw->ctl_f.int_en = 1;
  744. hw->ctl_f.fence = !!(flags & DMA_PREP_FENCE);
  745. hw->ctl_f.compl_write = 1;
  746. hw->size = NULL_DESC_BUFFER_SIZE;
  747. hw->src_addr = 0;
  748. hw->dst_addr = 0;
  749. desc->txd.flags = flags;
  750. desc->len = 1;
  751. dump_desc_dbg(ioat, desc);
  752. /* we leave the channel locked to ensure in order submission */
  753. return &desc->txd;
  754. }
  755. static void __devinit ioat3_dma_test_callback(void *dma_async_param)
  756. {
  757. struct completion *cmp = dma_async_param;
  758. complete(cmp);
  759. }
  760. #define IOAT_NUM_SRC_TEST 6 /* must be <= 8 */
  761. static int __devinit ioat_xor_val_self_test(struct ioatdma_device *device)
  762. {
  763. int i, src_idx;
  764. struct page *dest;
  765. struct page *xor_srcs[IOAT_NUM_SRC_TEST];
  766. struct page *xor_val_srcs[IOAT_NUM_SRC_TEST + 1];
  767. dma_addr_t dma_srcs[IOAT_NUM_SRC_TEST + 1];
  768. dma_addr_t dma_addr, dest_dma;
  769. struct dma_async_tx_descriptor *tx;
  770. struct dma_chan *dma_chan;
  771. dma_cookie_t cookie;
  772. u8 cmp_byte = 0;
  773. u32 cmp_word;
  774. u32 xor_val_result;
  775. int err = 0;
  776. struct completion cmp;
  777. unsigned long tmo;
  778. struct device *dev = &device->pdev->dev;
  779. struct dma_device *dma = &device->common;
  780. dev_dbg(dev, "%s\n", __func__);
  781. if (!dma_has_cap(DMA_XOR, dma->cap_mask))
  782. return 0;
  783. for (src_idx = 0; src_idx < IOAT_NUM_SRC_TEST; src_idx++) {
  784. xor_srcs[src_idx] = alloc_page(GFP_KERNEL);
  785. if (!xor_srcs[src_idx]) {
  786. while (src_idx--)
  787. __free_page(xor_srcs[src_idx]);
  788. return -ENOMEM;
  789. }
  790. }
  791. dest = alloc_page(GFP_KERNEL);
  792. if (!dest) {
  793. while (src_idx--)
  794. __free_page(xor_srcs[src_idx]);
  795. return -ENOMEM;
  796. }
  797. /* Fill in src buffers */
  798. for (src_idx = 0; src_idx < IOAT_NUM_SRC_TEST; src_idx++) {
  799. u8 *ptr = page_address(xor_srcs[src_idx]);
  800. for (i = 0; i < PAGE_SIZE; i++)
  801. ptr[i] = (1 << src_idx);
  802. }
  803. for (src_idx = 0; src_idx < IOAT_NUM_SRC_TEST; src_idx++)
  804. cmp_byte ^= (u8) (1 << src_idx);
  805. cmp_word = (cmp_byte << 24) | (cmp_byte << 16) |
  806. (cmp_byte << 8) | cmp_byte;
  807. memset(page_address(dest), 0, PAGE_SIZE);
  808. dma_chan = container_of(dma->channels.next, struct dma_chan,
  809. device_node);
  810. if (dma->device_alloc_chan_resources(dma_chan) < 1) {
  811. err = -ENODEV;
  812. goto out;
  813. }
  814. /* test xor */
  815. dest_dma = dma_map_page(dev, dest, 0, PAGE_SIZE, DMA_FROM_DEVICE);
  816. for (i = 0; i < IOAT_NUM_SRC_TEST; i++)
  817. dma_srcs[i] = dma_map_page(dev, xor_srcs[i], 0, PAGE_SIZE,
  818. DMA_TO_DEVICE);
  819. tx = dma->device_prep_dma_xor(dma_chan, dest_dma, dma_srcs,
  820. IOAT_NUM_SRC_TEST, PAGE_SIZE,
  821. DMA_PREP_INTERRUPT);
  822. if (!tx) {
  823. dev_err(dev, "Self-test xor prep failed\n");
  824. err = -ENODEV;
  825. goto free_resources;
  826. }
  827. async_tx_ack(tx);
  828. init_completion(&cmp);
  829. tx->callback = ioat3_dma_test_callback;
  830. tx->callback_param = &cmp;
  831. cookie = tx->tx_submit(tx);
  832. if (cookie < 0) {
  833. dev_err(dev, "Self-test xor setup failed\n");
  834. err = -ENODEV;
  835. goto free_resources;
  836. }
  837. dma->device_issue_pending(dma_chan);
  838. tmo = wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000));
  839. if (dma->device_is_tx_complete(dma_chan, cookie, NULL, NULL) != DMA_SUCCESS) {
  840. dev_err(dev, "Self-test xor timed out\n");
  841. err = -ENODEV;
  842. goto free_resources;
  843. }
  844. dma_sync_single_for_cpu(dev, dest_dma, PAGE_SIZE, DMA_FROM_DEVICE);
  845. for (i = 0; i < (PAGE_SIZE / sizeof(u32)); i++) {
  846. u32 *ptr = page_address(dest);
  847. if (ptr[i] != cmp_word) {
  848. dev_err(dev, "Self-test xor failed compare\n");
  849. err = -ENODEV;
  850. goto free_resources;
  851. }
  852. }
  853. dma_sync_single_for_device(dev, dest_dma, PAGE_SIZE, DMA_TO_DEVICE);
  854. /* skip validate if the capability is not present */
  855. if (!dma_has_cap(DMA_XOR_VAL, dma_chan->device->cap_mask))
  856. goto free_resources;
  857. /* validate the sources with the destintation page */
  858. for (i = 0; i < IOAT_NUM_SRC_TEST; i++)
  859. xor_val_srcs[i] = xor_srcs[i];
  860. xor_val_srcs[i] = dest;
  861. xor_val_result = 1;
  862. for (i = 0; i < IOAT_NUM_SRC_TEST + 1; i++)
  863. dma_srcs[i] = dma_map_page(dev, xor_val_srcs[i], 0, PAGE_SIZE,
  864. DMA_TO_DEVICE);
  865. tx = dma->device_prep_dma_xor_val(dma_chan, dma_srcs,
  866. IOAT_NUM_SRC_TEST + 1, PAGE_SIZE,
  867. &xor_val_result, DMA_PREP_INTERRUPT);
  868. if (!tx) {
  869. dev_err(dev, "Self-test zero prep failed\n");
  870. err = -ENODEV;
  871. goto free_resources;
  872. }
  873. async_tx_ack(tx);
  874. init_completion(&cmp);
  875. tx->callback = ioat3_dma_test_callback;
  876. tx->callback_param = &cmp;
  877. cookie = tx->tx_submit(tx);
  878. if (cookie < 0) {
  879. dev_err(dev, "Self-test zero setup failed\n");
  880. err = -ENODEV;
  881. goto free_resources;
  882. }
  883. dma->device_issue_pending(dma_chan);
  884. tmo = wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000));
  885. if (dma->device_is_tx_complete(dma_chan, cookie, NULL, NULL) != DMA_SUCCESS) {
  886. dev_err(dev, "Self-test validate timed out\n");
  887. err = -ENODEV;
  888. goto free_resources;
  889. }
  890. if (xor_val_result != 0) {
  891. dev_err(dev, "Self-test validate failed compare\n");
  892. err = -ENODEV;
  893. goto free_resources;
  894. }
  895. /* skip memset if the capability is not present */
  896. if (!dma_has_cap(DMA_MEMSET, dma_chan->device->cap_mask))
  897. goto free_resources;
  898. /* test memset */
  899. dma_addr = dma_map_page(dev, dest, 0,
  900. PAGE_SIZE, DMA_FROM_DEVICE);
  901. tx = dma->device_prep_dma_memset(dma_chan, dma_addr, 0, PAGE_SIZE,
  902. DMA_PREP_INTERRUPT);
  903. if (!tx) {
  904. dev_err(dev, "Self-test memset prep failed\n");
  905. err = -ENODEV;
  906. goto free_resources;
  907. }
  908. async_tx_ack(tx);
  909. init_completion(&cmp);
  910. tx->callback = ioat3_dma_test_callback;
  911. tx->callback_param = &cmp;
  912. cookie = tx->tx_submit(tx);
  913. if (cookie < 0) {
  914. dev_err(dev, "Self-test memset setup failed\n");
  915. err = -ENODEV;
  916. goto free_resources;
  917. }
  918. dma->device_issue_pending(dma_chan);
  919. tmo = wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000));
  920. if (dma->device_is_tx_complete(dma_chan, cookie, NULL, NULL) != DMA_SUCCESS) {
  921. dev_err(dev, "Self-test memset timed out\n");
  922. err = -ENODEV;
  923. goto free_resources;
  924. }
  925. for (i = 0; i < PAGE_SIZE/sizeof(u32); i++) {
  926. u32 *ptr = page_address(dest);
  927. if (ptr[i]) {
  928. dev_err(dev, "Self-test memset failed compare\n");
  929. err = -ENODEV;
  930. goto free_resources;
  931. }
  932. }
  933. /* test for non-zero parity sum */
  934. xor_val_result = 0;
  935. for (i = 0; i < IOAT_NUM_SRC_TEST + 1; i++)
  936. dma_srcs[i] = dma_map_page(dev, xor_val_srcs[i], 0, PAGE_SIZE,
  937. DMA_TO_DEVICE);
  938. tx = dma->device_prep_dma_xor_val(dma_chan, dma_srcs,
  939. IOAT_NUM_SRC_TEST + 1, PAGE_SIZE,
  940. &xor_val_result, DMA_PREP_INTERRUPT);
  941. if (!tx) {
  942. dev_err(dev, "Self-test 2nd zero prep failed\n");
  943. err = -ENODEV;
  944. goto free_resources;
  945. }
  946. async_tx_ack(tx);
  947. init_completion(&cmp);
  948. tx->callback = ioat3_dma_test_callback;
  949. tx->callback_param = &cmp;
  950. cookie = tx->tx_submit(tx);
  951. if (cookie < 0) {
  952. dev_err(dev, "Self-test 2nd zero setup failed\n");
  953. err = -ENODEV;
  954. goto free_resources;
  955. }
  956. dma->device_issue_pending(dma_chan);
  957. tmo = wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000));
  958. if (dma->device_is_tx_complete(dma_chan, cookie, NULL, NULL) != DMA_SUCCESS) {
  959. dev_err(dev, "Self-test 2nd validate timed out\n");
  960. err = -ENODEV;
  961. goto free_resources;
  962. }
  963. if (xor_val_result != SUM_CHECK_P_RESULT) {
  964. dev_err(dev, "Self-test validate failed compare\n");
  965. err = -ENODEV;
  966. goto free_resources;
  967. }
  968. free_resources:
  969. dma->device_free_chan_resources(dma_chan);
  970. out:
  971. src_idx = IOAT_NUM_SRC_TEST;
  972. while (src_idx--)
  973. __free_page(xor_srcs[src_idx]);
  974. __free_page(dest);
  975. return err;
  976. }
  977. static int __devinit ioat3_dma_self_test(struct ioatdma_device *device)
  978. {
  979. int rc = ioat_dma_self_test(device);
  980. if (rc)
  981. return rc;
  982. rc = ioat_xor_val_self_test(device);
  983. if (rc)
  984. return rc;
  985. return 0;
  986. }
  987. static int ioat3_reset_hw(struct ioat_chan_common *chan)
  988. {
  989. /* throw away whatever the channel was doing and get it
  990. * initialized, with ioat3 specific workarounds
  991. */
  992. struct ioatdma_device *device = chan->device;
  993. struct pci_dev *pdev = device->pdev;
  994. u32 chanerr;
  995. u16 dev_id;
  996. int err;
  997. ioat2_quiesce(chan, msecs_to_jiffies(100));
  998. chanerr = readl(chan->reg_base + IOAT_CHANERR_OFFSET);
  999. writel(chanerr, chan->reg_base + IOAT_CHANERR_OFFSET);
  1000. /* -= IOAT ver.3 workarounds =- */
  1001. /* Write CHANERRMSK_INT with 3E07h to mask out the errors
  1002. * that can cause stability issues for IOAT ver.3, and clear any
  1003. * pending errors
  1004. */
  1005. pci_write_config_dword(pdev, IOAT_PCI_CHANERRMASK_INT_OFFSET, 0x3e07);
  1006. err = pci_read_config_dword(pdev, IOAT_PCI_CHANERR_INT_OFFSET, &chanerr);
  1007. if (err) {
  1008. dev_err(&pdev->dev, "channel error register unreachable\n");
  1009. return err;
  1010. }
  1011. pci_write_config_dword(pdev, IOAT_PCI_CHANERR_INT_OFFSET, chanerr);
  1012. /* Clear DMAUNCERRSTS Cfg-Reg Parity Error status bit
  1013. * (workaround for spurious config parity error after restart)
  1014. */
  1015. pci_read_config_word(pdev, IOAT_PCI_DEVICE_ID_OFFSET, &dev_id);
  1016. if (dev_id == PCI_DEVICE_ID_INTEL_IOAT_TBG0)
  1017. pci_write_config_dword(pdev, IOAT_PCI_DMAUNCERRSTS_OFFSET, 0x10);
  1018. return ioat2_reset_sync(chan, msecs_to_jiffies(200));
  1019. }
  1020. int __devinit ioat3_dma_probe(struct ioatdma_device *device, int dca)
  1021. {
  1022. struct pci_dev *pdev = device->pdev;
  1023. int dca_en = system_has_dca_enabled(pdev);
  1024. struct dma_device *dma;
  1025. struct dma_chan *c;
  1026. struct ioat_chan_common *chan;
  1027. bool is_raid_device = false;
  1028. int err;
  1029. u32 cap;
  1030. device->enumerate_channels = ioat2_enumerate_channels;
  1031. device->reset_hw = ioat3_reset_hw;
  1032. device->self_test = ioat3_dma_self_test;
  1033. dma = &device->common;
  1034. dma->device_prep_dma_memcpy = ioat2_dma_prep_memcpy_lock;
  1035. dma->device_issue_pending = ioat2_issue_pending;
  1036. dma->device_alloc_chan_resources = ioat2_alloc_chan_resources;
  1037. dma->device_free_chan_resources = ioat2_free_chan_resources;
  1038. dma_cap_set(DMA_INTERRUPT, dma->cap_mask);
  1039. dma->device_prep_dma_interrupt = ioat3_prep_interrupt_lock;
  1040. cap = readl(device->reg_base + IOAT_DMA_CAP_OFFSET);
  1041. /* dca is incompatible with raid operations */
  1042. if (dca_en && (cap & (IOAT_CAP_XOR|IOAT_CAP_PQ)))
  1043. cap &= ~(IOAT_CAP_XOR|IOAT_CAP_PQ);
  1044. if (cap & IOAT_CAP_XOR) {
  1045. is_raid_device = true;
  1046. dma->max_xor = 8;
  1047. dma->xor_align = 2;
  1048. dma_cap_set(DMA_XOR, dma->cap_mask);
  1049. dma->device_prep_dma_xor = ioat3_prep_xor;
  1050. dma_cap_set(DMA_XOR_VAL, dma->cap_mask);
  1051. dma->device_prep_dma_xor_val = ioat3_prep_xor_val;
  1052. }
  1053. if (cap & IOAT_CAP_PQ) {
  1054. is_raid_device = true;
  1055. dma_set_maxpq(dma, 8, 0);
  1056. dma->pq_align = 2;
  1057. dma_cap_set(DMA_PQ, dma->cap_mask);
  1058. dma->device_prep_dma_pq = ioat3_prep_pq;
  1059. dma_cap_set(DMA_PQ_VAL, dma->cap_mask);
  1060. dma->device_prep_dma_pq_val = ioat3_prep_pq_val;
  1061. if (!(cap & IOAT_CAP_XOR)) {
  1062. dma->max_xor = 8;
  1063. dma->xor_align = 2;
  1064. dma_cap_set(DMA_XOR, dma->cap_mask);
  1065. dma->device_prep_dma_xor = ioat3_prep_pqxor;
  1066. dma_cap_set(DMA_XOR_VAL, dma->cap_mask);
  1067. dma->device_prep_dma_xor_val = ioat3_prep_pqxor_val;
  1068. }
  1069. }
  1070. if (is_raid_device && (cap & IOAT_CAP_FILL_BLOCK)) {
  1071. dma_cap_set(DMA_MEMSET, dma->cap_mask);
  1072. dma->device_prep_dma_memset = ioat3_prep_memset_lock;
  1073. }
  1074. if (is_raid_device) {
  1075. dma->device_is_tx_complete = ioat3_is_complete;
  1076. device->cleanup_fn = ioat3_cleanup_event;
  1077. device->timer_fn = ioat3_timer_event;
  1078. } else {
  1079. dma->device_is_tx_complete = ioat_is_dma_complete;
  1080. device->cleanup_fn = ioat2_cleanup_event;
  1081. device->timer_fn = ioat2_timer_event;
  1082. }
  1083. #ifdef CONFIG_ASYNC_TX_DISABLE_PQ_VAL_DMA
  1084. dma_cap_clear(DMA_PQ_VAL, dma->cap_mask);
  1085. dma->device_prep_dma_pq_val = NULL;
  1086. #endif
  1087. #ifdef CONFIG_ASYNC_TX_DISABLE_XOR_VAL_DMA
  1088. dma_cap_clear(DMA_XOR_VAL, dma->cap_mask);
  1089. dma->device_prep_dma_xor_val = NULL;
  1090. #endif
  1091. err = ioat_probe(device);
  1092. if (err)
  1093. return err;
  1094. ioat_set_tcp_copy_break(262144);
  1095. list_for_each_entry(c, &dma->channels, device_node) {
  1096. chan = to_chan_common(c);
  1097. writel(IOAT_DMA_DCA_ANY_CPU,
  1098. chan->reg_base + IOAT_DCACTRL_OFFSET);
  1099. }
  1100. err = ioat_register(device);
  1101. if (err)
  1102. return err;
  1103. ioat_kobject_add(device, &ioat2_ktype);
  1104. if (dca)
  1105. device->dca = ioat3_dca_init(pdev, device->reg_base);
  1106. return 0;
  1107. }