Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
nx.c
Go to the documentation of this file.
1 
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/pSeries_reconfig.h>
37 #include <asm/hvcall.h>
38 #include <asm/vio.h>
39 
40 #include "nx_csbcpb.h"
41 #include "nx.h"
42 
43 
54 int nx_hcall_sync(struct nx_crypto_ctx *nx_ctx,
55  struct vio_pfo_op *op,
56  u32 may_sleep)
57 {
58  int rc, retries = 10;
59  struct vio_dev *viodev = nx_driver.viodev;
60 
61  atomic_inc(&(nx_ctx->stats->sync_ops));
62 
63  do {
64  rc = vio_h_cop_sync(viodev, op);
65  } while ((rc == -EBUSY && !may_sleep && retries--) ||
66  (rc == -EBUSY && may_sleep && cond_resched()));
67 
68  if (rc) {
69  dev_dbg(&viodev->dev, "vio_h_cop_sync failed: rc: %d "
70  "hcall rc: %ld\n", rc, op->hcall_err);
71  atomic_inc(&(nx_ctx->stats->errors));
72  atomic_set(&(nx_ctx->stats->last_error), op->hcall_err);
73  atomic_set(&(nx_ctx->stats->last_error_pid), current->pid);
74  }
75 
76  return rc;
77 }
78 
93 struct nx_sg *nx_build_sg_list(struct nx_sg *sg_head,
94  u8 *start_addr,
95  unsigned int len,
96  u32 sgmax)
97 {
98  unsigned int sg_len = 0;
99  struct nx_sg *sg;
100  u64 sg_addr = (u64)start_addr;
101  u64 end_addr;
102 
103  /* determine the start and end for this address range - slightly
104  * different if this is in VMALLOC_REGION */
105  if (is_vmalloc_addr(start_addr))
106  sg_addr = page_to_phys(vmalloc_to_page(start_addr))
107  + offset_in_page(sg_addr);
108  else
109  sg_addr = __pa(sg_addr);
110 
111  end_addr = sg_addr + len;
112 
113  /* each iteration will write one struct nx_sg element and add the
114  * length of data described by that element to sg_len. Once @len bytes
115  * have been described (or @sgmax elements have been written), the
116  * loop ends. min_t is used to ensure @end_addr falls on the same page
117  * as sg_addr, if not, we need to create another nx_sg element for the
118  * data on the next page */
119  for (sg = sg_head; sg_len < len; sg++) {
120  sg->addr = sg_addr;
121  sg_addr = min_t(u64, NX_PAGE_NUM(sg_addr + NX_PAGE_SIZE), end_addr);
122  sg->len = sg_addr - sg->addr;
123  sg_len += sg->len;
124 
125  if ((sg - sg_head) == sgmax) {
126  pr_err("nx: scatter/gather list overflow, pid: %d\n",
127  current->pid);
128  return NULL;
129  }
130  }
131 
132  /* return the moved sg_head pointer */
133  return sg;
134 }
135 
145 struct nx_sg *nx_walk_and_build(struct nx_sg *nx_dst,
146  unsigned int sglen,
147  struct scatterlist *sg_src,
148  unsigned int start,
149  unsigned int src_len)
150 {
151  struct scatter_walk walk;
152  struct nx_sg *nx_sg = nx_dst;
153  unsigned int n, offset = 0, len = src_len;
154  char *dst;
155 
156  /* we need to fast forward through @start bytes first */
157  for (;;) {
158  scatterwalk_start(&walk, sg_src);
159 
160  if (start < offset + sg_src->length)
161  break;
162 
163  offset += sg_src->length;
164  sg_src = scatterwalk_sg_next(sg_src);
165  }
166 
167  /* start - offset is the number of bytes to advance in the scatterlist
168  * element we're currently looking at */
169  scatterwalk_advance(&walk, start - offset);
170 
171  while (len && nx_sg) {
172  n = scatterwalk_clamp(&walk, len);
173  if (!n) {
174  scatterwalk_start(&walk, sg_next(walk.sg));
175  n = scatterwalk_clamp(&walk, len);
176  }
177  dst = scatterwalk_map(&walk);
178 
179  nx_sg = nx_build_sg_list(nx_sg, dst, n, sglen);
180  len -= n;
181 
182  scatterwalk_unmap(dst);
183  scatterwalk_advance(&walk, n);
185  }
186 
187  /* return the moved destination pointer */
188  return nx_sg;
189 }
190 
206 int nx_build_sg_lists(struct nx_crypto_ctx *nx_ctx,
207  struct blkcipher_desc *desc,
208  struct scatterlist *dst,
209  struct scatterlist *src,
210  unsigned int nbytes,
211  u8 *iv)
212 {
213  struct nx_sg *nx_insg = nx_ctx->in_sg;
214  struct nx_sg *nx_outsg = nx_ctx->out_sg;
215  struct blkcipher_walk walk;
216  int rc;
217 
218  blkcipher_walk_init(&walk, dst, src, nbytes);
219  rc = blkcipher_walk_virt_block(desc, &walk, AES_BLOCK_SIZE);
220  if (rc)
221  goto out;
222 
223  if (iv)
224  memcpy(iv, walk.iv, AES_BLOCK_SIZE);
225 
226  while (walk.nbytes) {
227  nx_insg = nx_build_sg_list(nx_insg, walk.src.virt.addr,
228  walk.nbytes, nx_ctx->ap->sglen);
229  nx_outsg = nx_build_sg_list(nx_outsg, walk.dst.virt.addr,
230  walk.nbytes, nx_ctx->ap->sglen);
231 
232  rc = blkcipher_walk_done(desc, &walk, 0);
233  if (rc)
234  break;
235  }
236 
237  if (walk.nbytes) {
238  nx_insg = nx_build_sg_list(nx_insg, walk.src.virt.addr,
239  walk.nbytes, nx_ctx->ap->sglen);
240  nx_outsg = nx_build_sg_list(nx_outsg, walk.dst.virt.addr,
241  walk.nbytes, nx_ctx->ap->sglen);
242 
243  rc = 0;
244  }
245 
246  /* these lengths should be negative, which will indicate to phyp that
247  * the input and output parameters are scatterlists, not linear
248  * buffers */
249  nx_ctx->op.inlen = (nx_ctx->in_sg - nx_insg) * sizeof(struct nx_sg);
250  nx_ctx->op.outlen = (nx_ctx->out_sg - nx_outsg) * sizeof(struct nx_sg);
251 out:
252  return rc;
253 }
254 
261 void nx_ctx_init(struct nx_crypto_ctx *nx_ctx, unsigned int function)
262 {
263  memset(nx_ctx->kmem, 0, nx_ctx->kmem_len);
264  nx_ctx->csbcpb->csb.valid |= NX_CSB_VALID_BIT;
265 
266  nx_ctx->op.flags = function;
267  nx_ctx->op.csbcpb = __pa(nx_ctx->csbcpb);
268  nx_ctx->op.in = __pa(nx_ctx->in_sg);
269  nx_ctx->op.out = __pa(nx_ctx->out_sg);
270 
271  if (nx_ctx->csbcpb_aead) {
272  nx_ctx->csbcpb_aead->csb.valid |= NX_CSB_VALID_BIT;
273 
274  nx_ctx->op_aead.flags = function;
275  nx_ctx->op_aead.csbcpb = __pa(nx_ctx->csbcpb_aead);
276  nx_ctx->op_aead.in = __pa(nx_ctx->in_sg);
277  nx_ctx->op_aead.out = __pa(nx_ctx->out_sg);
278  }
279 }
280 
281 static void nx_of_update_status(struct device *dev,
282  struct property *p,
283  struct nx_of *props)
284 {
285  if (!strncmp(p->value, "okay", p->length)) {
286  props->status = NX_WAITING;
287  props->flags |= NX_OF_FLAG_STATUS_SET;
288  } else {
289  dev_info(dev, "%s: status '%s' is not 'okay'\n", __func__,
290  (char *)p->value);
291  }
292 }
293 
294 static void nx_of_update_sglen(struct device *dev,
295  struct property *p,
296  struct nx_of *props)
297 {
298  if (p->length != sizeof(props->max_sg_len)) {
299  dev_err(dev, "%s: unexpected format for "
300  "ibm,max-sg-len property\n", __func__);
301  dev_dbg(dev, "%s: ibm,max-sg-len is %d bytes "
302  "long, expected %zd bytes\n", __func__,
303  p->length, sizeof(props->max_sg_len));
304  return;
305  }
306 
307  props->max_sg_len = *(u32 *)p->value;
308  props->flags |= NX_OF_FLAG_MAXSGLEN_SET;
309 }
310 
311 static void nx_of_update_msc(struct device *dev,
312  struct property *p,
313  struct nx_of *props)
314 {
315  struct msc_triplet *trip;
316  struct max_sync_cop *msc;
317  unsigned int bytes_so_far, i, lenp;
318 
319  msc = (struct max_sync_cop *)p->value;
320  lenp = p->length;
321 
322  /* You can't tell if the data read in for this property is sane by its
323  * size alone. This is because there are sizes embedded in the data
324  * structure. The best we can do is check lengths as we parse and bail
325  * as soon as a length error is detected. */
326  bytes_so_far = 0;
327 
328  while ((bytes_so_far + sizeof(struct max_sync_cop)) <= lenp) {
329  bytes_so_far += sizeof(struct max_sync_cop);
330 
331  trip = msc->trip;
332 
333  for (i = 0;
334  ((bytes_so_far + sizeof(struct msc_triplet)) <= lenp) &&
335  i < msc->triplets;
336  i++) {
337  if (msc->fc > NX_MAX_FC || msc->mode > NX_MAX_MODE) {
338  dev_err(dev, "unknown function code/mode "
339  "combo: %d/%d (ignored)\n", msc->fc,
340  msc->mode);
341  goto next_loop;
342  }
343 
344  switch (trip->keybitlen) {
345  case 128:
346  case 160:
347  props->ap[msc->fc][msc->mode][0].databytelen =
348  trip->databytelen;
349  props->ap[msc->fc][msc->mode][0].sglen =
350  trip->sglen;
351  break;
352  case 192:
353  props->ap[msc->fc][msc->mode][1].databytelen =
354  trip->databytelen;
355  props->ap[msc->fc][msc->mode][1].sglen =
356  trip->sglen;
357  break;
358  case 256:
359  if (msc->fc == NX_FC_AES) {
360  props->ap[msc->fc][msc->mode][2].
361  databytelen = trip->databytelen;
362  props->ap[msc->fc][msc->mode][2].sglen =
363  trip->sglen;
364  } else if (msc->fc == NX_FC_AES_HMAC ||
365  msc->fc == NX_FC_SHA) {
366  props->ap[msc->fc][msc->mode][1].
367  databytelen = trip->databytelen;
368  props->ap[msc->fc][msc->mode][1].sglen =
369  trip->sglen;
370  } else {
371  dev_warn(dev, "unknown function "
372  "code/key bit len combo"
373  ": (%u/256)\n", msc->fc);
374  }
375  break;
376  case 512:
377  props->ap[msc->fc][msc->mode][2].databytelen =
378  trip->databytelen;
379  props->ap[msc->fc][msc->mode][2].sglen =
380  trip->sglen;
381  break;
382  default:
383  dev_warn(dev, "unknown function code/key bit "
384  "len combo: (%u/%u)\n", msc->fc,
385  trip->keybitlen);
386  break;
387  }
388 next_loop:
389  bytes_so_far += sizeof(struct msc_triplet);
390  trip++;
391  }
392 
393  msc = (struct max_sync_cop *)trip;
394  }
395 
397 }
398 
410 static void nx_of_init(struct device *dev, struct nx_of *props)
411 {
412  struct device_node *base_node = dev->of_node;
413  struct property *p;
414 
415  p = of_find_property(base_node, "status", NULL);
416  if (!p)
417  dev_info(dev, "%s: property 'status' not found\n", __func__);
418  else
419  nx_of_update_status(dev, p, props);
420 
421  p = of_find_property(base_node, "ibm,max-sg-len", NULL);
422  if (!p)
423  dev_info(dev, "%s: property 'ibm,max-sg-len' not found\n",
424  __func__);
425  else
426  nx_of_update_sglen(dev, p, props);
427 
428  p = of_find_property(base_node, "ibm,max-sync-cop", NULL);
429  if (!p)
430  dev_info(dev, "%s: property 'ibm,max-sync-cop' not found\n",
431  __func__);
432  else
433  nx_of_update_msc(dev, p, props);
434 }
435 
445 static int nx_register_algs(void)
446 {
447  int rc = -1;
448 
449  if (nx_driver.of.flags != NX_OF_FLAG_MASK_READY)
450  goto out;
451 
452  memset(&nx_driver.stats, 0, sizeof(struct nx_stats));
453 
454  rc = NX_DEBUGFS_INIT(&nx_driver);
455  if (rc)
456  goto out;
457 
459  if (rc)
460  goto out;
461 
463  if (rc)
464  goto out_unreg_ecb;
465 
467  if (rc)
468  goto out_unreg_cbc;
469 
471  if (rc)
472  goto out_unreg_ctr;
473 
475  if (rc)
476  goto out_unreg_ctr3686;
477 
479  if (rc)
480  goto out_unreg_gcm;
481 
483  if (rc)
484  goto out_unreg_gcm4106;
485 
487  if (rc)
488  goto out_unreg_ccm;
489 
491  if (rc)
492  goto out_unreg_ccm4309;
493 
495  if (rc)
496  goto out_unreg_s256;
497 
499  if (rc)
500  goto out_unreg_s512;
501 
502  nx_driver.of.status = NX_OKAY;
503 
504  goto out;
505 
506 out_unreg_s512:
508 out_unreg_s256:
510 out_unreg_ccm4309:
512 out_unreg_ccm:
514 out_unreg_gcm4106:
516 out_unreg_gcm:
518 out_unreg_ctr3686:
520 out_unreg_ctr:
522 out_unreg_cbc:
524 out_unreg_ecb:
526 out:
527  return rc;
528 }
529 
537 static int nx_crypto_ctx_init(struct nx_crypto_ctx *nx_ctx, u32 fc, u32 mode)
538 {
539  if (nx_driver.of.status != NX_OKAY) {
540  pr_err("Attempt to initialize NX crypto context while device "
541  "is not available!\n");
542  return -ENODEV;
543  }
544 
545  /* we need an extra page for csbcpb_aead for these modes */
546  if (mode == NX_MODE_AES_GCM || mode == NX_MODE_AES_CCM)
547  nx_ctx->kmem_len = (4 * NX_PAGE_SIZE) +
548  sizeof(struct nx_csbcpb);
549  else
550  nx_ctx->kmem_len = (3 * NX_PAGE_SIZE) +
551  sizeof(struct nx_csbcpb);
552 
553  nx_ctx->kmem = kmalloc(nx_ctx->kmem_len, GFP_KERNEL);
554  if (!nx_ctx->kmem)
555  return -ENOMEM;
556 
557  /* the csbcpb and scatterlists must be 4K aligned pages */
558  nx_ctx->csbcpb = (struct nx_csbcpb *)(round_up((u64)nx_ctx->kmem,
559  (u64)NX_PAGE_SIZE));
560  nx_ctx->in_sg = (struct nx_sg *)((u8 *)nx_ctx->csbcpb + NX_PAGE_SIZE);
561  nx_ctx->out_sg = (struct nx_sg *)((u8 *)nx_ctx->in_sg + NX_PAGE_SIZE);
562 
563  if (mode == NX_MODE_AES_GCM || mode == NX_MODE_AES_CCM)
564  nx_ctx->csbcpb_aead =
565  (struct nx_csbcpb *)((u8 *)nx_ctx->out_sg +
566  NX_PAGE_SIZE);
567 
568  /* give each context a pointer to global stats and their OF
569  * properties */
570  nx_ctx->stats = &nx_driver.stats;
571  memcpy(nx_ctx->props, nx_driver.of.ap[fc][mode],
572  sizeof(struct alg_props) * 3);
573 
574  return 0;
575 }
576 
577 /* entry points from the crypto tfm initializers */
579 {
580  return nx_crypto_ctx_init(crypto_tfm_ctx(tfm), NX_FC_AES,
582 }
583 
585 {
586  return nx_crypto_ctx_init(crypto_tfm_ctx(tfm), NX_FC_AES,
588 }
589 
591 {
592  return nx_crypto_ctx_init(crypto_tfm_ctx(tfm), NX_FC_AES,
594 }
595 
597 {
598  return nx_crypto_ctx_init(crypto_tfm_ctx(tfm), NX_FC_AES,
600 }
601 
603 {
604  return nx_crypto_ctx_init(crypto_tfm_ctx(tfm), NX_FC_AES,
606 }
607 
609 {
610  return nx_crypto_ctx_init(crypto_tfm_ctx(tfm), NX_FC_SHA, NX_MODE_SHA);
611 }
612 
614 {
615  return nx_crypto_ctx_init(crypto_tfm_ctx(tfm), NX_FC_AES,
617 }
618 
628 {
629  struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(tfm);
630 
631  kzfree(nx_ctx->kmem);
632  nx_ctx->csbcpb = NULL;
633  nx_ctx->csbcpb_aead = NULL;
634  nx_ctx->in_sg = NULL;
635  nx_ctx->out_sg = NULL;
636 }
637 
638 static int __devinit nx_probe(struct vio_dev *viodev,
639  const struct vio_device_id *id)
640 {
641  dev_dbg(&viodev->dev, "driver probed: %s resource id: 0x%x\n",
642  viodev->name, viodev->resource_id);
643 
644  if (nx_driver.viodev) {
645  dev_err(&viodev->dev, "%s: Attempt to register more than one "
646  "instance of the hardware\n", __func__);
647  return -EINVAL;
648  }
649 
650  nx_driver.viodev = viodev;
651 
652  nx_of_init(&viodev->dev, &nx_driver.of);
653 
654  return nx_register_algs();
655 }
656 
657 static int __devexit nx_remove(struct vio_dev *viodev)
658 {
659  dev_dbg(&viodev->dev, "entering nx_remove for UA 0x%x\n",
660  viodev->unit_address);
661 
662  if (nx_driver.of.status == NX_OKAY) {
664 
676  }
677 
678  return 0;
679 }
680 
681 
682 /* module wide initialization/cleanup */
683 static int __init nx_init(void)
684 {
685  return vio_register_driver(&nx_driver.viodriver);
686 }
687 
688 static void __exit nx_fini(void)
689 {
690  vio_unregister_driver(&nx_driver.viodriver);
691 }
692 
693 static struct vio_device_id nx_crypto_driver_ids[] __devinitdata = {
694  { "ibm,sym-encryption-v1", "ibm,sym-encryption" },
695  { "", "" }
696 };
697 MODULE_DEVICE_TABLE(vio, nx_crypto_driver_ids);
698 
699 /* driver state structure */
701  .viodriver = {
702  .id_table = nx_crypto_driver_ids,
703  .probe = nx_probe,
704  .remove = nx_remove,
705  .name = NX_NAME,
706  },
707 };
708 
709 module_init(nx_init);
710 module_exit(nx_fini);
711 
712 MODULE_AUTHOR("Kent Yoder <[email protected]>");
714 MODULE_LICENSE("GPL");