caif_dev.c 13 KB

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