bus.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (c) 2010-2011 EIA Electronics,
  3. // Kurt Van Dijck <[email protected]>
  4. // Copyright (c) 2017-2019 Pengutronix,
  5. // Marc Kleine-Budde <[email protected]>
  6. // Copyright (c) 2017-2019 Pengutronix,
  7. // Oleksij Rempel <[email protected]>
  8. /* bus for j1939 remote devices
  9. * Since rtnetlink, no real bus is used.
  10. */
  11. #include <net/sock.h>
  12. #include "j1939-priv.h"
  13. static void __j1939_ecu_release(struct kref *kref)
  14. {
  15. struct j1939_ecu *ecu = container_of(kref, struct j1939_ecu, kref);
  16. struct j1939_priv *priv = ecu->priv;
  17. list_del(&ecu->list);
  18. kfree(ecu);
  19. j1939_priv_put(priv);
  20. }
  21. void j1939_ecu_put(struct j1939_ecu *ecu)
  22. {
  23. kref_put(&ecu->kref, __j1939_ecu_release);
  24. }
  25. static void j1939_ecu_get(struct j1939_ecu *ecu)
  26. {
  27. kref_get(&ecu->kref);
  28. }
  29. static bool j1939_ecu_is_mapped_locked(struct j1939_ecu *ecu)
  30. {
  31. struct j1939_priv *priv = ecu->priv;
  32. lockdep_assert_held(&priv->lock);
  33. return j1939_ecu_find_by_addr_locked(priv, ecu->addr) == ecu;
  34. }
  35. /* ECU device interface */
  36. /* map ECU to a bus address space */
  37. static void j1939_ecu_map_locked(struct j1939_ecu *ecu)
  38. {
  39. struct j1939_priv *priv = ecu->priv;
  40. struct j1939_addr_ent *ent;
  41. lockdep_assert_held(&priv->lock);
  42. if (!j1939_address_is_unicast(ecu->addr))
  43. return;
  44. ent = &priv->ents[ecu->addr];
  45. if (ent->ecu) {
  46. netdev_warn(priv->ndev, "Trying to map already mapped ECU, addr: 0x%02x, name: 0x%016llx. Skip it.\n",
  47. ecu->addr, ecu->name);
  48. return;
  49. }
  50. j1939_ecu_get(ecu);
  51. ent->ecu = ecu;
  52. ent->nusers += ecu->nusers;
  53. }
  54. /* unmap ECU from a bus address space */
  55. void j1939_ecu_unmap_locked(struct j1939_ecu *ecu)
  56. {
  57. struct j1939_priv *priv = ecu->priv;
  58. struct j1939_addr_ent *ent;
  59. lockdep_assert_held(&priv->lock);
  60. if (!j1939_address_is_unicast(ecu->addr))
  61. return;
  62. if (!j1939_ecu_is_mapped_locked(ecu))
  63. return;
  64. ent = &priv->ents[ecu->addr];
  65. ent->ecu = NULL;
  66. ent->nusers -= ecu->nusers;
  67. j1939_ecu_put(ecu);
  68. }
  69. void j1939_ecu_unmap(struct j1939_ecu *ecu)
  70. {
  71. write_lock_bh(&ecu->priv->lock);
  72. j1939_ecu_unmap_locked(ecu);
  73. write_unlock_bh(&ecu->priv->lock);
  74. }
  75. void j1939_ecu_unmap_all(struct j1939_priv *priv)
  76. {
  77. int i;
  78. write_lock_bh(&priv->lock);
  79. for (i = 0; i < ARRAY_SIZE(priv->ents); i++)
  80. if (priv->ents[i].ecu)
  81. j1939_ecu_unmap_locked(priv->ents[i].ecu);
  82. write_unlock_bh(&priv->lock);
  83. }
  84. void j1939_ecu_timer_start(struct j1939_ecu *ecu)
  85. {
  86. /* The ECU is held here and released in the
  87. * j1939_ecu_timer_handler() or j1939_ecu_timer_cancel().
  88. */
  89. j1939_ecu_get(ecu);
  90. /* Schedule timer in 250 msec to commit address change. */
  91. hrtimer_start(&ecu->ac_timer, ms_to_ktime(250),
  92. HRTIMER_MODE_REL_SOFT);
  93. }
  94. void j1939_ecu_timer_cancel(struct j1939_ecu *ecu)
  95. {
  96. if (hrtimer_cancel(&ecu->ac_timer))
  97. j1939_ecu_put(ecu);
  98. }
  99. static enum hrtimer_restart j1939_ecu_timer_handler(struct hrtimer *hrtimer)
  100. {
  101. struct j1939_ecu *ecu =
  102. container_of(hrtimer, struct j1939_ecu, ac_timer);
  103. struct j1939_priv *priv = ecu->priv;
  104. write_lock_bh(&priv->lock);
  105. /* TODO: can we test if ecu->addr is unicast before starting
  106. * the timer?
  107. */
  108. j1939_ecu_map_locked(ecu);
  109. /* The corresponding j1939_ecu_get() is in
  110. * j1939_ecu_timer_start().
  111. */
  112. j1939_ecu_put(ecu);
  113. write_unlock_bh(&priv->lock);
  114. return HRTIMER_NORESTART;
  115. }
  116. struct j1939_ecu *j1939_ecu_create_locked(struct j1939_priv *priv, name_t name)
  117. {
  118. struct j1939_ecu *ecu;
  119. lockdep_assert_held(&priv->lock);
  120. ecu = kzalloc(sizeof(*ecu), gfp_any());
  121. if (!ecu)
  122. return ERR_PTR(-ENOMEM);
  123. kref_init(&ecu->kref);
  124. ecu->addr = J1939_IDLE_ADDR;
  125. ecu->name = name;
  126. hrtimer_init(&ecu->ac_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_SOFT);
  127. ecu->ac_timer.function = j1939_ecu_timer_handler;
  128. INIT_LIST_HEAD(&ecu->list);
  129. j1939_priv_get(priv);
  130. ecu->priv = priv;
  131. list_add_tail(&ecu->list, &priv->ecus);
  132. return ecu;
  133. }
  134. struct j1939_ecu *j1939_ecu_find_by_addr_locked(struct j1939_priv *priv,
  135. u8 addr)
  136. {
  137. lockdep_assert_held(&priv->lock);
  138. return priv->ents[addr].ecu;
  139. }
  140. struct j1939_ecu *j1939_ecu_get_by_addr_locked(struct j1939_priv *priv, u8 addr)
  141. {
  142. struct j1939_ecu *ecu;
  143. lockdep_assert_held(&priv->lock);
  144. if (!j1939_address_is_unicast(addr))
  145. return NULL;
  146. ecu = j1939_ecu_find_by_addr_locked(priv, addr);
  147. if (ecu)
  148. j1939_ecu_get(ecu);
  149. return ecu;
  150. }
  151. struct j1939_ecu *j1939_ecu_get_by_addr(struct j1939_priv *priv, u8 addr)
  152. {
  153. struct j1939_ecu *ecu;
  154. read_lock_bh(&priv->lock);
  155. ecu = j1939_ecu_get_by_addr_locked(priv, addr);
  156. read_unlock_bh(&priv->lock);
  157. return ecu;
  158. }
  159. /* get pointer to ecu without increasing ref counter */
  160. static struct j1939_ecu *j1939_ecu_find_by_name_locked(struct j1939_priv *priv,
  161. name_t name)
  162. {
  163. struct j1939_ecu *ecu;
  164. lockdep_assert_held(&priv->lock);
  165. list_for_each_entry(ecu, &priv->ecus, list) {
  166. if (ecu->name == name)
  167. return ecu;
  168. }
  169. return NULL;
  170. }
  171. struct j1939_ecu *j1939_ecu_get_by_name_locked(struct j1939_priv *priv,
  172. name_t name)
  173. {
  174. struct j1939_ecu *ecu;
  175. lockdep_assert_held(&priv->lock);
  176. if (!name)
  177. return NULL;
  178. ecu = j1939_ecu_find_by_name_locked(priv, name);
  179. if (ecu)
  180. j1939_ecu_get(ecu);
  181. return ecu;
  182. }
  183. struct j1939_ecu *j1939_ecu_get_by_name(struct j1939_priv *priv, name_t name)
  184. {
  185. struct j1939_ecu *ecu;
  186. read_lock_bh(&priv->lock);
  187. ecu = j1939_ecu_get_by_name_locked(priv, name);
  188. read_unlock_bh(&priv->lock);
  189. return ecu;
  190. }
  191. u8 j1939_name_to_addr(struct j1939_priv *priv, name_t name)
  192. {
  193. struct j1939_ecu *ecu;
  194. int addr = J1939_IDLE_ADDR;
  195. if (!name)
  196. return J1939_NO_ADDR;
  197. read_lock_bh(&priv->lock);
  198. ecu = j1939_ecu_find_by_name_locked(priv, name);
  199. if (ecu && j1939_ecu_is_mapped_locked(ecu))
  200. /* ecu's SA is registered */
  201. addr = ecu->addr;
  202. read_unlock_bh(&priv->lock);
  203. return addr;
  204. }
  205. /* TX addr/name accounting
  206. * Transport protocol needs to know if a SA is local or not
  207. * These functions originate from userspace manipulating sockets,
  208. * so locking is straigforward
  209. */
  210. int j1939_local_ecu_get(struct j1939_priv *priv, name_t name, u8 sa)
  211. {
  212. struct j1939_ecu *ecu;
  213. int err = 0;
  214. write_lock_bh(&priv->lock);
  215. if (j1939_address_is_unicast(sa))
  216. priv->ents[sa].nusers++;
  217. if (!name)
  218. goto done;
  219. ecu = j1939_ecu_get_by_name_locked(priv, name);
  220. if (!ecu)
  221. ecu = j1939_ecu_create_locked(priv, name);
  222. err = PTR_ERR_OR_ZERO(ecu);
  223. if (err)
  224. goto done;
  225. ecu->nusers++;
  226. /* TODO: do we care if ecu->addr != sa? */
  227. if (j1939_ecu_is_mapped_locked(ecu))
  228. /* ecu's sa is active already */
  229. priv->ents[ecu->addr].nusers++;
  230. done:
  231. write_unlock_bh(&priv->lock);
  232. return err;
  233. }
  234. void j1939_local_ecu_put(struct j1939_priv *priv, name_t name, u8 sa)
  235. {
  236. struct j1939_ecu *ecu;
  237. write_lock_bh(&priv->lock);
  238. if (j1939_address_is_unicast(sa))
  239. priv->ents[sa].nusers--;
  240. if (!name)
  241. goto done;
  242. ecu = j1939_ecu_find_by_name_locked(priv, name);
  243. if (WARN_ON_ONCE(!ecu))
  244. goto done;
  245. ecu->nusers--;
  246. /* TODO: do we care if ecu->addr != sa? */
  247. if (j1939_ecu_is_mapped_locked(ecu))
  248. /* ecu's sa is active already */
  249. priv->ents[ecu->addr].nusers--;
  250. j1939_ecu_put(ecu);
  251. done:
  252. write_unlock_bh(&priv->lock);
  253. }