sg_sw_sec4.h 2.1 KB

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