Path: blob/master/crypto/async_tx/async_raid6_recov.c
10817 views
/*1* Asynchronous RAID-6 recovery calculations ASYNC_TX API.2* Copyright(c) 2009 Intel Corporation3*4* based on raid6recov.c:5* Copyright 2002 H. Peter Anvin6*7* This program is free software; you can redistribute it and/or modify it8* under the terms of the GNU General Public License as published by the Free9* Software Foundation; either version 2 of the License, or (at your option)10* any later version.11*12* This program is distributed in the hope that it will be useful, but WITHOUT13* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or14* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for15* more details.16*17* You should have received a copy of the GNU General Public License along with18* this program; if not, write to the Free Software Foundation, Inc., 5119* Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.20*21*/22#include <linux/kernel.h>23#include <linux/interrupt.h>24#include <linux/dma-mapping.h>25#include <linux/raid/pq.h>26#include <linux/async_tx.h>2728static struct dma_async_tx_descriptor *29async_sum_product(struct page *dest, struct page **srcs, unsigned char *coef,30size_t len, struct async_submit_ctl *submit)31{32struct dma_chan *chan = async_tx_find_channel(submit, DMA_PQ,33&dest, 1, srcs, 2, len);34struct dma_device *dma = chan ? chan->device : NULL;35const u8 *amul, *bmul;36u8 ax, bx;37u8 *a, *b, *c;3839if (dma) {40dma_addr_t dma_dest[2];41dma_addr_t dma_src[2];42struct device *dev = dma->dev;43struct dma_async_tx_descriptor *tx;44enum dma_ctrl_flags dma_flags = DMA_PREP_PQ_DISABLE_P;4546if (submit->flags & ASYNC_TX_FENCE)47dma_flags |= DMA_PREP_FENCE;48dma_dest[1] = dma_map_page(dev, dest, 0, len, DMA_BIDIRECTIONAL);49dma_src[0] = dma_map_page(dev, srcs[0], 0, len, DMA_TO_DEVICE);50dma_src[1] = dma_map_page(dev, srcs[1], 0, len, DMA_TO_DEVICE);51tx = dma->device_prep_dma_pq(chan, dma_dest, dma_src, 2, coef,52len, dma_flags);53if (tx) {54async_tx_submit(chan, tx, submit);55return tx;56}5758/* could not get a descriptor, unmap and fall through to59* the synchronous path60*/61dma_unmap_page(dev, dma_dest[1], len, DMA_BIDIRECTIONAL);62dma_unmap_page(dev, dma_src[0], len, DMA_TO_DEVICE);63dma_unmap_page(dev, dma_src[1], len, DMA_TO_DEVICE);64}6566/* run the operation synchronously */67async_tx_quiesce(&submit->depend_tx);68amul = raid6_gfmul[coef[0]];69bmul = raid6_gfmul[coef[1]];70a = page_address(srcs[0]);71b = page_address(srcs[1]);72c = page_address(dest);7374while (len--) {75ax = amul[*a++];76bx = bmul[*b++];77*c++ = ax ^ bx;78}7980return NULL;81}8283static struct dma_async_tx_descriptor *84async_mult(struct page *dest, struct page *src, u8 coef, size_t len,85struct async_submit_ctl *submit)86{87struct dma_chan *chan = async_tx_find_channel(submit, DMA_PQ,88&dest, 1, &src, 1, len);89struct dma_device *dma = chan ? chan->device : NULL;90const u8 *qmul; /* Q multiplier table */91u8 *d, *s;9293if (dma) {94dma_addr_t dma_dest[2];95dma_addr_t dma_src[1];96struct device *dev = dma->dev;97struct dma_async_tx_descriptor *tx;98enum dma_ctrl_flags dma_flags = DMA_PREP_PQ_DISABLE_P;99100if (submit->flags & ASYNC_TX_FENCE)101dma_flags |= DMA_PREP_FENCE;102dma_dest[1] = dma_map_page(dev, dest, 0, len, DMA_BIDIRECTIONAL);103dma_src[0] = dma_map_page(dev, src, 0, len, DMA_TO_DEVICE);104tx = dma->device_prep_dma_pq(chan, dma_dest, dma_src, 1, &coef,105len, dma_flags);106if (tx) {107async_tx_submit(chan, tx, submit);108return tx;109}110111/* could not get a descriptor, unmap and fall through to112* the synchronous path113*/114dma_unmap_page(dev, dma_dest[1], len, DMA_BIDIRECTIONAL);115dma_unmap_page(dev, dma_src[0], len, DMA_TO_DEVICE);116}117118/* no channel available, or failed to allocate a descriptor, so119* perform the operation synchronously120*/121async_tx_quiesce(&submit->depend_tx);122qmul = raid6_gfmul[coef];123d = page_address(dest);124s = page_address(src);125126while (len--)127*d++ = qmul[*s++];128129return NULL;130}131132static struct dma_async_tx_descriptor *133__2data_recov_4(int disks, size_t bytes, int faila, int failb,134struct page **blocks, struct async_submit_ctl *submit)135{136struct dma_async_tx_descriptor *tx = NULL;137struct page *p, *q, *a, *b;138struct page *srcs[2];139unsigned char coef[2];140enum async_tx_flags flags = submit->flags;141dma_async_tx_callback cb_fn = submit->cb_fn;142void *cb_param = submit->cb_param;143void *scribble = submit->scribble;144145p = blocks[disks-2];146q = blocks[disks-1];147148a = blocks[faila];149b = blocks[failb];150151/* in the 4 disk case P + Pxy == P and Q + Qxy == Q */152/* Dx = A*(P+Pxy) + B*(Q+Qxy) */153srcs[0] = p;154srcs[1] = q;155coef[0] = raid6_gfexi[failb-faila];156coef[1] = raid6_gfinv[raid6_gfexp[faila]^raid6_gfexp[failb]];157init_async_submit(submit, ASYNC_TX_FENCE, tx, NULL, NULL, scribble);158tx = async_sum_product(b, srcs, coef, bytes, submit);159160/* Dy = P+Pxy+Dx */161srcs[0] = p;162srcs[1] = b;163init_async_submit(submit, flags | ASYNC_TX_XOR_ZERO_DST, tx, cb_fn,164cb_param, scribble);165tx = async_xor(a, srcs, 0, 2, bytes, submit);166167return tx;168169}170171static struct dma_async_tx_descriptor *172__2data_recov_5(int disks, size_t bytes, int faila, int failb,173struct page **blocks, struct async_submit_ctl *submit)174{175struct dma_async_tx_descriptor *tx = NULL;176struct page *p, *q, *g, *dp, *dq;177struct page *srcs[2];178unsigned char coef[2];179enum async_tx_flags flags = submit->flags;180dma_async_tx_callback cb_fn = submit->cb_fn;181void *cb_param = submit->cb_param;182void *scribble = submit->scribble;183int good_srcs, good, i;184185good_srcs = 0;186good = -1;187for (i = 0; i < disks-2; i++) {188if (blocks[i] == NULL)189continue;190if (i == faila || i == failb)191continue;192good = i;193good_srcs++;194}195BUG_ON(good_srcs > 1);196197p = blocks[disks-2];198q = blocks[disks-1];199g = blocks[good];200201/* Compute syndrome with zero for the missing data pages202* Use the dead data pages as temporary storage for delta p and203* delta q204*/205dp = blocks[faila];206dq = blocks[failb];207208init_async_submit(submit, ASYNC_TX_FENCE, tx, NULL, NULL, scribble);209tx = async_memcpy(dp, g, 0, 0, bytes, submit);210init_async_submit(submit, ASYNC_TX_FENCE, tx, NULL, NULL, scribble);211tx = async_mult(dq, g, raid6_gfexp[good], bytes, submit);212213/* compute P + Pxy */214srcs[0] = dp;215srcs[1] = p;216init_async_submit(submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,217NULL, NULL, scribble);218tx = async_xor(dp, srcs, 0, 2, bytes, submit);219220/* compute Q + Qxy */221srcs[0] = dq;222srcs[1] = q;223init_async_submit(submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,224NULL, NULL, scribble);225tx = async_xor(dq, srcs, 0, 2, bytes, submit);226227/* Dx = A*(P+Pxy) + B*(Q+Qxy) */228srcs[0] = dp;229srcs[1] = dq;230coef[0] = raid6_gfexi[failb-faila];231coef[1] = raid6_gfinv[raid6_gfexp[faila]^raid6_gfexp[failb]];232init_async_submit(submit, ASYNC_TX_FENCE, tx, NULL, NULL, scribble);233tx = async_sum_product(dq, srcs, coef, bytes, submit);234235/* Dy = P+Pxy+Dx */236srcs[0] = dp;237srcs[1] = dq;238init_async_submit(submit, flags | ASYNC_TX_XOR_DROP_DST, tx, cb_fn,239cb_param, scribble);240tx = async_xor(dp, srcs, 0, 2, bytes, submit);241242return tx;243}244245static struct dma_async_tx_descriptor *246__2data_recov_n(int disks, size_t bytes, int faila, int failb,247struct page **blocks, struct async_submit_ctl *submit)248{249struct dma_async_tx_descriptor *tx = NULL;250struct page *p, *q, *dp, *dq;251struct page *srcs[2];252unsigned char coef[2];253enum async_tx_flags flags = submit->flags;254dma_async_tx_callback cb_fn = submit->cb_fn;255void *cb_param = submit->cb_param;256void *scribble = submit->scribble;257258p = blocks[disks-2];259q = blocks[disks-1];260261/* Compute syndrome with zero for the missing data pages262* Use the dead data pages as temporary storage for263* delta p and delta q264*/265dp = blocks[faila];266blocks[faila] = NULL;267blocks[disks-2] = dp;268dq = blocks[failb];269blocks[failb] = NULL;270blocks[disks-1] = dq;271272init_async_submit(submit, ASYNC_TX_FENCE, tx, NULL, NULL, scribble);273tx = async_gen_syndrome(blocks, 0, disks, bytes, submit);274275/* Restore pointer table */276blocks[faila] = dp;277blocks[failb] = dq;278blocks[disks-2] = p;279blocks[disks-1] = q;280281/* compute P + Pxy */282srcs[0] = dp;283srcs[1] = p;284init_async_submit(submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,285NULL, NULL, scribble);286tx = async_xor(dp, srcs, 0, 2, bytes, submit);287288/* compute Q + Qxy */289srcs[0] = dq;290srcs[1] = q;291init_async_submit(submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,292NULL, NULL, scribble);293tx = async_xor(dq, srcs, 0, 2, bytes, submit);294295/* Dx = A*(P+Pxy) + B*(Q+Qxy) */296srcs[0] = dp;297srcs[1] = dq;298coef[0] = raid6_gfexi[failb-faila];299coef[1] = raid6_gfinv[raid6_gfexp[faila]^raid6_gfexp[failb]];300init_async_submit(submit, ASYNC_TX_FENCE, tx, NULL, NULL, scribble);301tx = async_sum_product(dq, srcs, coef, bytes, submit);302303/* Dy = P+Pxy+Dx */304srcs[0] = dp;305srcs[1] = dq;306init_async_submit(submit, flags | ASYNC_TX_XOR_DROP_DST, tx, cb_fn,307cb_param, scribble);308tx = async_xor(dp, srcs, 0, 2, bytes, submit);309310return tx;311}312313/**314* async_raid6_2data_recov - asynchronously calculate two missing data blocks315* @disks: number of disks in the RAID-6 array316* @bytes: block size317* @faila: first failed drive index318* @failb: second failed drive index319* @blocks: array of source pointers where the last two entries are p and q320* @submit: submission/completion modifiers321*/322struct dma_async_tx_descriptor *323async_raid6_2data_recov(int disks, size_t bytes, int faila, int failb,324struct page **blocks, struct async_submit_ctl *submit)325{326void *scribble = submit->scribble;327int non_zero_srcs, i;328329BUG_ON(faila == failb);330if (failb < faila)331swap(faila, failb);332333pr_debug("%s: disks: %d len: %zu\n", __func__, disks, bytes);334335/* if a dma resource is not available or a scribble buffer is not336* available punt to the synchronous path. In the 'dma not337* available' case be sure to use the scribble buffer to338* preserve the content of 'blocks' as the caller intended.339*/340if (!async_dma_find_channel(DMA_PQ) || !scribble) {341void **ptrs = scribble ? scribble : (void **) blocks;342343async_tx_quiesce(&submit->depend_tx);344for (i = 0; i < disks; i++)345if (blocks[i] == NULL)346ptrs[i] = (void *) raid6_empty_zero_page;347else348ptrs[i] = page_address(blocks[i]);349350raid6_2data_recov(disks, bytes, faila, failb, ptrs);351352async_tx_sync_epilog(submit);353354return NULL;355}356357non_zero_srcs = 0;358for (i = 0; i < disks-2 && non_zero_srcs < 4; i++)359if (blocks[i])360non_zero_srcs++;361switch (non_zero_srcs) {362case 0:363case 1:364/* There must be at least 2 sources - the failed devices. */365BUG();366367case 2:368/* dma devices do not uniformly understand a zero source pq369* operation (in contrast to the synchronous case), so370* explicitly handle the special case of a 4 disk array with371* both data disks missing.372*/373return __2data_recov_4(disks, bytes, faila, failb, blocks, submit);374case 3:375/* dma devices do not uniformly understand a single376* source pq operation (in contrast to the synchronous377* case), so explicitly handle the special case of a 5 disk378* array with 2 of 3 data disks missing.379*/380return __2data_recov_5(disks, bytes, faila, failb, blocks, submit);381default:382return __2data_recov_n(disks, bytes, faila, failb, blocks, submit);383}384}385EXPORT_SYMBOL_GPL(async_raid6_2data_recov);386387/**388* async_raid6_datap_recov - asynchronously calculate a data and the 'p' block389* @disks: number of disks in the RAID-6 array390* @bytes: block size391* @faila: failed drive index392* @blocks: array of source pointers where the last two entries are p and q393* @submit: submission/completion modifiers394*/395struct dma_async_tx_descriptor *396async_raid6_datap_recov(int disks, size_t bytes, int faila,397struct page **blocks, struct async_submit_ctl *submit)398{399struct dma_async_tx_descriptor *tx = NULL;400struct page *p, *q, *dq;401u8 coef;402enum async_tx_flags flags = submit->flags;403dma_async_tx_callback cb_fn = submit->cb_fn;404void *cb_param = submit->cb_param;405void *scribble = submit->scribble;406int good_srcs, good, i;407struct page *srcs[2];408409pr_debug("%s: disks: %d len: %zu\n", __func__, disks, bytes);410411/* if a dma resource is not available or a scribble buffer is not412* available punt to the synchronous path. In the 'dma not413* available' case be sure to use the scribble buffer to414* preserve the content of 'blocks' as the caller intended.415*/416if (!async_dma_find_channel(DMA_PQ) || !scribble) {417void **ptrs = scribble ? scribble : (void **) blocks;418419async_tx_quiesce(&submit->depend_tx);420for (i = 0; i < disks; i++)421if (blocks[i] == NULL)422ptrs[i] = (void*)raid6_empty_zero_page;423else424ptrs[i] = page_address(blocks[i]);425426raid6_datap_recov(disks, bytes, faila, ptrs);427428async_tx_sync_epilog(submit);429430return NULL;431}432433good_srcs = 0;434good = -1;435for (i = 0; i < disks-2; i++) {436if (i == faila)437continue;438if (blocks[i]) {439good = i;440good_srcs++;441if (good_srcs > 1)442break;443}444}445BUG_ON(good_srcs == 0);446447p = blocks[disks-2];448q = blocks[disks-1];449450/* Compute syndrome with zero for the missing data page451* Use the dead data page as temporary storage for delta q452*/453dq = blocks[faila];454blocks[faila] = NULL;455blocks[disks-1] = dq;456457/* in the 4-disk case we only need to perform a single source458* multiplication with the one good data block.459*/460if (good_srcs == 1) {461struct page *g = blocks[good];462463init_async_submit(submit, ASYNC_TX_FENCE, tx, NULL, NULL,464scribble);465tx = async_memcpy(p, g, 0, 0, bytes, submit);466467init_async_submit(submit, ASYNC_TX_FENCE, tx, NULL, NULL,468scribble);469tx = async_mult(dq, g, raid6_gfexp[good], bytes, submit);470} else {471init_async_submit(submit, ASYNC_TX_FENCE, tx, NULL, NULL,472scribble);473tx = async_gen_syndrome(blocks, 0, disks, bytes, submit);474}475476/* Restore pointer table */477blocks[faila] = dq;478blocks[disks-1] = q;479480/* calculate g^{-faila} */481coef = raid6_gfinv[raid6_gfexp[faila]];482483srcs[0] = dq;484srcs[1] = q;485init_async_submit(submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,486NULL, NULL, scribble);487tx = async_xor(dq, srcs, 0, 2, bytes, submit);488489init_async_submit(submit, ASYNC_TX_FENCE, tx, NULL, NULL, scribble);490tx = async_mult(dq, dq, coef, bytes, submit);491492srcs[0] = p;493srcs[1] = dq;494init_async_submit(submit, flags | ASYNC_TX_XOR_DROP_DST, tx, cb_fn,495cb_param, scribble);496tx = async_xor(p, srcs, 0, 2, bytes, submit);497498return tx;499}500EXPORT_SYMBOL_GPL(async_raid6_datap_recov);501502MODULE_AUTHOR("Dan Williams <[email protected]>");503MODULE_DESCRIPTION("asynchronous RAID-6 recovery api");504MODULE_LICENSE("GPL");505506507