caif_dev.c 13 KB

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