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