dma_v3.c 36 KB

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