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>
10 #define _J1939_PRIV_H_
12 #include <linux/can/j1939.h>
15 /* Timeout to receive the abort signal over loop back. In case CAN
16 * bus is open, the timeout should be triggered.
18 #define J1939_XTP_ABORT_TIMEOUT_MS 500
19 #define J1939_SIMPLE_ECHO_TIMEOUT_MS (10 * 1000)
22 enum j1939_sk_errqueue_type
{
30 struct list_head list
;
34 /* indicates that this ecu successfully claimed @sa as its address */
35 struct hrtimer ac_timer
;
37 struct j1939_priv
*priv
;
39 /* count users, to help transport protocol decide for interaction */
44 struct list_head ecus
;
45 /* local list entry in priv
46 * These allow irq (& softirq) context lookups on j1939 devices
47 * This approach (separate lists) is done as the other 2 alternatives
48 * are not easier or even wrong
49 * 1) using the pure kobject methods involves mutexes, which are not
50 * allowed in irq context.
51 * 2) duplicating data structures would require a lot of synchronization
56 /* segments need a lock to protect the above list */
59 struct net_device
*ndev
;
61 /* list of 256 ecu ptrs, that cache the claimed addresses.
62 * also protected by the above lock
64 struct j1939_addr_ent
{
65 struct j1939_ecu
*ecu
;
66 /* count users, to help transport protocol */
72 /* List of active sessions to prevent start of conflicting
75 * Do not start two sessions of same type, addresses and
78 struct list_head active_session_list
;
80 /* protects active_session_list */
81 spinlock_t active_session_list_lock
;
83 unsigned int tp_max_packet_size
;
85 /* lock for j1939_socks list */
86 spinlock_t j1939_socks_lock
;
87 struct list_head j1939_socks
;
92 void j1939_ecu_put(struct j1939_ecu
*ecu
);
94 /* keep the cache of what is local */
95 int j1939_local_ecu_get(struct j1939_priv
*priv
, name_t name
, u8 sa
);
96 void j1939_local_ecu_put(struct j1939_priv
*priv
, name_t name
, u8 sa
);
98 static inline bool j1939_address_is_unicast(u8 addr
)
100 return addr
<= J1939_MAX_UNICAST_ADDR
;
103 static inline bool j1939_address_is_idle(u8 addr
)
105 return addr
== J1939_IDLE_ADDR
;
108 static inline bool j1939_address_is_valid(u8 addr
)
110 return addr
!= J1939_NO_ADDR
;
113 static inline bool j1939_pgn_is_pdu1(pgn_t pgn
)
115 /* ignore dp & res bits for this */
116 return (pgn
& 0xff00) < 0xf000;
119 /* utility to correctly unmap an ECU */
120 void j1939_ecu_unmap_locked(struct j1939_ecu
*ecu
);
121 void j1939_ecu_unmap(struct j1939_ecu
*ecu
);
123 u8
j1939_name_to_addr(struct j1939_priv
*priv
, name_t name
);
124 struct j1939_ecu
*j1939_ecu_find_by_addr_locked(struct j1939_priv
*priv
,
126 struct j1939_ecu
*j1939_ecu_get_by_addr(struct j1939_priv
*priv
, u8 addr
);
127 struct j1939_ecu
*j1939_ecu_get_by_addr_locked(struct j1939_priv
*priv
,
129 struct j1939_ecu
*j1939_ecu_get_by_name(struct j1939_priv
*priv
, name_t name
);
130 struct j1939_ecu
*j1939_ecu_get_by_name_locked(struct j1939_priv
*priv
,
133 enum j1939_transfer_type
{
150 /* control buffer of the sk_buff */
151 struct j1939_sk_buff_cb
{
152 /* Offset in bytes within one ETP session */
155 /* for tx, MSG_SYN will be used to sync on sockets */
159 struct j1939_addr addr
;
161 /* Flags for quick lookups during skb processing.
162 * These are set in the receive path only.
164 #define J1939_ECU_LOCAL_SRC BIT(0)
165 #define J1939_ECU_LOCAL_DST BIT(1)
172 struct j1939_sk_buff_cb
*j1939_skb_to_cb(const struct sk_buff
*skb
)
174 BUILD_BUG_ON(sizeof(struct j1939_sk_buff_cb
) > sizeof(skb
->cb
));
176 return (struct j1939_sk_buff_cb
*)skb
->cb
;
179 int j1939_send_one(struct j1939_priv
*priv
, struct sk_buff
*skb
);
180 void j1939_sk_recv(struct j1939_priv
*priv
, struct sk_buff
*skb
);
181 bool j1939_sk_recv_match(struct j1939_priv
*priv
,
182 struct j1939_sk_buff_cb
*skcb
);
183 void j1939_sk_send_loop_abort(struct sock
*sk
, int err
);
184 void j1939_sk_errqueue(struct j1939_session
*session
,
185 enum j1939_sk_errqueue_type type
);
186 void j1939_sk_queue_activate_next(struct j1939_session
*session
);
189 struct j1939_session
*j1939_tp_send(struct j1939_priv
*priv
,
190 struct sk_buff
*skb
, size_t size
);
191 int j1939_tp_recv(struct j1939_priv
*priv
, struct sk_buff
*skb
);
192 int j1939_ac_fixup(struct j1939_priv
*priv
, struct sk_buff
*skb
);
193 void j1939_ac_recv(struct j1939_priv
*priv
, struct sk_buff
*skb
);
194 void j1939_simple_recv(struct j1939_priv
*priv
, struct sk_buff
*skb
);
196 /* network management */
197 struct j1939_ecu
*j1939_ecu_create_locked(struct j1939_priv
*priv
, name_t name
);
199 void j1939_ecu_timer_start(struct j1939_ecu
*ecu
);
200 void j1939_ecu_timer_cancel(struct j1939_ecu
*ecu
);
201 void j1939_ecu_unmap_all(struct j1939_priv
*priv
);
203 struct j1939_priv
*j1939_netdev_start(struct net_device
*ndev
);
204 void j1939_netdev_stop(struct j1939_priv
*priv
);
206 void j1939_priv_put(struct j1939_priv
*priv
);
207 void j1939_priv_get(struct j1939_priv
*priv
);
209 /* notify/alert all j1939 sockets bound to ifindex */
210 void j1939_sk_netdev_event_netdown(struct j1939_priv
*priv
);
211 int j1939_cancel_active_session(struct j1939_priv
*priv
, struct sock
*sk
);
212 void j1939_tp_init(struct j1939_priv
*priv
);
214 /* decrement pending skb for a j1939 socket */
215 void j1939_sock_pending_del(struct sock
*sk
);
217 enum j1939_session_state
{
219 J1939_SESSION_ACTIVE
,
220 /* waiting for abort signal on the bus */
221 J1939_SESSION_WAITING_ABORT
,
222 J1939_SESSION_ACTIVE_MAX
,
226 struct j1939_session
{
227 struct j1939_priv
*priv
;
228 struct list_head active_session_list_entry
;
229 struct list_head sk_session_queue_entry
;
233 /* ifindex, src, dst, pgn define the session block
234 * the are _never_ modified after insertion in the list
235 * this decreases locking problems a _lot_
237 struct j1939_sk_buff_cb skcb
;
238 struct sk_buff_head skb_queue
;
240 /* all tx related stuff (last_txcmd, pkt.tx)
241 * is protected (modified only) with the txtimer hrtimer
242 * 'total' & 'block' are never changed,
243 * last_cmd, last & block are protected by ->lock
244 * this means that the tx may run after cts is received that should
245 * have stopped tx, but this time discrepancy is never avoided anyhow
247 u8 last_cmd
, last_txcmd
;
250 /* Total message size, number of bytes */
251 unsigned int total_message_size
;
252 /* Total number of bytes queue from socket to the session */
253 unsigned int total_queued_size
;
254 unsigned int tx_retry
;
258 enum j1939_session_state state
;
260 /* Packets counters for a (extended) transfer session. The packet is
261 * maximal of 7 bytes.
264 /* total - total number of packets for this session */
266 /* last - last packet of a transfer block after which
267 * responder should send ETP.CM_CTS and originator
271 /* tx - number of packets send by originator node.
272 * this counter can be set back if responder node
273 * didn't received all packets send by originator.
276 unsigned int tx_acked
;
277 /* rx - number of packets received */
279 /* block - amount of packets expected in one block */
281 /* dpo - ETP.CM_DPO, Data Packet Offset */
284 struct hrtimer txtimer
, rxtimer
;
288 struct sock sk
; /* must be first to skip with memset */
289 struct j1939_priv
*priv
;
290 struct list_head list
;
292 #define J1939_SOCK_BOUND BIT(0)
293 #define J1939_SOCK_CONNECTED BIT(1)
294 #define J1939_SOCK_PROMISC BIT(2)
295 #define J1939_SOCK_ERRQUEUE BIT(3)
299 struct j1939_addr addr
;
300 struct j1939_filter
*filters
;
304 /* j1939 may emit equal PGN (!= equal CAN-id's) out of order
305 * when transport protocol comes in.
306 * To allow emitting in order, keep a 'pending' nr. of packets
308 atomic_t skb_pending
;
309 wait_queue_head_t waitq
;
311 /* lock for the sk_session_queue list */
312 spinlock_t sk_session_queue_lock
;
313 struct list_head sk_session_queue
;
316 static inline struct j1939_sock
*j1939_sk(const struct sock
*sk
)
318 return container_of(sk
, struct j1939_sock
, sk
);
321 void j1939_session_get(struct j1939_session
*session
);
322 void j1939_session_put(struct j1939_session
*session
);
323 void j1939_session_skb_queue(struct j1939_session
*session
,
324 struct sk_buff
*skb
);
325 int j1939_session_activate(struct j1939_session
*session
);
326 void j1939_tp_schedule_txtimer(struct j1939_session
*session
, int msec
);
327 void j1939_session_timers_cancel(struct j1939_session
*session
);
329 #define J1939_MAX_TP_PACKET_SIZE (7 * 0xff)
330 #define J1939_MAX_ETP_PACKET_SIZE (7 * 0x00ffffff)
332 #define J1939_REGULAR 0
333 #define J1939_EXTENDED 1
336 extern const struct can_proto j1939_can_proto
;
338 #endif /* _J1939_PRIV_H_ */