caif_dev.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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. /* notify Caif of device events */
  226. static int caif_device_notify(struct notifier_block *me, unsigned long what,
  227. void *arg)
  228. {
  229. struct net_device *dev = arg;
  230. struct caif_device_entry *caifd = NULL;
  231. struct caif_dev_common *caifdev;
  232. struct cfcnfg *cfg;
  233. struct cflayer *layer, *link_support;
  234. int head_room = 0;
  235. struct caif_device_entry_list *caifdevs;
  236. cfg = get_cfcnfg(dev_net(dev));
  237. caifdevs = caif_device_list(dev_net(dev));
  238. if (!cfg || !caifdevs)
  239. return 0;
  240. caifd = caif_get(dev);
  241. if (caifd == NULL && dev->type != ARPHRD_CAIF)
  242. return 0;
  243. switch (what) {
  244. case NETDEV_REGISTER:
  245. if (caifd != NULL)
  246. break;
  247. caifdev = netdev_priv(dev);
  248. link_support = NULL;
  249. if (caifdev->use_frag) {
  250. head_room = 1;
  251. link_support = cfserl_create(dev->ifindex,
  252. caifdev->use_stx);
  253. if (!link_support) {
  254. pr_warn("Out of memory\n");
  255. break;
  256. }
  257. }
  258. caif_enroll_dev(dev, caifdev, link_support, head_room,
  259. &layer, NULL);
  260. caifdev->flowctrl = dev_flowctrl;
  261. break;
  262. case NETDEV_UP:
  263. rcu_read_lock();
  264. caifd = caif_get(dev);
  265. if (caifd == NULL) {
  266. rcu_read_unlock();
  267. break;
  268. }
  269. cfcnfg_set_phy_state(cfg, &caifd->layer, true);
  270. rcu_read_unlock();
  271. break;
  272. case NETDEV_DOWN:
  273. rcu_read_lock();
  274. caifd = caif_get(dev);
  275. if (!caifd || !caifd->layer.up || !caifd->layer.up->ctrlcmd) {
  276. rcu_read_unlock();
  277. return -EINVAL;
  278. }
  279. cfcnfg_set_phy_state(cfg, &caifd->layer, false);
  280. caifd_hold(caifd);
  281. rcu_read_unlock();
  282. caifd->layer.up->ctrlcmd(caifd->layer.up,
  283. _CAIF_CTRLCMD_PHYIF_DOWN_IND,
  284. caifd->layer.id);
  285. caifd_put(caifd);
  286. break;
  287. case NETDEV_UNREGISTER:
  288. mutex_lock(&caifdevs->lock);
  289. caifd = caif_get(dev);
  290. if (caifd == NULL) {
  291. mutex_unlock(&caifdevs->lock);
  292. break;
  293. }
  294. list_del_rcu(&caifd->list);
  295. /*
  296. * NETDEV_UNREGISTER is called repeatedly until all reference
  297. * counts for the net-device are released. If references to
  298. * caifd is taken, simply ignore NETDEV_UNREGISTER and wait for
  299. * the next call to NETDEV_UNREGISTER.
  300. *
  301. * If any packets are in flight down the CAIF Stack,
  302. * cfcnfg_del_phy_layer will return nonzero.
  303. * If no packets are in flight, the CAIF Stack associated
  304. * with the net-device un-registering is freed.
  305. */
  306. if (caifd_refcnt_read(caifd) != 0 ||
  307. cfcnfg_del_phy_layer(cfg, &caifd->layer) != 0) {
  308. pr_info("Wait for device inuse\n");
  309. /* Enrole device if CAIF Stack is still in use */
  310. list_add_rcu(&caifd->list, &caifdevs->list);
  311. mutex_unlock(&caifdevs->lock);
  312. break;
  313. }
  314. synchronize_rcu();
  315. dev_put(caifd->netdev);
  316. free_percpu(caifd->pcpu_refcnt);
  317. kfree(caifd);
  318. mutex_unlock(&caifdevs->lock);
  319. break;
  320. }
  321. return 0;
  322. }
  323. static struct notifier_block caif_device_notifier = {
  324. .notifier_call = caif_device_notify,
  325. .priority = 0,
  326. };
  327. /* Per-namespace Caif devices handling */
  328. static int caif_init_net(struct net *net)
  329. {
  330. struct caif_net *caifn = net_generic(net, caif_net_id);
  331. BUG_ON(!caifn);
  332. INIT_LIST_HEAD(&caifn->caifdevs.list);
  333. mutex_init(&caifn->caifdevs.lock);
  334. caifn->cfg = cfcnfg_create();
  335. if (!caifn->cfg) {
  336. pr_warn("can't create cfcnfg\n");
  337. return -ENOMEM;
  338. }
  339. return 0;
  340. }
  341. static void caif_exit_net(struct net *net)
  342. {
  343. struct caif_device_entry *caifd, *tmp;
  344. struct caif_device_entry_list *caifdevs =
  345. caif_device_list(net);
  346. struct cfcnfg *cfg = get_cfcnfg(net);
  347. if (!cfg || !caifdevs)
  348. return;
  349. rtnl_lock();
  350. mutex_lock(&caifdevs->lock);
  351. list_for_each_entry_safe(caifd, tmp, &caifdevs->list, list) {
  352. int i = 0;
  353. list_del_rcu(&caifd->list);
  354. cfcnfg_set_phy_state(cfg, &caifd->layer, false);
  355. while (i < 10 &&
  356. (caifd_refcnt_read(caifd) != 0 ||
  357. cfcnfg_del_phy_layer(cfg, &caifd->layer) != 0)) {
  358. pr_info("Wait for device inuse\n");
  359. msleep(250);
  360. i++;
  361. }
  362. synchronize_rcu();
  363. dev_put(caifd->netdev);
  364. free_percpu(caifd->pcpu_refcnt);
  365. kfree(caifd);
  366. }
  367. cfcnfg_remove(cfg);
  368. mutex_unlock(&caifdevs->lock);
  369. rtnl_unlock();
  370. }
  371. static struct pernet_operations caif_net_ops = {
  372. .init = caif_init_net,
  373. .exit = caif_exit_net,
  374. .id = &caif_net_id,
  375. .size = sizeof(struct caif_net),
  376. };
  377. /* Initialize Caif devices list */
  378. static int __init caif_device_init(void)
  379. {
  380. int result;
  381. result = register_pernet_device(&caif_net_ops);
  382. if (result)
  383. return result;
  384. register_netdevice_notifier(&caif_device_notifier);
  385. dev_add_pack(&caif_packet_type);
  386. return result;
  387. }
  388. static void __exit caif_device_exit(void)
  389. {
  390. unregister_pernet_device(&caif_net_ops);
  391. unregister_netdevice_notifier(&caif_device_notifier);
  392. dev_remove_pack(&caif_packet_type);
  393. }
  394. module_init(caif_device_init);
  395. module_exit(caif_device_exit);