caif_dev.c 9.4 KB

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