caif_dev.c 13 KB

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