cfmuxl.c 6.2 KB

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