caif_dev.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /*
  2. * CAIF Interface registration.
  3. * Copyright (C) ST-Ericsson AB 2010
  4. * Author: Sjur Brendeland/sjur.brandeland@stericsson.com
  5. * License terms: GNU General Public License (GPL) version 2
  6. *
  7. * Borrowed heavily from file: pn_dev.c. Thanks to
  8. * Remi Denis-Courmont <remi.denis-courmont@nokia.com>
  9. * and Sakari Ailus <sakari.ailus@nokia.com>
  10. */
  11. #define pr_fmt(fmt) KBUILD_MODNAME ":%s(): " fmt, __func__
  12. #include <linux/version.h>
  13. #include <linux/kernel.h>
  14. #include <linux/if_arp.h>
  15. #include <linux/net.h>
  16. #include <linux/netdevice.h>
  17. #include <linux/mutex.h>
  18. #include <net/netns/generic.h>
  19. #include <net/net_namespace.h>
  20. #include <net/pkt_sched.h>
  21. #include <net/caif/caif_device.h>
  22. #include <net/caif/caif_layer.h>
  23. #include <net/caif/cfpkt.h>
  24. #include <net/caif/cfcnfg.h>
  25. MODULE_LICENSE("GPL");
  26. /* Used for local tracking of the CAIF net devices */
  27. struct caif_device_entry {
  28. struct cflayer layer;
  29. struct list_head list;
  30. struct net_device *netdev;
  31. int __percpu *pcpu_refcnt;
  32. };
  33. struct caif_device_entry_list {
  34. struct list_head list;
  35. /* Protects simulanous deletes in list */
  36. struct mutex lock;
  37. };
  38. struct caif_net {
  39. struct cfcnfg *cfg;
  40. struct caif_device_entry_list caifdevs;
  41. };
  42. static int caif_net_id;
  43. struct cfcnfg *get_cfcnfg(struct net *net)
  44. {
  45. struct caif_net *caifn;
  46. BUG_ON(!net);
  47. caifn = net_generic(net, caif_net_id);
  48. BUG_ON(!caifn);
  49. return caifn->cfg;
  50. }
  51. EXPORT_SYMBOL(get_cfcnfg);
  52. static struct caif_device_entry_list *caif_device_list(struct net *net)
  53. {
  54. struct caif_net *caifn;
  55. BUG_ON(!net);
  56. caifn = net_generic(net, caif_net_id);
  57. BUG_ON(!caifn);
  58. return &caifn->caifdevs;
  59. }
  60. static void caifd_put(struct caif_device_entry *e)
  61. {
  62. irqsafe_cpu_dec(*e->pcpu_refcnt);
  63. }
  64. static void caifd_hold(struct caif_device_entry *e)
  65. {
  66. irqsafe_cpu_inc(*e->pcpu_refcnt);
  67. }
  68. static int caifd_refcnt_read(struct caif_device_entry *e)
  69. {
  70. int i, refcnt = 0;
  71. for_each_possible_cpu(i)
  72. refcnt += *per_cpu_ptr(e->pcpu_refcnt, i);
  73. return refcnt;
  74. }
  75. /* Allocate new CAIF device. */
  76. static struct caif_device_entry *caif_device_alloc(struct net_device *dev)
  77. {
  78. struct caif_device_entry_list *caifdevs;
  79. struct caif_device_entry *caifd;
  80. caifdevs = caif_device_list(dev_net(dev));
  81. BUG_ON(!caifdevs);
  82. caifd = kzalloc(sizeof(*caifd), GFP_ATOMIC);
  83. if (!caifd)
  84. return NULL;
  85. caifd->pcpu_refcnt = alloc_percpu(int);
  86. caifd->netdev = dev;
  87. dev_hold(dev);
  88. return caifd;
  89. }
  90. static struct caif_device_entry *caif_get(struct net_device *dev)
  91. {
  92. struct caif_device_entry_list *caifdevs =
  93. caif_device_list(dev_net(dev));
  94. struct caif_device_entry *caifd;
  95. BUG_ON(!caifdevs);
  96. list_for_each_entry_rcu(caifd, &caifdevs->list, list) {
  97. if (caifd->netdev == dev)
  98. return caifd;
  99. }
  100. return NULL;
  101. }
  102. static int transmit(struct cflayer *layer, struct cfpkt *pkt)
  103. {
  104. struct caif_device_entry *caifd =
  105. container_of(layer, struct caif_device_entry, layer);
  106. struct sk_buff *skb;
  107. skb = cfpkt_tonative(pkt);
  108. skb->dev = caifd->netdev;
  109. dev_queue_xmit(skb);
  110. return 0;
  111. }
  112. /*
  113. * Stuff received packets into the CAIF stack.
  114. * On error, returns non-zero and releases the skb.
  115. */
  116. static int receive(struct sk_buff *skb, struct net_device *dev,
  117. struct packet_type *pkttype, struct net_device *orig_dev)
  118. {
  119. struct cfpkt *pkt;
  120. struct caif_device_entry *caifd;
  121. pkt = cfpkt_fromnative(CAIF_DIR_IN, skb);
  122. rcu_read_lock();
  123. caifd = caif_get(dev);
  124. if (!caifd || !caifd->layer.up || !caifd->layer.up->receive ||
  125. !netif_oper_up(caifd->netdev)) {
  126. rcu_read_unlock();
  127. kfree_skb(skb);
  128. return NET_RX_DROP;
  129. }
  130. /* Hold reference to netdevice while using CAIF stack */
  131. caifd_hold(caifd);
  132. rcu_read_unlock();
  133. caifd->layer.up->receive(caifd->layer.up, pkt);
  134. /* Release reference to stack upwards */
  135. caifd_put(caifd);
  136. return 0;
  137. }
  138. static struct packet_type caif_packet_type __read_mostly = {
  139. .type = cpu_to_be16(ETH_P_CAIF),
  140. .func = receive,
  141. };
  142. static void dev_flowctrl(struct net_device *dev, int on)
  143. {
  144. struct caif_device_entry *caifd;
  145. rcu_read_lock();
  146. caifd = caif_get(dev);
  147. if (!caifd || !caifd->layer.up || !caifd->layer.up->ctrlcmd) {
  148. rcu_read_unlock();
  149. return;
  150. }
  151. caifd_hold(caifd);
  152. rcu_read_unlock();
  153. caifd->layer.up->ctrlcmd(caifd->layer.up,
  154. on ?
  155. _CAIF_CTRLCMD_PHYIF_FLOW_ON_IND :
  156. _CAIF_CTRLCMD_PHYIF_FLOW_OFF_IND,
  157. caifd->layer.id);
  158. caifd_put(caifd);
  159. }
  160. /* notify Caif of device events */
  161. static int caif_device_notify(struct notifier_block *me, unsigned long what,
  162. void *arg)
  163. {
  164. struct net_device *dev = arg;
  165. struct caif_device_entry *caifd = NULL;
  166. struct caif_dev_common *caifdev;
  167. enum cfcnfg_phy_preference pref;
  168. enum cfcnfg_phy_type phy_type;
  169. struct cfcnfg *cfg;
  170. struct caif_device_entry_list *caifdevs =
  171. caif_device_list(dev_net(dev));
  172. if (dev->type != ARPHRD_CAIF)
  173. return 0;
  174. cfg = get_cfcnfg(dev_net(dev));
  175. if (cfg == NULL)
  176. return 0;
  177. switch (what) {
  178. case NETDEV_REGISTER:
  179. caifd = caif_device_alloc(dev);
  180. if (!caifd)
  181. return 0;
  182. caifdev = netdev_priv(dev);
  183. caifdev->flowctrl = dev_flowctrl;
  184. caifd->layer.transmit = transmit;
  185. if (caifdev->use_frag)
  186. phy_type = CFPHYTYPE_FRAG;
  187. else
  188. phy_type = CFPHYTYPE_CAIF;
  189. switch (caifdev->link_select) {
  190. case CAIF_LINK_HIGH_BANDW:
  191. pref = CFPHYPREF_HIGH_BW;
  192. break;
  193. case CAIF_LINK_LOW_LATENCY:
  194. pref = CFPHYPREF_LOW_LAT;
  195. break;
  196. default:
  197. pref = CFPHYPREF_HIGH_BW;
  198. break;
  199. }
  200. strncpy(caifd->layer.name, dev->name,
  201. sizeof(caifd->layer.name) - 1);
  202. caifd->layer.name[sizeof(caifd->layer.name) - 1] = 0;
  203. mutex_lock(&caifdevs->lock);
  204. list_add_rcu(&caifd->list, &caifdevs->list);
  205. cfcnfg_add_phy_layer(cfg,
  206. phy_type,
  207. dev,
  208. &caifd->layer,
  209. pref,
  210. caifdev->use_fcs,
  211. caifdev->use_stx);
  212. mutex_unlock(&caifdevs->lock);
  213. break;
  214. case NETDEV_UP:
  215. rcu_read_lock();
  216. caifd = caif_get(dev);
  217. if (caifd == NULL) {
  218. rcu_read_unlock();
  219. break;
  220. }
  221. cfcnfg_set_phy_state(cfg, &caifd->layer, true);
  222. rcu_read_unlock();
  223. break;
  224. case NETDEV_DOWN:
  225. rcu_read_lock();
  226. caifd = caif_get(dev);
  227. if (!caifd || !caifd->layer.up || !caifd->layer.up->ctrlcmd) {
  228. rcu_read_unlock();
  229. return -EINVAL;
  230. }
  231. cfcnfg_set_phy_state(cfg, &caifd->layer, false);
  232. caifd_hold(caifd);
  233. rcu_read_unlock();
  234. caifd->layer.up->ctrlcmd(caifd->layer.up,
  235. _CAIF_CTRLCMD_PHYIF_DOWN_IND,
  236. caifd->layer.id);
  237. caifd_put(caifd);
  238. break;
  239. case NETDEV_UNREGISTER:
  240. mutex_lock(&caifdevs->lock);
  241. caifd = caif_get(dev);
  242. if (caifd == NULL) {
  243. mutex_unlock(&caifdevs->lock);
  244. break;
  245. }
  246. list_del_rcu(&caifd->list);
  247. /*
  248. * NETDEV_UNREGISTER is called repeatedly until all reference
  249. * counts for the net-device are released. If references to
  250. * caifd is taken, simply ignore NETDEV_UNREGISTER and wait for
  251. * the next call to NETDEV_UNREGISTER.
  252. *
  253. * If any packets are in flight down the CAIF Stack,
  254. * cfcnfg_del_phy_layer will return nonzero.
  255. * If no packets are in flight, the CAIF Stack associated
  256. * with the net-device un-registering is freed.
  257. */
  258. if (caifd_refcnt_read(caifd) != 0 ||
  259. cfcnfg_del_phy_layer(cfg, &caifd->layer) != 0) {
  260. pr_info("Wait for device inuse\n");
  261. /* Enrole device if CAIF Stack is still in use */
  262. list_add_rcu(&caifd->list, &caifdevs->list);
  263. mutex_unlock(&caifdevs->lock);
  264. break;
  265. }
  266. synchronize_rcu();
  267. dev_put(caifd->netdev);
  268. free_percpu(caifd->pcpu_refcnt);
  269. kfree(caifd);
  270. mutex_unlock(&caifdevs->lock);
  271. break;
  272. }
  273. return 0;
  274. }
  275. static struct notifier_block caif_device_notifier = {
  276. .notifier_call = caif_device_notify,
  277. .priority = 0,
  278. };
  279. /* Per-namespace Caif devices handling */
  280. static int caif_init_net(struct net *net)
  281. {
  282. struct caif_net *caifn = net_generic(net, caif_net_id);
  283. BUG_ON(!caifn);
  284. INIT_LIST_HEAD(&caifn->caifdevs.list);
  285. mutex_init(&caifn->caifdevs.lock);
  286. caifn->cfg = cfcnfg_create();
  287. if (!caifn->cfg) {
  288. pr_warn("can't create cfcnfg\n");
  289. return -ENOMEM;
  290. }
  291. return 0;
  292. }
  293. static void caif_exit_net(struct net *net)
  294. {
  295. struct caif_device_entry *caifd, *tmp;
  296. struct caif_device_entry_list *caifdevs =
  297. caif_device_list(net);
  298. struct cfcnfg *cfg;
  299. rtnl_lock();
  300. mutex_lock(&caifdevs->lock);
  301. cfg = get_cfcnfg(net);
  302. if (cfg == NULL) {
  303. mutex_unlock(&caifdevs->lock);
  304. return;
  305. }
  306. list_for_each_entry_safe(caifd, tmp, &caifdevs->list, list) {
  307. int i = 0;
  308. list_del_rcu(&caifd->list);
  309. cfcnfg_set_phy_state(cfg, &caifd->layer, false);
  310. while (i < 10 &&
  311. (caifd_refcnt_read(caifd) != 0 ||
  312. cfcnfg_del_phy_layer(cfg, &caifd->layer) != 0)) {
  313. pr_info("Wait for device inuse\n");
  314. msleep(250);
  315. i++;
  316. }
  317. synchronize_rcu();
  318. dev_put(caifd->netdev);
  319. free_percpu(caifd->pcpu_refcnt);
  320. kfree(caifd);
  321. }
  322. cfcnfg_remove(cfg);
  323. mutex_unlock(&caifdevs->lock);
  324. rtnl_unlock();
  325. }
  326. static struct pernet_operations caif_net_ops = {
  327. .init = caif_init_net,
  328. .exit = caif_exit_net,
  329. .id = &caif_net_id,
  330. .size = sizeof(struct caif_net),
  331. };
  332. /* Initialize Caif devices list */
  333. static int __init caif_device_init(void)
  334. {
  335. int result;
  336. result = register_pernet_device(&caif_net_ops);
  337. if (result)
  338. return result;
  339. register_netdevice_notifier(&caif_device_notifier);
  340. dev_add_pack(&caif_packet_type);
  341. return result;
  342. }
  343. static void __exit caif_device_exit(void)
  344. {
  345. unregister_pernet_device(&caif_net_ops);
  346. unregister_netdevice_notifier(&caif_device_notifier);
  347. dev_remove_pack(&caif_packet_type);
  348. }
  349. module_init(caif_device_init);
  350. module_exit(caif_device_exit);