scatterwalk.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Cryptographic API.
  3. *
  4. * Cipher operations.
  5. *
  6. * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
  7. * 2002 Adam J. Richter <adam@yggdrasil.com>
  8. * 2004 Jean-Luc Cooke <jlcooke@certainkey.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the Free
  12. * Software Foundation; either version 2 of the License, or (at your option)
  13. * any later version.
  14. *
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/mm.h>
  18. #include <linux/pagemap.h>
  19. #include <linux/highmem.h>
  20. #include <asm/bug.h>
  21. #include <asm/scatterlist.h>
  22. #include "internal.h"
  23. #include "scatterwalk.h"
  24. enum km_type crypto_km_types[] = {
  25. KM_USER0,
  26. KM_USER1,
  27. KM_SOFTIRQ0,
  28. KM_SOFTIRQ1,
  29. };
  30. static void memcpy_dir(void *buf, void *sgdata, size_t nbytes, int out)
  31. {
  32. if (out)
  33. memcpy(sgdata, buf, nbytes);
  34. else
  35. memcpy(buf, sgdata, nbytes);
  36. }
  37. void scatterwalk_start(struct scatter_walk *walk, struct scatterlist *sg)
  38. {
  39. unsigned int rest_of_page;
  40. walk->sg = sg;
  41. walk->page = sg->page;
  42. walk->len_this_segment = sg->length;
  43. BUG_ON(!sg->length);
  44. rest_of_page = PAGE_CACHE_SIZE - (sg->offset & (PAGE_CACHE_SIZE - 1));
  45. walk->len_this_page = min(sg->length, rest_of_page);
  46. walk->offset = sg->offset;
  47. }
  48. void scatterwalk_map(struct scatter_walk *walk, int out)
  49. {
  50. walk->data = crypto_kmap(walk->page, out) + walk->offset;
  51. }
  52. static inline void scatterwalk_unmap(struct scatter_walk *walk, int out)
  53. {
  54. /* walk->data may be pointing the first byte of the next page;
  55. however, we know we transfered at least one byte. So,
  56. walk->data - 1 will be a virtual address in the mapped page. */
  57. crypto_kunmap(walk->data - 1, out);
  58. }
  59. static void scatterwalk_pagedone(struct scatter_walk *walk, int out,
  60. unsigned int more)
  61. {
  62. if (out)
  63. flush_dcache_page(walk->page);
  64. if (more) {
  65. walk->len_this_segment -= walk->len_this_page;
  66. if (walk->len_this_segment) {
  67. walk->page++;
  68. walk->len_this_page = min(walk->len_this_segment,
  69. (unsigned)PAGE_CACHE_SIZE);
  70. walk->offset = 0;
  71. }
  72. else
  73. scatterwalk_start(walk, sg_next(walk->sg));
  74. }
  75. }
  76. void scatterwalk_done(struct scatter_walk *walk, int out, int more)
  77. {
  78. scatterwalk_unmap(walk, out);
  79. if (walk->len_this_page == 0 || !more)
  80. scatterwalk_pagedone(walk, out, more);
  81. }
  82. /*
  83. * Do not call this unless the total length of all of the fragments
  84. * has been verified as multiple of the block size.
  85. */
  86. int scatterwalk_copychunks(void *buf, struct scatter_walk *walk,
  87. size_t nbytes, int out)
  88. {
  89. while (nbytes > walk->len_this_page) {
  90. memcpy_dir(buf, walk->data, walk->len_this_page, out);
  91. buf += walk->len_this_page;
  92. nbytes -= walk->len_this_page;
  93. scatterwalk_unmap(walk, out);
  94. scatterwalk_pagedone(walk, out, 1);
  95. scatterwalk_map(walk, out);
  96. }
  97. memcpy_dir(buf, walk->data, nbytes, out);
  98. return nbytes;
  99. }