caif_dev.c 9.4 KB

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