cffrml.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * CAIF Framing Layer.
  3. *
  4. * Copyright (C) ST-Ericsson AB 2010
  5. * Author: Sjur Brendeland
  6. * License terms: GNU General Public License (GPL) version 2
  7. */
  8. #define pr_fmt(fmt) KBUILD_MODNAME ":%s(): " fmt, __func__
  9. #include <linux/stddef.h>
  10. #include <linux/spinlock.h>
  11. #include <linux/slab.h>
  12. #include <linux/crc-ccitt.h>
  13. #include <linux/netdevice.h>
  14. #include <net/caif/caif_layer.h>
  15. #include <net/caif/cfpkt.h>
  16. #include <net/caif/cffrml.h>
  17. #define container_obj(layr) container_of(layr, struct cffrml, layer)
  18. struct cffrml {
  19. struct cflayer layer;
  20. bool dofcs; /* !< FCS active */
  21. int __percpu *pcpu_refcnt;
  22. };
  23. static int cffrml_receive(struct cflayer *layr, struct cfpkt *pkt);
  24. static int cffrml_transmit(struct cflayer *layr, struct cfpkt *pkt);
  25. static void cffrml_ctrlcmd(struct cflayer *layr, enum caif_ctrlcmd ctrl,
  26. int phyid);
  27. static u32 cffrml_rcv_error;
  28. static u32 cffrml_rcv_checsum_error;
  29. struct cflayer *cffrml_create(u16 phyid, bool use_fcs)
  30. {
  31. struct cffrml *this = kzalloc(sizeof(struct cffrml), GFP_ATOMIC);
  32. if (!this)
  33. return NULL;
  34. this->pcpu_refcnt = alloc_percpu(int);
  35. if (this->pcpu_refcnt == NULL) {
  36. kfree(this);
  37. return NULL;
  38. }
  39. caif_assert(offsetof(struct cffrml, layer) == 0);
  40. this->layer.receive = cffrml_receive;
  41. this->layer.transmit = cffrml_transmit;
  42. this->layer.ctrlcmd = cffrml_ctrlcmd;
  43. snprintf(this->layer.name, CAIF_LAYER_NAME_SZ, "frm%d", phyid);
  44. this->dofcs = use_fcs;
  45. this->layer.id = phyid;
  46. return (struct cflayer *) this;
  47. }
  48. void cffrml_free(struct cflayer *layer)
  49. {
  50. struct cffrml *this = container_obj(layer);
  51. free_percpu(this->pcpu_refcnt);
  52. kfree(layer);
  53. }
  54. void cffrml_set_uplayer(struct cflayer *this, struct cflayer *up)
  55. {
  56. this->up = up;
  57. }
  58. void cffrml_set_dnlayer(struct cflayer *this, struct cflayer *dn)
  59. {
  60. this->dn = dn;
  61. }
  62. static u16 cffrml_checksum(u16 chks, void *buf, u16 len)
  63. {
  64. /* FIXME: FCS should be moved to glue in order to use OS-Specific
  65. * solutions
  66. */
  67. return crc_ccitt(chks, buf, len);
  68. }
  69. static int cffrml_receive(struct cflayer *layr, struct cfpkt *pkt)
  70. {
  71. u16 tmp;
  72. u16 len;
  73. u16 hdrchks;
  74. int pktchks;
  75. struct cffrml *this;
  76. this = container_obj(layr);
  77. cfpkt_extr_head(pkt, &tmp, 2);
  78. len = le16_to_cpu(tmp);
  79. /* Subtract for FCS on length if FCS is not used. */
  80. if (!this->dofcs)
  81. len -= 2;
  82. if (cfpkt_setlen(pkt, len) < 0) {
  83. ++cffrml_rcv_error;
  84. pr_err("Framing length error (%d)\n", len);
  85. cfpkt_destroy(pkt);
  86. return -EPROTO;
  87. }
  88. /*
  89. * Don't do extract if FCS is false, rather do setlen - then we don't
  90. * get a cache-miss.
  91. */
  92. if (this->dofcs) {
  93. cfpkt_extr_trail(pkt, &tmp, 2);
  94. hdrchks = le16_to_cpu(tmp);
  95. pktchks = cfpkt_iterate(pkt, cffrml_checksum, 0xffff);
  96. if (pktchks != hdrchks) {
  97. cfpkt_add_trail(pkt, &tmp, 2);
  98. ++cffrml_rcv_error;
  99. ++cffrml_rcv_checsum_error;
  100. pr_info("Frame checksum error (0x%x != 0x%x)\n",
  101. hdrchks, pktchks);
  102. return -EILSEQ;
  103. }
  104. }
  105. if (cfpkt_erroneous(pkt)) {
  106. ++cffrml_rcv_error;
  107. pr_err("Packet is erroneous!\n");
  108. cfpkt_destroy(pkt);
  109. return -EPROTO;
  110. }
  111. if (layr->up == NULL) {
  112. pr_err("Layr up is missing!\n");
  113. cfpkt_destroy(pkt);
  114. return -EINVAL;
  115. }
  116. return layr->up->receive(layr->up, pkt);
  117. }
  118. static int cffrml_transmit(struct cflayer *layr, struct cfpkt *pkt)
  119. {
  120. u16 chks;
  121. u16 len;
  122. __le16 data;
  123. struct cffrml *this = container_obj(layr);
  124. if (this->dofcs) {
  125. chks = cfpkt_iterate(pkt, cffrml_checksum, 0xffff);
  126. data = cpu_to_le16(chks);
  127. cfpkt_add_trail(pkt, &data, 2);
  128. } else {
  129. cfpkt_pad_trail(pkt, 2);
  130. }
  131. len = cfpkt_getlen(pkt);
  132. data = cpu_to_le16(len);
  133. cfpkt_add_head(pkt, &data, 2);
  134. cfpkt_info(pkt)->hdr_len += 2;
  135. if (cfpkt_erroneous(pkt)) {
  136. pr_err("Packet is erroneous!\n");
  137. cfpkt_destroy(pkt);
  138. return -EPROTO;
  139. }
  140. if (layr->dn == NULL) {
  141. cfpkt_destroy(pkt);
  142. return -ENODEV;
  143. }
  144. return layr->dn->transmit(layr->dn, pkt);
  145. }
  146. static void cffrml_ctrlcmd(struct cflayer *layr, enum caif_ctrlcmd ctrl,
  147. int phyid)
  148. {
  149. if (layr->up && layr->up->ctrlcmd)
  150. layr->up->ctrlcmd(layr->up, ctrl, layr->id);
  151. }
  152. void cffrml_put(struct cflayer *layr)
  153. {
  154. struct cffrml *this = container_obj(layr);
  155. if (layr != NULL && this->pcpu_refcnt != NULL)
  156. this_cpu_dec(*this->pcpu_refcnt);
  157. }
  158. void cffrml_hold(struct cflayer *layr)
  159. {
  160. struct cffrml *this = container_obj(layr);
  161. if (layr != NULL && this->pcpu_refcnt != NULL)
  162. this_cpu_inc(*this->pcpu_refcnt);
  163. }
  164. int cffrml_refcnt_read(struct cflayer *layr)
  165. {
  166. int i, refcnt = 0;
  167. struct cffrml *this = container_obj(layr);
  168. for_each_possible_cpu(i)
  169. refcnt += *per_cpu_ptr(this->pcpu_refcnt, i);
  170. return refcnt;
  171. }