caif_dev.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. /*
  2. * CAIF Interface registration.
  3. * Copyright (C) ST-Ericsson AB 2010
  4. * Author: Sjur Brendeland
  5. * License terms: GNU General Public License (GPL) version 2
  6. *
  7. * Borrowed heavily from file: pn_dev.c. Thanks to Remi Denis-Courmont
  8. * and Sakari Ailus <sakari.ailus@nokia.com>
  9. */
  10. #define pr_fmt(fmt) KBUILD_MODNAME ":%s(): " fmt, __func__
  11. #include <linux/kernel.h>
  12. #include <linux/if_arp.h>
  13. #include <linux/net.h>
  14. #include <linux/netdevice.h>
  15. #include <linux/mutex.h>
  16. #include <linux/module.h>
  17. #include <linux/spinlock.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/caif_dev.h>
  24. #include <net/caif/cfpkt.h>
  25. #include <net/caif/cfcnfg.h>
  26. #include <net/caif/cfserl.h>
  27. MODULE_LICENSE("GPL");
  28. /* Used for local tracking of the CAIF net devices */
  29. struct caif_device_entry {
  30. struct cflayer layer;
  31. struct list_head list;
  32. struct net_device *netdev;
  33. int __percpu *pcpu_refcnt;
  34. spinlock_t flow_lock;
  35. struct sk_buff *xoff_skb;
  36. void (*xoff_skb_dtor)(struct sk_buff *skb);
  37. bool xoff;
  38. };
  39. struct caif_device_entry_list {
  40. struct list_head list;
  41. /* Protects simulanous deletes in list */
  42. struct mutex lock;
  43. };
  44. struct caif_net {
  45. struct cfcnfg *cfg;
  46. struct caif_device_entry_list caifdevs;
  47. };
  48. static unsigned int caif_net_id;
  49. static int q_high = 50; /* Percent */
  50. struct cfcnfg *get_cfcnfg(struct net *net)
  51. {
  52. struct caif_net *caifn;
  53. caifn = net_generic(net, caif_net_id);
  54. return caifn->cfg;
  55. }
  56. EXPORT_SYMBOL(get_cfcnfg);
  57. static struct caif_device_entry_list *caif_device_list(struct net *net)
  58. {
  59. struct caif_net *caifn;
  60. caifn = net_generic(net, caif_net_id);
  61. return &caifn->caifdevs;
  62. }
  63. static void caifd_put(struct caif_device_entry *e)
  64. {
  65. this_cpu_dec(*e->pcpu_refcnt);
  66. }
  67. static void caifd_hold(struct caif_device_entry *e)
  68. {
  69. this_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 *caifd;
  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. list_for_each_entry_rcu(caifd, &caifdevs->list, list) {
  100. if (caifd->netdev == dev)
  101. return caifd;
  102. }
  103. return NULL;
  104. }
  105. static void caif_flow_cb(struct sk_buff *skb)
  106. {
  107. struct caif_device_entry *caifd;
  108. void (*dtor)(struct sk_buff *skb) = NULL;
  109. bool send_xoff;
  110. WARN_ON(skb->dev == NULL);
  111. rcu_read_lock();
  112. caifd = caif_get(skb->dev);
  113. WARN_ON(caifd == NULL);
  114. if (!caifd) {
  115. rcu_read_unlock();
  116. return;
  117. }
  118. caifd_hold(caifd);
  119. rcu_read_unlock();
  120. spin_lock_bh(&caifd->flow_lock);
  121. send_xoff = caifd->xoff;
  122. caifd->xoff = 0;
  123. dtor = caifd->xoff_skb_dtor;
  124. if (WARN_ON(caifd->xoff_skb != skb))
  125. skb = NULL;
  126. caifd->xoff_skb = NULL;
  127. caifd->xoff_skb_dtor = NULL;
  128. spin_unlock_bh(&caifd->flow_lock);
  129. if (dtor && skb)
  130. dtor(skb);
  131. if (send_xoff)
  132. caifd->layer.up->
  133. ctrlcmd(caifd->layer.up,
  134. _CAIF_CTRLCMD_PHYIF_FLOW_ON_IND,
  135. caifd->layer.id);
  136. caifd_put(caifd);
  137. }
  138. static int transmit(struct cflayer *layer, struct cfpkt *pkt)
  139. {
  140. int err, high = 0, qlen = 0;
  141. struct caif_device_entry *caifd =
  142. container_of(layer, struct caif_device_entry, layer);
  143. struct sk_buff *skb;
  144. struct netdev_queue *txq;
  145. rcu_read_lock_bh();
  146. skb = cfpkt_tonative(pkt);
  147. skb->dev = caifd->netdev;
  148. skb_reset_network_header(skb);
  149. skb->protocol = htons(ETH_P_CAIF);
  150. /* Check if we need to handle xoff */
  151. if (likely(caifd->netdev->priv_flags & IFF_NO_QUEUE))
  152. goto noxoff;
  153. if (unlikely(caifd->xoff))
  154. goto noxoff;
  155. if (likely(!netif_queue_stopped(caifd->netdev))) {
  156. /* If we run with a TX queue, check if the queue is too long*/
  157. txq = netdev_get_tx_queue(skb->dev, 0);
  158. qlen = qdisc_qlen(rcu_dereference_bh(txq->qdisc));
  159. if (likely(qlen == 0))
  160. goto noxoff;
  161. high = (caifd->netdev->tx_queue_len * q_high) / 100;
  162. if (likely(qlen < high))
  163. goto noxoff;
  164. }
  165. /* Hold lock while accessing xoff */
  166. spin_lock_bh(&caifd->flow_lock);
  167. if (caifd->xoff) {
  168. spin_unlock_bh(&caifd->flow_lock);
  169. goto noxoff;
  170. }
  171. /*
  172. * Handle flow off, we do this by temporary hi-jacking this
  173. * skb's destructor function, and replace it with our own
  174. * flow-on callback. The callback will set flow-on and call
  175. * the original destructor.
  176. */
  177. pr_debug("queue has stopped(%d) or is full (%d > %d)\n",
  178. netif_queue_stopped(caifd->netdev),
  179. qlen, high);
  180. caifd->xoff = 1;
  181. caifd->xoff_skb = skb;
  182. caifd->xoff_skb_dtor = skb->destructor;
  183. skb->destructor = caif_flow_cb;
  184. spin_unlock_bh(&caifd->flow_lock);
  185. caifd->layer.up->ctrlcmd(caifd->layer.up,
  186. _CAIF_CTRLCMD_PHYIF_FLOW_OFF_IND,
  187. caifd->layer.id);
  188. noxoff:
  189. rcu_read_unlock_bh();
  190. err = dev_queue_xmit(skb);
  191. if (err > 0)
  192. err = -EIO;
  193. return err;
  194. }
  195. /*
  196. * Stuff received packets into the CAIF stack.
  197. * On error, returns non-zero and releases the skb.
  198. */
  199. static int receive(struct sk_buff *skb, struct net_device *dev,
  200. struct packet_type *pkttype, struct net_device *orig_dev)
  201. {
  202. struct cfpkt *pkt;
  203. struct caif_device_entry *caifd;
  204. int err;
  205. pkt = cfpkt_fromnative(CAIF_DIR_IN, skb);
  206. rcu_read_lock();
  207. caifd = caif_get(dev);
  208. if (!caifd || !caifd->layer.up || !caifd->layer.up->receive ||
  209. !netif_oper_up(caifd->netdev)) {
  210. rcu_read_unlock();
  211. kfree_skb(skb);
  212. return NET_RX_DROP;
  213. }
  214. /* Hold reference to netdevice while using CAIF stack */
  215. caifd_hold(caifd);
  216. rcu_read_unlock();
  217. err = caifd->layer.up->receive(caifd->layer.up, pkt);
  218. /* For -EILSEQ the packet is not freed so so it now */
  219. if (err == -EILSEQ)
  220. cfpkt_destroy(pkt);
  221. /* Release reference to stack upwards */
  222. caifd_put(caifd);
  223. if (err != 0)
  224. err = NET_RX_DROP;
  225. return err;
  226. }
  227. static struct packet_type caif_packet_type __read_mostly = {
  228. .type = cpu_to_be16(ETH_P_CAIF),
  229. .func = receive,
  230. };
  231. static void dev_flowctrl(struct net_device *dev, int on)
  232. {
  233. struct caif_device_entry *caifd;
  234. rcu_read_lock();
  235. caifd = caif_get(dev);
  236. if (!caifd || !caifd->layer.up || !caifd->layer.up->ctrlcmd) {
  237. rcu_read_unlock();
  238. return;
  239. }
  240. caifd_hold(caifd);
  241. rcu_read_unlock();
  242. caifd->layer.up->ctrlcmd(caifd->layer.up,
  243. on ?
  244. _CAIF_CTRLCMD_PHYIF_FLOW_ON_IND :
  245. _CAIF_CTRLCMD_PHYIF_FLOW_OFF_IND,
  246. caifd->layer.id);
  247. caifd_put(caifd);
  248. }
  249. void caif_enroll_dev(struct net_device *dev, struct caif_dev_common *caifdev,
  250. struct cflayer *link_support, int head_room,
  251. struct cflayer **layer,
  252. int (**rcv_func)(struct sk_buff *, struct net_device *,
  253. struct packet_type *,
  254. struct net_device *))
  255. {
  256. struct caif_device_entry *caifd;
  257. enum cfcnfg_phy_preference pref;
  258. struct cfcnfg *cfg = get_cfcnfg(dev_net(dev));
  259. struct caif_device_entry_list *caifdevs;
  260. caifdevs = caif_device_list(dev_net(dev));
  261. caifd = caif_device_alloc(dev);
  262. if (!caifd)
  263. return;
  264. *layer = &caifd->layer;
  265. spin_lock_init(&caifd->flow_lock);
  266. switch (caifdev->link_select) {
  267. case CAIF_LINK_HIGH_BANDW:
  268. pref = CFPHYPREF_HIGH_BW;
  269. break;
  270. case CAIF_LINK_LOW_LATENCY:
  271. pref = CFPHYPREF_LOW_LAT;
  272. break;
  273. default:
  274. pref = CFPHYPREF_HIGH_BW;
  275. break;
  276. }
  277. mutex_lock(&caifdevs->lock);
  278. list_add_rcu(&caifd->list, &caifdevs->list);
  279. strlcpy(caifd->layer.name, dev->name,
  280. sizeof(caifd->layer.name));
  281. caifd->layer.transmit = transmit;
  282. cfcnfg_add_phy_layer(cfg,
  283. dev,
  284. &caifd->layer,
  285. pref,
  286. link_support,
  287. caifdev->use_fcs,
  288. head_room);
  289. mutex_unlock(&caifdevs->lock);
  290. if (rcv_func)
  291. *rcv_func = receive;
  292. }
  293. EXPORT_SYMBOL(caif_enroll_dev);
  294. /* notify Caif of device events */
  295. static int caif_device_notify(struct notifier_block *me, unsigned long what,
  296. void *ptr)
  297. {
  298. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  299. struct caif_device_entry *caifd = NULL;
  300. struct caif_dev_common *caifdev;
  301. struct cfcnfg *cfg;
  302. struct cflayer *layer, *link_support;
  303. int head_room = 0;
  304. struct caif_device_entry_list *caifdevs;
  305. cfg = get_cfcnfg(dev_net(dev));
  306. caifdevs = caif_device_list(dev_net(dev));
  307. caifd = caif_get(dev);
  308. if (caifd == NULL && dev->type != ARPHRD_CAIF)
  309. return 0;
  310. switch (what) {
  311. case NETDEV_REGISTER:
  312. if (caifd != NULL)
  313. break;
  314. caifdev = netdev_priv(dev);
  315. link_support = NULL;
  316. if (caifdev->use_frag) {
  317. head_room = 1;
  318. link_support = cfserl_create(dev->ifindex,
  319. caifdev->use_stx);
  320. if (!link_support) {
  321. pr_warn("Out of memory\n");
  322. break;
  323. }
  324. }
  325. caif_enroll_dev(dev, caifdev, link_support, head_room,
  326. &layer, NULL);
  327. caifdev->flowctrl = dev_flowctrl;
  328. break;
  329. case NETDEV_UP:
  330. rcu_read_lock();
  331. caifd = caif_get(dev);
  332. if (caifd == NULL) {
  333. rcu_read_unlock();
  334. break;
  335. }
  336. caifd->xoff = 0;
  337. cfcnfg_set_phy_state(cfg, &caifd->layer, true);
  338. rcu_read_unlock();
  339. break;
  340. case NETDEV_DOWN:
  341. rcu_read_lock();
  342. caifd = caif_get(dev);
  343. if (!caifd || !caifd->layer.up || !caifd->layer.up->ctrlcmd) {
  344. rcu_read_unlock();
  345. return -EINVAL;
  346. }
  347. cfcnfg_set_phy_state(cfg, &caifd->layer, false);
  348. caifd_hold(caifd);
  349. rcu_read_unlock();
  350. caifd->layer.up->ctrlcmd(caifd->layer.up,
  351. _CAIF_CTRLCMD_PHYIF_DOWN_IND,
  352. caifd->layer.id);
  353. spin_lock_bh(&caifd->flow_lock);
  354. /*
  355. * Replace our xoff-destructor with original destructor.
  356. * We trust that skb->destructor *always* is called before
  357. * the skb reference is invalid. The hijacked SKB destructor
  358. * takes the flow_lock so manipulating the skb->destructor here
  359. * should be safe.
  360. */
  361. if (caifd->xoff_skb_dtor != NULL && caifd->xoff_skb != NULL)
  362. caifd->xoff_skb->destructor = caifd->xoff_skb_dtor;
  363. caifd->xoff = 0;
  364. caifd->xoff_skb_dtor = NULL;
  365. caifd->xoff_skb = NULL;
  366. spin_unlock_bh(&caifd->flow_lock);
  367. caifd_put(caifd);
  368. break;
  369. case NETDEV_UNREGISTER:
  370. mutex_lock(&caifdevs->lock);
  371. caifd = caif_get(dev);
  372. if (caifd == NULL) {
  373. mutex_unlock(&caifdevs->lock);
  374. break;
  375. }
  376. list_del_rcu(&caifd->list);
  377. /*
  378. * NETDEV_UNREGISTER is called repeatedly until all reference
  379. * counts for the net-device are released. If references to
  380. * caifd is taken, simply ignore NETDEV_UNREGISTER and wait for
  381. * the next call to NETDEV_UNREGISTER.
  382. *
  383. * If any packets are in flight down the CAIF Stack,
  384. * cfcnfg_del_phy_layer will return nonzero.
  385. * If no packets are in flight, the CAIF Stack associated
  386. * with the net-device un-registering is freed.
  387. */
  388. if (caifd_refcnt_read(caifd) != 0 ||
  389. cfcnfg_del_phy_layer(cfg, &caifd->layer) != 0) {
  390. pr_info("Wait for device inuse\n");
  391. /* Enrole device if CAIF Stack is still in use */
  392. list_add_rcu(&caifd->list, &caifdevs->list);
  393. mutex_unlock(&caifdevs->lock);
  394. break;
  395. }
  396. synchronize_rcu();
  397. dev_put(caifd->netdev);
  398. free_percpu(caifd->pcpu_refcnt);
  399. kfree(caifd);
  400. mutex_unlock(&caifdevs->lock);
  401. break;
  402. }
  403. return 0;
  404. }
  405. static struct notifier_block caif_device_notifier = {
  406. .notifier_call = caif_device_notify,
  407. .priority = 0,
  408. };
  409. /* Per-namespace Caif devices handling */
  410. static int caif_init_net(struct net *net)
  411. {
  412. struct caif_net *caifn = net_generic(net, caif_net_id);
  413. INIT_LIST_HEAD(&caifn->caifdevs.list);
  414. mutex_init(&caifn->caifdevs.lock);
  415. caifn->cfg = cfcnfg_create();
  416. if (!caifn->cfg)
  417. return -ENOMEM;
  418. return 0;
  419. }
  420. static void caif_exit_net(struct net *net)
  421. {
  422. struct caif_device_entry *caifd, *tmp;
  423. struct caif_device_entry_list *caifdevs =
  424. caif_device_list(net);
  425. struct cfcnfg *cfg = get_cfcnfg(net);
  426. rtnl_lock();
  427. mutex_lock(&caifdevs->lock);
  428. list_for_each_entry_safe(caifd, tmp, &caifdevs->list, list) {
  429. int i = 0;
  430. list_del_rcu(&caifd->list);
  431. cfcnfg_set_phy_state(cfg, &caifd->layer, false);
  432. while (i < 10 &&
  433. (caifd_refcnt_read(caifd) != 0 ||
  434. cfcnfg_del_phy_layer(cfg, &caifd->layer) != 0)) {
  435. pr_info("Wait for device inuse\n");
  436. msleep(250);
  437. i++;
  438. }
  439. synchronize_rcu();
  440. dev_put(caifd->netdev);
  441. free_percpu(caifd->pcpu_refcnt);
  442. kfree(caifd);
  443. }
  444. cfcnfg_remove(cfg);
  445. mutex_unlock(&caifdevs->lock);
  446. rtnl_unlock();
  447. }
  448. static struct pernet_operations caif_net_ops = {
  449. .init = caif_init_net,
  450. .exit = caif_exit_net,
  451. .id = &caif_net_id,
  452. .size = sizeof(struct caif_net),
  453. };
  454. /* Initialize Caif devices list */
  455. static int __init caif_device_init(void)
  456. {
  457. int result;
  458. result = register_pernet_subsys(&caif_net_ops);
  459. if (result)
  460. return result;
  461. register_netdevice_notifier(&caif_device_notifier);
  462. dev_add_pack(&caif_packet_type);
  463. return result;
  464. }
  465. static void __exit caif_device_exit(void)
  466. {
  467. unregister_netdevice_notifier(&caif_device_notifier);
  468. dev_remove_pack(&caif_packet_type);
  469. unregister_pernet_subsys(&caif_net_ops);
  470. }
  471. module_init(caif_device_init);
  472. module_exit(caif_device_exit);