// SPDX-License-Identifier: GPL-2.0-only /* * Routines supporting the Power 7+ Nest Accelerators driver * * Copyright (C) 2011-2012 International Business Machines Inc. * * Author: Kent Yoder <yoder1@us.ibm.com>
*/
/** * nx_hcall_sync - make an H_COP_OP hcall for the passed in op structure * * @nx_ctx: the crypto context handle * @op: PFO operation struct to pass in * @may_sleep: flag indicating the request can sleep * * Make the hcall, retrying while the hardware is busy. If we cannot yield * the thread, limit the number of retries to 10 here.
*/ int nx_hcall_sync(struct nx_crypto_ctx *nx_ctx, struct vio_pfo_op *op,
u32 may_sleep)
{ int rc, retries = 10; struct vio_dev *viodev = nx_driver.viodev;
atomic_inc(&(nx_ctx->stats->sync_ops));
do {
rc = vio_h_cop_sync(viodev, op);
} while (rc == -EBUSY && !may_sleep && retries--);
/** * nx_build_sg_list - build an NX scatter list describing a single buffer * * @sg_head: pointer to the first scatter list element to build * @start_addr: pointer to the linear buffer * @len: length of the data at @start_addr * @sgmax: the largest number of scatter list elements we're allowed to create * * This function will start writing nx_sg elements at @sg_head and keep * writing them until all of the data from @start_addr is described or * until sgmax elements have been written. Scatter list elements will be * created such that none of the elements describes a buffer that crosses a 4K * boundary.
*/ struct nx_sg *nx_build_sg_list(struct nx_sg *sg_head,
u8 *start_addr, unsignedint *len,
u32 sgmax)
{ unsignedint sg_len = 0; struct nx_sg *sg;
u64 sg_addr = (u64)start_addr;
u64 end_addr;
/* determine the start and end for this address range - slightly
* different if this is in VMALLOC_REGION */ if (is_vmalloc_addr(start_addr))
sg_addr = page_to_phys(vmalloc_to_page(start_addr))
+ offset_in_page(sg_addr); else
sg_addr = __pa(sg_addr);
end_addr = sg_addr + *len;
/* each iteration will write one struct nx_sg element and add the * length of data described by that element to sg_len. Once @len bytes * have been described (or @sgmax elements have been written), the * loop ends. min_t is used to ensure @end_addr falls on the same page * as sg_addr, if not, we need to create another nx_sg element for the * data on the next page. * * Also when using vmalloc'ed data, every time that a system page * boundary is crossed the physical address needs to be re-calculated.
*/ for (sg = sg_head; sg_len < *len; sg++) {
u64 next_page;
/* return the moved sg_head pointer */ return sg;
}
/** * nx_walk_and_build - walk a linux scatterlist and build an nx scatterlist * * @nx_dst: pointer to the first nx_sg element to write * @sglen: max number of nx_sg entries we're allowed to write * @sg_src: pointer to the source linux scatterlist to walk * @start: number of bytes to fast-forward past at the beginning of @sg_src * @src_len: number of bytes to walk in @sg_src
*/ struct nx_sg *nx_walk_and_build(struct nx_sg *nx_dst, unsignedint sglen, struct scatterlist *sg_src, unsignedint start, unsignedint *src_len)
{ struct scatter_walk walk; struct nx_sg *nx_sg = nx_dst; unsignedint n, len = *src_len;
/* we need to fast forward through @start bytes first */
scatterwalk_start_at_pos(&walk, sg_src, start);
while (len && (nx_sg - nx_dst) < sglen) {
n = scatterwalk_next(&walk, len);
/* return the moved destination pointer */ return nx_sg;
}
/** * trim_sg_list - ensures the bound in sg list. * @sg: sg list head * @end: sg lisg end * @delta: is the amount we need to crop in order to bound the list. * @nbytes: length of data in the scatterlists or data length - whichever * is greater.
*/ staticlongint trim_sg_list(struct nx_sg *sg, struct nx_sg *end, unsignedint delta, unsignedint *nbytes)
{ longint oplen; longint data_back; unsignedint is_delta = delta;
while (delta && end > sg) { struct nx_sg *last = end - 1;
/* There are cases where we need to crop list in order to make it * a block size multiple, but we also need to align data. In order to * that we need to calculate how much we need to put back to be * processed
*/
oplen = (sg - end) * sizeof(struct nx_sg); if (is_delta) {
data_back = (abs(oplen) / AES_BLOCK_SIZE) * sg->len;
data_back = *nbytes - (data_back & ~(AES_BLOCK_SIZE - 1));
*nbytes -= data_back;
}
return oplen;
}
/** * nx_build_sg_lists - walk the input scatterlists and build arrays of NX * scatterlists based on them. * * @nx_ctx: NX crypto context for the lists we're building * @iv: iv data, if the algorithm requires it * @dst: destination scatterlist * @src: source scatterlist * @nbytes: length of data described in the scatterlists * @offset: number of bytes to fast-forward past at the beginning of * scatterlists. * @oiv: destination for the iv data, if the algorithm requires it * * This is common code shared by all the AES algorithms. It uses the crypto * scatterlist walk routines to traverse input and output scatterlists, building * corresponding NX scatterlists
*/ int nx_build_sg_lists(struct nx_crypto_ctx *nx_ctx, const u8 *iv, struct scatterlist *dst, struct scatterlist *src, unsignedint *nbytes, unsignedint offset,
u8 *oiv)
{ unsignedint delta = 0; unsignedint total = *nbytes; struct nx_sg *nx_insg = nx_ctx->in_sg; struct nx_sg *nx_outsg = nx_ctx->out_sg; unsignedint max_sg_len;
/* these lengths should be negative, which will indicate to phyp that * the input and output parameters are scatterlists, not linear
* buffers */
nx_ctx->op.inlen = trim_sg_list(nx_ctx->in_sg, nx_insg, delta, nbytes);
nx_ctx->op.outlen = trim_sg_list(nx_ctx->out_sg, nx_outsg, delta, nbytes);
return 0;
}
/** * nx_ctx_init - initialize an nx_ctx's vio_pfo_op struct * * @nx_ctx: the nx context to initialize * @function: the function code for the op
*/ void nx_ctx_init(struct nx_crypto_ctx *nx_ctx, unsignedint function)
{
spin_lock_init(&nx_ctx->lock);
memset(nx_ctx->kmem, 0, nx_ctx->kmem_len);
nx_ctx->csbcpb->csb.valid |= NX_CSB_VALID_BIT;
/* You can't tell if the data read in for this property is sane by its * size alone. This is because there are sizes embedded in the data * structure. The best we can do is check lengths as we parse and bail
* as soon as a length error is detected. */
bytes_so_far = 0;
switch (trip->keybitlen) { case 128: case 160:
props->ap[msc->fc][msc->mode][0].databytelen =
trip->databytelen;
props->ap[msc->fc][msc->mode][0].sglen =
trip->sglen; break; case 192:
props->ap[msc->fc][msc->mode][1].databytelen =
trip->databytelen;
props->ap[msc->fc][msc->mode][1].sglen =
trip->sglen; break; case 256: if (msc->fc == NX_FC_AES) {
props->ap[msc->fc][msc->mode][2].
databytelen = trip->databytelen;
props->ap[msc->fc][msc->mode][2].sglen =
trip->sglen;
} elseif (msc->fc == NX_FC_AES_HMAC ||
msc->fc == NX_FC_SHA) {
props->ap[msc->fc][msc->mode][1].
databytelen = trip->databytelen;
props->ap[msc->fc][msc->mode][1].sglen =
trip->sglen;
} else {
dev_warn(dev, "unknown function " "code/key bit len combo" ": (%u/256)\n", msc->fc);
} break; case 512:
props->ap[msc->fc][msc->mode][2].databytelen =
trip->databytelen;
props->ap[msc->fc][msc->mode][2].sglen =
trip->sglen; break; default:
dev_warn(dev, "unknown function code/key bit " "len combo: (%u/%u)\n", msc->fc,
trip->keybitlen); break;
}
next_loop:
bytes_so_far += sizeof(struct msc_triplet);
trip++;
}
msc = (struct max_sync_cop *)trip;
}
props->flags |= NX_OF_FLAG_MAXSYNCCOP_SET;
}
/** * nx_of_init - read openFirmware values from the device tree * * @dev: device handle * @props: pointer to struct to hold the properties values * * Called once at driver probe time, this function will read out the * openFirmware properties we use at runtime. If all the OF properties are * acceptable, when we exit this function props->flags will indicate that * we're ready to register our crypto algorithms.
*/ staticvoid nx_of_init(struct device *dev, struct nx_of *props)
{ struct device_node *base_node = dev->of_node; struct property *p;
p = of_find_property(base_node, "status", NULL); if (!p)
dev_info(dev, "%s: property 'status' not found\n", __func__); else
nx_of_update_status(dev, p, props);
p = of_find_property(base_node, "ibm,max-sg-len", NULL); if (!p)
dev_info(dev, "%s: property 'ibm,max-sg-len' not found\n",
__func__); else
nx_of_update_sglen(dev, p, props);
p = of_find_property(base_node, "ibm,max-sync-cop", NULL); if (!p)
dev_info(dev, "%s: property 'ibm,max-sync-cop' not found\n",
__func__); else
nx_of_update_msc(dev, p, props);
}
/** * nx_register_algs - register algorithms with the crypto API * * Called from nx_probe() * * If all OF properties are in an acceptable state, the driver flags will * indicate that we're ready and we'll create our debugfs files and register * out crypto algorithms.
*/ staticint nx_register_algs(void)
{ int rc = -1;
if (nx_driver.of.flags != NX_OF_FLAG_MASK_READY) goto out;
/** * nx_crypto_ctx_init - create and initialize a crypto api context * * @nx_ctx: the crypto api context * @fc: function code for the context * @mode: the function code specific mode for this context
*/ staticint nx_crypto_ctx_init(struct nx_crypto_ctx *nx_ctx, u32 fc, u32 mode)
{ if (nx_driver.of.status != NX_OKAY) {
pr_err("Attempt to initialize NX crypto context while device " "is not available!\n"); return -ENODEV;
}
/* we need an extra page for csbcpb_aead for these modes */ if (mode == NX_MODE_AES_GCM || mode == NX_MODE_AES_CCM)
nx_ctx->kmem_len = (5 * NX_PAGE_SIZE) + sizeof(struct nx_csbcpb); else
nx_ctx->kmem_len = (4 * NX_PAGE_SIZE) + sizeof(struct nx_csbcpb);
nx_ctx->kmem = kmalloc(nx_ctx->kmem_len, GFP_KERNEL); if (!nx_ctx->kmem) return -ENOMEM;
/* the csbcpb and scatterlists must be 4K aligned pages */
nx_ctx->csbcpb = (struct nx_csbcpb *)(round_up((u64)nx_ctx->kmem,
(u64)NX_PAGE_SIZE));
nx_ctx->in_sg = (struct nx_sg *)((u8 *)nx_ctx->csbcpb + NX_PAGE_SIZE);
nx_ctx->out_sg = (struct nx_sg *)((u8 *)nx_ctx->in_sg + NX_PAGE_SIZE);
/* give each context a pointer to global stats and their OF
* properties */
nx_ctx->stats = &nx_driver.stats;
memcpy(nx_ctx->props, nx_driver.of.ap[fc][mode], sizeof(struct alg_props) * 3);
return 0;
}
/* entry points from the crypto tfm initializers */ int nx_crypto_ctx_aes_ccm_init(struct crypto_aead *tfm)
{
crypto_aead_set_reqsize(tfm, sizeof(struct nx_ccm_rctx)); return nx_crypto_ctx_init(crypto_aead_ctx(tfm), NX_FC_AES,
NX_MODE_AES_CCM);
}
int nx_crypto_ctx_aes_ctr_init(struct crypto_skcipher *tfm)
{ return nx_crypto_ctx_init(crypto_skcipher_ctx(tfm), NX_FC_AES,
NX_MODE_AES_CTR);
}
int nx_crypto_ctx_aes_cbc_init(struct crypto_skcipher *tfm)
{ return nx_crypto_ctx_init(crypto_skcipher_ctx(tfm), NX_FC_AES,
NX_MODE_AES_CBC);
}
int nx_crypto_ctx_aes_ecb_init(struct crypto_skcipher *tfm)
{ return nx_crypto_ctx_init(crypto_skcipher_ctx(tfm), NX_FC_AES,
NX_MODE_AES_ECB);
}
int nx_crypto_ctx_sha_init(struct crypto_shash *tfm)
{ return nx_crypto_ctx_init(crypto_shash_ctx(tfm), NX_FC_SHA, NX_MODE_SHA);
}
int nx_crypto_ctx_aes_xcbc_init(struct crypto_shash *tfm)
{ return nx_crypto_ctx_init(crypto_shash_ctx(tfm), NX_FC_AES,
NX_MODE_AES_XCBC_MAC);
}
/** * nx_crypto_ctx_exit - destroy a crypto api context * * @tfm: the crypto transform pointer for the context * * As crypto API contexts are destroyed, this exit hook is called to free the * memory associated with it.
*/ void nx_crypto_ctx_exit(struct crypto_tfm *tfm)
{ struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(tfm);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.