dma_v3.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223
  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(!seen_current); /* no active descs have written a completion? */
  260. chan->last_completion = phys_complete;
  261. if (ioat->head == ioat->tail) {
  262. dev_dbg(to_dev(chan), "%s: cancel completion timeout\n",
  263. __func__);
  264. clear_bit(IOAT_COMPLETION_PENDING, &chan->state);
  265. mod_timer(&chan->timer, jiffies + IDLE_TIMEOUT);
  266. }
  267. }
  268. static void ioat3_cleanup(struct ioat2_dma_chan *ioat)
  269. {
  270. struct ioat_chan_common *chan = &ioat->base;
  271. unsigned long phys_complete;
  272. prefetch(chan->completion);
  273. if (!spin_trylock_bh(&chan->cleanup_lock))
  274. return;
  275. if (!ioat_cleanup_preamble(chan, &phys_complete)) {
  276. spin_unlock_bh(&chan->cleanup_lock);
  277. return;
  278. }
  279. if (!spin_trylock_bh(&ioat->ring_lock)) {
  280. spin_unlock_bh(&chan->cleanup_lock);
  281. return;
  282. }
  283. __cleanup(ioat, phys_complete);
  284. spin_unlock_bh(&ioat->ring_lock);
  285. spin_unlock_bh(&chan->cleanup_lock);
  286. }
  287. static void ioat3_cleanup_tasklet(unsigned long data)
  288. {
  289. struct ioat2_dma_chan *ioat = (void *) data;
  290. ioat3_cleanup(ioat);
  291. writew(IOAT_CHANCTRL_RUN | IOAT3_CHANCTRL_COMPL_DCA_EN,
  292. ioat->base.reg_base + IOAT_CHANCTRL_OFFSET);
  293. }
  294. static void ioat3_restart_channel(struct ioat2_dma_chan *ioat)
  295. {
  296. struct ioat_chan_common *chan = &ioat->base;
  297. unsigned long phys_complete;
  298. u32 status;
  299. status = ioat_chansts(chan);
  300. if (is_ioat_active(status) || is_ioat_idle(status))
  301. ioat_suspend(chan);
  302. while (is_ioat_active(status) || is_ioat_idle(status)) {
  303. status = ioat_chansts(chan);
  304. cpu_relax();
  305. }
  306. if (ioat_cleanup_preamble(chan, &phys_complete))
  307. __cleanup(ioat, phys_complete);
  308. __ioat2_restart_chan(ioat);
  309. }
  310. static void ioat3_timer_event(unsigned long data)
  311. {
  312. struct ioat2_dma_chan *ioat = (void *) data;
  313. struct ioat_chan_common *chan = &ioat->base;
  314. spin_lock_bh(&chan->cleanup_lock);
  315. if (test_bit(IOAT_COMPLETION_PENDING, &chan->state)) {
  316. unsigned long phys_complete;
  317. u64 status;
  318. spin_lock_bh(&ioat->ring_lock);
  319. status = ioat_chansts(chan);
  320. /* when halted due to errors check for channel
  321. * programming errors before advancing the completion state
  322. */
  323. if (is_ioat_halted(status)) {
  324. u32 chanerr;
  325. chanerr = readl(chan->reg_base + IOAT_CHANERR_OFFSET);
  326. BUG_ON(is_ioat_bug(chanerr));
  327. }
  328. /* if we haven't made progress and we have already
  329. * acknowledged a pending completion once, then be more
  330. * forceful with a restart
  331. */
  332. if (ioat_cleanup_preamble(chan, &phys_complete))
  333. __cleanup(ioat, phys_complete);
  334. else if (test_bit(IOAT_COMPLETION_ACK, &chan->state))
  335. ioat3_restart_channel(ioat);
  336. else {
  337. set_bit(IOAT_COMPLETION_ACK, &chan->state);
  338. mod_timer(&chan->timer, jiffies + COMPLETION_TIMEOUT);
  339. }
  340. spin_unlock_bh(&ioat->ring_lock);
  341. } else {
  342. u16 active;
  343. /* if the ring is idle, empty, and oversized try to step
  344. * down the size
  345. */
  346. spin_lock_bh(&ioat->ring_lock);
  347. active = ioat2_ring_active(ioat);
  348. if (active == 0 && ioat->alloc_order > ioat_get_alloc_order())
  349. reshape_ring(ioat, ioat->alloc_order-1);
  350. spin_unlock_bh(&ioat->ring_lock);
  351. /* keep shrinking until we get back to our minimum
  352. * default size
  353. */
  354. if (ioat->alloc_order > ioat_get_alloc_order())
  355. mod_timer(&chan->timer, jiffies + IDLE_TIMEOUT);
  356. }
  357. spin_unlock_bh(&chan->cleanup_lock);
  358. }
  359. static enum dma_status
  360. ioat3_is_complete(struct dma_chan *c, dma_cookie_t cookie,
  361. dma_cookie_t *done, dma_cookie_t *used)
  362. {
  363. struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
  364. if (ioat_is_complete(c, cookie, done, used) == DMA_SUCCESS)
  365. return DMA_SUCCESS;
  366. ioat3_cleanup(ioat);
  367. return ioat_is_complete(c, cookie, done, used);
  368. }
  369. static struct dma_async_tx_descriptor *
  370. ioat3_prep_memset_lock(struct dma_chan *c, dma_addr_t dest, int value,
  371. size_t len, unsigned long flags)
  372. {
  373. struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
  374. struct ioat_ring_ent *desc;
  375. size_t total_len = len;
  376. struct ioat_fill_descriptor *fill;
  377. int num_descs;
  378. u64 src_data = (0x0101010101010101ULL) * (value & 0xff);
  379. u16 idx;
  380. int i;
  381. num_descs = ioat2_xferlen_to_descs(ioat, len);
  382. if (likely(num_descs) &&
  383. ioat2_alloc_and_lock(&idx, ioat, num_descs) == 0)
  384. /* pass */;
  385. else
  386. return NULL;
  387. i = 0;
  388. do {
  389. size_t xfer_size = min_t(size_t, len, 1 << ioat->xfercap_log);
  390. desc = ioat2_get_ring_ent(ioat, idx + i);
  391. fill = desc->fill;
  392. fill->size = xfer_size;
  393. fill->src_data = src_data;
  394. fill->dst_addr = dest;
  395. fill->ctl = 0;
  396. fill->ctl_f.op = IOAT_OP_FILL;
  397. len -= xfer_size;
  398. dest += xfer_size;
  399. dump_desc_dbg(ioat, desc);
  400. } while (++i < num_descs);
  401. desc->txd.flags = flags;
  402. desc->len = total_len;
  403. fill->ctl_f.int_en = !!(flags & DMA_PREP_INTERRUPT);
  404. fill->ctl_f.fence = !!(flags & DMA_PREP_FENCE);
  405. fill->ctl_f.compl_write = 1;
  406. dump_desc_dbg(ioat, desc);
  407. /* we leave the channel locked to ensure in order submission */
  408. return &desc->txd;
  409. }
  410. static struct dma_async_tx_descriptor *
  411. __ioat3_prep_xor_lock(struct dma_chan *c, enum sum_check_flags *result,
  412. dma_addr_t dest, dma_addr_t *src, unsigned int src_cnt,
  413. size_t len, unsigned long flags)
  414. {
  415. struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
  416. struct ioat_ring_ent *compl_desc;
  417. struct ioat_ring_ent *desc;
  418. struct ioat_ring_ent *ext;
  419. size_t total_len = len;
  420. struct ioat_xor_descriptor *xor;
  421. struct ioat_xor_ext_descriptor *xor_ex = NULL;
  422. struct ioat_dma_descriptor *hw;
  423. u32 offset = 0;
  424. int num_descs;
  425. int with_ext;
  426. int i;
  427. u16 idx;
  428. u8 op = result ? IOAT_OP_XOR_VAL : IOAT_OP_XOR;
  429. BUG_ON(src_cnt < 2);
  430. num_descs = ioat2_xferlen_to_descs(ioat, len);
  431. /* we need 2x the number of descriptors to cover greater than 5
  432. * sources
  433. */
  434. if (src_cnt > 5) {
  435. with_ext = 1;
  436. num_descs *= 2;
  437. } else
  438. with_ext = 0;
  439. /* completion writes from the raid engine may pass completion
  440. * writes from the legacy engine, so we need one extra null
  441. * (legacy) descriptor to ensure all completion writes arrive in
  442. * order.
  443. */
  444. if (likely(num_descs) &&
  445. ioat2_alloc_and_lock(&idx, ioat, num_descs+1) == 0)
  446. /* pass */;
  447. else
  448. return NULL;
  449. i = 0;
  450. do {
  451. struct ioat_raw_descriptor *descs[2];
  452. size_t xfer_size = min_t(size_t, len, 1 << ioat->xfercap_log);
  453. int s;
  454. desc = ioat2_get_ring_ent(ioat, idx + i);
  455. xor = desc->xor;
  456. /* save a branch by unconditionally retrieving the
  457. * extended descriptor xor_set_src() knows to not write
  458. * to it in the single descriptor case
  459. */
  460. ext = ioat2_get_ring_ent(ioat, idx + i + 1);
  461. xor_ex = ext->xor_ex;
  462. descs[0] = (struct ioat_raw_descriptor *) xor;
  463. descs[1] = (struct ioat_raw_descriptor *) xor_ex;
  464. for (s = 0; s < src_cnt; s++)
  465. xor_set_src(descs, src[s], offset, s);
  466. xor->size = xfer_size;
  467. xor->dst_addr = dest + offset;
  468. xor->ctl = 0;
  469. xor->ctl_f.op = op;
  470. xor->ctl_f.src_cnt = src_cnt_to_hw(src_cnt);
  471. len -= xfer_size;
  472. offset += xfer_size;
  473. dump_desc_dbg(ioat, desc);
  474. } while ((i += 1 + with_ext) < num_descs);
  475. /* last xor descriptor carries the unmap parameters and fence bit */
  476. desc->txd.flags = flags;
  477. desc->len = total_len;
  478. if (result)
  479. desc->result = result;
  480. xor->ctl_f.fence = !!(flags & DMA_PREP_FENCE);
  481. /* completion descriptor carries interrupt bit */
  482. compl_desc = ioat2_get_ring_ent(ioat, idx + i);
  483. compl_desc->txd.flags = flags & DMA_PREP_INTERRUPT;
  484. hw = compl_desc->hw;
  485. hw->ctl = 0;
  486. hw->ctl_f.null = 1;
  487. hw->ctl_f.int_en = !!(flags & DMA_PREP_INTERRUPT);
  488. hw->ctl_f.compl_write = 1;
  489. hw->size = NULL_DESC_BUFFER_SIZE;
  490. dump_desc_dbg(ioat, compl_desc);
  491. /* we leave the channel locked to ensure in order submission */
  492. return &desc->txd;
  493. }
  494. static struct dma_async_tx_descriptor *
  495. ioat3_prep_xor(struct dma_chan *chan, dma_addr_t dest, dma_addr_t *src,
  496. unsigned int src_cnt, size_t len, unsigned long flags)
  497. {
  498. return __ioat3_prep_xor_lock(chan, NULL, dest, src, src_cnt, len, flags);
  499. }
  500. struct dma_async_tx_descriptor *
  501. ioat3_prep_xor_val(struct dma_chan *chan, dma_addr_t *src,
  502. unsigned int src_cnt, size_t len,
  503. enum sum_check_flags *result, unsigned long flags)
  504. {
  505. /* the cleanup routine only sets bits on validate failure, it
  506. * does not clear bits on validate success... so clear it here
  507. */
  508. *result = 0;
  509. return __ioat3_prep_xor_lock(chan, result, src[0], &src[1],
  510. src_cnt - 1, len, flags);
  511. }
  512. static void
  513. dump_pq_desc_dbg(struct ioat2_dma_chan *ioat, struct ioat_ring_ent *desc, struct ioat_ring_ent *ext)
  514. {
  515. struct device *dev = to_dev(&ioat->base);
  516. struct ioat_pq_descriptor *pq = desc->pq;
  517. struct ioat_pq_ext_descriptor *pq_ex = ext ? ext->pq_ex : NULL;
  518. struct ioat_raw_descriptor *descs[] = { (void *) pq, (void *) pq_ex };
  519. int src_cnt = src_cnt_to_sw(pq->ctl_f.src_cnt);
  520. int i;
  521. dev_dbg(dev, "desc[%d]: (%#llx->%#llx) flags: %#x"
  522. " sz: %#x ctl: %#x (op: %d int: %d compl: %d pq: '%s%s' src_cnt: %d)\n",
  523. desc_id(desc), (unsigned long long) desc->txd.phys,
  524. (unsigned long long) (pq_ex ? pq_ex->next : pq->next),
  525. desc->txd.flags, pq->size, pq->ctl, pq->ctl_f.op, pq->ctl_f.int_en,
  526. pq->ctl_f.compl_write,
  527. pq->ctl_f.p_disable ? "" : "p", pq->ctl_f.q_disable ? "" : "q",
  528. pq->ctl_f.src_cnt);
  529. for (i = 0; i < src_cnt; i++)
  530. dev_dbg(dev, "\tsrc[%d]: %#llx coef: %#x\n", i,
  531. (unsigned long long) pq_get_src(descs, i), pq->coef[i]);
  532. dev_dbg(dev, "\tP: %#llx\n", pq->p_addr);
  533. dev_dbg(dev, "\tQ: %#llx\n", pq->q_addr);
  534. }
  535. static struct dma_async_tx_descriptor *
  536. __ioat3_prep_pq_lock(struct dma_chan *c, enum sum_check_flags *result,
  537. const dma_addr_t *dst, const dma_addr_t *src,
  538. unsigned int src_cnt, const unsigned char *scf,
  539. size_t len, unsigned long flags)
  540. {
  541. struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
  542. struct ioat_chan_common *chan = &ioat->base;
  543. struct ioat_ring_ent *compl_desc;
  544. struct ioat_ring_ent *desc;
  545. struct ioat_ring_ent *ext;
  546. size_t total_len = len;
  547. struct ioat_pq_descriptor *pq;
  548. struct ioat_pq_ext_descriptor *pq_ex = NULL;
  549. struct ioat_dma_descriptor *hw;
  550. u32 offset = 0;
  551. int num_descs;
  552. int with_ext;
  553. int i, s;
  554. u16 idx;
  555. u8 op = result ? IOAT_OP_PQ_VAL : IOAT_OP_PQ;
  556. dev_dbg(to_dev(chan), "%s\n", __func__);
  557. /* the engine requires at least two sources (we provide
  558. * at least 1 implied source in the DMA_PREP_CONTINUE case)
  559. */
  560. BUG_ON(src_cnt + dmaf_continue(flags) < 2);
  561. num_descs = ioat2_xferlen_to_descs(ioat, len);
  562. /* we need 2x the number of descriptors to cover greater than 3
  563. * sources
  564. */
  565. if (src_cnt > 3 || flags & DMA_PREP_CONTINUE) {
  566. with_ext = 1;
  567. num_descs *= 2;
  568. } else
  569. with_ext = 0;
  570. /* completion writes from the raid engine may pass completion
  571. * writes from the legacy engine, so we need one extra null
  572. * (legacy) descriptor to ensure all completion writes arrive in
  573. * order.
  574. */
  575. if (likely(num_descs) &&
  576. ioat2_alloc_and_lock(&idx, ioat, num_descs+1) == 0)
  577. /* pass */;
  578. else
  579. return NULL;
  580. i = 0;
  581. do {
  582. struct ioat_raw_descriptor *descs[2];
  583. size_t xfer_size = min_t(size_t, len, 1 << ioat->xfercap_log);
  584. desc = ioat2_get_ring_ent(ioat, idx + i);
  585. pq = desc->pq;
  586. /* save a branch by unconditionally retrieving the
  587. * extended descriptor pq_set_src() knows to not write
  588. * to it in the single descriptor case
  589. */
  590. ext = ioat2_get_ring_ent(ioat, idx + i + with_ext);
  591. pq_ex = ext->pq_ex;
  592. descs[0] = (struct ioat_raw_descriptor *) pq;
  593. descs[1] = (struct ioat_raw_descriptor *) pq_ex;
  594. for (s = 0; s < src_cnt; s++)
  595. pq_set_src(descs, src[s], offset, scf[s], s);
  596. /* see the comment for dma_maxpq in include/linux/dmaengine.h */
  597. if (dmaf_p_disabled_continue(flags))
  598. pq_set_src(descs, dst[1], offset, 1, s++);
  599. else if (dmaf_continue(flags)) {
  600. pq_set_src(descs, dst[0], offset, 0, s++);
  601. pq_set_src(descs, dst[1], offset, 1, s++);
  602. pq_set_src(descs, dst[1], offset, 0, s++);
  603. }
  604. pq->size = xfer_size;
  605. pq->p_addr = dst[0] + offset;
  606. pq->q_addr = dst[1] + offset;
  607. pq->ctl = 0;
  608. pq->ctl_f.op = op;
  609. pq->ctl_f.src_cnt = src_cnt_to_hw(s);
  610. pq->ctl_f.p_disable = !!(flags & DMA_PREP_PQ_DISABLE_P);
  611. pq->ctl_f.q_disable = !!(flags & DMA_PREP_PQ_DISABLE_Q);
  612. len -= xfer_size;
  613. offset += xfer_size;
  614. } while ((i += 1 + with_ext) < num_descs);
  615. /* last pq descriptor carries the unmap parameters and fence bit */
  616. desc->txd.flags = flags;
  617. desc->len = total_len;
  618. if (result)
  619. desc->result = result;
  620. pq->ctl_f.fence = !!(flags & DMA_PREP_FENCE);
  621. dump_pq_desc_dbg(ioat, desc, ext);
  622. /* completion descriptor carries interrupt bit */
  623. compl_desc = ioat2_get_ring_ent(ioat, idx + i);
  624. compl_desc->txd.flags = flags & DMA_PREP_INTERRUPT;
  625. hw = compl_desc->hw;
  626. hw->ctl = 0;
  627. hw->ctl_f.null = 1;
  628. hw->ctl_f.int_en = !!(flags & DMA_PREP_INTERRUPT);
  629. hw->ctl_f.compl_write = 1;
  630. hw->size = NULL_DESC_BUFFER_SIZE;
  631. dump_desc_dbg(ioat, compl_desc);
  632. /* we leave the channel locked to ensure in order submission */
  633. return &desc->txd;
  634. }
  635. static struct dma_async_tx_descriptor *
  636. ioat3_prep_pq(struct dma_chan *chan, dma_addr_t *dst, dma_addr_t *src,
  637. unsigned int src_cnt, const unsigned char *scf, size_t len,
  638. unsigned long flags)
  639. {
  640. /* handle the single source multiply case from the raid6
  641. * recovery path
  642. */
  643. if (unlikely((flags & DMA_PREP_PQ_DISABLE_P) && src_cnt == 1)) {
  644. dma_addr_t single_source[2];
  645. unsigned char single_source_coef[2];
  646. BUG_ON(flags & DMA_PREP_PQ_DISABLE_Q);
  647. single_source[0] = src[0];
  648. single_source[1] = src[0];
  649. single_source_coef[0] = scf[0];
  650. single_source_coef[1] = 0;
  651. return __ioat3_prep_pq_lock(chan, NULL, dst, single_source, 2,
  652. single_source_coef, len, flags);
  653. } else
  654. return __ioat3_prep_pq_lock(chan, NULL, dst, src, src_cnt, scf,
  655. len, flags);
  656. }
  657. struct dma_async_tx_descriptor *
  658. ioat3_prep_pq_val(struct dma_chan *chan, dma_addr_t *pq, dma_addr_t *src,
  659. unsigned int src_cnt, const unsigned char *scf, size_t len,
  660. enum sum_check_flags *pqres, unsigned long flags)
  661. {
  662. /* the cleanup routine only sets bits on validate failure, it
  663. * does not clear bits on validate success... so clear it here
  664. */
  665. *pqres = 0;
  666. return __ioat3_prep_pq_lock(chan, pqres, pq, src, src_cnt, scf, len,
  667. flags);
  668. }
  669. static struct dma_async_tx_descriptor *
  670. ioat3_prep_pqxor(struct dma_chan *chan, dma_addr_t dst, dma_addr_t *src,
  671. unsigned int src_cnt, size_t len, unsigned long flags)
  672. {
  673. unsigned char scf[src_cnt];
  674. dma_addr_t pq[2];
  675. memset(scf, 0, src_cnt);
  676. flags |= DMA_PREP_PQ_DISABLE_Q;
  677. pq[0] = dst;
  678. pq[1] = ~0;
  679. return __ioat3_prep_pq_lock(chan, NULL, pq, src, src_cnt, scf, len,
  680. flags);
  681. }
  682. struct dma_async_tx_descriptor *
  683. ioat3_prep_pqxor_val(struct dma_chan *chan, dma_addr_t *src,
  684. unsigned int src_cnt, size_t len,
  685. enum sum_check_flags *result, unsigned long flags)
  686. {
  687. unsigned char scf[src_cnt];
  688. dma_addr_t pq[2];
  689. /* the cleanup routine only sets bits on validate failure, it
  690. * does not clear bits on validate success... so clear it here
  691. */
  692. *result = 0;
  693. memset(scf, 0, src_cnt);
  694. flags |= DMA_PREP_PQ_DISABLE_Q;
  695. pq[0] = src[0];
  696. pq[1] = ~0;
  697. return __ioat3_prep_pq_lock(chan, result, pq, &src[1], src_cnt - 1, scf,
  698. len, flags);
  699. }
  700. static struct dma_async_tx_descriptor *
  701. ioat3_prep_interrupt_lock(struct dma_chan *c, unsigned long flags)
  702. {
  703. struct ioat2_dma_chan *ioat = to_ioat2_chan(c);
  704. struct ioat_ring_ent *desc;
  705. struct ioat_dma_descriptor *hw;
  706. u16 idx;
  707. if (ioat2_alloc_and_lock(&idx, ioat, 1) == 0)
  708. desc = ioat2_get_ring_ent(ioat, idx);
  709. else
  710. return NULL;
  711. hw = desc->hw;
  712. hw->ctl = 0;
  713. hw->ctl_f.null = 1;
  714. hw->ctl_f.int_en = 1;
  715. hw->ctl_f.fence = !!(flags & DMA_PREP_FENCE);
  716. hw->ctl_f.compl_write = 1;
  717. hw->size = NULL_DESC_BUFFER_SIZE;
  718. hw->src_addr = 0;
  719. hw->dst_addr = 0;
  720. desc->txd.flags = flags;
  721. desc->len = 1;
  722. dump_desc_dbg(ioat, desc);
  723. /* we leave the channel locked to ensure in order submission */
  724. return &desc->txd;
  725. }
  726. static void __devinit ioat3_dma_test_callback(void *dma_async_param)
  727. {
  728. struct completion *cmp = dma_async_param;
  729. complete(cmp);
  730. }
  731. #define IOAT_NUM_SRC_TEST 6 /* must be <= 8 */
  732. static int __devinit ioat_xor_val_self_test(struct ioatdma_device *device)
  733. {
  734. int i, src_idx;
  735. struct page *dest;
  736. struct page *xor_srcs[IOAT_NUM_SRC_TEST];
  737. struct page *xor_val_srcs[IOAT_NUM_SRC_TEST + 1];
  738. dma_addr_t dma_srcs[IOAT_NUM_SRC_TEST + 1];
  739. dma_addr_t dma_addr, dest_dma;
  740. struct dma_async_tx_descriptor *tx;
  741. struct dma_chan *dma_chan;
  742. dma_cookie_t cookie;
  743. u8 cmp_byte = 0;
  744. u32 cmp_word;
  745. u32 xor_val_result;
  746. int err = 0;
  747. struct completion cmp;
  748. unsigned long tmo;
  749. struct device *dev = &device->pdev->dev;
  750. struct dma_device *dma = &device->common;
  751. dev_dbg(dev, "%s\n", __func__);
  752. if (!dma_has_cap(DMA_XOR, dma->cap_mask))
  753. return 0;
  754. for (src_idx = 0; src_idx < IOAT_NUM_SRC_TEST; src_idx++) {
  755. xor_srcs[src_idx] = alloc_page(GFP_KERNEL);
  756. if (!xor_srcs[src_idx]) {
  757. while (src_idx--)
  758. __free_page(xor_srcs[src_idx]);
  759. return -ENOMEM;
  760. }
  761. }
  762. dest = alloc_page(GFP_KERNEL);
  763. if (!dest) {
  764. while (src_idx--)
  765. __free_page(xor_srcs[src_idx]);
  766. return -ENOMEM;
  767. }
  768. /* Fill in src buffers */
  769. for (src_idx = 0; src_idx < IOAT_NUM_SRC_TEST; src_idx++) {
  770. u8 *ptr = page_address(xor_srcs[src_idx]);
  771. for (i = 0; i < PAGE_SIZE; i++)
  772. ptr[i] = (1 << src_idx);
  773. }
  774. for (src_idx = 0; src_idx < IOAT_NUM_SRC_TEST; src_idx++)
  775. cmp_byte ^= (u8) (1 << src_idx);
  776. cmp_word = (cmp_byte << 24) | (cmp_byte << 16) |
  777. (cmp_byte << 8) | cmp_byte;
  778. memset(page_address(dest), 0, PAGE_SIZE);
  779. dma_chan = container_of(dma->channels.next, struct dma_chan,
  780. device_node);
  781. if (dma->device_alloc_chan_resources(dma_chan) < 1) {
  782. err = -ENODEV;
  783. goto out;
  784. }
  785. /* test xor */
  786. dest_dma = dma_map_page(dev, dest, 0, PAGE_SIZE, DMA_FROM_DEVICE);
  787. for (i = 0; i < IOAT_NUM_SRC_TEST; i++)
  788. dma_srcs[i] = dma_map_page(dev, xor_srcs[i], 0, PAGE_SIZE,
  789. DMA_TO_DEVICE);
  790. tx = dma->device_prep_dma_xor(dma_chan, dest_dma, dma_srcs,
  791. IOAT_NUM_SRC_TEST, PAGE_SIZE,
  792. DMA_PREP_INTERRUPT);
  793. if (!tx) {
  794. dev_err(dev, "Self-test xor prep failed\n");
  795. err = -ENODEV;
  796. goto free_resources;
  797. }
  798. async_tx_ack(tx);
  799. init_completion(&cmp);
  800. tx->callback = ioat3_dma_test_callback;
  801. tx->callback_param = &cmp;
  802. cookie = tx->tx_submit(tx);
  803. if (cookie < 0) {
  804. dev_err(dev, "Self-test xor setup failed\n");
  805. err = -ENODEV;
  806. goto free_resources;
  807. }
  808. dma->device_issue_pending(dma_chan);
  809. tmo = wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000));
  810. if (dma->device_is_tx_complete(dma_chan, cookie, NULL, NULL) != DMA_SUCCESS) {
  811. dev_err(dev, "Self-test xor timed out\n");
  812. err = -ENODEV;
  813. goto free_resources;
  814. }
  815. dma_sync_single_for_cpu(dev, dest_dma, PAGE_SIZE, DMA_FROM_DEVICE);
  816. for (i = 0; i < (PAGE_SIZE / sizeof(u32)); i++) {
  817. u32 *ptr = page_address(dest);
  818. if (ptr[i] != cmp_word) {
  819. dev_err(dev, "Self-test xor failed compare\n");
  820. err = -ENODEV;
  821. goto free_resources;
  822. }
  823. }
  824. dma_sync_single_for_device(dev, dest_dma, PAGE_SIZE, DMA_TO_DEVICE);
  825. /* skip validate if the capability is not present */
  826. if (!dma_has_cap(DMA_XOR_VAL, dma_chan->device->cap_mask))
  827. goto free_resources;
  828. /* validate the sources with the destintation page */
  829. for (i = 0; i < IOAT_NUM_SRC_TEST; i++)
  830. xor_val_srcs[i] = xor_srcs[i];
  831. xor_val_srcs[i] = dest;
  832. xor_val_result = 1;
  833. for (i = 0; i < IOAT_NUM_SRC_TEST + 1; i++)
  834. dma_srcs[i] = dma_map_page(dev, xor_val_srcs[i], 0, PAGE_SIZE,
  835. DMA_TO_DEVICE);
  836. tx = dma->device_prep_dma_xor_val(dma_chan, dma_srcs,
  837. IOAT_NUM_SRC_TEST + 1, PAGE_SIZE,
  838. &xor_val_result, DMA_PREP_INTERRUPT);
  839. if (!tx) {
  840. dev_err(dev, "Self-test zero prep failed\n");
  841. err = -ENODEV;
  842. goto free_resources;
  843. }
  844. async_tx_ack(tx);
  845. init_completion(&cmp);
  846. tx->callback = ioat3_dma_test_callback;
  847. tx->callback_param = &cmp;
  848. cookie = tx->tx_submit(tx);
  849. if (cookie < 0) {
  850. dev_err(dev, "Self-test zero setup failed\n");
  851. err = -ENODEV;
  852. goto free_resources;
  853. }
  854. dma->device_issue_pending(dma_chan);
  855. tmo = wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000));
  856. if (dma->device_is_tx_complete(dma_chan, cookie, NULL, NULL) != DMA_SUCCESS) {
  857. dev_err(dev, "Self-test validate timed out\n");
  858. err = -ENODEV;
  859. goto free_resources;
  860. }
  861. if (xor_val_result != 0) {
  862. dev_err(dev, "Self-test validate failed compare\n");
  863. err = -ENODEV;
  864. goto free_resources;
  865. }
  866. /* skip memset if the capability is not present */
  867. if (!dma_has_cap(DMA_MEMSET, dma_chan->device->cap_mask))
  868. goto free_resources;
  869. /* test memset */
  870. dma_addr = dma_map_page(dev, dest, 0,
  871. PAGE_SIZE, DMA_FROM_DEVICE);
  872. tx = dma->device_prep_dma_memset(dma_chan, dma_addr, 0, PAGE_SIZE,
  873. DMA_PREP_INTERRUPT);
  874. if (!tx) {
  875. dev_err(dev, "Self-test memset prep failed\n");
  876. err = -ENODEV;
  877. goto free_resources;
  878. }
  879. async_tx_ack(tx);
  880. init_completion(&cmp);
  881. tx->callback = ioat3_dma_test_callback;
  882. tx->callback_param = &cmp;
  883. cookie = tx->tx_submit(tx);
  884. if (cookie < 0) {
  885. dev_err(dev, "Self-test memset setup failed\n");
  886. err = -ENODEV;
  887. goto free_resources;
  888. }
  889. dma->device_issue_pending(dma_chan);
  890. tmo = wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000));
  891. if (dma->device_is_tx_complete(dma_chan, cookie, NULL, NULL) != DMA_SUCCESS) {
  892. dev_err(dev, "Self-test memset timed out\n");
  893. err = -ENODEV;
  894. goto free_resources;
  895. }
  896. for (i = 0; i < PAGE_SIZE/sizeof(u32); i++) {
  897. u32 *ptr = page_address(dest);
  898. if (ptr[i]) {
  899. dev_err(dev, "Self-test memset failed compare\n");
  900. err = -ENODEV;
  901. goto free_resources;
  902. }
  903. }
  904. /* test for non-zero parity sum */
  905. xor_val_result = 0;
  906. for (i = 0; i < IOAT_NUM_SRC_TEST + 1; i++)
  907. dma_srcs[i] = dma_map_page(dev, xor_val_srcs[i], 0, PAGE_SIZE,
  908. DMA_TO_DEVICE);
  909. tx = dma->device_prep_dma_xor_val(dma_chan, dma_srcs,
  910. IOAT_NUM_SRC_TEST + 1, PAGE_SIZE,
  911. &xor_val_result, DMA_PREP_INTERRUPT);
  912. if (!tx) {
  913. dev_err(dev, "Self-test 2nd zero prep failed\n");
  914. err = -ENODEV;
  915. goto free_resources;
  916. }
  917. async_tx_ack(tx);
  918. init_completion(&cmp);
  919. tx->callback = ioat3_dma_test_callback;
  920. tx->callback_param = &cmp;
  921. cookie = tx->tx_submit(tx);
  922. if (cookie < 0) {
  923. dev_err(dev, "Self-test 2nd zero setup failed\n");
  924. err = -ENODEV;
  925. goto free_resources;
  926. }
  927. dma->device_issue_pending(dma_chan);
  928. tmo = wait_for_completion_timeout(&cmp, msecs_to_jiffies(3000));
  929. if (dma->device_is_tx_complete(dma_chan, cookie, NULL, NULL) != DMA_SUCCESS) {
  930. dev_err(dev, "Self-test 2nd validate timed out\n");
  931. err = -ENODEV;
  932. goto free_resources;
  933. }
  934. if (xor_val_result != SUM_CHECK_P_RESULT) {
  935. dev_err(dev, "Self-test validate failed compare\n");
  936. err = -ENODEV;
  937. goto free_resources;
  938. }
  939. free_resources:
  940. dma->device_free_chan_resources(dma_chan);
  941. out:
  942. src_idx = IOAT_NUM_SRC_TEST;
  943. while (src_idx--)
  944. __free_page(xor_srcs[src_idx]);
  945. __free_page(dest);
  946. return err;
  947. }
  948. static int __devinit ioat3_dma_self_test(struct ioatdma_device *device)
  949. {
  950. int rc = ioat_dma_self_test(device);
  951. if (rc)
  952. return rc;
  953. rc = ioat_xor_val_self_test(device);
  954. if (rc)
  955. return rc;
  956. return 0;
  957. }
  958. int __devinit ioat3_dma_probe(struct ioatdma_device *device, int dca)
  959. {
  960. struct pci_dev *pdev = device->pdev;
  961. struct dma_device *dma;
  962. struct dma_chan *c;
  963. struct ioat_chan_common *chan;
  964. bool is_raid_device = false;
  965. int err;
  966. u16 dev_id;
  967. u32 cap;
  968. device->enumerate_channels = ioat2_enumerate_channels;
  969. device->self_test = ioat3_dma_self_test;
  970. dma = &device->common;
  971. dma->device_prep_dma_memcpy = ioat2_dma_prep_memcpy_lock;
  972. dma->device_issue_pending = ioat2_issue_pending;
  973. dma->device_alloc_chan_resources = ioat2_alloc_chan_resources;
  974. dma->device_free_chan_resources = ioat2_free_chan_resources;
  975. dma_cap_set(DMA_INTERRUPT, dma->cap_mask);
  976. dma->device_prep_dma_interrupt = ioat3_prep_interrupt_lock;
  977. cap = readl(device->reg_base + IOAT_DMA_CAP_OFFSET);
  978. if (cap & IOAT_CAP_XOR) {
  979. is_raid_device = true;
  980. dma->max_xor = 8;
  981. dma->xor_align = 2;
  982. dma_cap_set(DMA_XOR, dma->cap_mask);
  983. dma->device_prep_dma_xor = ioat3_prep_xor;
  984. dma_cap_set(DMA_XOR_VAL, dma->cap_mask);
  985. dma->device_prep_dma_xor_val = ioat3_prep_xor_val;
  986. }
  987. if (cap & IOAT_CAP_PQ) {
  988. is_raid_device = true;
  989. dma_set_maxpq(dma, 8, 0);
  990. dma->pq_align = 2;
  991. dma_cap_set(DMA_PQ, dma->cap_mask);
  992. dma->device_prep_dma_pq = ioat3_prep_pq;
  993. dma_cap_set(DMA_PQ_VAL, dma->cap_mask);
  994. dma->device_prep_dma_pq_val = ioat3_prep_pq_val;
  995. if (!(cap & IOAT_CAP_XOR)) {
  996. dma->max_xor = 8;
  997. dma->xor_align = 2;
  998. dma_cap_set(DMA_XOR, dma->cap_mask);
  999. dma->device_prep_dma_xor = ioat3_prep_pqxor;
  1000. dma_cap_set(DMA_XOR_VAL, dma->cap_mask);
  1001. dma->device_prep_dma_xor_val = ioat3_prep_pqxor_val;
  1002. }
  1003. }
  1004. if (is_raid_device && (cap & IOAT_CAP_FILL_BLOCK)) {
  1005. dma_cap_set(DMA_MEMSET, dma->cap_mask);
  1006. dma->device_prep_dma_memset = ioat3_prep_memset_lock;
  1007. }
  1008. if (is_raid_device) {
  1009. dma->device_is_tx_complete = ioat3_is_complete;
  1010. device->cleanup_tasklet = ioat3_cleanup_tasklet;
  1011. device->timer_fn = ioat3_timer_event;
  1012. } else {
  1013. dma->device_is_tx_complete = ioat2_is_complete;
  1014. device->cleanup_tasklet = ioat2_cleanup_tasklet;
  1015. device->timer_fn = ioat2_timer_event;
  1016. }
  1017. /* -= IOAT ver.3 workarounds =- */
  1018. /* Write CHANERRMSK_INT with 3E07h to mask out the errors
  1019. * that can cause stability issues for IOAT ver.3
  1020. */
  1021. pci_write_config_dword(pdev, IOAT_PCI_CHANERRMASK_INT_OFFSET, 0x3e07);
  1022. /* Clear DMAUNCERRSTS Cfg-Reg Parity Error status bit
  1023. * (workaround for spurious config parity error after restart)
  1024. */
  1025. pci_read_config_word(pdev, IOAT_PCI_DEVICE_ID_OFFSET, &dev_id);
  1026. if (dev_id == PCI_DEVICE_ID_INTEL_IOAT_TBG0)
  1027. pci_write_config_dword(pdev, IOAT_PCI_DMAUNCERRSTS_OFFSET, 0x10);
  1028. err = ioat_probe(device);
  1029. if (err)
  1030. return err;
  1031. ioat_set_tcp_copy_break(262144);
  1032. list_for_each_entry(c, &dma->channels, device_node) {
  1033. chan = to_chan_common(c);
  1034. writel(IOAT_DMA_DCA_ANY_CPU,
  1035. chan->reg_base + IOAT_DCACTRL_OFFSET);
  1036. }
  1037. err = ioat_register(device);
  1038. if (err)
  1039. return err;
  1040. ioat_kobject_add(device, &ioat2_ktype);
  1041. if (dca)
  1042. device->dca = ioat3_dca_init(pdev, device->reg_base);
  1043. return 0;
  1044. }