cfmuxl.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. list_add(&up->node, &muxl->srvl_list);
  58. spin_unlock(&muxl->receive_lock);
  59. return 0;
  60. }
  61. bool cfmuxl_is_phy_inuse(struct cflayer *layr, u8 phyid)
  62. {
  63. struct list_head *node;
  64. struct cflayer *layer;
  65. struct cfmuxl *muxl = container_obj(layr);
  66. bool match = false;
  67. spin_lock(&muxl->receive_lock);
  68. list_for_each(node, &muxl->srvl_list) {
  69. layer = list_entry(node, struct cflayer, node);
  70. if (cfsrvl_phyid_match(layer, phyid)) {
  71. match = true;
  72. break;
  73. }
  74. }
  75. spin_unlock(&muxl->receive_lock);
  76. return match;
  77. }
  78. u8 cfmuxl_get_phyid(struct cflayer *layr, u8 channel_id)
  79. {
  80. struct cflayer *up;
  81. int phyid;
  82. struct cfmuxl *muxl = container_obj(layr);
  83. spin_lock(&muxl->receive_lock);
  84. up = get_up(muxl, channel_id);
  85. if (up != NULL)
  86. phyid = cfsrvl_getphyid(up);
  87. else
  88. phyid = 0;
  89. spin_unlock(&muxl->receive_lock);
  90. return phyid;
  91. }
  92. int cfmuxl_set_dnlayer(struct cflayer *layr, struct cflayer *dn, u8 phyid)
  93. {
  94. struct cfmuxl *muxl = (struct cfmuxl *) layr;
  95. spin_lock(&muxl->transmit_lock);
  96. list_add(&dn->node, &muxl->frml_list);
  97. spin_unlock(&muxl->transmit_lock);
  98. return 0;
  99. }
  100. static struct cflayer *get_from_id(struct list_head *list, u16 id)
  101. {
  102. struct list_head *node;
  103. struct cflayer *layer;
  104. list_for_each(node, list) {
  105. layer = list_entry(node, struct cflayer, node);
  106. if (layer->id == id)
  107. return layer;
  108. }
  109. return NULL;
  110. }
  111. struct cflayer *cfmuxl_remove_dnlayer(struct cflayer *layr, u8 phyid)
  112. {
  113. struct cfmuxl *muxl = container_obj(layr);
  114. struct cflayer *dn;
  115. spin_lock(&muxl->transmit_lock);
  116. memset(muxl->dn_cache, 0, sizeof(muxl->dn_cache));
  117. dn = get_from_id(&muxl->frml_list, phyid);
  118. if (dn == NULL) {
  119. spin_unlock(&muxl->transmit_lock);
  120. return NULL;
  121. }
  122. list_del(&dn->node);
  123. caif_assert(dn != NULL);
  124. spin_unlock(&muxl->transmit_lock);
  125. return dn;
  126. }
  127. /* Invariant: lock is taken */
  128. static struct cflayer *get_up(struct cfmuxl *muxl, u16 id)
  129. {
  130. struct cflayer *up;
  131. int idx = id % UP_CACHE_SIZE;
  132. up = muxl->up_cache[idx];
  133. if (up == NULL || up->id != id) {
  134. up = get_from_id(&muxl->srvl_list, id);
  135. muxl->up_cache[idx] = up;
  136. }
  137. return up;
  138. }
  139. /* Invariant: lock is taken */
  140. static struct cflayer *get_dn(struct cfmuxl *muxl, struct dev_info *dev_info)
  141. {
  142. struct cflayer *dn;
  143. int idx = dev_info->id % DN_CACHE_SIZE;
  144. dn = muxl->dn_cache[idx];
  145. if (dn == NULL || dn->id != dev_info->id) {
  146. dn = get_from_id(&muxl->frml_list, dev_info->id);
  147. muxl->dn_cache[idx] = dn;
  148. }
  149. return dn;
  150. }
  151. struct cflayer *cfmuxl_remove_uplayer(struct cflayer *layr, u8 id)
  152. {
  153. struct cflayer *up;
  154. struct cfmuxl *muxl = container_obj(layr);
  155. spin_lock(&muxl->receive_lock);
  156. up = get_up(muxl, id);
  157. memset(muxl->up_cache, 0, sizeof(muxl->up_cache));
  158. list_del(&up->node);
  159. spin_unlock(&muxl->receive_lock);
  160. return up;
  161. }
  162. static int cfmuxl_receive(struct cflayer *layr, struct cfpkt *pkt)
  163. {
  164. int ret;
  165. struct cfmuxl *muxl = container_obj(layr);
  166. u8 id;
  167. struct cflayer *up;
  168. if (cfpkt_extr_head(pkt, &id, 1) < 0) {
  169. pr_err("CAIF: %s(): erroneous Caif Packet\n", __func__);
  170. cfpkt_destroy(pkt);
  171. return -EPROTO;
  172. }
  173. spin_lock(&muxl->receive_lock);
  174. up = get_up(muxl, id);
  175. spin_unlock(&muxl->receive_lock);
  176. if (up == NULL) {
  177. pr_info("CAIF: %s():Received data on unknown link ID = %d "
  178. "(0x%x) up == NULL", __func__, id, id);
  179. cfpkt_destroy(pkt);
  180. /*
  181. * Don't return ERROR, since modem misbehaves and sends out
  182. * flow on before linksetup response.
  183. */
  184. return /* CFGLU_EPROT; */ 0;
  185. }
  186. ret = up->receive(up, pkt);
  187. return ret;
  188. }
  189. static int cfmuxl_transmit(struct cflayer *layr, struct cfpkt *pkt)
  190. {
  191. int ret;
  192. struct cfmuxl *muxl = container_obj(layr);
  193. u8 linkid;
  194. struct cflayer *dn;
  195. struct caif_payload_info *info = cfpkt_info(pkt);
  196. dn = get_dn(muxl, cfpkt_info(pkt)->dev_info);
  197. if (dn == NULL) {
  198. pr_warning("CAIF: %s(): Send data on unknown phy "
  199. "ID = %d (0x%x)\n",
  200. __func__, info->dev_info->id, info->dev_info->id);
  201. return -ENOTCONN;
  202. }
  203. info->hdr_len += 1;
  204. linkid = info->channel_id;
  205. cfpkt_add_head(pkt, &linkid, 1);
  206. ret = dn->transmit(dn, pkt);
  207. /* Remove MUX protocol header upon error. */
  208. if (ret < 0)
  209. cfpkt_extr_head(pkt, &linkid, 1);
  210. return ret;
  211. }
  212. static void cfmuxl_ctrlcmd(struct cflayer *layr, enum caif_ctrlcmd ctrl,
  213. int phyid)
  214. {
  215. struct cfmuxl *muxl = container_obj(layr);
  216. struct list_head *node;
  217. struct cflayer *layer;
  218. list_for_each(node, &muxl->srvl_list) {
  219. layer = list_entry(node, struct cflayer, node);
  220. if (cfsrvl_phyid_match(layer, phyid))
  221. layer->ctrlcmd(layer, ctrl, phyid);
  222. }
  223. }