Version:  2.0.40 2.2.26 2.4.37 3.4 3.5 3.6 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15 3.16 3.17 3.18 3.19 4.0

Linux/drivers/crypto/nx/nx.c

  1 /**
  2  * Routines supporting the Power 7+ Nest Accelerators driver
  3  *
  4  * Copyright (C) 2011-2012 International Business Machines Inc.
  5  *
  6  * This program is free software; you can redistribute it and/or modify
  7  * it under the terms of the GNU General Public License as published by
  8  * the Free Software Foundation; version 2 only.
  9  *
 10  * This program is distributed in the hope that it will be useful,
 11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13  * GNU General Public License for more details.
 14  *
 15  * You should have received a copy of the GNU General Public License
 16  * along with this program; if not, write to the Free Software
 17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 18  *
 19  * Author: Kent Yoder <yoder1@us.ibm.com>
 20  */
 21 
 22 #include <crypto/internal/hash.h>
 23 #include <crypto/hash.h>
 24 #include <crypto/aes.h>
 25 #include <crypto/sha.h>
 26 #include <crypto/algapi.h>
 27 #include <crypto/scatterwalk.h>
 28 #include <linux/module.h>
 29 #include <linux/moduleparam.h>
 30 #include <linux/types.h>
 31 #include <linux/mm.h>
 32 #include <linux/crypto.h>
 33 #include <linux/scatterlist.h>
 34 #include <linux/device.h>
 35 #include <linux/of.h>
 36 #include <asm/hvcall.h>
 37 #include <asm/vio.h>
 38 
 39 #include "nx_csbcpb.h"
 40 #include "nx.h"
 41 
 42 
 43 /**
 44  * nx_hcall_sync - make an H_COP_OP hcall for the passed in op structure
 45  *
 46  * @nx_ctx: the crypto context handle
 47  * @op: PFO operation struct to pass in
 48  * @may_sleep: flag indicating the request can sleep
 49  *
 50  * Make the hcall, retrying while the hardware is busy. If we cannot yield
 51  * the thread, limit the number of retries to 10 here.
 52  */
 53 int nx_hcall_sync(struct nx_crypto_ctx *nx_ctx,
 54                   struct vio_pfo_op    *op,
 55                   u32                   may_sleep)
 56 {
 57         int rc, retries = 10;
 58         struct vio_dev *viodev = nx_driver.viodev;
 59 
 60         atomic_inc(&(nx_ctx->stats->sync_ops));
 61 
 62         do {
 63                 rc = vio_h_cop_sync(viodev, op);
 64         } while (rc == -EBUSY && !may_sleep && retries--);
 65 
 66         if (rc) {
 67                 dev_dbg(&viodev->dev, "vio_h_cop_sync failed: rc: %d "
 68                         "hcall rc: %ld\n", rc, op->hcall_err);
 69                 atomic_inc(&(nx_ctx->stats->errors));
 70                 atomic_set(&(nx_ctx->stats->last_error), op->hcall_err);
 71                 atomic_set(&(nx_ctx->stats->last_error_pid), current->pid);
 72         }
 73 
 74         return rc;
 75 }
 76 
 77 /**
 78  * nx_build_sg_list - build an NX scatter list describing a single  buffer
 79  *
 80  * @sg_head: pointer to the first scatter list element to build
 81  * @start_addr: pointer to the linear buffer
 82  * @len: length of the data at @start_addr
 83  * @sgmax: the largest number of scatter list elements we're allowed to create
 84  *
 85  * This function will start writing nx_sg elements at @sg_head and keep
 86  * writing them until all of the data from @start_addr is described or
 87  * until sgmax elements have been written. Scatter list elements will be
 88  * created such that none of the elements describes a buffer that crosses a 4K
 89  * boundary.
 90  */
 91 struct nx_sg *nx_build_sg_list(struct nx_sg *sg_head,
 92                                u8           *start_addr,
 93                                unsigned int *len,
 94                                u32           sgmax)
 95 {
 96         unsigned int sg_len = 0;
 97         struct nx_sg *sg;
 98         u64 sg_addr = (u64)start_addr;
 99         u64 end_addr;
100 
101         /* determine the start and end for this address range - slightly
102          * different if this is in VMALLOC_REGION */
103         if (is_vmalloc_addr(start_addr))
104                 sg_addr = page_to_phys(vmalloc_to_page(start_addr))
105                           + offset_in_page(sg_addr);
106         else
107                 sg_addr = __pa(sg_addr);
108 
109         end_addr = sg_addr + *len;
110 
111         /* each iteration will write one struct nx_sg element and add the
112          * length of data described by that element to sg_len. Once @len bytes
113          * have been described (or @sgmax elements have been written), the
114          * loop ends. min_t is used to ensure @end_addr falls on the same page
115          * as sg_addr, if not, we need to create another nx_sg element for the
116          * data on the next page.
117          *
118          * Also when using vmalloc'ed data, every time that a system page
119          * boundary is crossed the physical address needs to be re-calculated.
120          */
121         for (sg = sg_head; sg_len < *len; sg++) {
122                 u64 next_page;
123 
124                 sg->addr = sg_addr;
125                 sg_addr = min_t(u64, NX_PAGE_NUM(sg_addr + NX_PAGE_SIZE),
126                                 end_addr);
127 
128                 next_page = (sg->addr & PAGE_MASK) + PAGE_SIZE;
129                 sg->len = min_t(u64, sg_addr, next_page) - sg->addr;
130                 sg_len += sg->len;
131 
132                 if (sg_addr >= next_page &&
133                                 is_vmalloc_addr(start_addr + sg_len)) {
134                         sg_addr = page_to_phys(vmalloc_to_page(
135                                                 start_addr + sg_len));
136                         end_addr = sg_addr + *len - sg_len;
137                 }
138 
139                 if ((sg - sg_head) == sgmax) {
140                         pr_err("nx: scatter/gather list overflow, pid: %d\n",
141                                current->pid);
142                         sg++;
143                         break;
144                 }
145         }
146         *len = sg_len;
147 
148         /* return the moved sg_head pointer */
149         return sg;
150 }
151 
152 /**
153  * nx_walk_and_build - walk a linux scatterlist and build an nx scatterlist
154  *
155  * @nx_dst: pointer to the first nx_sg element to write
156  * @sglen: max number of nx_sg entries we're allowed to write
157  * @sg_src: pointer to the source linux scatterlist to walk
158  * @start: number of bytes to fast-forward past at the beginning of @sg_src
159  * @src_len: number of bytes to walk in @sg_src
160  */
161 struct nx_sg *nx_walk_and_build(struct nx_sg       *nx_dst,
162                                 unsigned int        sglen,
163                                 struct scatterlist *sg_src,
164                                 unsigned int        start,
165                                 unsigned int       *src_len)
166 {
167         struct scatter_walk walk;
168         struct nx_sg *nx_sg = nx_dst;
169         unsigned int n, offset = 0, len = *src_len;
170         char *dst;
171 
172         /* we need to fast forward through @start bytes first */
173         for (;;) {
174                 scatterwalk_start(&walk, sg_src);
175 
176                 if (start < offset + sg_src->length)
177                         break;
178 
179                 offset += sg_src->length;
180                 sg_src = sg_next(sg_src);
181         }
182 
183         /* start - offset is the number of bytes to advance in the scatterlist
184          * element we're currently looking at */
185         scatterwalk_advance(&walk, start - offset);
186 
187         while (len && (nx_sg - nx_dst) < sglen) {
188                 n = scatterwalk_clamp(&walk, len);
189                 if (!n) {
190                         /* In cases where we have scatterlist chain sg_next
191                          * handles with it properly */
192                         scatterwalk_start(&walk, sg_next(walk.sg));
193                         n = scatterwalk_clamp(&walk, len);
194                 }
195                 dst = scatterwalk_map(&walk);
196 
197                 nx_sg = nx_build_sg_list(nx_sg, dst, &n, sglen - (nx_sg - nx_dst));
198                 len -= n;
199 
200                 scatterwalk_unmap(dst);
201                 scatterwalk_advance(&walk, n);
202                 scatterwalk_done(&walk, SCATTERWALK_FROM_SG, len);
203         }
204         /* update to_process */
205         *src_len -= len;
206 
207         /* return the moved destination pointer */
208         return nx_sg;
209 }
210 
211 /**
212  * trim_sg_list - ensures the bound in sg list.
213  * @sg: sg list head
214  * @end: sg lisg end
215  * @delta:  is the amount we need to crop in order to bound the list.
216  *
217  */
218 static long int trim_sg_list(struct nx_sg *sg, struct nx_sg *end, unsigned int delta)
219 {
220         while (delta && end > sg) {
221                 struct nx_sg *last = end - 1;
222 
223                 if (last->len > delta) {
224                         last->len -= delta;
225                         delta = 0;
226                 } else {
227                         end--;
228                         delta -= last->len;
229                 }
230         }
231         return (sg - end) * sizeof(struct nx_sg);
232 }
233 
234 /**
235  * nx_sha_build_sg_list - walk and build sg list to sha modes
236  *                        using right bounds and limits.
237  * @nx_ctx: NX crypto context for the lists we're building
238  * @nx_sg: current sg list in or out list
239  * @op_len: current op_len to be used in order to build a sg list
240  * @nbytes:  number or bytes to be processed
241  * @offset: buf offset
242  * @mode: SHA256 or SHA512
243  */
244 int nx_sha_build_sg_list(struct nx_crypto_ctx *nx_ctx,
245                           struct nx_sg        *nx_in_outsg,
246                           s64                 *op_len,
247                           unsigned int        *nbytes,
248                           u8                  *offset,
249                           u32                 mode)
250 {
251         unsigned int delta = 0;
252         unsigned int total = *nbytes;
253         struct nx_sg *nx_insg = nx_in_outsg;
254         unsigned int max_sg_len;
255 
256         max_sg_len = min_t(u64, nx_ctx->ap->sglen,
257                         nx_driver.of.max_sg_len/sizeof(struct nx_sg));
258         max_sg_len = min_t(u64, max_sg_len,
259                         nx_ctx->ap->databytelen/NX_PAGE_SIZE);
260 
261         *nbytes = min_t(u64, *nbytes, nx_ctx->ap->databytelen);
262         nx_insg = nx_build_sg_list(nx_insg, offset, nbytes, max_sg_len);
263 
264         switch (mode) {
265         case NX_DS_SHA256:
266                 if (*nbytes < total)
267                         delta = *nbytes - (*nbytes & ~(SHA256_BLOCK_SIZE - 1));
268                 break;
269         case NX_DS_SHA512:
270                 if (*nbytes < total)
271                         delta = *nbytes - (*nbytes & ~(SHA512_BLOCK_SIZE - 1));
272                 break;
273         default:
274                 return -EINVAL;
275         }
276         *op_len = trim_sg_list(nx_in_outsg, nx_insg, delta);
277 
278         return 0;
279 }
280 
281 /**
282  * nx_build_sg_lists - walk the input scatterlists and build arrays of NX
283  *                     scatterlists based on them.
284  *
285  * @nx_ctx: NX crypto context for the lists we're building
286  * @desc: the block cipher descriptor for the operation
287  * @dst: destination scatterlist
288  * @src: source scatterlist
289  * @nbytes: length of data described in the scatterlists
290  * @offset: number of bytes to fast-forward past at the beginning of
291  *          scatterlists.
292  * @iv: destination for the iv data, if the algorithm requires it
293  *
294  * This is common code shared by all the AES algorithms. It uses the block
295  * cipher walk routines to traverse input and output scatterlists, building
296  * corresponding NX scatterlists
297  */
298 int nx_build_sg_lists(struct nx_crypto_ctx  *nx_ctx,
299                       struct blkcipher_desc *desc,
300                       struct scatterlist    *dst,
301                       struct scatterlist    *src,
302                       unsigned int          *nbytes,
303                       unsigned int           offset,
304                       u8                    *iv)
305 {
306         unsigned int delta = 0;
307         unsigned int total = *nbytes;
308         struct nx_sg *nx_insg = nx_ctx->in_sg;
309         struct nx_sg *nx_outsg = nx_ctx->out_sg;
310         unsigned int max_sg_len;
311 
312         max_sg_len = min_t(u64, nx_ctx->ap->sglen,
313                         nx_driver.of.max_sg_len/sizeof(struct nx_sg));
314         max_sg_len = min_t(u64, max_sg_len,
315                         nx_ctx->ap->databytelen/NX_PAGE_SIZE);
316 
317         if (iv)
318                 memcpy(iv, desc->info, AES_BLOCK_SIZE);
319 
320         *nbytes = min_t(u64, *nbytes, nx_ctx->ap->databytelen);
321 
322         nx_outsg = nx_walk_and_build(nx_outsg, max_sg_len, dst,
323                                         offset, nbytes);
324         nx_insg = nx_walk_and_build(nx_insg, max_sg_len, src,
325                                         offset, nbytes);
326 
327         if (*nbytes < total)
328                 delta = *nbytes - (*nbytes & ~(AES_BLOCK_SIZE - 1));
329 
330         /* these lengths should be negative, which will indicate to phyp that
331          * the input and output parameters are scatterlists, not linear
332          * buffers */
333         nx_ctx->op.inlen = trim_sg_list(nx_ctx->in_sg, nx_insg, delta);
334         nx_ctx->op.outlen = trim_sg_list(nx_ctx->out_sg, nx_outsg, delta);
335 
336         return 0;
337 }
338 
339 /**
340  * nx_ctx_init - initialize an nx_ctx's vio_pfo_op struct
341  *
342  * @nx_ctx: the nx context to initialize
343  * @function: the function code for the op
344  */
345 void nx_ctx_init(struct nx_crypto_ctx *nx_ctx, unsigned int function)
346 {
347         spin_lock_init(&nx_ctx->lock);
348         memset(nx_ctx->kmem, 0, nx_ctx->kmem_len);
349         nx_ctx->csbcpb->csb.valid |= NX_CSB_VALID_BIT;
350 
351         nx_ctx->op.flags = function;
352         nx_ctx->op.csbcpb = __pa(nx_ctx->csbcpb);
353         nx_ctx->op.in = __pa(nx_ctx->in_sg);
354         nx_ctx->op.out = __pa(nx_ctx->out_sg);
355 
356         if (nx_ctx->csbcpb_aead) {
357                 nx_ctx->csbcpb_aead->csb.valid |= NX_CSB_VALID_BIT;
358 
359                 nx_ctx->op_aead.flags = function;
360                 nx_ctx->op_aead.csbcpb = __pa(nx_ctx->csbcpb_aead);
361                 nx_ctx->op_aead.in = __pa(nx_ctx->in_sg);
362                 nx_ctx->op_aead.out = __pa(nx_ctx->out_sg);
363         }
364 }
365 
366 static void nx_of_update_status(struct device   *dev,
367                                struct property *p,
368                                struct nx_of    *props)
369 {
370         if (!strncmp(p->value, "okay", p->length)) {
371                 props->status = NX_WAITING;
372                 props->flags |= NX_OF_FLAG_STATUS_SET;
373         } else {
374                 dev_info(dev, "%s: status '%s' is not 'okay'\n", __func__,
375                          (char *)p->value);
376         }
377 }
378 
379 static void nx_of_update_sglen(struct device   *dev,
380                                struct property *p,
381                                struct nx_of    *props)
382 {
383         if (p->length != sizeof(props->max_sg_len)) {
384                 dev_err(dev, "%s: unexpected format for "
385                         "ibm,max-sg-len property\n", __func__);
386                 dev_dbg(dev, "%s: ibm,max-sg-len is %d bytes "
387                         "long, expected %zd bytes\n", __func__,
388                         p->length, sizeof(props->max_sg_len));
389                 return;
390         }
391 
392         props->max_sg_len = *(u32 *)p->value;
393         props->flags |= NX_OF_FLAG_MAXSGLEN_SET;
394 }
395 
396 static void nx_of_update_msc(struct device   *dev,
397                              struct property *p,
398                              struct nx_of    *props)
399 {
400         struct msc_triplet *trip;
401         struct max_sync_cop *msc;
402         unsigned int bytes_so_far, i, lenp;
403 
404         msc = (struct max_sync_cop *)p->value;
405         lenp = p->length;
406 
407         /* You can't tell if the data read in for this property is sane by its
408          * size alone. This is because there are sizes embedded in the data
409          * structure. The best we can do is check lengths as we parse and bail
410          * as soon as a length error is detected. */
411         bytes_so_far = 0;
412 
413         while ((bytes_so_far + sizeof(struct max_sync_cop)) <= lenp) {
414                 bytes_so_far += sizeof(struct max_sync_cop);
415 
416                 trip = msc->trip;
417 
418                 for (i = 0;
419                      ((bytes_so_far + sizeof(struct msc_triplet)) <= lenp) &&
420                      i < msc->triplets;
421                      i++) {
422                         if (msc->fc > NX_MAX_FC || msc->mode > NX_MAX_MODE) {
423                                 dev_err(dev, "unknown function code/mode "
424                                         "combo: %d/%d (ignored)\n", msc->fc,
425                                         msc->mode);
426                                 goto next_loop;
427                         }
428 
429                         switch (trip->keybitlen) {
430                         case 128:
431                         case 160:
432                                 props->ap[msc->fc][msc->mode][0].databytelen =
433                                         trip->databytelen;
434                                 props->ap[msc->fc][msc->mode][0].sglen =
435                                         trip->sglen;
436                                 break;
437                         case 192:
438                                 props->ap[msc->fc][msc->mode][1].databytelen =
439                                         trip->databytelen;
440                                 props->ap[msc->fc][msc->mode][1].sglen =
441                                         trip->sglen;
442                                 break;
443                         case 256:
444                                 if (msc->fc == NX_FC_AES) {
445                                         props->ap[msc->fc][msc->mode][2].
446                                                 databytelen = trip->databytelen;
447                                         props->ap[msc->fc][msc->mode][2].sglen =
448                                                 trip->sglen;
449                                 } else if (msc->fc == NX_FC_AES_HMAC ||
450                                            msc->fc == NX_FC_SHA) {
451                                         props->ap[msc->fc][msc->mode][1].
452                                                 databytelen = trip->databytelen;
453                                         props->ap[msc->fc][msc->mode][1].sglen =
454                                                 trip->sglen;
455                                 } else {
456                                         dev_warn(dev, "unknown function "
457                                                 "code/key bit len combo"
458                                                 ": (%u/256)\n", msc->fc);
459                                 }
460                                 break;
461                         case 512:
462                                 props->ap[msc->fc][msc->mode][2].databytelen =
463                                         trip->databytelen;
464                                 props->ap[msc->fc][msc->mode][2].sglen =
465                                         trip->sglen;
466                                 break;
467                         default:
468                                 dev_warn(dev, "unknown function code/key bit "
469                                          "len combo: (%u/%u)\n", msc->fc,
470                                          trip->keybitlen);
471                                 break;
472                         }
473 next_loop:
474                         bytes_so_far += sizeof(struct msc_triplet);
475                         trip++;
476                 }
477 
478                 msc = (struct max_sync_cop *)trip;
479         }
480 
481         props->flags |= NX_OF_FLAG_MAXSYNCCOP_SET;
482 }
483 
484 /**
485  * nx_of_init - read openFirmware values from the device tree
486  *
487  * @dev: device handle
488  * @props: pointer to struct to hold the properties values
489  *
490  * Called once at driver probe time, this function will read out the
491  * openFirmware properties we use at runtime. If all the OF properties are
492  * acceptable, when we exit this function props->flags will indicate that
493  * we're ready to register our crypto algorithms.
494  */
495 static void nx_of_init(struct device *dev, struct nx_of *props)
496 {
497         struct device_node *base_node = dev->of_node;
498         struct property *p;
499 
500         p = of_find_property(base_node, "status", NULL);
501         if (!p)
502                 dev_info(dev, "%s: property 'status' not found\n", __func__);
503         else
504                 nx_of_update_status(dev, p, props);
505 
506         p = of_find_property(base_node, "ibm,max-sg-len", NULL);
507         if (!p)
508                 dev_info(dev, "%s: property 'ibm,max-sg-len' not found\n",
509                          __func__);
510         else
511                 nx_of_update_sglen(dev, p, props);
512 
513         p = of_find_property(base_node, "ibm,max-sync-cop", NULL);
514         if (!p)
515                 dev_info(dev, "%s: property 'ibm,max-sync-cop' not found\n",
516                          __func__);
517         else
518                 nx_of_update_msc(dev, p, props);
519 }
520 
521 /**
522  * nx_register_algs - register algorithms with the crypto API
523  *
524  * Called from nx_probe()
525  *
526  * If all OF properties are in an acceptable state, the driver flags will
527  * indicate that we're ready and we'll create our debugfs files and register
528  * out crypto algorithms.
529  */
530 static int nx_register_algs(void)
531 {
532         int rc = -1;
533 
534         if (nx_driver.of.flags != NX_OF_FLAG_MASK_READY)
535                 goto out;
536 
537         memset(&nx_driver.stats, 0, sizeof(struct nx_stats));
538 
539         rc = NX_DEBUGFS_INIT(&nx_driver);
540         if (rc)
541                 goto out;
542 
543         nx_driver.of.status = NX_OKAY;
544 
545         rc = crypto_register_alg(&nx_ecb_aes_alg);
546         if (rc)
547                 goto out;
548 
549         rc = crypto_register_alg(&nx_cbc_aes_alg);
550         if (rc)
551                 goto out_unreg_ecb;
552 
553         rc = crypto_register_alg(&nx_ctr_aes_alg);
554         if (rc)
555                 goto out_unreg_cbc;
556 
557         rc = crypto_register_alg(&nx_ctr3686_aes_alg);
558         if (rc)
559                 goto out_unreg_ctr;
560 
561         rc = crypto_register_alg(&nx_gcm_aes_alg);
562         if (rc)
563                 goto out_unreg_ctr3686;
564 
565         rc = crypto_register_alg(&nx_gcm4106_aes_alg);
566         if (rc)
567                 goto out_unreg_gcm;
568 
569         rc = crypto_register_alg(&nx_ccm_aes_alg);
570         if (rc)
571                 goto out_unreg_gcm4106;
572 
573         rc = crypto_register_alg(&nx_ccm4309_aes_alg);
574         if (rc)
575                 goto out_unreg_ccm;
576 
577         rc = crypto_register_shash(&nx_shash_sha256_alg);
578         if (rc)
579                 goto out_unreg_ccm4309;
580 
581         rc = crypto_register_shash(&nx_shash_sha512_alg);
582         if (rc)
583                 goto out_unreg_s256;
584 
585         rc = crypto_register_shash(&nx_shash_aes_xcbc_alg);
586         if (rc)
587                 goto out_unreg_s512;
588 
589         goto out;
590 
591 out_unreg_s512:
592         crypto_unregister_shash(&nx_shash_sha512_alg);
593 out_unreg_s256:
594         crypto_unregister_shash(&nx_shash_sha256_alg);
595 out_unreg_ccm4309:
596         crypto_unregister_alg(&nx_ccm4309_aes_alg);
597 out_unreg_ccm:
598         crypto_unregister_alg(&nx_ccm_aes_alg);
599 out_unreg_gcm4106:
600         crypto_unregister_alg(&nx_gcm4106_aes_alg);
601 out_unreg_gcm:
602         crypto_unregister_alg(&nx_gcm_aes_alg);
603 out_unreg_ctr3686:
604         crypto_unregister_alg(&nx_ctr3686_aes_alg);
605 out_unreg_ctr:
606         crypto_unregister_alg(&nx_ctr_aes_alg);
607 out_unreg_cbc:
608         crypto_unregister_alg(&nx_cbc_aes_alg);
609 out_unreg_ecb:
610         crypto_unregister_alg(&nx_ecb_aes_alg);
611 out:
612         return rc;
613 }
614 
615 /**
616  * nx_crypto_ctx_init - create and initialize a crypto api context
617  *
618  * @nx_ctx: the crypto api context
619  * @fc: function code for the context
620  * @mode: the function code specific mode for this context
621  */
622 static int nx_crypto_ctx_init(struct nx_crypto_ctx *nx_ctx, u32 fc, u32 mode)
623 {
624         if (nx_driver.of.status != NX_OKAY) {
625                 pr_err("Attempt to initialize NX crypto context while device "
626                        "is not available!\n");
627                 return -ENODEV;
628         }
629 
630         /* we need an extra page for csbcpb_aead for these modes */
631         if (mode == NX_MODE_AES_GCM || mode == NX_MODE_AES_CCM)
632                 nx_ctx->kmem_len = (5 * NX_PAGE_SIZE) +
633                                    sizeof(struct nx_csbcpb);
634         else
635                 nx_ctx->kmem_len = (4 * NX_PAGE_SIZE) +
636                                    sizeof(struct nx_csbcpb);
637 
638         nx_ctx->kmem = kmalloc(nx_ctx->kmem_len, GFP_KERNEL);
639         if (!nx_ctx->kmem)
640                 return -ENOMEM;
641 
642         /* the csbcpb and scatterlists must be 4K aligned pages */
643         nx_ctx->csbcpb = (struct nx_csbcpb *)(round_up((u64)nx_ctx->kmem,
644                                                        (u64)NX_PAGE_SIZE));
645         nx_ctx->in_sg = (struct nx_sg *)((u8 *)nx_ctx->csbcpb + NX_PAGE_SIZE);
646         nx_ctx->out_sg = (struct nx_sg *)((u8 *)nx_ctx->in_sg + NX_PAGE_SIZE);
647 
648         if (mode == NX_MODE_AES_GCM || mode == NX_MODE_AES_CCM)
649                 nx_ctx->csbcpb_aead =
650                         (struct nx_csbcpb *)((u8 *)nx_ctx->out_sg +
651                                              NX_PAGE_SIZE);
652 
653         /* give each context a pointer to global stats and their OF
654          * properties */
655         nx_ctx->stats = &nx_driver.stats;
656         memcpy(nx_ctx->props, nx_driver.of.ap[fc][mode],
657                sizeof(struct alg_props) * 3);
658 
659         return 0;
660 }
661 
662 /* entry points from the crypto tfm initializers */
663 int nx_crypto_ctx_aes_ccm_init(struct crypto_tfm *tfm)
664 {
665         return nx_crypto_ctx_init(crypto_tfm_ctx(tfm), NX_FC_AES,
666                                   NX_MODE_AES_CCM);
667 }
668 
669 int nx_crypto_ctx_aes_gcm_init(struct crypto_tfm *tfm)
670 {
671         return nx_crypto_ctx_init(crypto_tfm_ctx(tfm), NX_FC_AES,
672                                   NX_MODE_AES_GCM);
673 }
674 
675 int nx_crypto_ctx_aes_ctr_init(struct crypto_tfm *tfm)
676 {
677         return nx_crypto_ctx_init(crypto_tfm_ctx(tfm), NX_FC_AES,
678                                   NX_MODE_AES_CTR);
679 }
680 
681 int nx_crypto_ctx_aes_cbc_init(struct crypto_tfm *tfm)
682 {
683         return nx_crypto_ctx_init(crypto_tfm_ctx(tfm), NX_FC_AES,
684                                   NX_MODE_AES_CBC);
685 }
686 
687 int nx_crypto_ctx_aes_ecb_init(struct crypto_tfm *tfm)
688 {
689         return nx_crypto_ctx_init(crypto_tfm_ctx(tfm), NX_FC_AES,
690                                   NX_MODE_AES_ECB);
691 }
692 
693 int nx_crypto_ctx_sha_init(struct crypto_tfm *tfm)
694 {
695         return nx_crypto_ctx_init(crypto_tfm_ctx(tfm), NX_FC_SHA, NX_MODE_SHA);
696 }
697 
698 int nx_crypto_ctx_aes_xcbc_init(struct crypto_tfm *tfm)
699 {
700         return nx_crypto_ctx_init(crypto_tfm_ctx(tfm), NX_FC_AES,
701                                   NX_MODE_AES_XCBC_MAC);
702 }
703 
704 /**
705  * nx_crypto_ctx_exit - destroy a crypto api context
706  *
707  * @tfm: the crypto transform pointer for the context
708  *
709  * As crypto API contexts are destroyed, this exit hook is called to free the
710  * memory associated with it.
711  */
712 void nx_crypto_ctx_exit(struct crypto_tfm *tfm)
713 {
714         struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(tfm);
715 
716         kzfree(nx_ctx->kmem);
717         nx_ctx->csbcpb = NULL;
718         nx_ctx->csbcpb_aead = NULL;
719         nx_ctx->in_sg = NULL;
720         nx_ctx->out_sg = NULL;
721 }
722 
723 static int nx_probe(struct vio_dev *viodev, const struct vio_device_id *id)
724 {
725         dev_dbg(&viodev->dev, "driver probed: %s resource id: 0x%x\n",
726                 viodev->name, viodev->resource_id);
727 
728         if (nx_driver.viodev) {
729                 dev_err(&viodev->dev, "%s: Attempt to register more than one "
730                         "instance of the hardware\n", __func__);
731                 return -EINVAL;
732         }
733 
734         nx_driver.viodev = viodev;
735 
736         nx_of_init(&viodev->dev, &nx_driver.of);
737 
738         return nx_register_algs();
739 }
740 
741 static int nx_remove(struct vio_dev *viodev)
742 {
743         dev_dbg(&viodev->dev, "entering nx_remove for UA 0x%x\n",
744                 viodev->unit_address);
745 
746         if (nx_driver.of.status == NX_OKAY) {
747                 NX_DEBUGFS_FINI(&nx_driver);
748 
749                 crypto_unregister_alg(&nx_ccm_aes_alg);
750                 crypto_unregister_alg(&nx_ccm4309_aes_alg);
751                 crypto_unregister_alg(&nx_gcm_aes_alg);
752                 crypto_unregister_alg(&nx_gcm4106_aes_alg);
753                 crypto_unregister_alg(&nx_ctr_aes_alg);
754                 crypto_unregister_alg(&nx_ctr3686_aes_alg);
755                 crypto_unregister_alg(&nx_cbc_aes_alg);
756                 crypto_unregister_alg(&nx_ecb_aes_alg);
757                 crypto_unregister_shash(&nx_shash_sha256_alg);
758                 crypto_unregister_shash(&nx_shash_sha512_alg);
759                 crypto_unregister_shash(&nx_shash_aes_xcbc_alg);
760         }
761 
762         return 0;
763 }
764 
765 
766 /* module wide initialization/cleanup */
767 static int __init nx_init(void)
768 {
769         return vio_register_driver(&nx_driver.viodriver);
770 }
771 
772 static void __exit nx_fini(void)
773 {
774         vio_unregister_driver(&nx_driver.viodriver);
775 }
776 
777 static struct vio_device_id nx_crypto_driver_ids[] = {
778         { "ibm,sym-encryption-v1", "ibm,sym-encryption" },
779         { "", "" }
780 };
781 MODULE_DEVICE_TABLE(vio, nx_crypto_driver_ids);
782 
783 /* driver state structure */
784 struct nx_crypto_driver nx_driver = {
785         .viodriver = {
786                 .id_table = nx_crypto_driver_ids,
787                 .probe = nx_probe,
788                 .remove = nx_remove,
789                 .name  = NX_NAME,
790         },
791 };
792 
793 module_init(nx_init);
794 module_exit(nx_fini);
795 
796 MODULE_AUTHOR("Kent Yoder <yoder1@us.ibm.com>");
797 MODULE_DESCRIPTION(NX_STRING);
798 MODULE_LICENSE("GPL");
799 MODULE_VERSION(NX_VERSION);
800 

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us