caif_dev.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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. return caifn->cfg;
  49. }
  50. EXPORT_SYMBOL(get_cfcnfg);
  51. static struct caif_device_entry_list *caif_device_list(struct net *net)
  52. {
  53. struct caif_net *caifn;
  54. BUG_ON(!net);
  55. caifn = net_generic(net, caif_net_id);
  56. return &caifn->caifdevs;
  57. }
  58. static void caifd_put(struct caif_device_entry *e)
  59. {
  60. irqsafe_cpu_dec(*e->pcpu_refcnt);
  61. }
  62. static void caifd_hold(struct caif_device_entry *e)
  63. {
  64. irqsafe_cpu_inc(*e->pcpu_refcnt);
  65. }
  66. static int caifd_refcnt_read(struct caif_device_entry *e)
  67. {
  68. int i, refcnt = 0;
  69. for_each_possible_cpu(i)
  70. refcnt += *per_cpu_ptr(e->pcpu_refcnt, i);
  71. return refcnt;
  72. }
  73. /* Allocate new CAIF device. */
  74. static struct caif_device_entry *caif_device_alloc(struct net_device *dev)
  75. {
  76. struct caif_device_entry_list *caifdevs;
  77. struct caif_device_entry *caifd;
  78. caifdevs = caif_device_list(dev_net(dev));
  79. caifd = kzalloc(sizeof(*caifd), GFP_ATOMIC);
  80. if (!caifd)
  81. return NULL;
  82. caifd->pcpu_refcnt = alloc_percpu(int);
  83. caifd->netdev = dev;
  84. dev_hold(dev);
  85. return caifd;
  86. }
  87. static struct caif_device_entry *caif_get(struct net_device *dev)
  88. {
  89. struct caif_device_entry_list *caifdevs =
  90. caif_device_list(dev_net(dev));
  91. struct caif_device_entry *caifd;
  92. list_for_each_entry_rcu(caifd, &caifdevs->list, list) {
  93. if (caifd->netdev == dev)
  94. return caifd;
  95. }
  96. return NULL;
  97. }
  98. static int transmit(struct cflayer *layer, struct cfpkt *pkt)
  99. {
  100. int err;
  101. struct caif_device_entry *caifd =
  102. container_of(layer, struct caif_device_entry, layer);
  103. struct sk_buff *skb;
  104. skb = cfpkt_tonative(pkt);
  105. skb->dev = caifd->netdev;
  106. err = dev_queue_xmit(skb);
  107. if (err > 0)
  108. err = -EIO;
  109. return err;
  110. }
  111. /*
  112. * Stuff received packets into the CAIF stack.
  113. * On error, returns non-zero and releases the skb.
  114. */
  115. static int receive(struct sk_buff *skb, struct net_device *dev,
  116. struct packet_type *pkttype, struct net_device *orig_dev)
  117. {
  118. struct cfpkt *pkt;
  119. struct caif_device_entry *caifd;
  120. int err;
  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. err = caifd->layer.up->receive(caifd->layer.up, pkt);
  134. /* For -EILSEQ the packet is not freed so so it now */
  135. if (err == -EILSEQ)
  136. cfpkt_destroy(pkt);
  137. /* Release reference to stack upwards */
  138. caifd_put(caifd);
  139. return 0;
  140. }
  141. static struct packet_type caif_packet_type __read_mostly = {
  142. .type = cpu_to_be16(ETH_P_CAIF),
  143. .func = receive,
  144. };
  145. static void dev_flowctrl(struct net_device *dev, int on)
  146. {
  147. struct caif_device_entry *caifd;
  148. rcu_read_lock();
  149. caifd = caif_get(dev);
  150. if (!caifd || !caifd->layer.up || !caifd->layer.up->ctrlcmd) {
  151. rcu_read_unlock();
  152. return;
  153. }
  154. caifd_hold(caifd);
  155. rcu_read_unlock();
  156. caifd->layer.up->ctrlcmd(caifd->layer.up,
  157. on ?
  158. _CAIF_CTRLCMD_PHYIF_FLOW_ON_IND :
  159. _CAIF_CTRLCMD_PHYIF_FLOW_OFF_IND,
  160. caifd->layer.id);
  161. caifd_put(caifd);
  162. }
  163. /* notify Caif of device events */
  164. static int caif_device_notify(struct notifier_block *me, unsigned long what,
  165. void *arg)
  166. {
  167. struct net_device *dev = arg;
  168. struct caif_device_entry *caifd = NULL;
  169. struct caif_dev_common *caifdev;
  170. enum cfcnfg_phy_preference pref;
  171. enum cfcnfg_phy_type phy_type;
  172. struct cfcnfg *cfg;
  173. struct caif_device_entry_list *caifdevs;
  174. if (dev->type != ARPHRD_CAIF)
  175. return 0;
  176. cfg = get_cfcnfg(dev_net(dev));
  177. if (cfg == NULL)
  178. return 0;
  179. caifdevs = caif_device_list(dev_net(dev));
  180. switch (what) {
  181. case NETDEV_REGISTER:
  182. caifd = caif_device_alloc(dev);
  183. if (!caifd)
  184. return 0;
  185. caifdev = netdev_priv(dev);
  186. caifdev->flowctrl = dev_flowctrl;
  187. caifd->layer.transmit = transmit;
  188. if (caifdev->use_frag)
  189. phy_type = CFPHYTYPE_FRAG;
  190. else
  191. phy_type = CFPHYTYPE_CAIF;
  192. switch (caifdev->link_select) {
  193. case CAIF_LINK_HIGH_BANDW:
  194. pref = CFPHYPREF_HIGH_BW;
  195. break;
  196. case CAIF_LINK_LOW_LATENCY:
  197. pref = CFPHYPREF_LOW_LAT;
  198. break;
  199. default:
  200. pref = CFPHYPREF_HIGH_BW;
  201. break;
  202. }
  203. strncpy(caifd->layer.name, dev->name,
  204. sizeof(caifd->layer.name) - 1);
  205. caifd->layer.name[sizeof(caifd->layer.name) - 1] = 0;
  206. mutex_lock(&caifdevs->lock);
  207. list_add_rcu(&caifd->list, &caifdevs->list);
  208. cfcnfg_add_phy_layer(cfg,
  209. phy_type,
  210. dev,
  211. &caifd->layer,
  212. pref,
  213. caifdev->use_fcs,
  214. caifdev->use_stx);
  215. mutex_unlock(&caifdevs->lock);
  216. break;
  217. case NETDEV_UP:
  218. rcu_read_lock();
  219. caifd = caif_get(dev);
  220. if (caifd == NULL) {
  221. rcu_read_unlock();
  222. break;
  223. }
  224. cfcnfg_set_phy_state(cfg, &caifd->layer, true);
  225. rcu_read_unlock();
  226. break;
  227. case NETDEV_DOWN:
  228. rcu_read_lock();
  229. caifd = caif_get(dev);
  230. if (!caifd || !caifd->layer.up || !caifd->layer.up->ctrlcmd) {
  231. rcu_read_unlock();
  232. return -EINVAL;
  233. }
  234. cfcnfg_set_phy_state(cfg, &caifd->layer, false);
  235. caifd_hold(caifd);
  236. rcu_read_unlock();
  237. caifd->layer.up->ctrlcmd(caifd->layer.up,
  238. _CAIF_CTRLCMD_PHYIF_DOWN_IND,
  239. caifd->layer.id);
  240. caifd_put(caifd);
  241. break;
  242. case NETDEV_UNREGISTER:
  243. mutex_lock(&caifdevs->lock);
  244. caifd = caif_get(dev);
  245. if (caifd == NULL) {
  246. mutex_unlock(&caifdevs->lock);
  247. break;
  248. }
  249. list_del_rcu(&caifd->list);
  250. /*
  251. * NETDEV_UNREGISTER is called repeatedly until all reference
  252. * counts for the net-device are released. If references to
  253. * caifd is taken, simply ignore NETDEV_UNREGISTER and wait for
  254. * the next call to NETDEV_UNREGISTER.
  255. *
  256. * If any packets are in flight down the CAIF Stack,
  257. * cfcnfg_del_phy_layer will return nonzero.
  258. * If no packets are in flight, the CAIF Stack associated
  259. * with the net-device un-registering is freed.
  260. */
  261. if (caifd_refcnt_read(caifd) != 0 ||
  262. cfcnfg_del_phy_layer(cfg, &caifd->layer) != 0) {
  263. pr_info("Wait for device inuse\n");
  264. /* Enrole device if CAIF Stack is still in use */
  265. list_add_rcu(&caifd->list, &caifdevs->list);
  266. mutex_unlock(&caifdevs->lock);
  267. break;
  268. }
  269. synchronize_rcu();
  270. dev_put(caifd->netdev);
  271. free_percpu(caifd->pcpu_refcnt);
  272. kfree(caifd);
  273. mutex_unlock(&caifdevs->lock);
  274. break;
  275. }
  276. return 0;
  277. }
  278. static struct notifier_block caif_device_notifier = {
  279. .notifier_call = caif_device_notify,
  280. .priority = 0,
  281. };
  282. /* Per-namespace Caif devices handling */
  283. static int caif_init_net(struct net *net)
  284. {
  285. struct caif_net *caifn = net_generic(net, caif_net_id);
  286. INIT_LIST_HEAD(&caifn->caifdevs.list);
  287. mutex_init(&caifn->caifdevs.lock);
  288. caifn->cfg = cfcnfg_create();
  289. if (!caifn->cfg) {
  290. pr_warn("can't create cfcnfg\n");
  291. return -ENOMEM;
  292. }
  293. return 0;
  294. }
  295. static void caif_exit_net(struct net *net)
  296. {
  297. struct caif_device_entry *caifd, *tmp;
  298. struct caif_device_entry_list *caifdevs =
  299. caif_device_list(net);
  300. struct cfcnfg *cfg;
  301. rtnl_lock();
  302. mutex_lock(&caifdevs->lock);
  303. cfg = get_cfcnfg(net);
  304. if (cfg == NULL) {
  305. mutex_unlock(&caifdevs->lock);
  306. return;
  307. }
  308. list_for_each_entry_safe(caifd, tmp, &caifdevs->list, list) {
  309. int i = 0;
  310. list_del_rcu(&caifd->list);
  311. cfcnfg_set_phy_state(cfg, &caifd->layer, false);
  312. while (i < 10 &&
  313. (caifd_refcnt_read(caifd) != 0 ||
  314. cfcnfg_del_phy_layer(cfg, &caifd->layer) != 0)) {
  315. pr_info("Wait for device inuse\n");
  316. msleep(250);
  317. i++;
  318. }
  319. synchronize_rcu();
  320. dev_put(caifd->netdev);
  321. free_percpu(caifd->pcpu_refcnt);
  322. kfree(caifd);
  323. }
  324. cfcnfg_remove(cfg);
  325. mutex_unlock(&caifdevs->lock);
  326. rtnl_unlock();
  327. }
  328. static struct pernet_operations caif_net_ops = {
  329. .init = caif_init_net,
  330. .exit = caif_exit_net,
  331. .id = &caif_net_id,
  332. .size = sizeof(struct caif_net),
  333. };
  334. /* Initialize Caif devices list */
  335. static int __init caif_device_init(void)
  336. {
  337. int result;
  338. result = register_pernet_subsys(&caif_net_ops);
  339. if (result)
  340. return result;
  341. register_netdevice_notifier(&caif_device_notifier);
  342. dev_add_pack(&caif_packet_type);
  343. return result;
  344. }
  345. static void __exit caif_device_exit(void)
  346. {
  347. unregister_pernet_subsys(&caif_net_ops);
  348. unregister_netdevice_notifier(&caif_device_notifier);
  349. dev_remove_pack(&caif_packet_type);
  350. }
  351. module_init(caif_device_init);
  352. module_exit(caif_device_exit);