cfmuxl.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * Copyright (C) ST-Ericsson AB 2010
  3. * Author: Sjur Brendeland/sjur.brandeland@stericsson.com
  4. * License terms: GNU General Public License (GPL) version 2
  5. */
  6. #define pr_fmt(fmt) KBUILD_MODNAME ":%s(): " fmt, __func__
  7. #include <linux/stddef.h>
  8. #include <linux/spinlock.h>
  9. #include <linux/slab.h>
  10. #include <net/caif/cfpkt.h>
  11. #include <net/caif/cfmuxl.h>
  12. #include <net/caif/cfsrvl.h>
  13. #include <net/caif/cffrml.h>
  14. #define container_obj(layr) container_of(layr, struct cfmuxl, layer)
  15. #define CAIF_CTRL_CHANNEL 0
  16. #define UP_CACHE_SIZE 8
  17. #define DN_CACHE_SIZE 8
  18. struct cfmuxl {
  19. struct cflayer layer;
  20. struct list_head srvl_list;
  21. struct list_head frml_list;
  22. struct cflayer *up_cache[UP_CACHE_SIZE];
  23. struct cflayer *dn_cache[DN_CACHE_SIZE];
  24. /*
  25. * Set when inserting or removing downwards layers.
  26. */
  27. spinlock_t transmit_lock;
  28. /*
  29. * Set when inserting or removing upwards layers.
  30. */
  31. spinlock_t receive_lock;
  32. };
  33. static int cfmuxl_receive(struct cflayer *layr, struct cfpkt *pkt);
  34. static int cfmuxl_transmit(struct cflayer *layr, struct cfpkt *pkt);
  35. static void cfmuxl_ctrlcmd(struct cflayer *layr, enum caif_ctrlcmd ctrl,
  36. int phyid);
  37. static struct cflayer *get_up(struct cfmuxl *muxl, u16 id);
  38. struct cflayer *cfmuxl_create(void)
  39. {
  40. struct cfmuxl *this = kmalloc(sizeof(struct cfmuxl), GFP_ATOMIC);
  41. if (!this)
  42. return NULL;
  43. memset(this, 0, sizeof(*this));
  44. this->layer.receive = cfmuxl_receive;
  45. this->layer.transmit = cfmuxl_transmit;
  46. this->layer.ctrlcmd = cfmuxl_ctrlcmd;
  47. INIT_LIST_HEAD(&this->srvl_list);
  48. INIT_LIST_HEAD(&this->frml_list);
  49. spin_lock_init(&this->transmit_lock);
  50. spin_lock_init(&this->receive_lock);
  51. snprintf(this->layer.name, CAIF_LAYER_NAME_SZ, "mux");
  52. return &this->layer;
  53. }
  54. int cfmuxl_set_uplayer(struct cflayer *layr, struct cflayer *up, u8 linkid)
  55. {
  56. struct cfmuxl *muxl = container_obj(layr);
  57. spin_lock(&muxl->receive_lock);
  58. cfsrvl_get(up);
  59. list_add(&up->node, &muxl->srvl_list);
  60. spin_unlock(&muxl->receive_lock);
  61. return 0;
  62. }
  63. bool cfmuxl_is_phy_inuse(struct cflayer *layr, u8 phyid)
  64. {
  65. struct list_head *node;
  66. struct cflayer *layer;
  67. struct cfmuxl *muxl = container_obj(layr);
  68. bool match = false;
  69. spin_lock(&muxl->receive_lock);
  70. list_for_each(node, &muxl->srvl_list) {
  71. layer = list_entry(node, struct cflayer, node);
  72. if (cfsrvl_phyid_match(layer, phyid)) {
  73. match = true;
  74. break;
  75. }
  76. }
  77. spin_unlock(&muxl->receive_lock);
  78. return match;
  79. }
  80. u8 cfmuxl_get_phyid(struct cflayer *layr, u8 channel_id)
  81. {
  82. struct cflayer *up;
  83. int phyid;
  84. struct cfmuxl *muxl = container_obj(layr);
  85. spin_lock(&muxl->receive_lock);
  86. up = get_up(muxl, channel_id);
  87. if (up != NULL)
  88. phyid = cfsrvl_getphyid(up);
  89. else
  90. phyid = 0;
  91. spin_unlock(&muxl->receive_lock);
  92. return phyid;
  93. }
  94. int cfmuxl_set_dnlayer(struct cflayer *layr, struct cflayer *dn, u8 phyid)
  95. {
  96. struct cfmuxl *muxl = (struct cfmuxl *) layr;
  97. spin_lock(&muxl->transmit_lock);
  98. list_add(&dn->node, &muxl->frml_list);
  99. spin_unlock(&muxl->transmit_lock);
  100. return 0;
  101. }
  102. static struct cflayer *get_from_id(struct list_head *list, u16 id)
  103. {
  104. struct list_head *node;
  105. struct cflayer *layer;
  106. list_for_each(node, list) {
  107. layer = list_entry(node, struct cflayer, node);
  108. if (layer->id == id)
  109. return layer;
  110. }
  111. return NULL;
  112. }
  113. struct cflayer *cfmuxl_remove_dnlayer(struct cflayer *layr, u8 phyid)
  114. {
  115. struct cfmuxl *muxl = container_obj(layr);
  116. struct cflayer *dn;
  117. spin_lock(&muxl->transmit_lock);
  118. memset(muxl->dn_cache, 0, sizeof(muxl->dn_cache));
  119. dn = get_from_id(&muxl->frml_list, phyid);
  120. if (dn == NULL) {
  121. spin_unlock(&muxl->transmit_lock);
  122. return NULL;
  123. }
  124. list_del(&dn->node);
  125. caif_assert(dn != NULL);
  126. spin_unlock(&muxl->transmit_lock);
  127. return dn;
  128. }
  129. /* Invariant: lock is taken */
  130. static struct cflayer *get_up(struct cfmuxl *muxl, u16 id)
  131. {
  132. struct cflayer *up;
  133. int idx = id % UP_CACHE_SIZE;
  134. up = muxl->up_cache[idx];
  135. if (up == NULL || up->id != id) {
  136. up = get_from_id(&muxl->srvl_list, id);
  137. muxl->up_cache[idx] = up;
  138. }
  139. return up;
  140. }
  141. /* Invariant: lock is taken */
  142. static struct cflayer *get_dn(struct cfmuxl *muxl, struct dev_info *dev_info)
  143. {
  144. struct cflayer *dn;
  145. int idx = dev_info->id % DN_CACHE_SIZE;
  146. dn = muxl->dn_cache[idx];
  147. if (dn == NULL || dn->id != dev_info->id) {
  148. dn = get_from_id(&muxl->frml_list, dev_info->id);
  149. muxl->dn_cache[idx] = dn;
  150. }
  151. return dn;
  152. }
  153. struct cflayer *cfmuxl_remove_uplayer(struct cflayer *layr, u8 id)
  154. {
  155. struct cflayer *up;
  156. struct cfmuxl *muxl = container_obj(layr);
  157. spin_lock(&muxl->receive_lock);
  158. up = get_up(muxl, id);
  159. if (up == NULL)
  160. goto out;
  161. memset(muxl->up_cache, 0, sizeof(muxl->up_cache));
  162. list_del(&up->node);
  163. cfsrvl_put(up);
  164. out:
  165. spin_unlock(&muxl->receive_lock);
  166. return up;
  167. }
  168. static int cfmuxl_receive(struct cflayer *layr, struct cfpkt *pkt)
  169. {
  170. int ret;
  171. struct cfmuxl *muxl = container_obj(layr);
  172. u8 id;
  173. struct cflayer *up;
  174. if (cfpkt_extr_head(pkt, &id, 1) < 0) {
  175. pr_err("erroneous Caif Packet\n");
  176. cfpkt_destroy(pkt);
  177. return -EPROTO;
  178. }
  179. spin_lock(&muxl->receive_lock);
  180. up = get_up(muxl, id);
  181. spin_unlock(&muxl->receive_lock);
  182. if (up == NULL) {
  183. pr_info("Received data on unknown link ID = %d (0x%x) up == NULL",
  184. id, id);
  185. cfpkt_destroy(pkt);
  186. /*
  187. * Don't return ERROR, since modem misbehaves and sends out
  188. * flow on before linksetup response.
  189. */
  190. return /* CFGLU_EPROT; */ 0;
  191. }
  192. cfsrvl_get(up);
  193. ret = up->receive(up, pkt);
  194. cfsrvl_put(up);
  195. return ret;
  196. }
  197. static int cfmuxl_transmit(struct cflayer *layr, struct cfpkt *pkt)
  198. {
  199. int ret;
  200. struct cfmuxl *muxl = container_obj(layr);
  201. u8 linkid;
  202. struct cflayer *dn;
  203. struct caif_payload_info *info = cfpkt_info(pkt);
  204. dn = get_dn(muxl, cfpkt_info(pkt)->dev_info);
  205. if (dn == NULL) {
  206. pr_warn("Send data on unknown phy ID = %d (0x%x)\n",
  207. info->dev_info->id, info->dev_info->id);
  208. return -ENOTCONN;
  209. }
  210. info->hdr_len += 1;
  211. linkid = info->channel_id;
  212. cfpkt_add_head(pkt, &linkid, 1);
  213. ret = dn->transmit(dn, pkt);
  214. /* Remove MUX protocol header upon error. */
  215. if (ret < 0)
  216. cfpkt_extr_head(pkt, &linkid, 1);
  217. return ret;
  218. }
  219. static void cfmuxl_ctrlcmd(struct cflayer *layr, enum caif_ctrlcmd ctrl,
  220. int phyid)
  221. {
  222. struct cfmuxl *muxl = container_obj(layr);
  223. struct list_head *node;
  224. struct cflayer *layer;
  225. list_for_each(node, &muxl->srvl_list) {
  226. layer = list_entry(node, struct cflayer, node);
  227. if (cfsrvl_phyid_match(layer, phyid))
  228. layer->ctrlcmd(layer, ctrl, phyid);
  229. }
  230. }