fc_libfc.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright(c) 2009 Intel Corporation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with
  14. * this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  16. *
  17. * Maintained at www.Open-FCoE.org
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/types.h>
  21. #include <linux/scatterlist.h>
  22. #include <linux/crc32.h>
  23. #include <scsi/libfc.h>
  24. #include "fc_libfc.h"
  25. MODULE_AUTHOR("Open-FCoE.org");
  26. MODULE_DESCRIPTION("libfc");
  27. MODULE_LICENSE("GPL v2");
  28. unsigned int fc_debug_logging;
  29. module_param_named(debug_logging, fc_debug_logging, int, S_IRUGO|S_IWUSR);
  30. MODULE_PARM_DESC(debug_logging, "a bit mask of logging levels");
  31. /**
  32. * libfc_init() - Initialize libfc.ko
  33. */
  34. static int __init libfc_init(void)
  35. {
  36. int rc = 0;
  37. rc = fc_setup_fcp();
  38. if (rc)
  39. return rc;
  40. rc = fc_setup_exch_mgr();
  41. if (rc)
  42. goto destroy_pkt_cache;
  43. rc = fc_setup_rport();
  44. if (rc)
  45. goto destroy_em;
  46. return rc;
  47. destroy_em:
  48. fc_destroy_exch_mgr();
  49. destroy_pkt_cache:
  50. fc_destroy_fcp();
  51. return rc;
  52. }
  53. module_init(libfc_init);
  54. /**
  55. * libfc_exit() - Tear down libfc.ko
  56. */
  57. static void __exit libfc_exit(void)
  58. {
  59. fc_destroy_fcp();
  60. fc_destroy_exch_mgr();
  61. fc_destroy_rport();
  62. }
  63. module_exit(libfc_exit);
  64. /**
  65. * fc_copy_buffer_to_sglist() - This routine copies the data of a buffer
  66. * into a scatter-gather list (SG list).
  67. *
  68. * @buf: pointer to the data buffer.
  69. * @len: the byte-length of the data buffer.
  70. * @sg: pointer to the pointer of the SG list.
  71. * @nents: pointer to the remaining number of entries in the SG list.
  72. * @offset: pointer to the current offset in the SG list.
  73. * @km_type: dedicated page table slot type for kmap_atomic.
  74. * @crc: pointer to the 32-bit crc value.
  75. * If crc is NULL, CRC is not calculated.
  76. */
  77. u32 fc_copy_buffer_to_sglist(void *buf, size_t len,
  78. struct scatterlist *sg,
  79. u32 *nents, size_t *offset,
  80. enum km_type km_type, u32 *crc)
  81. {
  82. size_t remaining = len;
  83. u32 copy_len = 0;
  84. while (remaining > 0 && sg) {
  85. size_t off, sg_bytes;
  86. void *page_addr;
  87. if (*offset >= sg->length) {
  88. /*
  89. * Check for end and drop resources
  90. * from the last iteration.
  91. */
  92. if (!(*nents))
  93. break;
  94. --(*nents);
  95. *offset -= sg->length;
  96. sg = sg_next(sg);
  97. continue;
  98. }
  99. sg_bytes = min(remaining, sg->length - *offset);
  100. /*
  101. * The scatterlist item may be bigger than PAGE_SIZE,
  102. * but we are limited to mapping PAGE_SIZE at a time.
  103. */
  104. off = *offset + sg->offset;
  105. sg_bytes = min(sg_bytes,
  106. (size_t)(PAGE_SIZE - (off & ~PAGE_MASK)));
  107. page_addr = kmap_atomic(sg_page(sg) + (off >> PAGE_SHIFT),
  108. km_type);
  109. if (crc)
  110. *crc = crc32(*crc, buf, sg_bytes);
  111. memcpy((char *)page_addr + (off & ~PAGE_MASK), buf, sg_bytes);
  112. kunmap_atomic(page_addr, km_type);
  113. buf += sg_bytes;
  114. *offset += sg_bytes;
  115. remaining -= sg_bytes;
  116. copy_len += sg_bytes;
  117. }
  118. return copy_len;
  119. }