cfmuxl.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 <linux/rculist.h>
  11. #include <net/caif/cfpkt.h>
  12. #include <net/caif/cfmuxl.h>
  13. #include <net/caif/cfsrvl.h>
  14. #include <net/caif/cffrml.h>
  15. #define container_obj(layr) container_of(layr, struct cfmuxl, layer)
  16. #define CAIF_CTRL_CHANNEL 0
  17. #define UP_CACHE_SIZE 8
  18. #define DN_CACHE_SIZE 8
  19. struct cfmuxl {
  20. struct cflayer layer;
  21. struct list_head srvl_list;
  22. struct list_head frml_list;
  23. struct cflayer *up_cache[UP_CACHE_SIZE];
  24. struct cflayer *dn_cache[DN_CACHE_SIZE];
  25. /*
  26. * Set when inserting or removing downwards layers.
  27. */
  28. spinlock_t transmit_lock;
  29. /*
  30. * Set when inserting or removing upwards layers.
  31. */
  32. spinlock_t receive_lock;
  33. };
  34. static int cfmuxl_receive(struct cflayer *layr, struct cfpkt *pkt);
  35. static int cfmuxl_transmit(struct cflayer *layr, struct cfpkt *pkt);
  36. static void cfmuxl_ctrlcmd(struct cflayer *layr, enum caif_ctrlcmd ctrl,
  37. int phyid);
  38. static struct cflayer *get_up(struct cfmuxl *muxl, u16 id);
  39. struct cflayer *cfmuxl_create(void)
  40. {
  41. struct cfmuxl *this = kmalloc(sizeof(struct cfmuxl), GFP_ATOMIC);
  42. if (!this)
  43. return NULL;
  44. memset(this, 0, sizeof(*this));
  45. this->layer.receive = cfmuxl_receive;
  46. this->layer.transmit = cfmuxl_transmit;
  47. this->layer.ctrlcmd = cfmuxl_ctrlcmd;
  48. INIT_LIST_HEAD(&this->srvl_list);
  49. INIT_LIST_HEAD(&this->frml_list);
  50. spin_lock_init(&this->transmit_lock);
  51. spin_lock_init(&this->receive_lock);
  52. snprintf(this->layer.name, CAIF_LAYER_NAME_SZ, "mux");
  53. return &this->layer;
  54. }
  55. int cfmuxl_set_dnlayer(struct cflayer *layr, struct cflayer *dn, u8 phyid)
  56. {
  57. struct cfmuxl *muxl = (struct cfmuxl *) layr;
  58. spin_lock_bh(&muxl->transmit_lock);
  59. list_add_rcu(&dn->node, &muxl->frml_list);
  60. spin_unlock_bh(&muxl->transmit_lock);
  61. return 0;
  62. }
  63. static struct cflayer *get_from_id(struct list_head *list, u16 id)
  64. {
  65. struct cflayer *lyr;
  66. list_for_each_entry_rcu(lyr, list, node) {
  67. if (lyr->id == id)
  68. return lyr;
  69. }
  70. return NULL;
  71. }
  72. int cfmuxl_set_uplayer(struct cflayer *layr, struct cflayer *up, u8 linkid)
  73. {
  74. struct cfmuxl *muxl = container_obj(layr);
  75. struct cflayer *old;
  76. spin_lock_bh(&muxl->receive_lock);
  77. /* Two entries with same id is wrong, so remove old layer from mux */
  78. old = get_from_id(&muxl->srvl_list, linkid);
  79. if (old != NULL)
  80. list_del_rcu(&old->node);
  81. list_add_rcu(&up->node, &muxl->srvl_list);
  82. spin_unlock_bh(&muxl->receive_lock);
  83. return 0;
  84. }
  85. struct cflayer *cfmuxl_remove_dnlayer(struct cflayer *layr, u8 phyid)
  86. {
  87. struct cfmuxl *muxl = container_obj(layr);
  88. struct cflayer *dn;
  89. int idx = phyid % DN_CACHE_SIZE;
  90. spin_lock_bh(&muxl->transmit_lock);
  91. RCU_INIT_POINTER(muxl->dn_cache[idx], NULL);
  92. dn = get_from_id(&muxl->frml_list, phyid);
  93. if (dn == NULL)
  94. goto out;
  95. list_del_rcu(&dn->node);
  96. caif_assert(dn != NULL);
  97. out:
  98. spin_unlock_bh(&muxl->transmit_lock);
  99. return dn;
  100. }
  101. static struct cflayer *get_up(struct cfmuxl *muxl, u16 id)
  102. {
  103. struct cflayer *up;
  104. int idx = id % UP_CACHE_SIZE;
  105. up = rcu_dereference(muxl->up_cache[idx]);
  106. if (up == NULL || up->id != id) {
  107. spin_lock_bh(&muxl->receive_lock);
  108. up = get_from_id(&muxl->srvl_list, id);
  109. rcu_assign_pointer(muxl->up_cache[idx], up);
  110. spin_unlock_bh(&muxl->receive_lock);
  111. }
  112. return up;
  113. }
  114. static struct cflayer *get_dn(struct cfmuxl *muxl, struct dev_info *dev_info)
  115. {
  116. struct cflayer *dn;
  117. int idx = dev_info->id % DN_CACHE_SIZE;
  118. dn = rcu_dereference(muxl->dn_cache[idx]);
  119. if (dn == NULL || dn->id != dev_info->id) {
  120. spin_lock_bh(&muxl->transmit_lock);
  121. dn = get_from_id(&muxl->frml_list, dev_info->id);
  122. rcu_assign_pointer(muxl->dn_cache[idx], dn);
  123. spin_unlock_bh(&muxl->transmit_lock);
  124. }
  125. return dn;
  126. }
  127. struct cflayer *cfmuxl_remove_uplayer(struct cflayer *layr, u8 id)
  128. {
  129. struct cflayer *up;
  130. struct cfmuxl *muxl = container_obj(layr);
  131. int idx = id % UP_CACHE_SIZE;
  132. if (id == 0) {
  133. pr_warn("Trying to remove control layer\n");
  134. return NULL;
  135. }
  136. spin_lock_bh(&muxl->receive_lock);
  137. up = get_from_id(&muxl->srvl_list, id);
  138. if (up == NULL)
  139. goto out;
  140. RCU_INIT_POINTER(muxl->up_cache[idx], NULL);
  141. list_del_rcu(&up->node);
  142. out:
  143. spin_unlock_bh(&muxl->receive_lock);
  144. return up;
  145. }
  146. static int cfmuxl_receive(struct cflayer *layr, struct cfpkt *pkt)
  147. {
  148. int ret;
  149. struct cfmuxl *muxl = container_obj(layr);
  150. u8 id;
  151. struct cflayer *up;
  152. if (cfpkt_extr_head(pkt, &id, 1) < 0) {
  153. pr_err("erroneous Caif Packet\n");
  154. cfpkt_destroy(pkt);
  155. return -EPROTO;
  156. }
  157. rcu_read_lock();
  158. up = get_up(muxl, id);
  159. if (up == NULL) {
  160. pr_debug("Received data on unknown link ID = %d (0x%x)"
  161. " up == NULL", id, id);
  162. cfpkt_destroy(pkt);
  163. /*
  164. * Don't return ERROR, since modem misbehaves and sends out
  165. * flow on before linksetup response.
  166. */
  167. rcu_read_unlock();
  168. return /* CFGLU_EPROT; */ 0;
  169. }
  170. /* We can't hold rcu_lock during receive, so take a ref count instead */
  171. cfsrvl_get(up);
  172. rcu_read_unlock();
  173. ret = up->receive(up, pkt);
  174. cfsrvl_put(up);
  175. return ret;
  176. }
  177. static int cfmuxl_transmit(struct cflayer *layr, struct cfpkt *pkt)
  178. {
  179. struct cfmuxl *muxl = container_obj(layr);
  180. int err;
  181. u8 linkid;
  182. struct cflayer *dn;
  183. struct caif_payload_info *info = cfpkt_info(pkt);
  184. BUG_ON(!info);
  185. rcu_read_lock();
  186. dn = get_dn(muxl, info->dev_info);
  187. if (dn == NULL) {
  188. pr_debug("Send data on unknown phy ID = %d (0x%x)\n",
  189. info->dev_info->id, info->dev_info->id);
  190. rcu_read_unlock();
  191. cfpkt_destroy(pkt);
  192. return -ENOTCONN;
  193. }
  194. info->hdr_len += 1;
  195. linkid = info->channel_id;
  196. cfpkt_add_head(pkt, &linkid, 1);
  197. /* We can't hold rcu_lock during receive, so take a ref count instead */
  198. cffrml_hold(dn);
  199. rcu_read_unlock();
  200. err = dn->transmit(dn, pkt);
  201. cffrml_put(dn);
  202. return err;
  203. }
  204. static void cfmuxl_ctrlcmd(struct cflayer *layr, enum caif_ctrlcmd ctrl,
  205. int phyid)
  206. {
  207. struct cfmuxl *muxl = container_obj(layr);
  208. struct cflayer *layer;
  209. rcu_read_lock();
  210. list_for_each_entry_rcu(layer, &muxl->srvl_list, node) {
  211. if (cfsrvl_phyid_match(layer, phyid) && layer->ctrlcmd) {
  212. if ((ctrl == _CAIF_CTRLCMD_PHYIF_DOWN_IND ||
  213. ctrl == CAIF_CTRLCMD_REMOTE_SHUTDOWN_IND) &&
  214. layer->id != 0)
  215. cfmuxl_remove_uplayer(layr, layer->id);
  216. /* NOTE: ctrlcmd is not allowed to block */
  217. layer->ctrlcmd(layer, ctrl, phyid);
  218. }
  219. }
  220. rcu_read_unlock();
  221. }