caif_dev.c 13 KB

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