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