caif_dev.c 13 KB

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