caif_dev.c 10 KB

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