sg_sw_sec4.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * CAAM/SEC 4.x functions for using scatterlists in caam driver
  3. *
  4. * Copyright 2008-2011 Freescale Semiconductor, Inc.
  5. *
  6. */
  7. struct sec4_sg_entry;
  8. /*
  9. * convert single dma address to h/w link table format
  10. */
  11. static inline void dma_to_sec4_sg_one(struct sec4_sg_entry *sec4_sg_ptr,
  12. dma_addr_t dma, u32 len, u32 offset)
  13. {
  14. sec4_sg_ptr->ptr = dma;
  15. sec4_sg_ptr->len = len;
  16. sec4_sg_ptr->reserved = 0;
  17. sec4_sg_ptr->buf_pool_id = 0;
  18. sec4_sg_ptr->offset = offset;
  19. #ifdef DEBUG
  20. print_hex_dump(KERN_ERR, "sec4_sg_ptr@: ",
  21. DUMP_PREFIX_ADDRESS, 16, 4, sec4_sg_ptr,
  22. sizeof(struct sec4_sg_entry), 1);
  23. #endif
  24. }
  25. /*
  26. * convert scatterlist to h/w link table format
  27. * but does not have final bit; instead, returns last entry
  28. */
  29. static inline struct sec4_sg_entry *
  30. sg_to_sec4_sg(struct scatterlist *sg, int sg_count,
  31. struct sec4_sg_entry *sec4_sg_ptr, u32 offset)
  32. {
  33. while (sg_count) {
  34. dma_to_sec4_sg_one(sec4_sg_ptr, sg_dma_address(sg),
  35. sg_dma_len(sg), offset);
  36. sec4_sg_ptr++;
  37. sg = sg_next(sg);
  38. sg_count--;
  39. }
  40. return sec4_sg_ptr - 1;
  41. }
  42. /*
  43. * convert scatterlist to h/w link table format
  44. * scatterlist must have been previously dma mapped
  45. */
  46. static inline void sg_to_sec4_sg_last(struct scatterlist *sg, int sg_count,
  47. struct sec4_sg_entry *sec4_sg_ptr,
  48. u32 offset)
  49. {
  50. sec4_sg_ptr = sg_to_sec4_sg(sg, sg_count, sec4_sg_ptr, offset);
  51. sec4_sg_ptr->len |= SEC4_SG_LEN_FIN;
  52. }
  53. /* count number of elements in scatterlist */
  54. static inline int __sg_count(struct scatterlist *sg_list, int nbytes)
  55. {
  56. struct scatterlist *sg = sg_list;
  57. int sg_nents = 0;
  58. while (nbytes > 0) {
  59. sg_nents++;
  60. nbytes -= sg->length;
  61. if (!sg_is_last(sg) && (sg + 1)->length == 0)
  62. BUG(); /* Not support chaining */
  63. sg = scatterwalk_sg_next(sg);
  64. }
  65. return sg_nents;
  66. }
  67. /* derive number of elements in scatterlist, but return 0 for 1 */
  68. static inline int sg_count(struct scatterlist *sg_list, int nbytes)
  69. {
  70. int sg_nents = __sg_count(sg_list, nbytes);
  71. if (likely(sg_nents == 1))
  72. return 0;
  73. return sg_nents;
  74. }