cfcnfg.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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/kernel.h>
  8. #include <linux/stddef.h>
  9. #include <linux/slab.h>
  10. #include <linux/netdevice.h>
  11. #include <net/caif/caif_layer.h>
  12. #include <net/caif/cfpkt.h>
  13. #include <net/caif/cfcnfg.h>
  14. #include <net/caif/cfctrl.h>
  15. #include <net/caif/cfmuxl.h>
  16. #include <net/caif/cffrml.h>
  17. #include <net/caif/cfserl.h>
  18. #include <net/caif/cfsrvl.h>
  19. #include <linux/module.h>
  20. #include <asm/atomic.h>
  21. #define MAX_PHY_LAYERS 7
  22. #define container_obj(layr) container_of(layr, struct cfcnfg, layer)
  23. /* Information about CAIF physical interfaces held by Config Module in order
  24. * to manage physical interfaces
  25. */
  26. struct cfcnfg_phyinfo {
  27. /* Pointer to the layer below the MUX (framing layer) */
  28. struct cflayer *frm_layer;
  29. /* Pointer to the lowest actual physical layer */
  30. struct cflayer *phy_layer;
  31. /* Unique identifier of the physical interface */
  32. unsigned int id;
  33. /* Preference of the physical in interface */
  34. enum cfcnfg_phy_preference pref;
  35. /* Reference count, number of channels using the device */
  36. int phy_ref_count;
  37. /* Information about the physical device */
  38. struct dev_info dev_info;
  39. /* Interface index */
  40. int ifindex;
  41. /* Use Start of frame extension */
  42. bool use_stx;
  43. /* Use Start of frame checksum */
  44. bool use_fcs;
  45. };
  46. struct cfcnfg {
  47. struct cflayer layer;
  48. struct cflayer *ctrl;
  49. struct cflayer *mux;
  50. u8 last_phyid;
  51. struct cfcnfg_phyinfo phy_layers[MAX_PHY_LAYERS];
  52. };
  53. static void cfcnfg_linkup_rsp(struct cflayer *layer, u8 channel_id,
  54. enum cfctrl_srv serv, u8 phyid,
  55. struct cflayer *adapt_layer);
  56. static void cfcnfg_linkdestroy_rsp(struct cflayer *layer, u8 channel_id);
  57. static void cfcnfg_reject_rsp(struct cflayer *layer, u8 channel_id,
  58. struct cflayer *adapt_layer);
  59. static void cfctrl_resp_func(void);
  60. static void cfctrl_enum_resp(void);
  61. struct cfcnfg *cfcnfg_create(void)
  62. {
  63. struct cfcnfg *this;
  64. struct cfctrl_rsp *resp;
  65. /* Initiate this layer */
  66. this = kzalloc(sizeof(struct cfcnfg), GFP_ATOMIC);
  67. if (!this) {
  68. pr_warn("Out of memory\n");
  69. return NULL;
  70. }
  71. this->mux = cfmuxl_create();
  72. if (!this->mux)
  73. goto out_of_mem;
  74. this->ctrl = cfctrl_create();
  75. if (!this->ctrl)
  76. goto out_of_mem;
  77. /* Initiate response functions */
  78. resp = cfctrl_get_respfuncs(this->ctrl);
  79. resp->enum_rsp = cfctrl_enum_resp;
  80. resp->linkerror_ind = cfctrl_resp_func;
  81. resp->linkdestroy_rsp = cfcnfg_linkdestroy_rsp;
  82. resp->sleep_rsp = cfctrl_resp_func;
  83. resp->wake_rsp = cfctrl_resp_func;
  84. resp->restart_rsp = cfctrl_resp_func;
  85. resp->radioset_rsp = cfctrl_resp_func;
  86. resp->linksetup_rsp = cfcnfg_linkup_rsp;
  87. resp->reject_rsp = cfcnfg_reject_rsp;
  88. this->last_phyid = 1;
  89. cfmuxl_set_uplayer(this->mux, this->ctrl, 0);
  90. layer_set_dn(this->ctrl, this->mux);
  91. layer_set_up(this->ctrl, this);
  92. return this;
  93. out_of_mem:
  94. pr_warn("Out of memory\n");
  95. kfree(this->mux);
  96. kfree(this->ctrl);
  97. kfree(this);
  98. return NULL;
  99. }
  100. EXPORT_SYMBOL(cfcnfg_create);
  101. void cfcnfg_remove(struct cfcnfg *cfg)
  102. {
  103. if (cfg) {
  104. kfree(cfg->mux);
  105. kfree(cfg->ctrl);
  106. kfree(cfg);
  107. }
  108. }
  109. static void cfctrl_resp_func(void)
  110. {
  111. }
  112. static void cfctrl_enum_resp(void)
  113. {
  114. }
  115. struct dev_info *cfcnfg_get_phyid(struct cfcnfg *cnfg,
  116. enum cfcnfg_phy_preference phy_pref)
  117. {
  118. u16 i;
  119. /* Try to match with specified preference */
  120. for (i = 1; i < MAX_PHY_LAYERS; i++) {
  121. if (cnfg->phy_layers[i].id == i &&
  122. cnfg->phy_layers[i].pref == phy_pref &&
  123. cnfg->phy_layers[i].frm_layer != NULL) {
  124. caif_assert(cnfg->phy_layers != NULL);
  125. caif_assert(cnfg->phy_layers[i].id == i);
  126. return &cnfg->phy_layers[i].dev_info;
  127. }
  128. }
  129. /* Otherwise just return something */
  130. for (i = 1; i < MAX_PHY_LAYERS; i++) {
  131. if (cnfg->phy_layers[i].id == i) {
  132. caif_assert(cnfg->phy_layers != NULL);
  133. caif_assert(cnfg->phy_layers[i].id == i);
  134. return &cnfg->phy_layers[i].dev_info;
  135. }
  136. }
  137. return NULL;
  138. }
  139. static struct cfcnfg_phyinfo *cfcnfg_get_phyinfo(struct cfcnfg *cnfg,
  140. u8 phyid)
  141. {
  142. int i;
  143. /* Try to match with specified preference */
  144. for (i = 0; i < MAX_PHY_LAYERS; i++)
  145. if (cnfg->phy_layers[i].frm_layer != NULL &&
  146. cnfg->phy_layers[i].id == phyid)
  147. return &cnfg->phy_layers[i];
  148. return NULL;
  149. }
  150. int cfcnfg_get_id_from_ifi(struct cfcnfg *cnfg, int ifi)
  151. {
  152. int i;
  153. for (i = 0; i < MAX_PHY_LAYERS; i++)
  154. if (cnfg->phy_layers[i].frm_layer != NULL &&
  155. cnfg->phy_layers[i].ifindex == ifi)
  156. return i;
  157. return -ENODEV;
  158. }
  159. int cfcnfg_disconn_adapt_layer(struct cfcnfg *cnfg, struct cflayer *adap_layer)
  160. {
  161. u8 channel_id = 0;
  162. int ret = 0;
  163. struct cflayer *servl = NULL;
  164. struct cfcnfg_phyinfo *phyinfo = NULL;
  165. u8 phyid = 0;
  166. caif_assert(adap_layer != NULL);
  167. channel_id = adap_layer->id;
  168. if (adap_layer->dn == NULL || channel_id == 0) {
  169. pr_err("adap_layer->dn == NULL or adap_layer->id is 0\n");
  170. ret = -ENOTCONN;
  171. goto end;
  172. }
  173. servl = cfmuxl_remove_uplayer(cnfg->mux, channel_id);
  174. if (servl == NULL) {
  175. pr_err("PROTOCOL ERROR - Error removing service_layer Channel_Id(%d)",
  176. channel_id);
  177. ret = -EINVAL;
  178. goto end;
  179. }
  180. layer_set_up(servl, NULL);
  181. ret = cfctrl_linkdown_req(cnfg->ctrl, channel_id, adap_layer);
  182. if (ret)
  183. goto end;
  184. caif_assert(channel_id == servl->id);
  185. if (adap_layer->dn != NULL) {
  186. phyid = cfsrvl_getphyid(adap_layer->dn);
  187. phyinfo = cfcnfg_get_phyinfo(cnfg, phyid);
  188. if (phyinfo == NULL) {
  189. pr_warn("No interface to send disconnect to\n");
  190. ret = -ENODEV;
  191. goto end;
  192. }
  193. if (phyinfo->id != phyid ||
  194. phyinfo->phy_layer->id != phyid ||
  195. phyinfo->frm_layer->id != phyid) {
  196. pr_err("Inconsistency in phy registration\n");
  197. ret = -EINVAL;
  198. goto end;
  199. }
  200. }
  201. if (phyinfo != NULL && --phyinfo->phy_ref_count == 0 &&
  202. phyinfo->phy_layer != NULL &&
  203. phyinfo->phy_layer->modemcmd != NULL) {
  204. phyinfo->phy_layer->modemcmd(phyinfo->phy_layer,
  205. _CAIF_MODEMCMD_PHYIF_USELESS);
  206. }
  207. end:
  208. cfsrvl_put(servl);
  209. cfctrl_cancel_req(cnfg->ctrl, adap_layer);
  210. if (adap_layer->ctrlcmd != NULL)
  211. adap_layer->ctrlcmd(adap_layer, CAIF_CTRLCMD_DEINIT_RSP, 0);
  212. return ret;
  213. }
  214. EXPORT_SYMBOL(cfcnfg_disconn_adapt_layer);
  215. void cfcnfg_release_adap_layer(struct cflayer *adap_layer)
  216. {
  217. if (adap_layer->dn)
  218. cfsrvl_put(adap_layer->dn);
  219. }
  220. EXPORT_SYMBOL(cfcnfg_release_adap_layer);
  221. static void cfcnfg_linkdestroy_rsp(struct cflayer *layer, u8 channel_id)
  222. {
  223. }
  224. int protohead[CFCTRL_SRV_MASK] = {
  225. [CFCTRL_SRV_VEI] = 4,
  226. [CFCTRL_SRV_DATAGRAM] = 7,
  227. [CFCTRL_SRV_UTIL] = 4,
  228. [CFCTRL_SRV_RFM] = 3,
  229. [CFCTRL_SRV_DBG] = 3,
  230. };
  231. int cfcnfg_add_adaptation_layer(struct cfcnfg *cnfg,
  232. struct cfctrl_link_param *param,
  233. struct cflayer *adap_layer,
  234. int *ifindex,
  235. int *proto_head,
  236. int *proto_tail)
  237. {
  238. struct cflayer *frml;
  239. if (adap_layer == NULL) {
  240. pr_err("adap_layer is zero\n");
  241. return -EINVAL;
  242. }
  243. if (adap_layer->receive == NULL) {
  244. pr_err("adap_layer->receive is NULL\n");
  245. return -EINVAL;
  246. }
  247. if (adap_layer->ctrlcmd == NULL) {
  248. pr_err("adap_layer->ctrlcmd == NULL\n");
  249. return -EINVAL;
  250. }
  251. frml = cnfg->phy_layers[param->phyid].frm_layer;
  252. if (frml == NULL) {
  253. pr_err("Specified PHY type does not exist!\n");
  254. return -ENODEV;
  255. }
  256. caif_assert(param->phyid == cnfg->phy_layers[param->phyid].id);
  257. caif_assert(cnfg->phy_layers[param->phyid].frm_layer->id ==
  258. param->phyid);
  259. caif_assert(cnfg->phy_layers[param->phyid].phy_layer->id ==
  260. param->phyid);
  261. *ifindex = cnfg->phy_layers[param->phyid].ifindex;
  262. *proto_head =
  263. protohead[param->linktype]+
  264. (cnfg->phy_layers[param->phyid].use_stx ? 1 : 0);
  265. *proto_tail = 2;
  266. /* FIXME: ENUMERATE INITIALLY WHEN ACTIVATING PHYSICAL INTERFACE */
  267. cfctrl_enum_req(cnfg->ctrl, param->phyid);
  268. return cfctrl_linkup_request(cnfg->ctrl, param, adap_layer);
  269. }
  270. EXPORT_SYMBOL(cfcnfg_add_adaptation_layer);
  271. static void cfcnfg_reject_rsp(struct cflayer *layer, u8 channel_id,
  272. struct cflayer *adapt_layer)
  273. {
  274. if (adapt_layer != NULL && adapt_layer->ctrlcmd != NULL)
  275. adapt_layer->ctrlcmd(adapt_layer,
  276. CAIF_CTRLCMD_INIT_FAIL_RSP, 0);
  277. }
  278. static void
  279. cfcnfg_linkup_rsp(struct cflayer *layer, u8 channel_id, enum cfctrl_srv serv,
  280. u8 phyid, struct cflayer *adapt_layer)
  281. {
  282. struct cfcnfg *cnfg = container_obj(layer);
  283. struct cflayer *servicel = NULL;
  284. struct cfcnfg_phyinfo *phyinfo;
  285. struct net_device *netdev;
  286. if (adapt_layer == NULL) {
  287. pr_debug("link setup response but no client exist, send linkdown back\n");
  288. cfctrl_linkdown_req(cnfg->ctrl, channel_id, NULL);
  289. return;
  290. }
  291. caif_assert(cnfg != NULL);
  292. caif_assert(phyid != 0);
  293. phyinfo = &cnfg->phy_layers[phyid];
  294. caif_assert(phyinfo->id == phyid);
  295. caif_assert(phyinfo->phy_layer != NULL);
  296. caif_assert(phyinfo->phy_layer->id == phyid);
  297. phyinfo->phy_ref_count++;
  298. if (phyinfo->phy_ref_count == 1 &&
  299. phyinfo->phy_layer->modemcmd != NULL) {
  300. phyinfo->phy_layer->modemcmd(phyinfo->phy_layer,
  301. _CAIF_MODEMCMD_PHYIF_USEFULL);
  302. }
  303. adapt_layer->id = channel_id;
  304. switch (serv) {
  305. case CFCTRL_SRV_VEI:
  306. servicel = cfvei_create(channel_id, &phyinfo->dev_info);
  307. break;
  308. case CFCTRL_SRV_DATAGRAM:
  309. servicel = cfdgml_create(channel_id, &phyinfo->dev_info);
  310. break;
  311. case CFCTRL_SRV_RFM:
  312. netdev = phyinfo->dev_info.dev;
  313. servicel = cfrfml_create(channel_id, &phyinfo->dev_info,
  314. netdev->mtu);
  315. break;
  316. case CFCTRL_SRV_UTIL:
  317. servicel = cfutill_create(channel_id, &phyinfo->dev_info);
  318. break;
  319. case CFCTRL_SRV_VIDEO:
  320. servicel = cfvidl_create(channel_id, &phyinfo->dev_info);
  321. break;
  322. case CFCTRL_SRV_DBG:
  323. servicel = cfdbgl_create(channel_id, &phyinfo->dev_info);
  324. break;
  325. default:
  326. pr_err("Protocol error. Link setup response - unknown channel type\n");
  327. return;
  328. }
  329. if (!servicel) {
  330. pr_warn("Out of memory\n");
  331. return;
  332. }
  333. layer_set_dn(servicel, cnfg->mux);
  334. cfmuxl_set_uplayer(cnfg->mux, servicel, channel_id);
  335. layer_set_up(servicel, adapt_layer);
  336. layer_set_dn(adapt_layer, servicel);
  337. cfsrvl_get(servicel);
  338. servicel->ctrlcmd(servicel, CAIF_CTRLCMD_INIT_RSP, 0);
  339. }
  340. void
  341. cfcnfg_add_phy_layer(struct cfcnfg *cnfg, enum cfcnfg_phy_type phy_type,
  342. struct net_device *dev, struct cflayer *phy_layer,
  343. u16 *phyid, enum cfcnfg_phy_preference pref,
  344. bool fcs, bool stx)
  345. {
  346. struct cflayer *frml;
  347. struct cflayer *phy_driver = NULL;
  348. int i;
  349. if (cnfg->phy_layers[cnfg->last_phyid].frm_layer == NULL) {
  350. *phyid = cnfg->last_phyid;
  351. /* range: * 1..(MAX_PHY_LAYERS-1) */
  352. cnfg->last_phyid =
  353. (cnfg->last_phyid % (MAX_PHY_LAYERS - 1)) + 1;
  354. } else {
  355. *phyid = 0;
  356. for (i = 1; i < MAX_PHY_LAYERS; i++) {
  357. if (cnfg->phy_layers[i].frm_layer == NULL) {
  358. *phyid = i;
  359. break;
  360. }
  361. }
  362. }
  363. if (*phyid == 0) {
  364. pr_err("No Available PHY ID\n");
  365. return;
  366. }
  367. switch (phy_type) {
  368. case CFPHYTYPE_FRAG:
  369. phy_driver =
  370. cfserl_create(CFPHYTYPE_FRAG, *phyid, stx);
  371. if (!phy_driver) {
  372. pr_warn("Out of memory\n");
  373. return;
  374. }
  375. break;
  376. case CFPHYTYPE_CAIF:
  377. phy_driver = NULL;
  378. break;
  379. default:
  380. pr_err("%d\n", phy_type);
  381. return;
  382. break;
  383. }
  384. phy_layer->id = *phyid;
  385. cnfg->phy_layers[*phyid].pref = pref;
  386. cnfg->phy_layers[*phyid].id = *phyid;
  387. cnfg->phy_layers[*phyid].dev_info.id = *phyid;
  388. cnfg->phy_layers[*phyid].dev_info.dev = dev;
  389. cnfg->phy_layers[*phyid].phy_layer = phy_layer;
  390. cnfg->phy_layers[*phyid].phy_ref_count = 0;
  391. cnfg->phy_layers[*phyid].ifindex = dev->ifindex;
  392. cnfg->phy_layers[*phyid].use_stx = stx;
  393. cnfg->phy_layers[*phyid].use_fcs = fcs;
  394. phy_layer->type = phy_type;
  395. frml = cffrml_create(*phyid, fcs);
  396. if (!frml) {
  397. pr_warn("Out of memory\n");
  398. return;
  399. }
  400. cnfg->phy_layers[*phyid].frm_layer = frml;
  401. cfmuxl_set_dnlayer(cnfg->mux, frml, *phyid);
  402. layer_set_up(frml, cnfg->mux);
  403. if (phy_driver != NULL) {
  404. phy_driver->id = *phyid;
  405. layer_set_dn(frml, phy_driver);
  406. layer_set_up(phy_driver, frml);
  407. layer_set_dn(phy_driver, phy_layer);
  408. layer_set_up(phy_layer, phy_driver);
  409. } else {
  410. layer_set_dn(frml, phy_layer);
  411. layer_set_up(phy_layer, frml);
  412. }
  413. }
  414. EXPORT_SYMBOL(cfcnfg_add_phy_layer);
  415. int cfcnfg_del_phy_layer(struct cfcnfg *cnfg, struct cflayer *phy_layer)
  416. {
  417. struct cflayer *frml, *frml_dn;
  418. u16 phyid;
  419. phyid = phy_layer->id;
  420. caif_assert(phyid == cnfg->phy_layers[phyid].id);
  421. caif_assert(phy_layer == cnfg->phy_layers[phyid].phy_layer);
  422. caif_assert(phy_layer->id == phyid);
  423. caif_assert(cnfg->phy_layers[phyid].frm_layer->id == phyid);
  424. memset(&cnfg->phy_layers[phy_layer->id], 0,
  425. sizeof(struct cfcnfg_phyinfo));
  426. frml = cfmuxl_remove_dnlayer(cnfg->mux, phy_layer->id);
  427. frml_dn = frml->dn;
  428. cffrml_set_uplayer(frml, NULL);
  429. cffrml_set_dnlayer(frml, NULL);
  430. kfree(frml);
  431. if (phy_layer != frml_dn) {
  432. layer_set_up(frml_dn, NULL);
  433. layer_set_dn(frml_dn, NULL);
  434. kfree(frml_dn);
  435. }
  436. layer_set_up(phy_layer, NULL);
  437. return 0;
  438. }
  439. EXPORT_SYMBOL(cfcnfg_del_phy_layer);