caif_dev.c 13 KB

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