caif_dev.c 9.4 KB

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