cfcnfg.c 13 KB

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