xref: /linux/drivers/firmware/arm_scmi/driver.c (revision f7f0adfe64de08803990dc4cbecd2849c04e314a)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * System Control and Management Interface (SCMI) Message Protocol driver
4  *
5  * SCMI Message Protocol is used between the System Control Processor(SCP)
6  * and the Application Processors(AP). The Message Handling Unit(MHU)
7  * provides a mechanism for inter-processor communication between SCP's
8  * Cortex M3 and AP.
9  *
10  * SCP offers control and management of the core/cluster power states,
11  * various power domain DVFS including the core/cluster, certain system
12  * clocks configuration, thermal sensors and many others.
13  *
14  * Copyright (C) 2018-2024 ARM Ltd.
15  */
16 
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18 
19 #include <linux/bitmap.h>
20 #include <linux/debugfs.h>
21 #include <linux/device.h>
22 #include <linux/export.h>
23 #include <linux/idr.h>
24 #include <linux/io.h>
25 #include <linux/io-64-nonatomic-hi-lo.h>
26 #include <linux/kernel.h>
27 #include <linux/kmod.h>
28 #include <linux/ktime.h>
29 #include <linux/hashtable.h>
30 #include <linux/list.h>
31 #include <linux/module.h>
32 #include <linux/of.h>
33 #include <linux/platform_device.h>
34 #include <linux/processor.h>
35 #include <linux/refcount.h>
36 #include <linux/slab.h>
37 #include <linux/xarray.h>
38 
39 #include "common.h"
40 #include "notify.h"
41 
42 #include "raw_mode.h"
43 
44 #define CREATE_TRACE_POINTS
45 #include <trace/events/scmi.h>
46 
47 #define SCMI_VENDOR_MODULE_ALIAS_FMT	"scmi-protocol-0x%02x-%s"
48 
49 static DEFINE_IDA(scmi_id);
50 
51 static DEFINE_XARRAY(scmi_protocols);
52 
53 /* List of all SCMI devices active in system */
54 static LIST_HEAD(scmi_list);
55 /* Protection for the entire list */
56 static DEFINE_MUTEX(scmi_list_mutex);
57 /* Track the unique id for the transfers for debug & profiling purpose */
58 static atomic_t transfer_last_id;
59 
60 static struct dentry *scmi_top_dentry;
61 
62 /**
63  * struct scmi_xfers_info - Structure to manage transfer information
64  *
65  * @xfer_alloc_table: Bitmap table for allocated messages.
66  *	Index of this bitmap table is also used for message
67  *	sequence identifier.
68  * @xfer_lock: Protection for message allocation
69  * @max_msg: Maximum number of messages that can be pending
70  * @free_xfers: A free list for available to use xfers. It is initialized with
71  *		a number of xfers equal to the maximum allowed in-flight
72  *		messages.
73  * @pending_xfers: An hashtable, indexed by msg_hdr.seq, used to keep all the
74  *		   currently in-flight messages.
75  */
76 struct scmi_xfers_info {
77 	unsigned long *xfer_alloc_table;
78 	spinlock_t xfer_lock;
79 	int max_msg;
80 	struct hlist_head free_xfers;
81 	DECLARE_HASHTABLE(pending_xfers, SCMI_PENDING_XFERS_HT_ORDER_SZ);
82 };
83 
84 /**
85  * struct scmi_protocol_instance  - Describe an initialized protocol instance.
86  * @handle: Reference to the SCMI handle associated to this protocol instance.
87  * @proto: A reference to the protocol descriptor.
88  * @gid: A reference for per-protocol devres management.
89  * @users: A refcount to track effective users of this protocol.
90  * @priv: Reference for optional protocol private data.
91  * @version: Protocol version supported by the platform as detected at runtime.
92  * @negotiated_version: When the platform supports a newer protocol version,
93  *			the agent will try to negotiate with the platform the
94  *			usage of the newest version known to it, since
95  *			backward compatibility is NOT automatically assured.
96  *			This field is NON-zero when a successful negotiation
97  *			has completed.
98  * @ph: An embedded protocol handle that will be passed down to protocol
99  *	initialization code to identify this instance.
100  *
101  * Each protocol is initialized independently once for each SCMI platform in
102  * which is defined by DT and implemented by the SCMI server fw.
103  */
104 struct scmi_protocol_instance {
105 	const struct scmi_handle	*handle;
106 	const struct scmi_protocol	*proto;
107 	void				*gid;
108 	refcount_t			users;
109 	void				*priv;
110 	unsigned int			version;
111 	unsigned int			negotiated_version;
112 	struct scmi_protocol_handle	ph;
113 };
114 
115 #define ph_to_pi(h)	container_of(h, struct scmi_protocol_instance, ph)
116 
117 /**
118  * struct scmi_debug_info  - Debug common info
119  * @top_dentry: A reference to the top debugfs dentry
120  * @name: Name of this SCMI instance
121  * @type: Type of this SCMI instance
122  * @is_atomic: Flag to state if the transport of this instance is atomic
123  * @counters: An array of atomic_c's used for tracking statistics (if enabled)
124  */
125 struct scmi_debug_info {
126 	struct dentry *top_dentry;
127 	const char *name;
128 	const char *type;
129 	bool is_atomic;
130 	atomic_t counters[SCMI_DEBUG_COUNTERS_LAST];
131 };
132 
133 /**
134  * struct scmi_info - Structure representing a SCMI instance
135  *
136  * @id: A sequence number starting from zero identifying this instance
137  * @dev: Device pointer
138  * @desc: SoC description for this instance
139  * @version: SCMI revision information containing protocol version,
140  *	implementation version and (sub-)vendor identification.
141  * @handle: Instance of SCMI handle to send to clients
142  * @tx_minfo: Universal Transmit Message management info
143  * @rx_minfo: Universal Receive Message management info
144  * @tx_idr: IDR object to map protocol id to Tx channel info pointer
145  * @rx_idr: IDR object to map protocol id to Rx channel info pointer
146  * @protocols: IDR for protocols' instance descriptors initialized for
147  *	       this SCMI instance: populated on protocol's first attempted
148  *	       usage.
149  * @protocols_mtx: A mutex to protect protocols instances initialization.
150  * @protocols_imp: List of protocols implemented, currently maximum of
151  *		   scmi_revision_info.num_protocols elements allocated by the
152  *		   base protocol
153  * @active_protocols: IDR storing device_nodes for protocols actually defined
154  *		      in the DT and confirmed as implemented by fw.
155  * @notify_priv: Pointer to private data structure specific to notifications.
156  * @node: List head
157  * @users: Number of users of this instance
158  * @bus_nb: A notifier to listen for device bind/unbind on the scmi bus
159  * @dev_req_nb: A notifier to listen for device request/unrequest on the scmi
160  *		bus
161  * @devreq_mtx: A mutex to serialize device creation for this SCMI instance
162  * @dbg: A pointer to debugfs related data (if any)
163  * @raw: An opaque reference handle used by SCMI Raw mode.
164  */
165 struct scmi_info {
166 	int id;
167 	struct device *dev;
168 	const struct scmi_desc *desc;
169 	struct scmi_revision_info version;
170 	struct scmi_handle handle;
171 	struct scmi_xfers_info tx_minfo;
172 	struct scmi_xfers_info rx_minfo;
173 	struct idr tx_idr;
174 	struct idr rx_idr;
175 	struct idr protocols;
176 	/* Ensure mutual exclusive access to protocols instance array */
177 	struct mutex protocols_mtx;
178 	u8 *protocols_imp;
179 	struct idr active_protocols;
180 	void *notify_priv;
181 	struct list_head node;
182 	int users;
183 	struct notifier_block bus_nb;
184 	struct notifier_block dev_req_nb;
185 	/* Serialize device creation process for this instance */
186 	struct mutex devreq_mtx;
187 	struct scmi_debug_info *dbg;
188 	void *raw;
189 };
190 
191 #define handle_to_scmi_info(h)	container_of(h, struct scmi_info, handle)
192 #define bus_nb_to_scmi_info(nb)	container_of(nb, struct scmi_info, bus_nb)
193 #define req_nb_to_scmi_info(nb)	container_of(nb, struct scmi_info, dev_req_nb)
194 
195 static void scmi_rx_callback(struct scmi_chan_info *cinfo,
196 			     u32 msg_hdr, void *priv);
197 static void scmi_bad_message_trace(struct scmi_chan_info *cinfo,
198 				   u32 msg_hdr, enum scmi_bad_msg err);
199 
200 static struct scmi_transport_core_operations scmi_trans_core_ops = {
201 	.bad_message_trace = scmi_bad_message_trace,
202 	.rx_callback = scmi_rx_callback,
203 };
204 
205 static unsigned long
206 scmi_vendor_protocol_signature(unsigned int protocol_id, char *vendor_id,
207 			       char *sub_vendor_id, u32 impl_ver)
208 {
209 	char *signature, *p;
210 	unsigned long hash = 0;
211 
212 	/* vendor_id/sub_vendor_id guaranteed <= SCMI_SHORT_NAME_MAX_SIZE */
213 	signature = kasprintf(GFP_KERNEL, "%02X|%s|%s|0x%08X", protocol_id,
214 			      vendor_id ?: "", sub_vendor_id ?: "", impl_ver);
215 	if (!signature)
216 		return 0;
217 
218 	p = signature;
219 	while (*p)
220 		hash = partial_name_hash(tolower(*p++), hash);
221 	hash = end_name_hash(hash);
222 
223 	kfree(signature);
224 
225 	return hash;
226 }
227 
228 static unsigned long
229 scmi_protocol_key_calculate(int protocol_id, char *vendor_id,
230 			    char *sub_vendor_id, u32 impl_ver)
231 {
232 	if (protocol_id < SCMI_PROTOCOL_VENDOR_BASE)
233 		return protocol_id;
234 	else
235 		return scmi_vendor_protocol_signature(protocol_id, vendor_id,
236 						      sub_vendor_id, impl_ver);
237 }
238 
239 static const struct scmi_protocol *
240 __scmi_vendor_protocol_lookup(int protocol_id, char *vendor_id,
241 			      char *sub_vendor_id, u32 impl_ver)
242 {
243 	unsigned long key;
244 	struct scmi_protocol *proto = NULL;
245 
246 	key = scmi_protocol_key_calculate(protocol_id, vendor_id,
247 					  sub_vendor_id, impl_ver);
248 	if (key)
249 		proto = xa_load(&scmi_protocols, key);
250 
251 	return proto;
252 }
253 
254 static const struct scmi_protocol *
255 scmi_vendor_protocol_lookup(int protocol_id, char *vendor_id,
256 			    char *sub_vendor_id, u32 impl_ver)
257 {
258 	const struct scmi_protocol *proto = NULL;
259 
260 	/* Searching for closest match ...*/
261 	proto = __scmi_vendor_protocol_lookup(protocol_id, vendor_id,
262 					      sub_vendor_id, impl_ver);
263 	if (proto)
264 		return proto;
265 
266 	/* Any match just on vendor/sub_vendor ? */
267 	if (impl_ver) {
268 		proto = __scmi_vendor_protocol_lookup(protocol_id, vendor_id,
269 						      sub_vendor_id, 0);
270 		if (proto)
271 			return proto;
272 	}
273 
274 	/* Any match just on the vendor ? */
275 	if (sub_vendor_id)
276 		proto = __scmi_vendor_protocol_lookup(protocol_id, vendor_id,
277 						      NULL, 0);
278 	return proto;
279 }
280 
281 static const struct scmi_protocol *
282 scmi_vendor_protocol_get(int protocol_id, struct scmi_revision_info *version)
283 {
284 	const struct scmi_protocol *proto;
285 
286 	proto = scmi_vendor_protocol_lookup(protocol_id, version->vendor_id,
287 					    version->sub_vendor_id,
288 					    version->impl_ver);
289 	if (!proto) {
290 		int ret;
291 
292 		pr_debug("Looking for '" SCMI_VENDOR_MODULE_ALIAS_FMT "'\n",
293 			 protocol_id, version->vendor_id);
294 
295 		/* Note that vendor_id is mandatory for vendor protocols */
296 		ret = request_module(SCMI_VENDOR_MODULE_ALIAS_FMT,
297 				     protocol_id, version->vendor_id);
298 		if (ret) {
299 			pr_warn("Problem loading module for protocol 0x%x\n",
300 				protocol_id);
301 			return NULL;
302 		}
303 
304 		/* Lookup again, once modules loaded */
305 		proto = scmi_vendor_protocol_lookup(protocol_id,
306 						    version->vendor_id,
307 						    version->sub_vendor_id,
308 						    version->impl_ver);
309 	}
310 
311 	if (proto)
312 		pr_info("Loaded SCMI Vendor Protocol 0x%x - %s %s %X\n",
313 			protocol_id, proto->vendor_id ?: "",
314 			proto->sub_vendor_id ?: "", proto->impl_ver);
315 
316 	return proto;
317 }
318 
319 static const struct scmi_protocol *
320 scmi_protocol_get(int protocol_id, struct scmi_revision_info *version)
321 {
322 	const struct scmi_protocol *proto = NULL;
323 
324 	if (protocol_id < SCMI_PROTOCOL_VENDOR_BASE)
325 		proto = xa_load(&scmi_protocols, protocol_id);
326 	else
327 		proto = scmi_vendor_protocol_get(protocol_id, version);
328 
329 	if (!proto || !try_module_get(proto->owner)) {
330 		pr_warn("SCMI Protocol 0x%x not found!\n", protocol_id);
331 		return NULL;
332 	}
333 
334 	pr_debug("Found SCMI Protocol 0x%x\n", protocol_id);
335 
336 	return proto;
337 }
338 
339 static void scmi_protocol_put(const struct scmi_protocol *proto)
340 {
341 	if (proto)
342 		module_put(proto->owner);
343 }
344 
345 static int scmi_vendor_protocol_check(const struct scmi_protocol *proto)
346 {
347 	if (!proto->vendor_id) {
348 		pr_err("missing vendor_id for protocol 0x%x\n", proto->id);
349 		return -EINVAL;
350 	}
351 
352 	if (strlen(proto->vendor_id) >= SCMI_SHORT_NAME_MAX_SIZE) {
353 		pr_err("malformed vendor_id for protocol 0x%x\n", proto->id);
354 		return -EINVAL;
355 	}
356 
357 	if (proto->sub_vendor_id &&
358 	    strlen(proto->sub_vendor_id) >= SCMI_SHORT_NAME_MAX_SIZE) {
359 		pr_err("malformed sub_vendor_id for protocol 0x%x\n",
360 		       proto->id);
361 		return -EINVAL;
362 	}
363 
364 	return 0;
365 }
366 
367 int scmi_protocol_register(const struct scmi_protocol *proto)
368 {
369 	int ret;
370 	unsigned long key;
371 
372 	if (!proto) {
373 		pr_err("invalid protocol\n");
374 		return -EINVAL;
375 	}
376 
377 	if (!proto->instance_init) {
378 		pr_err("missing init for protocol 0x%x\n", proto->id);
379 		return -EINVAL;
380 	}
381 
382 	if (proto->id >= SCMI_PROTOCOL_VENDOR_BASE &&
383 	    scmi_vendor_protocol_check(proto))
384 		return -EINVAL;
385 
386 	/*
387 	 * Calculate a protocol key to register this protocol with the core;
388 	 * key value 0 is considered invalid.
389 	 */
390 	key = scmi_protocol_key_calculate(proto->id, proto->vendor_id,
391 					  proto->sub_vendor_id,
392 					  proto->impl_ver);
393 	if (!key)
394 		return -EINVAL;
395 
396 	ret = xa_insert(&scmi_protocols, key, (void *)proto, GFP_KERNEL);
397 	if (ret) {
398 		pr_err("unable to allocate SCMI protocol slot for 0x%x - err %d\n",
399 		       proto->id, ret);
400 		return ret;
401 	}
402 
403 	pr_debug("Registered SCMI Protocol 0x%x - %s  %s  0x%08X\n",
404 		 proto->id, proto->vendor_id, proto->sub_vendor_id,
405 		 proto->impl_ver);
406 
407 	return 0;
408 }
409 EXPORT_SYMBOL_GPL(scmi_protocol_register);
410 
411 void scmi_protocol_unregister(const struct scmi_protocol *proto)
412 {
413 	unsigned long key;
414 
415 	key = scmi_protocol_key_calculate(proto->id, proto->vendor_id,
416 					  proto->sub_vendor_id,
417 					  proto->impl_ver);
418 	if (!key)
419 		return;
420 
421 	xa_erase(&scmi_protocols, key);
422 
423 	pr_debug("Unregistered SCMI Protocol 0x%x\n", proto->id);
424 }
425 EXPORT_SYMBOL_GPL(scmi_protocol_unregister);
426 
427 /**
428  * scmi_create_protocol_devices  - Create devices for all pending requests for
429  * this SCMI instance.
430  *
431  * @np: The device node describing the protocol
432  * @info: The SCMI instance descriptor
433  * @prot_id: The protocol ID
434  * @name: The optional name of the device to be created: if not provided this
435  *	  call will lead to the creation of all the devices currently requested
436  *	  for the specified protocol.
437  */
438 static void scmi_create_protocol_devices(struct device_node *np,
439 					 struct scmi_info *info,
440 					 int prot_id, const char *name)
441 {
442 	struct scmi_device *sdev;
443 
444 	mutex_lock(&info->devreq_mtx);
445 	sdev = scmi_device_create(np, info->dev, prot_id, name);
446 	if (name && !sdev)
447 		dev_err(info->dev,
448 			"failed to create device for protocol 0x%X (%s)\n",
449 			prot_id, name);
450 	mutex_unlock(&info->devreq_mtx);
451 }
452 
453 static void scmi_destroy_protocol_devices(struct scmi_info *info,
454 					  int prot_id, const char *name)
455 {
456 	mutex_lock(&info->devreq_mtx);
457 	scmi_device_destroy(info->dev, prot_id, name);
458 	mutex_unlock(&info->devreq_mtx);
459 }
460 
461 void scmi_notification_instance_data_set(const struct scmi_handle *handle,
462 					 void *priv)
463 {
464 	struct scmi_info *info = handle_to_scmi_info(handle);
465 
466 	info->notify_priv = priv;
467 	/* Ensure updated protocol private date are visible */
468 	smp_wmb();
469 }
470 
471 void *scmi_notification_instance_data_get(const struct scmi_handle *handle)
472 {
473 	struct scmi_info *info = handle_to_scmi_info(handle);
474 
475 	/* Ensure protocols_private_data has been updated */
476 	smp_rmb();
477 	return info->notify_priv;
478 }
479 
480 /**
481  * scmi_xfer_token_set  - Reserve and set new token for the xfer at hand
482  *
483  * @minfo: Pointer to Tx/Rx Message management info based on channel type
484  * @xfer: The xfer to act upon
485  *
486  * Pick the next unused monotonically increasing token and set it into
487  * xfer->hdr.seq: picking a monotonically increasing value avoids immediate
488  * reuse of freshly completed or timed-out xfers, thus mitigating the risk
489  * of incorrect association of a late and expired xfer with a live in-flight
490  * transaction, both happening to re-use the same token identifier.
491  *
492  * Since platform is NOT required to answer our request in-order we should
493  * account for a few rare but possible scenarios:
494  *
495  *  - exactly 'next_token' may be NOT available so pick xfer_id >= next_token
496  *    using find_next_zero_bit() starting from candidate next_token bit
497  *
498  *  - all tokens ahead upto (MSG_TOKEN_ID_MASK - 1) are used in-flight but we
499  *    are plenty of free tokens at start, so try a second pass using
500  *    find_next_zero_bit() and starting from 0.
501  *
502  *  X = used in-flight
503  *
504  * Normal
505  * ------
506  *
507  *		|- xfer_id picked
508  *   -----------+----------------------------------------------------------
509  *   | | |X|X|X| | | | | | ... ... ... ... ... ... ... ... ... ... ...|X|X|
510  *   ----------------------------------------------------------------------
511  *		^
512  *		|- next_token
513  *
514  * Out-of-order pending at start
515  * -----------------------------
516  *
517  *	  |- xfer_id picked, last_token fixed
518  *   -----+----------------------------------------------------------------
519  *   |X|X| | | | |X|X| ... ... ... ... ... ... ... ... ... ... ... ...|X| |
520  *   ----------------------------------------------------------------------
521  *    ^
522  *    |- next_token
523  *
524  *
525  * Out-of-order pending at end
526  * ---------------------------
527  *
528  *	  |- xfer_id picked, last_token fixed
529  *   -----+----------------------------------------------------------------
530  *   |X|X| | | | |X|X| ... ... ... ... ... ... ... ... ... ... |X|X|X||X|X|
531  *   ----------------------------------------------------------------------
532  *								^
533  *								|- next_token
534  *
535  * Context: Assumes to be called with @xfer_lock already acquired.
536  *
537  * Return: 0 on Success or error
538  */
539 static int scmi_xfer_token_set(struct scmi_xfers_info *minfo,
540 			       struct scmi_xfer *xfer)
541 {
542 	unsigned long xfer_id, next_token;
543 
544 	/*
545 	 * Pick a candidate monotonic token in range [0, MSG_TOKEN_MAX - 1]
546 	 * using the pre-allocated transfer_id as a base.
547 	 * Note that the global transfer_id is shared across all message types
548 	 * so there could be holes in the allocated set of monotonic sequence
549 	 * numbers, but that is going to limit the effectiveness of the
550 	 * mitigation only in very rare limit conditions.
551 	 */
552 	next_token = (xfer->transfer_id & (MSG_TOKEN_MAX - 1));
553 
554 	/* Pick the next available xfer_id >= next_token */
555 	xfer_id = find_next_zero_bit(minfo->xfer_alloc_table,
556 				     MSG_TOKEN_MAX, next_token);
557 	if (xfer_id == MSG_TOKEN_MAX) {
558 		/*
559 		 * After heavily out-of-order responses, there are no free
560 		 * tokens ahead, but only at start of xfer_alloc_table so
561 		 * try again from the beginning.
562 		 */
563 		xfer_id = find_next_zero_bit(minfo->xfer_alloc_table,
564 					     MSG_TOKEN_MAX, 0);
565 		/*
566 		 * Something is wrong if we got here since there can be a
567 		 * maximum number of (MSG_TOKEN_MAX - 1) in-flight messages
568 		 * but we have not found any free token [0, MSG_TOKEN_MAX - 1].
569 		 */
570 		if (WARN_ON_ONCE(xfer_id == MSG_TOKEN_MAX))
571 			return -ENOMEM;
572 	}
573 
574 	/* Update +/- last_token accordingly if we skipped some hole */
575 	if (xfer_id != next_token)
576 		atomic_add((int)(xfer_id - next_token), &transfer_last_id);
577 
578 	xfer->hdr.seq = (u16)xfer_id;
579 
580 	return 0;
581 }
582 
583 /**
584  * scmi_xfer_token_clear  - Release the token
585  *
586  * @minfo: Pointer to Tx/Rx Message management info based on channel type
587  * @xfer: The xfer to act upon
588  */
589 static inline void scmi_xfer_token_clear(struct scmi_xfers_info *minfo,
590 					 struct scmi_xfer *xfer)
591 {
592 	clear_bit(xfer->hdr.seq, minfo->xfer_alloc_table);
593 }
594 
595 /**
596  * scmi_xfer_inflight_register_unlocked  - Register the xfer as in-flight
597  *
598  * @xfer: The xfer to register
599  * @minfo: Pointer to Tx/Rx Message management info based on channel type
600  *
601  * Note that this helper assumes that the xfer to be registered as in-flight
602  * had been built using an xfer sequence number which still corresponds to a
603  * free slot in the xfer_alloc_table.
604  *
605  * Context: Assumes to be called with @xfer_lock already acquired.
606  */
607 static inline void
608 scmi_xfer_inflight_register_unlocked(struct scmi_xfer *xfer,
609 				     struct scmi_xfers_info *minfo)
610 {
611 	/* Set in-flight */
612 	set_bit(xfer->hdr.seq, minfo->xfer_alloc_table);
613 	hash_add(minfo->pending_xfers, &xfer->node, xfer->hdr.seq);
614 	xfer->pending = true;
615 }
616 
617 /**
618  * scmi_xfer_inflight_register  - Try to register an xfer as in-flight
619  *
620  * @xfer: The xfer to register
621  * @minfo: Pointer to Tx/Rx Message management info based on channel type
622  *
623  * Note that this helper does NOT assume anything about the sequence number
624  * that was baked into the provided xfer, so it checks at first if it can
625  * be mapped to a free slot and fails with an error if another xfer with the
626  * same sequence number is currently still registered as in-flight.
627  *
628  * Return: 0 on Success or -EBUSY if sequence number embedded in the xfer
629  *	   could not rbe mapped to a free slot in the xfer_alloc_table.
630  */
631 static int scmi_xfer_inflight_register(struct scmi_xfer *xfer,
632 				       struct scmi_xfers_info *minfo)
633 {
634 	int ret = 0;
635 	unsigned long flags;
636 
637 	spin_lock_irqsave(&minfo->xfer_lock, flags);
638 	if (!test_bit(xfer->hdr.seq, minfo->xfer_alloc_table))
639 		scmi_xfer_inflight_register_unlocked(xfer, minfo);
640 	else
641 		ret = -EBUSY;
642 	spin_unlock_irqrestore(&minfo->xfer_lock, flags);
643 
644 	return ret;
645 }
646 
647 /**
648  * scmi_xfer_raw_inflight_register  - An helper to register the given xfer as in
649  * flight on the TX channel, if possible.
650  *
651  * @handle: Pointer to SCMI entity handle
652  * @xfer: The xfer to register
653  *
654  * Return: 0 on Success, error otherwise
655  */
656 int scmi_xfer_raw_inflight_register(const struct scmi_handle *handle,
657 				    struct scmi_xfer *xfer)
658 {
659 	struct scmi_info *info = handle_to_scmi_info(handle);
660 
661 	return scmi_xfer_inflight_register(xfer, &info->tx_minfo);
662 }
663 
664 /**
665  * scmi_xfer_pending_set  - Pick a proper sequence number and mark the xfer
666  * as pending in-flight
667  *
668  * @xfer: The xfer to act upon
669  * @minfo: Pointer to Tx/Rx Message management info based on channel type
670  *
671  * Return: 0 on Success or error otherwise
672  */
673 static inline int scmi_xfer_pending_set(struct scmi_xfer *xfer,
674 					struct scmi_xfers_info *minfo)
675 {
676 	int ret;
677 	unsigned long flags;
678 
679 	spin_lock_irqsave(&minfo->xfer_lock, flags);
680 	/* Set a new monotonic token as the xfer sequence number */
681 	ret = scmi_xfer_token_set(minfo, xfer);
682 	if (!ret)
683 		scmi_xfer_inflight_register_unlocked(xfer, minfo);
684 	spin_unlock_irqrestore(&minfo->xfer_lock, flags);
685 
686 	return ret;
687 }
688 
689 /**
690  * scmi_xfer_get() - Allocate one message
691  *
692  * @handle: Pointer to SCMI entity handle
693  * @minfo: Pointer to Tx/Rx Message management info based on channel type
694  *
695  * Helper function which is used by various message functions that are
696  * exposed to clients of this driver for allocating a message traffic event.
697  *
698  * Picks an xfer from the free list @free_xfers (if any available) and perform
699  * a basic initialization.
700  *
701  * Note that, at this point, still no sequence number is assigned to the
702  * allocated xfer, nor it is registered as a pending transaction.
703  *
704  * The successfully initialized xfer is refcounted.
705  *
706  * Context: Holds @xfer_lock while manipulating @free_xfers.
707  *
708  * Return: An initialized xfer if all went fine, else pointer error.
709  */
710 static struct scmi_xfer *scmi_xfer_get(const struct scmi_handle *handle,
711 				       struct scmi_xfers_info *minfo)
712 {
713 	unsigned long flags;
714 	struct scmi_xfer *xfer;
715 
716 	spin_lock_irqsave(&minfo->xfer_lock, flags);
717 	if (hlist_empty(&minfo->free_xfers)) {
718 		spin_unlock_irqrestore(&minfo->xfer_lock, flags);
719 		return ERR_PTR(-ENOMEM);
720 	}
721 
722 	/* grab an xfer from the free_list */
723 	xfer = hlist_entry(minfo->free_xfers.first, struct scmi_xfer, node);
724 	hlist_del_init(&xfer->node);
725 
726 	/*
727 	 * Allocate transfer_id early so that can be used also as base for
728 	 * monotonic sequence number generation if needed.
729 	 */
730 	xfer->transfer_id = atomic_inc_return(&transfer_last_id);
731 
732 	refcount_set(&xfer->users, 1);
733 	atomic_set(&xfer->busy, SCMI_XFER_FREE);
734 	spin_unlock_irqrestore(&minfo->xfer_lock, flags);
735 
736 	return xfer;
737 }
738 
739 /**
740  * scmi_xfer_raw_get  - Helper to get a bare free xfer from the TX channel
741  *
742  * @handle: Pointer to SCMI entity handle
743  *
744  * Note that xfer is taken from the TX channel structures.
745  *
746  * Return: A valid xfer on Success, or an error-pointer otherwise
747  */
748 struct scmi_xfer *scmi_xfer_raw_get(const struct scmi_handle *handle)
749 {
750 	struct scmi_xfer *xfer;
751 	struct scmi_info *info = handle_to_scmi_info(handle);
752 
753 	xfer = scmi_xfer_get(handle, &info->tx_minfo);
754 	if (!IS_ERR(xfer))
755 		xfer->flags |= SCMI_XFER_FLAG_IS_RAW;
756 
757 	return xfer;
758 }
759 
760 /**
761  * scmi_xfer_raw_channel_get  - Helper to get a reference to the proper channel
762  * to use for a specific protocol_id Raw transaction.
763  *
764  * @handle: Pointer to SCMI entity handle
765  * @protocol_id: Identifier of the protocol
766  *
767  * Note that in a regular SCMI stack, usually, a protocol has to be defined in
768  * the DT to have an associated channel and be usable; but in Raw mode any
769  * protocol in range is allowed, re-using the Base channel, so as to enable
770  * fuzzing on any protocol without the need of a fully compiled DT.
771  *
772  * Return: A reference to the channel to use, or an ERR_PTR
773  */
774 struct scmi_chan_info *
775 scmi_xfer_raw_channel_get(const struct scmi_handle *handle, u8 protocol_id)
776 {
777 	struct scmi_chan_info *cinfo;
778 	struct scmi_info *info = handle_to_scmi_info(handle);
779 
780 	cinfo = idr_find(&info->tx_idr, protocol_id);
781 	if (!cinfo) {
782 		if (protocol_id == SCMI_PROTOCOL_BASE)
783 			return ERR_PTR(-EINVAL);
784 		/* Use Base channel for protocols not defined for DT */
785 		cinfo = idr_find(&info->tx_idr, SCMI_PROTOCOL_BASE);
786 		if (!cinfo)
787 			return ERR_PTR(-EINVAL);
788 		dev_warn_once(handle->dev,
789 			      "Using Base channel for protocol 0x%X\n",
790 			      protocol_id);
791 	}
792 
793 	return cinfo;
794 }
795 
796 /**
797  * __scmi_xfer_put() - Release a message
798  *
799  * @minfo: Pointer to Tx/Rx Message management info based on channel type
800  * @xfer: message that was reserved by scmi_xfer_get
801  *
802  * After refcount check, possibly release an xfer, clearing the token slot,
803  * removing xfer from @pending_xfers and putting it back into free_xfers.
804  *
805  * This holds a spinlock to maintain integrity of internal data structures.
806  */
807 static void
808 __scmi_xfer_put(struct scmi_xfers_info *minfo, struct scmi_xfer *xfer)
809 {
810 	unsigned long flags;
811 
812 	spin_lock_irqsave(&minfo->xfer_lock, flags);
813 	if (refcount_dec_and_test(&xfer->users)) {
814 		if (xfer->pending) {
815 			scmi_xfer_token_clear(minfo, xfer);
816 			hash_del(&xfer->node);
817 			xfer->pending = false;
818 		}
819 		hlist_add_head(&xfer->node, &minfo->free_xfers);
820 	}
821 	spin_unlock_irqrestore(&minfo->xfer_lock, flags);
822 }
823 
824 /**
825  * scmi_xfer_raw_put  - Release an xfer that was taken by @scmi_xfer_raw_get
826  *
827  * @handle: Pointer to SCMI entity handle
828  * @xfer: A reference to the xfer to put
829  *
830  * Note that as with other xfer_put() handlers the xfer is really effectively
831  * released only if there are no more users on the system.
832  */
833 void scmi_xfer_raw_put(const struct scmi_handle *handle, struct scmi_xfer *xfer)
834 {
835 	struct scmi_info *info = handle_to_scmi_info(handle);
836 
837 	xfer->flags &= ~SCMI_XFER_FLAG_IS_RAW;
838 	xfer->flags &= ~SCMI_XFER_FLAG_CHAN_SET;
839 	return __scmi_xfer_put(&info->tx_minfo, xfer);
840 }
841 
842 /**
843  * scmi_xfer_lookup_unlocked  -  Helper to lookup an xfer_id
844  *
845  * @minfo: Pointer to Tx/Rx Message management info based on channel type
846  * @xfer_id: Token ID to lookup in @pending_xfers
847  *
848  * Refcounting is untouched.
849  *
850  * Context: Assumes to be called with @xfer_lock already acquired.
851  *
852  * Return: A valid xfer on Success or error otherwise
853  */
854 static struct scmi_xfer *
855 scmi_xfer_lookup_unlocked(struct scmi_xfers_info *minfo, u16 xfer_id)
856 {
857 	struct scmi_xfer *xfer = NULL;
858 
859 	if (test_bit(xfer_id, minfo->xfer_alloc_table))
860 		xfer = XFER_FIND(minfo->pending_xfers, xfer_id);
861 
862 	return xfer ?: ERR_PTR(-EINVAL);
863 }
864 
865 /**
866  * scmi_bad_message_trace  - A helper to trace weird messages
867  *
868  * @cinfo: A reference to the channel descriptor on which the message was
869  *	   received
870  * @msg_hdr: Message header to track
871  * @err: A specific error code used as a status value in traces.
872  *
873  * This helper can be used to trace any kind of weird, incomplete, unexpected,
874  * timed-out message that arrives and as such, can be traced only referring to
875  * the header content, since the payload is missing/unreliable.
876  */
877 static void scmi_bad_message_trace(struct scmi_chan_info *cinfo, u32 msg_hdr,
878 				   enum scmi_bad_msg err)
879 {
880 	char *tag;
881 	struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
882 
883 	switch (MSG_XTRACT_TYPE(msg_hdr)) {
884 	case MSG_TYPE_COMMAND:
885 		tag = "!RESP";
886 		break;
887 	case MSG_TYPE_DELAYED_RESP:
888 		tag = "!DLYD";
889 		break;
890 	case MSG_TYPE_NOTIFICATION:
891 		tag = "!NOTI";
892 		break;
893 	default:
894 		tag = "!UNKN";
895 		break;
896 	}
897 
898 	trace_scmi_msg_dump(info->id, cinfo->id,
899 			    MSG_XTRACT_PROT_ID(msg_hdr),
900 			    MSG_XTRACT_ID(msg_hdr), tag,
901 			    MSG_XTRACT_TOKEN(msg_hdr), err, NULL, 0);
902 }
903 
904 /**
905  * scmi_msg_response_validate  - Validate message type against state of related
906  * xfer
907  *
908  * @cinfo: A reference to the channel descriptor.
909  * @msg_type: Message type to check
910  * @xfer: A reference to the xfer to validate against @msg_type
911  *
912  * This function checks if @msg_type is congruent with the current state of
913  * a pending @xfer; if an asynchronous delayed response is received before the
914  * related synchronous response (Out-of-Order Delayed Response) the missing
915  * synchronous response is assumed to be OK and completed, carrying on with the
916  * Delayed Response: this is done to address the case in which the underlying
917  * SCMI transport can deliver such out-of-order responses.
918  *
919  * Context: Assumes to be called with xfer->lock already acquired.
920  *
921  * Return: 0 on Success, error otherwise
922  */
923 static inline int scmi_msg_response_validate(struct scmi_chan_info *cinfo,
924 					     u8 msg_type,
925 					     struct scmi_xfer *xfer)
926 {
927 	/*
928 	 * Even if a response was indeed expected on this slot at this point,
929 	 * a buggy platform could wrongly reply feeding us an unexpected
930 	 * delayed response we're not prepared to handle: bail-out safely
931 	 * blaming firmware.
932 	 */
933 	if (msg_type == MSG_TYPE_DELAYED_RESP && !xfer->async_done) {
934 		dev_err(cinfo->dev,
935 			"Delayed Response for %d not expected! Buggy F/W ?\n",
936 			xfer->hdr.seq);
937 		return -EINVAL;
938 	}
939 
940 	switch (xfer->state) {
941 	case SCMI_XFER_SENT_OK:
942 		if (msg_type == MSG_TYPE_DELAYED_RESP) {
943 			/*
944 			 * Delayed Response expected but delivered earlier.
945 			 * Assume message RESPONSE was OK and skip state.
946 			 */
947 			xfer->hdr.status = SCMI_SUCCESS;
948 			xfer->state = SCMI_XFER_RESP_OK;
949 			complete(&xfer->done);
950 			dev_warn(cinfo->dev,
951 				 "Received valid OoO Delayed Response for %d\n",
952 				 xfer->hdr.seq);
953 		}
954 		break;
955 	case SCMI_XFER_RESP_OK:
956 		if (msg_type != MSG_TYPE_DELAYED_RESP)
957 			return -EINVAL;
958 		break;
959 	case SCMI_XFER_DRESP_OK:
960 		/* No further message expected once in SCMI_XFER_DRESP_OK */
961 		return -EINVAL;
962 	}
963 
964 	return 0;
965 }
966 
967 /**
968  * scmi_xfer_state_update  - Update xfer state
969  *
970  * @xfer: A reference to the xfer to update
971  * @msg_type: Type of message being processed.
972  *
973  * Note that this message is assumed to have been already successfully validated
974  * by @scmi_msg_response_validate(), so here we just update the state.
975  *
976  * Context: Assumes to be called on an xfer exclusively acquired using the
977  *	    busy flag.
978  */
979 static inline void scmi_xfer_state_update(struct scmi_xfer *xfer, u8 msg_type)
980 {
981 	xfer->hdr.type = msg_type;
982 
983 	/* Unknown command types were already discarded earlier */
984 	if (xfer->hdr.type == MSG_TYPE_COMMAND)
985 		xfer->state = SCMI_XFER_RESP_OK;
986 	else
987 		xfer->state = SCMI_XFER_DRESP_OK;
988 }
989 
990 static bool scmi_xfer_acquired(struct scmi_xfer *xfer)
991 {
992 	int ret;
993 
994 	ret = atomic_cmpxchg(&xfer->busy, SCMI_XFER_FREE, SCMI_XFER_BUSY);
995 
996 	return ret == SCMI_XFER_FREE;
997 }
998 
999 /**
1000  * scmi_xfer_command_acquire  -  Helper to lookup and acquire a command xfer
1001  *
1002  * @cinfo: A reference to the channel descriptor.
1003  * @msg_hdr: A message header to use as lookup key
1004  *
1005  * When a valid xfer is found for the sequence number embedded in the provided
1006  * msg_hdr, reference counting is properly updated and exclusive access to this
1007  * xfer is granted till released with @scmi_xfer_command_release.
1008  *
1009  * Return: A valid @xfer on Success or error otherwise.
1010  */
1011 static inline struct scmi_xfer *
1012 scmi_xfer_command_acquire(struct scmi_chan_info *cinfo, u32 msg_hdr)
1013 {
1014 	int ret;
1015 	unsigned long flags;
1016 	struct scmi_xfer *xfer;
1017 	struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
1018 	struct scmi_xfers_info *minfo = &info->tx_minfo;
1019 	u8 msg_type = MSG_XTRACT_TYPE(msg_hdr);
1020 	u16 xfer_id = MSG_XTRACT_TOKEN(msg_hdr);
1021 
1022 	/* Are we even expecting this? */
1023 	spin_lock_irqsave(&minfo->xfer_lock, flags);
1024 	xfer = scmi_xfer_lookup_unlocked(minfo, xfer_id);
1025 	if (IS_ERR(xfer)) {
1026 		dev_err(cinfo->dev,
1027 			"Message for %d type %d is not expected!\n",
1028 			xfer_id, msg_type);
1029 		spin_unlock_irqrestore(&minfo->xfer_lock, flags);
1030 
1031 		scmi_bad_message_trace(cinfo, msg_hdr, MSG_UNEXPECTED);
1032 		scmi_inc_count(info->dbg->counters, ERR_MSG_UNEXPECTED);
1033 
1034 		return xfer;
1035 	}
1036 	refcount_inc(&xfer->users);
1037 	spin_unlock_irqrestore(&minfo->xfer_lock, flags);
1038 
1039 	spin_lock_irqsave(&xfer->lock, flags);
1040 	ret = scmi_msg_response_validate(cinfo, msg_type, xfer);
1041 	/*
1042 	 * If a pending xfer was found which was also in a congruent state with
1043 	 * the received message, acquire exclusive access to it setting the busy
1044 	 * flag.
1045 	 * Spins only on the rare limit condition of concurrent reception of
1046 	 * RESP and DRESP for the same xfer.
1047 	 */
1048 	if (!ret) {
1049 		spin_until_cond(scmi_xfer_acquired(xfer));
1050 		scmi_xfer_state_update(xfer, msg_type);
1051 	}
1052 	spin_unlock_irqrestore(&xfer->lock, flags);
1053 
1054 	if (ret) {
1055 		dev_err(cinfo->dev,
1056 			"Invalid message type:%d for %d - HDR:0x%X  state:%d\n",
1057 			msg_type, xfer_id, msg_hdr, xfer->state);
1058 
1059 		scmi_bad_message_trace(cinfo, msg_hdr, MSG_INVALID);
1060 		scmi_inc_count(info->dbg->counters, ERR_MSG_INVALID);
1061 
1062 		/* On error the refcount incremented above has to be dropped */
1063 		__scmi_xfer_put(minfo, xfer);
1064 		xfer = ERR_PTR(-EINVAL);
1065 	}
1066 
1067 	return xfer;
1068 }
1069 
1070 static inline void scmi_xfer_command_release(struct scmi_info *info,
1071 					     struct scmi_xfer *xfer)
1072 {
1073 	atomic_set(&xfer->busy, SCMI_XFER_FREE);
1074 	__scmi_xfer_put(&info->tx_minfo, xfer);
1075 }
1076 
1077 static inline void scmi_clear_channel(struct scmi_info *info,
1078 				      struct scmi_chan_info *cinfo)
1079 {
1080 	if (!cinfo->is_p2a) {
1081 		dev_warn(cinfo->dev, "Invalid clear on A2P channel !\n");
1082 		return;
1083 	}
1084 
1085 	if (info->desc->ops->clear_channel)
1086 		info->desc->ops->clear_channel(cinfo);
1087 }
1088 
1089 static void scmi_handle_notification(struct scmi_chan_info *cinfo,
1090 				     u32 msg_hdr, void *priv)
1091 {
1092 	struct scmi_xfer *xfer;
1093 	struct device *dev = cinfo->dev;
1094 	struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
1095 	struct scmi_xfers_info *minfo = &info->rx_minfo;
1096 	ktime_t ts;
1097 
1098 	ts = ktime_get_boottime();
1099 	xfer = scmi_xfer_get(cinfo->handle, minfo);
1100 	if (IS_ERR(xfer)) {
1101 		dev_err(dev, "failed to get free message slot (%ld)\n",
1102 			PTR_ERR(xfer));
1103 
1104 		scmi_bad_message_trace(cinfo, msg_hdr, MSG_NOMEM);
1105 		scmi_inc_count(info->dbg->counters, ERR_MSG_NOMEM);
1106 
1107 		scmi_clear_channel(info, cinfo);
1108 		return;
1109 	}
1110 
1111 	unpack_scmi_header(msg_hdr, &xfer->hdr);
1112 	if (priv)
1113 		/* Ensure order between xfer->priv store and following ops */
1114 		smp_store_mb(xfer->priv, priv);
1115 	info->desc->ops->fetch_notification(cinfo, info->desc->max_msg_size,
1116 					    xfer);
1117 
1118 	trace_scmi_msg_dump(info->id, cinfo->id, xfer->hdr.protocol_id,
1119 			    xfer->hdr.id, "NOTI", xfer->hdr.seq,
1120 			    xfer->hdr.status, xfer->rx.buf, xfer->rx.len);
1121 	scmi_inc_count(info->dbg->counters, NOTIFICATION_OK);
1122 
1123 	scmi_notify(cinfo->handle, xfer->hdr.protocol_id,
1124 		    xfer->hdr.id, xfer->rx.buf, xfer->rx.len, ts);
1125 
1126 	trace_scmi_rx_done(xfer->transfer_id, xfer->hdr.id,
1127 			   xfer->hdr.protocol_id, xfer->hdr.seq,
1128 			   MSG_TYPE_NOTIFICATION);
1129 
1130 	if (IS_ENABLED(CONFIG_ARM_SCMI_RAW_MODE_SUPPORT)) {
1131 		xfer->hdr.seq = MSG_XTRACT_TOKEN(msg_hdr);
1132 		scmi_raw_message_report(info->raw, xfer, SCMI_RAW_NOTIF_QUEUE,
1133 					cinfo->id);
1134 	}
1135 
1136 	__scmi_xfer_put(minfo, xfer);
1137 
1138 	scmi_clear_channel(info, cinfo);
1139 }
1140 
1141 static void scmi_handle_response(struct scmi_chan_info *cinfo,
1142 				 u32 msg_hdr, void *priv)
1143 {
1144 	struct scmi_xfer *xfer;
1145 	struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
1146 
1147 	xfer = scmi_xfer_command_acquire(cinfo, msg_hdr);
1148 	if (IS_ERR(xfer)) {
1149 		if (IS_ENABLED(CONFIG_ARM_SCMI_RAW_MODE_SUPPORT))
1150 			scmi_raw_error_report(info->raw, cinfo, msg_hdr, priv);
1151 
1152 		if (MSG_XTRACT_TYPE(msg_hdr) == MSG_TYPE_DELAYED_RESP)
1153 			scmi_clear_channel(info, cinfo);
1154 		return;
1155 	}
1156 
1157 	/* rx.len could be shrunk in the sync do_xfer, so reset to maxsz */
1158 	if (xfer->hdr.type == MSG_TYPE_DELAYED_RESP)
1159 		xfer->rx.len = info->desc->max_msg_size;
1160 
1161 	if (priv)
1162 		/* Ensure order between xfer->priv store and following ops */
1163 		smp_store_mb(xfer->priv, priv);
1164 	info->desc->ops->fetch_response(cinfo, xfer);
1165 
1166 	trace_scmi_msg_dump(info->id, cinfo->id, xfer->hdr.protocol_id,
1167 			    xfer->hdr.id,
1168 			    xfer->hdr.type == MSG_TYPE_DELAYED_RESP ?
1169 			    (!SCMI_XFER_IS_RAW(xfer) ? "DLYD" : "dlyd") :
1170 			    (!SCMI_XFER_IS_RAW(xfer) ? "RESP" : "resp"),
1171 			    xfer->hdr.seq, xfer->hdr.status,
1172 			    xfer->rx.buf, xfer->rx.len);
1173 
1174 	trace_scmi_rx_done(xfer->transfer_id, xfer->hdr.id,
1175 			   xfer->hdr.protocol_id, xfer->hdr.seq,
1176 			   xfer->hdr.type);
1177 
1178 	if (xfer->hdr.type == MSG_TYPE_DELAYED_RESP) {
1179 		scmi_clear_channel(info, cinfo);
1180 		complete(xfer->async_done);
1181 		scmi_inc_count(info->dbg->counters, DELAYED_RESPONSE_OK);
1182 	} else {
1183 		complete(&xfer->done);
1184 		scmi_inc_count(info->dbg->counters, RESPONSE_OK);
1185 	}
1186 
1187 	if (IS_ENABLED(CONFIG_ARM_SCMI_RAW_MODE_SUPPORT)) {
1188 		/*
1189 		 * When in polling mode avoid to queue the Raw xfer on the IRQ
1190 		 * RX path since it will be already queued at the end of the TX
1191 		 * poll loop.
1192 		 */
1193 		if (!xfer->hdr.poll_completion)
1194 			scmi_raw_message_report(info->raw, xfer,
1195 						SCMI_RAW_REPLY_QUEUE,
1196 						cinfo->id);
1197 	}
1198 
1199 	scmi_xfer_command_release(info, xfer);
1200 }
1201 
1202 /**
1203  * scmi_rx_callback() - callback for receiving messages
1204  *
1205  * @cinfo: SCMI channel info
1206  * @msg_hdr: Message header
1207  * @priv: Transport specific private data.
1208  *
1209  * Processes one received message to appropriate transfer information and
1210  * signals completion of the transfer.
1211  *
1212  * NOTE: This function will be invoked in IRQ context, hence should be
1213  * as optimal as possible.
1214  */
1215 static void scmi_rx_callback(struct scmi_chan_info *cinfo, u32 msg_hdr,
1216 			     void *priv)
1217 {
1218 	u8 msg_type = MSG_XTRACT_TYPE(msg_hdr);
1219 
1220 	switch (msg_type) {
1221 	case MSG_TYPE_NOTIFICATION:
1222 		scmi_handle_notification(cinfo, msg_hdr, priv);
1223 		break;
1224 	case MSG_TYPE_COMMAND:
1225 	case MSG_TYPE_DELAYED_RESP:
1226 		scmi_handle_response(cinfo, msg_hdr, priv);
1227 		break;
1228 	default:
1229 		WARN_ONCE(1, "received unknown msg_type:%d\n", msg_type);
1230 		scmi_bad_message_trace(cinfo, msg_hdr, MSG_UNKNOWN);
1231 		break;
1232 	}
1233 }
1234 
1235 /**
1236  * xfer_put() - Release a transmit message
1237  *
1238  * @ph: Pointer to SCMI protocol handle
1239  * @xfer: message that was reserved by xfer_get_init
1240  */
1241 static void xfer_put(const struct scmi_protocol_handle *ph,
1242 		     struct scmi_xfer *xfer)
1243 {
1244 	const struct scmi_protocol_instance *pi = ph_to_pi(ph);
1245 	struct scmi_info *info = handle_to_scmi_info(pi->handle);
1246 
1247 	__scmi_xfer_put(&info->tx_minfo, xfer);
1248 }
1249 
1250 static bool scmi_xfer_done_no_timeout(struct scmi_chan_info *cinfo,
1251 				      struct scmi_xfer *xfer, ktime_t stop)
1252 {
1253 	struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
1254 
1255 	/*
1256 	 * Poll also on xfer->done so that polling can be forcibly terminated
1257 	 * in case of out-of-order receptions of delayed responses
1258 	 */
1259 	return info->desc->ops->poll_done(cinfo, xfer) ||
1260 	       try_wait_for_completion(&xfer->done) ||
1261 	       ktime_after(ktime_get(), stop);
1262 }
1263 
1264 static int scmi_wait_for_reply(struct device *dev, const struct scmi_desc *desc,
1265 			       struct scmi_chan_info *cinfo,
1266 			       struct scmi_xfer *xfer, unsigned int timeout_ms)
1267 {
1268 	int ret = 0;
1269 	struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
1270 
1271 	if (xfer->hdr.poll_completion) {
1272 		/*
1273 		 * Real polling is needed only if transport has NOT declared
1274 		 * itself to support synchronous commands replies.
1275 		 */
1276 		if (!desc->sync_cmds_completed_on_ret) {
1277 			/*
1278 			 * Poll on xfer using transport provided .poll_done();
1279 			 * assumes no completion interrupt was available.
1280 			 */
1281 			ktime_t stop = ktime_add_ms(ktime_get(), timeout_ms);
1282 
1283 			spin_until_cond(scmi_xfer_done_no_timeout(cinfo,
1284 								  xfer, stop));
1285 			if (ktime_after(ktime_get(), stop)) {
1286 				dev_err(dev,
1287 					"timed out in resp(caller: %pS) - polling\n",
1288 					(void *)_RET_IP_);
1289 				ret = -ETIMEDOUT;
1290 				scmi_inc_count(info->dbg->counters, XFERS_RESPONSE_POLLED_TIMEOUT);
1291 			}
1292 		}
1293 
1294 		if (!ret) {
1295 			unsigned long flags;
1296 
1297 			/*
1298 			 * Do not fetch_response if an out-of-order delayed
1299 			 * response is being processed.
1300 			 */
1301 			spin_lock_irqsave(&xfer->lock, flags);
1302 			if (xfer->state == SCMI_XFER_SENT_OK) {
1303 				desc->ops->fetch_response(cinfo, xfer);
1304 				xfer->state = SCMI_XFER_RESP_OK;
1305 			}
1306 			spin_unlock_irqrestore(&xfer->lock, flags);
1307 
1308 			/* Trace polled replies. */
1309 			trace_scmi_msg_dump(info->id, cinfo->id,
1310 					    xfer->hdr.protocol_id, xfer->hdr.id,
1311 					    !SCMI_XFER_IS_RAW(xfer) ?
1312 					    "RESP" : "resp",
1313 					    xfer->hdr.seq, xfer->hdr.status,
1314 					    xfer->rx.buf, xfer->rx.len);
1315 			scmi_inc_count(info->dbg->counters, RESPONSE_POLLED_OK);
1316 
1317 			if (IS_ENABLED(CONFIG_ARM_SCMI_RAW_MODE_SUPPORT)) {
1318 				scmi_raw_message_report(info->raw, xfer,
1319 							SCMI_RAW_REPLY_QUEUE,
1320 							cinfo->id);
1321 			}
1322 		}
1323 	} else {
1324 		/* And we wait for the response. */
1325 		if (!wait_for_completion_timeout(&xfer->done,
1326 						 msecs_to_jiffies(timeout_ms))) {
1327 			dev_err(dev, "timed out in resp(caller: %pS)\n",
1328 				(void *)_RET_IP_);
1329 			ret = -ETIMEDOUT;
1330 			scmi_inc_count(info->dbg->counters, XFERS_RESPONSE_TIMEOUT);
1331 		}
1332 	}
1333 
1334 	return ret;
1335 }
1336 
1337 /**
1338  * scmi_wait_for_message_response  - An helper to group all the possible ways of
1339  * waiting for a synchronous message response.
1340  *
1341  * @cinfo: SCMI channel info
1342  * @xfer: Reference to the transfer being waited for.
1343  *
1344  * Chooses waiting strategy (sleep-waiting vs busy-waiting) depending on
1345  * configuration flags like xfer->hdr.poll_completion.
1346  *
1347  * Return: 0 on Success, error otherwise.
1348  */
1349 static int scmi_wait_for_message_response(struct scmi_chan_info *cinfo,
1350 					  struct scmi_xfer *xfer)
1351 {
1352 	struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
1353 	struct device *dev = info->dev;
1354 
1355 	trace_scmi_xfer_response_wait(xfer->transfer_id, xfer->hdr.id,
1356 				      xfer->hdr.protocol_id, xfer->hdr.seq,
1357 				      info->desc->max_rx_timeout_ms,
1358 				      xfer->hdr.poll_completion);
1359 
1360 	return scmi_wait_for_reply(dev, info->desc, cinfo, xfer,
1361 				   info->desc->max_rx_timeout_ms);
1362 }
1363 
1364 /**
1365  * scmi_xfer_raw_wait_for_message_response  - An helper to wait for a message
1366  * reply to an xfer raw request on a specific channel for the required timeout.
1367  *
1368  * @cinfo: SCMI channel info
1369  * @xfer: Reference to the transfer being waited for.
1370  * @timeout_ms: The maximum timeout in milliseconds
1371  *
1372  * Return: 0 on Success, error otherwise.
1373  */
1374 int scmi_xfer_raw_wait_for_message_response(struct scmi_chan_info *cinfo,
1375 					    struct scmi_xfer *xfer,
1376 					    unsigned int timeout_ms)
1377 {
1378 	int ret;
1379 	struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
1380 	struct device *dev = info->dev;
1381 
1382 	ret = scmi_wait_for_reply(dev, info->desc, cinfo, xfer, timeout_ms);
1383 	if (ret)
1384 		dev_dbg(dev, "timed out in RAW response - HDR:%08X\n",
1385 			pack_scmi_header(&xfer->hdr));
1386 
1387 	return ret;
1388 }
1389 
1390 /**
1391  * do_xfer() - Do one transfer
1392  *
1393  * @ph: Pointer to SCMI protocol handle
1394  * @xfer: Transfer to initiate and wait for response
1395  *
1396  * Return: -ETIMEDOUT in case of no response, if transmit error,
1397  *	return corresponding error, else if all goes well,
1398  *	return 0.
1399  */
1400 static int do_xfer(const struct scmi_protocol_handle *ph,
1401 		   struct scmi_xfer *xfer)
1402 {
1403 	int ret;
1404 	const struct scmi_protocol_instance *pi = ph_to_pi(ph);
1405 	struct scmi_info *info = handle_to_scmi_info(pi->handle);
1406 	struct device *dev = info->dev;
1407 	struct scmi_chan_info *cinfo;
1408 
1409 	/* Check for polling request on custom command xfers at first */
1410 	if (xfer->hdr.poll_completion &&
1411 	    !is_transport_polling_capable(info->desc)) {
1412 		dev_warn_once(dev,
1413 			      "Polling mode is not supported by transport.\n");
1414 		scmi_inc_count(info->dbg->counters, SENT_FAIL_POLLING_UNSUPPORTED);
1415 		return -EINVAL;
1416 	}
1417 
1418 	cinfo = idr_find(&info->tx_idr, pi->proto->id);
1419 	if (unlikely(!cinfo)) {
1420 		scmi_inc_count(info->dbg->counters, SENT_FAIL_CHANNEL_NOT_FOUND);
1421 		return -EINVAL;
1422 	}
1423 	/* True ONLY if also supported by transport. */
1424 	if (is_polling_enabled(cinfo, info->desc))
1425 		xfer->hdr.poll_completion = true;
1426 
1427 	/*
1428 	 * Initialise protocol id now from protocol handle to avoid it being
1429 	 * overridden by mistake (or malice) by the protocol code mangling with
1430 	 * the scmi_xfer structure prior to this.
1431 	 */
1432 	xfer->hdr.protocol_id = pi->proto->id;
1433 	reinit_completion(&xfer->done);
1434 
1435 	trace_scmi_xfer_begin(xfer->transfer_id, xfer->hdr.id,
1436 			      xfer->hdr.protocol_id, xfer->hdr.seq,
1437 			      xfer->hdr.poll_completion);
1438 
1439 	/* Clear any stale status */
1440 	xfer->hdr.status = SCMI_SUCCESS;
1441 	xfer->state = SCMI_XFER_SENT_OK;
1442 	/*
1443 	 * Even though spinlocking is not needed here since no race is possible
1444 	 * on xfer->state due to the monotonically increasing tokens allocation,
1445 	 * we must anyway ensure xfer->state initialization is not re-ordered
1446 	 * after the .send_message() to be sure that on the RX path an early
1447 	 * ISR calling scmi_rx_callback() cannot see an old stale xfer->state.
1448 	 */
1449 	smp_mb();
1450 
1451 	ret = info->desc->ops->send_message(cinfo, xfer);
1452 	if (ret < 0) {
1453 		dev_dbg(dev, "Failed to send message %d\n", ret);
1454 		scmi_inc_count(info->dbg->counters, SENT_FAIL);
1455 		return ret;
1456 	}
1457 
1458 	trace_scmi_msg_dump(info->id, cinfo->id, xfer->hdr.protocol_id,
1459 			    xfer->hdr.id, "CMND", xfer->hdr.seq,
1460 			    xfer->hdr.status, xfer->tx.buf, xfer->tx.len);
1461 	scmi_inc_count(info->dbg->counters, SENT_OK);
1462 
1463 	ret = scmi_wait_for_message_response(cinfo, xfer);
1464 	if (!ret && xfer->hdr.status) {
1465 		ret = scmi_to_linux_errno(xfer->hdr.status);
1466 		scmi_inc_count(info->dbg->counters, ERR_PROTOCOL);
1467 	}
1468 
1469 	if (info->desc->ops->mark_txdone)
1470 		info->desc->ops->mark_txdone(cinfo, ret, xfer);
1471 
1472 	trace_scmi_xfer_end(xfer->transfer_id, xfer->hdr.id,
1473 			    xfer->hdr.protocol_id, xfer->hdr.seq, ret);
1474 
1475 	return ret;
1476 }
1477 
1478 static void reset_rx_to_maxsz(const struct scmi_protocol_handle *ph,
1479 			      struct scmi_xfer *xfer)
1480 {
1481 	const struct scmi_protocol_instance *pi = ph_to_pi(ph);
1482 	struct scmi_info *info = handle_to_scmi_info(pi->handle);
1483 
1484 	xfer->rx.len = info->desc->max_msg_size;
1485 }
1486 
1487 /**
1488  * do_xfer_with_response() - Do one transfer and wait until the delayed
1489  *	response is received
1490  *
1491  * @ph: Pointer to SCMI protocol handle
1492  * @xfer: Transfer to initiate and wait for response
1493  *
1494  * Using asynchronous commands in atomic/polling mode should be avoided since
1495  * it could cause long busy-waiting here, so ignore polling for the delayed
1496  * response and WARN if it was requested for this command transaction since
1497  * upper layers should refrain from issuing such kind of requests.
1498  *
1499  * The only other option would have been to refrain from using any asynchronous
1500  * command even if made available, when an atomic transport is detected, and
1501  * instead forcibly use the synchronous version (thing that can be easily
1502  * attained at the protocol layer), but this would also have led to longer
1503  * stalls of the channel for synchronous commands and possibly timeouts.
1504  * (in other words there is usually a good reason if a platform provides an
1505  *  asynchronous version of a command and we should prefer to use it...just not
1506  *  when using atomic/polling mode)
1507  *
1508  * Return: -ETIMEDOUT in case of no delayed response, if transmit error,
1509  *	return corresponding error, else if all goes well, return 0.
1510  */
1511 static int do_xfer_with_response(const struct scmi_protocol_handle *ph,
1512 				 struct scmi_xfer *xfer)
1513 {
1514 	int ret, timeout = msecs_to_jiffies(SCMI_MAX_RESPONSE_TIMEOUT);
1515 	DECLARE_COMPLETION_ONSTACK(async_response);
1516 
1517 	xfer->async_done = &async_response;
1518 
1519 	/*
1520 	 * Delayed responses should not be polled, so an async command should
1521 	 * not have been used when requiring an atomic/poll context; WARN and
1522 	 * perform instead a sleeping wait.
1523 	 * (Note Async + IgnoreDelayedResponses are sent via do_xfer)
1524 	 */
1525 	WARN_ON_ONCE(xfer->hdr.poll_completion);
1526 
1527 	ret = do_xfer(ph, xfer);
1528 	if (!ret) {
1529 		if (!wait_for_completion_timeout(xfer->async_done, timeout)) {
1530 			dev_err(ph->dev,
1531 				"timed out in delayed resp(caller: %pS)\n",
1532 				(void *)_RET_IP_);
1533 			ret = -ETIMEDOUT;
1534 		} else if (xfer->hdr.status) {
1535 			ret = scmi_to_linux_errno(xfer->hdr.status);
1536 		}
1537 	}
1538 
1539 	xfer->async_done = NULL;
1540 	return ret;
1541 }
1542 
1543 /**
1544  * xfer_get_init() - Allocate and initialise one message for transmit
1545  *
1546  * @ph: Pointer to SCMI protocol handle
1547  * @msg_id: Message identifier
1548  * @tx_size: transmit message size
1549  * @rx_size: receive message size
1550  * @p: pointer to the allocated and initialised message
1551  *
1552  * This function allocates the message using @scmi_xfer_get and
1553  * initialise the header.
1554  *
1555  * Return: 0 if all went fine with @p pointing to message, else
1556  *	corresponding error.
1557  */
1558 static int xfer_get_init(const struct scmi_protocol_handle *ph,
1559 			 u8 msg_id, size_t tx_size, size_t rx_size,
1560 			 struct scmi_xfer **p)
1561 {
1562 	int ret;
1563 	struct scmi_xfer *xfer;
1564 	const struct scmi_protocol_instance *pi = ph_to_pi(ph);
1565 	struct scmi_info *info = handle_to_scmi_info(pi->handle);
1566 	struct scmi_xfers_info *minfo = &info->tx_minfo;
1567 	struct device *dev = info->dev;
1568 
1569 	/* Ensure we have sane transfer sizes */
1570 	if (rx_size > info->desc->max_msg_size ||
1571 	    tx_size > info->desc->max_msg_size)
1572 		return -ERANGE;
1573 
1574 	xfer = scmi_xfer_get(pi->handle, minfo);
1575 	if (IS_ERR(xfer)) {
1576 		ret = PTR_ERR(xfer);
1577 		dev_err(dev, "failed to get free message slot(%d)\n", ret);
1578 		return ret;
1579 	}
1580 
1581 	/* Pick a sequence number and register this xfer as in-flight */
1582 	ret = scmi_xfer_pending_set(xfer, minfo);
1583 	if (ret) {
1584 		dev_err(pi->handle->dev,
1585 			"Failed to get monotonic token %d\n", ret);
1586 		__scmi_xfer_put(minfo, xfer);
1587 		return ret;
1588 	}
1589 
1590 	xfer->tx.len = tx_size;
1591 	xfer->rx.len = rx_size ? : info->desc->max_msg_size;
1592 	xfer->hdr.type = MSG_TYPE_COMMAND;
1593 	xfer->hdr.id = msg_id;
1594 	xfer->hdr.poll_completion = false;
1595 
1596 	*p = xfer;
1597 
1598 	return 0;
1599 }
1600 
1601 /**
1602  * version_get() - command to get the revision of the SCMI entity
1603  *
1604  * @ph: Pointer to SCMI protocol handle
1605  * @version: Holds returned version of protocol.
1606  *
1607  * Updates the SCMI information in the internal data structure.
1608  *
1609  * Return: 0 if all went fine, else return appropriate error.
1610  */
1611 static int version_get(const struct scmi_protocol_handle *ph, u32 *version)
1612 {
1613 	int ret;
1614 	__le32 *rev_info;
1615 	struct scmi_xfer *t;
1616 
1617 	ret = xfer_get_init(ph, PROTOCOL_VERSION, 0, sizeof(*version), &t);
1618 	if (ret)
1619 		return ret;
1620 
1621 	ret = do_xfer(ph, t);
1622 	if (!ret) {
1623 		rev_info = t->rx.buf;
1624 		*version = le32_to_cpu(*rev_info);
1625 	}
1626 
1627 	xfer_put(ph, t);
1628 	return ret;
1629 }
1630 
1631 /**
1632  * scmi_set_protocol_priv  - Set protocol specific data at init time
1633  *
1634  * @ph: A reference to the protocol handle.
1635  * @priv: The private data to set.
1636  * @version: The detected protocol version for the core to register.
1637  *
1638  * Return: 0 on Success
1639  */
1640 static int scmi_set_protocol_priv(const struct scmi_protocol_handle *ph,
1641 				  void *priv, u32 version)
1642 {
1643 	struct scmi_protocol_instance *pi = ph_to_pi(ph);
1644 
1645 	pi->priv = priv;
1646 	pi->version = version;
1647 
1648 	return 0;
1649 }
1650 
1651 /**
1652  * scmi_get_protocol_priv  - Set protocol specific data at init time
1653  *
1654  * @ph: A reference to the protocol handle.
1655  *
1656  * Return: Protocol private data if any was set.
1657  */
1658 static void *scmi_get_protocol_priv(const struct scmi_protocol_handle *ph)
1659 {
1660 	const struct scmi_protocol_instance *pi = ph_to_pi(ph);
1661 
1662 	return pi->priv;
1663 }
1664 
1665 static const struct scmi_xfer_ops xfer_ops = {
1666 	.version_get = version_get,
1667 	.xfer_get_init = xfer_get_init,
1668 	.reset_rx_to_maxsz = reset_rx_to_maxsz,
1669 	.do_xfer = do_xfer,
1670 	.do_xfer_with_response = do_xfer_with_response,
1671 	.xfer_put = xfer_put,
1672 };
1673 
1674 struct scmi_msg_resp_domain_name_get {
1675 	__le32 flags;
1676 	u8 name[SCMI_MAX_STR_SIZE];
1677 };
1678 
1679 /**
1680  * scmi_common_extended_name_get  - Common helper to get extended resources name
1681  * @ph: A protocol handle reference.
1682  * @cmd_id: The specific command ID to use.
1683  * @res_id: The specific resource ID to use.
1684  * @flags: A pointer to specific flags to use, if any.
1685  * @name: A pointer to the preallocated area where the retrieved name will be
1686  *	  stored as a NULL terminated string.
1687  * @len: The len in bytes of the @name char array.
1688  *
1689  * Return: 0 on Succcess
1690  */
1691 static int scmi_common_extended_name_get(const struct scmi_protocol_handle *ph,
1692 					 u8 cmd_id, u32 res_id, u32 *flags,
1693 					 char *name, size_t len)
1694 {
1695 	int ret;
1696 	size_t txlen;
1697 	struct scmi_xfer *t;
1698 	struct scmi_msg_resp_domain_name_get *resp;
1699 
1700 	txlen = !flags ? sizeof(res_id) : sizeof(res_id) + sizeof(*flags);
1701 	ret = ph->xops->xfer_get_init(ph, cmd_id, txlen, sizeof(*resp), &t);
1702 	if (ret)
1703 		goto out;
1704 
1705 	put_unaligned_le32(res_id, t->tx.buf);
1706 	if (flags)
1707 		put_unaligned_le32(*flags, t->tx.buf + sizeof(res_id));
1708 	resp = t->rx.buf;
1709 
1710 	ret = ph->xops->do_xfer(ph, t);
1711 	if (!ret)
1712 		strscpy(name, resp->name, len);
1713 
1714 	ph->xops->xfer_put(ph, t);
1715 out:
1716 	if (ret)
1717 		dev_warn(ph->dev,
1718 			 "Failed to get extended name - id:%u (ret:%d). Using %s\n",
1719 			 res_id, ret, name);
1720 	return ret;
1721 }
1722 
1723 /**
1724  * scmi_common_get_max_msg_size  - Get maximum message size
1725  * @ph: A protocol handle reference.
1726  *
1727  * Return: Maximum message size for the current protocol.
1728  */
1729 static int scmi_common_get_max_msg_size(const struct scmi_protocol_handle *ph)
1730 {
1731 	const struct scmi_protocol_instance *pi = ph_to_pi(ph);
1732 	struct scmi_info *info = handle_to_scmi_info(pi->handle);
1733 
1734 	return info->desc->max_msg_size;
1735 }
1736 
1737 /**
1738  * struct scmi_iterator  - Iterator descriptor
1739  * @msg: A reference to the message TX buffer; filled by @prepare_message with
1740  *	 a proper custom command payload for each multi-part command request.
1741  * @resp: A reference to the response RX buffer; used by @update_state and
1742  *	  @process_response to parse the multi-part replies.
1743  * @t: A reference to the underlying xfer initialized and used transparently by
1744  *     the iterator internal routines.
1745  * @ph: A reference to the associated protocol handle to be used.
1746  * @ops: A reference to the custom provided iterator operations.
1747  * @state: The current iterator state; used and updated in turn by the iterators
1748  *	   internal routines and by the caller-provided @scmi_iterator_ops.
1749  * @priv: A reference to optional private data as provided by the caller and
1750  *	  passed back to the @@scmi_iterator_ops.
1751  */
1752 struct scmi_iterator {
1753 	void *msg;
1754 	void *resp;
1755 	struct scmi_xfer *t;
1756 	const struct scmi_protocol_handle *ph;
1757 	struct scmi_iterator_ops *ops;
1758 	struct scmi_iterator_state state;
1759 	void *priv;
1760 };
1761 
1762 static void *scmi_iterator_init(const struct scmi_protocol_handle *ph,
1763 				struct scmi_iterator_ops *ops,
1764 				unsigned int max_resources, u8 msg_id,
1765 				size_t tx_size, void *priv)
1766 {
1767 	int ret;
1768 	struct scmi_iterator *i;
1769 
1770 	i = devm_kzalloc(ph->dev, sizeof(*i), GFP_KERNEL);
1771 	if (!i)
1772 		return ERR_PTR(-ENOMEM);
1773 
1774 	i->ph = ph;
1775 	i->ops = ops;
1776 	i->priv = priv;
1777 
1778 	ret = ph->xops->xfer_get_init(ph, msg_id, tx_size, 0, &i->t);
1779 	if (ret) {
1780 		devm_kfree(ph->dev, i);
1781 		return ERR_PTR(ret);
1782 	}
1783 
1784 	i->state.max_resources = max_resources;
1785 	i->msg = i->t->tx.buf;
1786 	i->resp = i->t->rx.buf;
1787 
1788 	return i;
1789 }
1790 
1791 static int scmi_iterator_run(void *iter)
1792 {
1793 	int ret = -EINVAL;
1794 	struct scmi_iterator_ops *iops;
1795 	const struct scmi_protocol_handle *ph;
1796 	struct scmi_iterator_state *st;
1797 	struct scmi_iterator *i = iter;
1798 
1799 	if (!i || !i->ops || !i->ph)
1800 		return ret;
1801 
1802 	iops = i->ops;
1803 	ph = i->ph;
1804 	st = &i->state;
1805 
1806 	do {
1807 		iops->prepare_message(i->msg, st->desc_index, i->priv);
1808 		ret = ph->xops->do_xfer(ph, i->t);
1809 		if (ret)
1810 			break;
1811 
1812 		st->rx_len = i->t->rx.len;
1813 		ret = iops->update_state(st, i->resp, i->priv);
1814 		if (ret)
1815 			break;
1816 
1817 		if (st->num_returned > st->max_resources - st->desc_index) {
1818 			dev_err(ph->dev,
1819 				"No. of resources can't exceed %d\n",
1820 				st->max_resources);
1821 			ret = -EINVAL;
1822 			break;
1823 		}
1824 
1825 		for (st->loop_idx = 0; st->loop_idx < st->num_returned;
1826 		     st->loop_idx++) {
1827 			ret = iops->process_response(ph, i->resp, st, i->priv);
1828 			if (ret)
1829 				goto out;
1830 		}
1831 
1832 		st->desc_index += st->num_returned;
1833 		ph->xops->reset_rx_to_maxsz(ph, i->t);
1834 		/*
1835 		 * check for both returned and remaining to avoid infinite
1836 		 * loop due to buggy firmware
1837 		 */
1838 	} while (st->num_returned && st->num_remaining);
1839 
1840 out:
1841 	/* Finalize and destroy iterator */
1842 	ph->xops->xfer_put(ph, i->t);
1843 	devm_kfree(ph->dev, i);
1844 
1845 	return ret;
1846 }
1847 
1848 struct scmi_msg_get_fc_info {
1849 	__le32 domain;
1850 	__le32 message_id;
1851 };
1852 
1853 struct scmi_msg_resp_desc_fc {
1854 	__le32 attr;
1855 #define SUPPORTS_DOORBELL(x)		((x) & BIT(0))
1856 #define DOORBELL_REG_WIDTH(x)		FIELD_GET(GENMASK(2, 1), (x))
1857 	__le32 rate_limit;
1858 	__le32 chan_addr_low;
1859 	__le32 chan_addr_high;
1860 	__le32 chan_size;
1861 	__le32 db_addr_low;
1862 	__le32 db_addr_high;
1863 	__le32 db_set_lmask;
1864 	__le32 db_set_hmask;
1865 	__le32 db_preserve_lmask;
1866 	__le32 db_preserve_hmask;
1867 };
1868 
1869 static void
1870 scmi_common_fastchannel_init(const struct scmi_protocol_handle *ph,
1871 			     u8 describe_id, u32 message_id, u32 valid_size,
1872 			     u32 domain, void __iomem **p_addr,
1873 			     struct scmi_fc_db_info **p_db, u32 *rate_limit)
1874 {
1875 	int ret;
1876 	u32 flags;
1877 	u64 phys_addr;
1878 	u8 size;
1879 	void __iomem *addr;
1880 	struct scmi_xfer *t;
1881 	struct scmi_fc_db_info *db = NULL;
1882 	struct scmi_msg_get_fc_info *info;
1883 	struct scmi_msg_resp_desc_fc *resp;
1884 	const struct scmi_protocol_instance *pi = ph_to_pi(ph);
1885 
1886 	if (!p_addr) {
1887 		ret = -EINVAL;
1888 		goto err_out;
1889 	}
1890 
1891 	ret = ph->xops->xfer_get_init(ph, describe_id,
1892 				      sizeof(*info), sizeof(*resp), &t);
1893 	if (ret)
1894 		goto err_out;
1895 
1896 	info = t->tx.buf;
1897 	info->domain = cpu_to_le32(domain);
1898 	info->message_id = cpu_to_le32(message_id);
1899 
1900 	/*
1901 	 * Bail out on error leaving fc_info addresses zeroed; this includes
1902 	 * the case in which the requested domain/message_id does NOT support
1903 	 * fastchannels at all.
1904 	 */
1905 	ret = ph->xops->do_xfer(ph, t);
1906 	if (ret)
1907 		goto err_xfer;
1908 
1909 	resp = t->rx.buf;
1910 	flags = le32_to_cpu(resp->attr);
1911 	size = le32_to_cpu(resp->chan_size);
1912 	if (size != valid_size) {
1913 		ret = -EINVAL;
1914 		goto err_xfer;
1915 	}
1916 
1917 	if (rate_limit)
1918 		*rate_limit = le32_to_cpu(resp->rate_limit) & GENMASK(19, 0);
1919 
1920 	phys_addr = le32_to_cpu(resp->chan_addr_low);
1921 	phys_addr |= (u64)le32_to_cpu(resp->chan_addr_high) << 32;
1922 	addr = devm_ioremap(ph->dev, phys_addr, size);
1923 	if (!addr) {
1924 		ret = -EADDRNOTAVAIL;
1925 		goto err_xfer;
1926 	}
1927 
1928 	*p_addr = addr;
1929 
1930 	if (p_db && SUPPORTS_DOORBELL(flags)) {
1931 		db = devm_kzalloc(ph->dev, sizeof(*db), GFP_KERNEL);
1932 		if (!db) {
1933 			ret = -ENOMEM;
1934 			goto err_db;
1935 		}
1936 
1937 		size = 1 << DOORBELL_REG_WIDTH(flags);
1938 		phys_addr = le32_to_cpu(resp->db_addr_low);
1939 		phys_addr |= (u64)le32_to_cpu(resp->db_addr_high) << 32;
1940 		addr = devm_ioremap(ph->dev, phys_addr, size);
1941 		if (!addr) {
1942 			ret = -EADDRNOTAVAIL;
1943 			goto err_db_mem;
1944 		}
1945 
1946 		db->addr = addr;
1947 		db->width = size;
1948 		db->set = le32_to_cpu(resp->db_set_lmask);
1949 		db->set |= (u64)le32_to_cpu(resp->db_set_hmask) << 32;
1950 		db->mask = le32_to_cpu(resp->db_preserve_lmask);
1951 		db->mask |= (u64)le32_to_cpu(resp->db_preserve_hmask) << 32;
1952 
1953 		*p_db = db;
1954 	}
1955 
1956 	ph->xops->xfer_put(ph, t);
1957 
1958 	dev_dbg(ph->dev,
1959 		"Using valid FC for protocol %X [MSG_ID:%u / RES_ID:%u]\n",
1960 		pi->proto->id, message_id, domain);
1961 
1962 	return;
1963 
1964 err_db_mem:
1965 	devm_kfree(ph->dev, db);
1966 
1967 err_db:
1968 	*p_addr = NULL;
1969 
1970 err_xfer:
1971 	ph->xops->xfer_put(ph, t);
1972 
1973 err_out:
1974 	dev_warn(ph->dev,
1975 		 "Failed to get FC for protocol %X [MSG_ID:%u / RES_ID:%u] - ret:%d. Using regular messaging.\n",
1976 		 pi->proto->id, message_id, domain, ret);
1977 }
1978 
1979 #define SCMI_PROTO_FC_RING_DB(w)			\
1980 do {							\
1981 	u##w val = 0;					\
1982 							\
1983 	if (db->mask)					\
1984 		val = ioread##w(db->addr) & db->mask;	\
1985 	iowrite##w((u##w)db->set | val, db->addr);	\
1986 } while (0)
1987 
1988 static void scmi_common_fastchannel_db_ring(struct scmi_fc_db_info *db)
1989 {
1990 	if (!db || !db->addr)
1991 		return;
1992 
1993 	if (db->width == 1)
1994 		SCMI_PROTO_FC_RING_DB(8);
1995 	else if (db->width == 2)
1996 		SCMI_PROTO_FC_RING_DB(16);
1997 	else if (db->width == 4)
1998 		SCMI_PROTO_FC_RING_DB(32);
1999 	else /* db->width == 8 */
2000 #ifdef CONFIG_64BIT
2001 		SCMI_PROTO_FC_RING_DB(64);
2002 #else
2003 	{
2004 		u64 val = 0;
2005 
2006 		if (db->mask)
2007 			val = ioread64_hi_lo(db->addr) & db->mask;
2008 		iowrite64_hi_lo(db->set | val, db->addr);
2009 	}
2010 #endif
2011 }
2012 
2013 /**
2014  * scmi_protocol_msg_check  - Check protocol message attributes
2015  *
2016  * @ph: A reference to the protocol handle.
2017  * @message_id: The ID of the message to check.
2018  * @attributes: A parameter to optionally return the retrieved message
2019  *		attributes, in case of Success.
2020  *
2021  * An helper to check protocol message attributes for a specific protocol
2022  * and message pair.
2023  *
2024  * Return: 0 on SUCCESS
2025  */
2026 static int scmi_protocol_msg_check(const struct scmi_protocol_handle *ph,
2027 				   u32 message_id, u32 *attributes)
2028 {
2029 	int ret;
2030 	struct scmi_xfer *t;
2031 
2032 	ret = xfer_get_init(ph, PROTOCOL_MESSAGE_ATTRIBUTES,
2033 			    sizeof(__le32), 0, &t);
2034 	if (ret)
2035 		return ret;
2036 
2037 	put_unaligned_le32(message_id, t->tx.buf);
2038 	ret = do_xfer(ph, t);
2039 	if (!ret && attributes)
2040 		*attributes = get_unaligned_le32(t->rx.buf);
2041 	xfer_put(ph, t);
2042 
2043 	return ret;
2044 }
2045 
2046 static const struct scmi_proto_helpers_ops helpers_ops = {
2047 	.extended_name_get = scmi_common_extended_name_get,
2048 	.get_max_msg_size = scmi_common_get_max_msg_size,
2049 	.iter_response_init = scmi_iterator_init,
2050 	.iter_response_run = scmi_iterator_run,
2051 	.protocol_msg_check = scmi_protocol_msg_check,
2052 	.fastchannel_init = scmi_common_fastchannel_init,
2053 	.fastchannel_db_ring = scmi_common_fastchannel_db_ring,
2054 };
2055 
2056 /**
2057  * scmi_revision_area_get  - Retrieve version memory area.
2058  *
2059  * @ph: A reference to the protocol handle.
2060  *
2061  * A helper to grab the version memory area reference during SCMI Base protocol
2062  * initialization.
2063  *
2064  * Return: A reference to the version memory area associated to the SCMI
2065  *	   instance underlying this protocol handle.
2066  */
2067 struct scmi_revision_info *
2068 scmi_revision_area_get(const struct scmi_protocol_handle *ph)
2069 {
2070 	const struct scmi_protocol_instance *pi = ph_to_pi(ph);
2071 
2072 	return pi->handle->version;
2073 }
2074 
2075 /**
2076  * scmi_protocol_version_negotiate  - Negotiate protocol version
2077  *
2078  * @ph: A reference to the protocol handle.
2079  *
2080  * An helper to negotiate a protocol version different from the latest
2081  * advertised as supported from the platform: on Success backward
2082  * compatibility is assured by the platform.
2083  *
2084  * Return: 0 on Success
2085  */
2086 static int scmi_protocol_version_negotiate(struct scmi_protocol_handle *ph)
2087 {
2088 	int ret;
2089 	struct scmi_xfer *t;
2090 	struct scmi_protocol_instance *pi = ph_to_pi(ph);
2091 
2092 	/* At first check if NEGOTIATE_PROTOCOL_VERSION is supported ... */
2093 	ret = scmi_protocol_msg_check(ph, NEGOTIATE_PROTOCOL_VERSION, NULL);
2094 	if (ret)
2095 		return ret;
2096 
2097 	/* ... then attempt protocol version negotiation */
2098 	ret = xfer_get_init(ph, NEGOTIATE_PROTOCOL_VERSION,
2099 			    sizeof(__le32), 0, &t);
2100 	if (ret)
2101 		return ret;
2102 
2103 	put_unaligned_le32(pi->proto->supported_version, t->tx.buf);
2104 	ret = do_xfer(ph, t);
2105 	if (!ret)
2106 		pi->negotiated_version = pi->proto->supported_version;
2107 
2108 	xfer_put(ph, t);
2109 
2110 	return ret;
2111 }
2112 
2113 /**
2114  * scmi_alloc_init_protocol_instance  - Allocate and initialize a protocol
2115  * instance descriptor.
2116  * @info: The reference to the related SCMI instance.
2117  * @proto: The protocol descriptor.
2118  *
2119  * Allocate a new protocol instance descriptor, using the provided @proto
2120  * description, against the specified SCMI instance @info, and initialize it;
2121  * all resources management is handled via a dedicated per-protocol devres
2122  * group.
2123  *
2124  * Context: Assumes to be called with @protocols_mtx already acquired.
2125  * Return: A reference to a freshly allocated and initialized protocol instance
2126  *	   or ERR_PTR on failure. On failure the @proto reference is at first
2127  *	   put using @scmi_protocol_put() before releasing all the devres group.
2128  */
2129 static struct scmi_protocol_instance *
2130 scmi_alloc_init_protocol_instance(struct scmi_info *info,
2131 				  const struct scmi_protocol *proto)
2132 {
2133 	int ret = -ENOMEM;
2134 	void *gid;
2135 	struct scmi_protocol_instance *pi;
2136 	const struct scmi_handle *handle = &info->handle;
2137 
2138 	/* Protocol specific devres group */
2139 	gid = devres_open_group(handle->dev, NULL, GFP_KERNEL);
2140 	if (!gid) {
2141 		scmi_protocol_put(proto);
2142 		goto out;
2143 	}
2144 
2145 	pi = devm_kzalloc(handle->dev, sizeof(*pi), GFP_KERNEL);
2146 	if (!pi)
2147 		goto clean;
2148 
2149 	pi->gid = gid;
2150 	pi->proto = proto;
2151 	pi->handle = handle;
2152 	pi->ph.dev = handle->dev;
2153 	pi->ph.xops = &xfer_ops;
2154 	pi->ph.hops = &helpers_ops;
2155 	pi->ph.set_priv = scmi_set_protocol_priv;
2156 	pi->ph.get_priv = scmi_get_protocol_priv;
2157 	refcount_set(&pi->users, 1);
2158 	/* proto->init is assured NON NULL by scmi_protocol_register */
2159 	ret = pi->proto->instance_init(&pi->ph);
2160 	if (ret)
2161 		goto clean;
2162 
2163 	ret = idr_alloc(&info->protocols, pi, proto->id, proto->id + 1,
2164 			GFP_KERNEL);
2165 	if (ret != proto->id)
2166 		goto clean;
2167 
2168 	/*
2169 	 * Warn but ignore events registration errors since we do not want
2170 	 * to skip whole protocols if their notifications are messed up.
2171 	 */
2172 	if (pi->proto->events) {
2173 		ret = scmi_register_protocol_events(handle, pi->proto->id,
2174 						    &pi->ph,
2175 						    pi->proto->events);
2176 		if (ret)
2177 			dev_warn(handle->dev,
2178 				 "Protocol:%X - Events Registration Failed - err:%d\n",
2179 				 pi->proto->id, ret);
2180 	}
2181 
2182 	devres_close_group(handle->dev, pi->gid);
2183 	dev_dbg(handle->dev, "Initialized protocol: 0x%X\n", pi->proto->id);
2184 
2185 	if (pi->version > proto->supported_version) {
2186 		ret = scmi_protocol_version_negotiate(&pi->ph);
2187 		if (!ret) {
2188 			dev_info(handle->dev,
2189 				 "Protocol 0x%X successfully negotiated version 0x%X\n",
2190 				 proto->id, pi->negotiated_version);
2191 		} else {
2192 			dev_warn(handle->dev,
2193 				 "Detected UNSUPPORTED higher version 0x%X for protocol 0x%X.\n",
2194 				 pi->version, pi->proto->id);
2195 			dev_warn(handle->dev,
2196 				 "Trying version 0x%X. Backward compatibility is NOT assured.\n",
2197 				 pi->proto->supported_version);
2198 		}
2199 	}
2200 
2201 	return pi;
2202 
2203 clean:
2204 	/* Take care to put the protocol module's owner before releasing all */
2205 	scmi_protocol_put(proto);
2206 	devres_release_group(handle->dev, gid);
2207 out:
2208 	return ERR_PTR(ret);
2209 }
2210 
2211 /**
2212  * scmi_get_protocol_instance  - Protocol initialization helper.
2213  * @handle: A reference to the SCMI platform instance.
2214  * @protocol_id: The protocol being requested.
2215  *
2216  * In case the required protocol has never been requested before for this
2217  * instance, allocate and initialize all the needed structures while handling
2218  * resource allocation with a dedicated per-protocol devres subgroup.
2219  *
2220  * Return: A reference to an initialized protocol instance or error on failure:
2221  *	   in particular returns -EPROBE_DEFER when the desired protocol could
2222  *	   NOT be found.
2223  */
2224 static struct scmi_protocol_instance * __must_check
2225 scmi_get_protocol_instance(const struct scmi_handle *handle, u8 protocol_id)
2226 {
2227 	struct scmi_protocol_instance *pi;
2228 	struct scmi_info *info = handle_to_scmi_info(handle);
2229 
2230 	mutex_lock(&info->protocols_mtx);
2231 	pi = idr_find(&info->protocols, protocol_id);
2232 
2233 	if (pi) {
2234 		refcount_inc(&pi->users);
2235 	} else {
2236 		const struct scmi_protocol *proto;
2237 
2238 		/* Fails if protocol not registered on bus */
2239 		proto = scmi_protocol_get(protocol_id, &info->version);
2240 		if (proto)
2241 			pi = scmi_alloc_init_protocol_instance(info, proto);
2242 		else
2243 			pi = ERR_PTR(-EPROBE_DEFER);
2244 	}
2245 	mutex_unlock(&info->protocols_mtx);
2246 
2247 	return pi;
2248 }
2249 
2250 /**
2251  * scmi_protocol_acquire  - Protocol acquire
2252  * @handle: A reference to the SCMI platform instance.
2253  * @protocol_id: The protocol being requested.
2254  *
2255  * Register a new user for the requested protocol on the specified SCMI
2256  * platform instance, possibly triggering its initialization on first user.
2257  *
2258  * Return: 0 if protocol was acquired successfully.
2259  */
2260 int scmi_protocol_acquire(const struct scmi_handle *handle, u8 protocol_id)
2261 {
2262 	return PTR_ERR_OR_ZERO(scmi_get_protocol_instance(handle, protocol_id));
2263 }
2264 
2265 /**
2266  * scmi_protocol_release  - Protocol de-initialization helper.
2267  * @handle: A reference to the SCMI platform instance.
2268  * @protocol_id: The protocol being requested.
2269  *
2270  * Remove one user for the specified protocol and triggers de-initialization
2271  * and resources de-allocation once the last user has gone.
2272  */
2273 void scmi_protocol_release(const struct scmi_handle *handle, u8 protocol_id)
2274 {
2275 	struct scmi_info *info = handle_to_scmi_info(handle);
2276 	struct scmi_protocol_instance *pi;
2277 
2278 	mutex_lock(&info->protocols_mtx);
2279 	pi = idr_find(&info->protocols, protocol_id);
2280 	if (WARN_ON(!pi))
2281 		goto out;
2282 
2283 	if (refcount_dec_and_test(&pi->users)) {
2284 		void *gid = pi->gid;
2285 
2286 		if (pi->proto->events)
2287 			scmi_deregister_protocol_events(handle, protocol_id);
2288 
2289 		if (pi->proto->instance_deinit)
2290 			pi->proto->instance_deinit(&pi->ph);
2291 
2292 		idr_remove(&info->protocols, protocol_id);
2293 
2294 		scmi_protocol_put(pi->proto);
2295 
2296 		devres_release_group(handle->dev, gid);
2297 		dev_dbg(handle->dev, "De-Initialized protocol: 0x%X\n",
2298 			protocol_id);
2299 	}
2300 
2301 out:
2302 	mutex_unlock(&info->protocols_mtx);
2303 }
2304 
2305 void scmi_setup_protocol_implemented(const struct scmi_protocol_handle *ph,
2306 				     u8 *prot_imp)
2307 {
2308 	const struct scmi_protocol_instance *pi = ph_to_pi(ph);
2309 	struct scmi_info *info = handle_to_scmi_info(pi->handle);
2310 
2311 	info->protocols_imp = prot_imp;
2312 }
2313 
2314 static bool
2315 scmi_is_protocol_implemented(const struct scmi_handle *handle, u8 prot_id)
2316 {
2317 	int i;
2318 	struct scmi_info *info = handle_to_scmi_info(handle);
2319 	struct scmi_revision_info *rev = handle->version;
2320 
2321 	if (!info->protocols_imp)
2322 		return false;
2323 
2324 	for (i = 0; i < rev->num_protocols; i++)
2325 		if (info->protocols_imp[i] == prot_id)
2326 			return true;
2327 	return false;
2328 }
2329 
2330 struct scmi_protocol_devres {
2331 	const struct scmi_handle *handle;
2332 	u8 protocol_id;
2333 };
2334 
2335 static void scmi_devm_release_protocol(struct device *dev, void *res)
2336 {
2337 	struct scmi_protocol_devres *dres = res;
2338 
2339 	scmi_protocol_release(dres->handle, dres->protocol_id);
2340 }
2341 
2342 static struct scmi_protocol_instance __must_check *
2343 scmi_devres_protocol_instance_get(struct scmi_device *sdev, u8 protocol_id)
2344 {
2345 	struct scmi_protocol_instance *pi;
2346 	struct scmi_protocol_devres *dres;
2347 
2348 	dres = devres_alloc(scmi_devm_release_protocol,
2349 			    sizeof(*dres), GFP_KERNEL);
2350 	if (!dres)
2351 		return ERR_PTR(-ENOMEM);
2352 
2353 	pi = scmi_get_protocol_instance(sdev->handle, protocol_id);
2354 	if (IS_ERR(pi)) {
2355 		devres_free(dres);
2356 		return pi;
2357 	}
2358 
2359 	dres->handle = sdev->handle;
2360 	dres->protocol_id = protocol_id;
2361 	devres_add(&sdev->dev, dres);
2362 
2363 	return pi;
2364 }
2365 
2366 /**
2367  * scmi_devm_protocol_get  - Devres managed get protocol operations and handle
2368  * @sdev: A reference to an scmi_device whose embedded struct device is to
2369  *	  be used for devres accounting.
2370  * @protocol_id: The protocol being requested.
2371  * @ph: A pointer reference used to pass back the associated protocol handle.
2372  *
2373  * Get hold of a protocol accounting for its usage, eventually triggering its
2374  * initialization, and returning the protocol specific operations and related
2375  * protocol handle which will be used as first argument in most of the
2376  * protocols operations methods.
2377  * Being a devres based managed method, protocol hold will be automatically
2378  * released, and possibly de-initialized on last user, once the SCMI driver
2379  * owning the scmi_device is unbound from it.
2380  *
2381  * Return: A reference to the requested protocol operations or error.
2382  *	   Must be checked for errors by caller.
2383  */
2384 static const void __must_check *
2385 scmi_devm_protocol_get(struct scmi_device *sdev, u8 protocol_id,
2386 		       struct scmi_protocol_handle **ph)
2387 {
2388 	struct scmi_protocol_instance *pi;
2389 
2390 	if (!ph)
2391 		return ERR_PTR(-EINVAL);
2392 
2393 	pi = scmi_devres_protocol_instance_get(sdev, protocol_id);
2394 	if (IS_ERR(pi))
2395 		return pi;
2396 
2397 	*ph = &pi->ph;
2398 
2399 	return pi->proto->ops;
2400 }
2401 
2402 /**
2403  * scmi_devm_protocol_acquire  - Devres managed helper to get hold of a protocol
2404  * @sdev: A reference to an scmi_device whose embedded struct device is to
2405  *	  be used for devres accounting.
2406  * @protocol_id: The protocol being requested.
2407  *
2408  * Get hold of a protocol accounting for its usage, possibly triggering its
2409  * initialization but without getting access to its protocol specific operations
2410  * and handle.
2411  *
2412  * Being a devres based managed method, protocol hold will be automatically
2413  * released, and possibly de-initialized on last user, once the SCMI driver
2414  * owning the scmi_device is unbound from it.
2415  *
2416  * Return: 0 on SUCCESS
2417  */
2418 static int __must_check scmi_devm_protocol_acquire(struct scmi_device *sdev,
2419 						   u8 protocol_id)
2420 {
2421 	struct scmi_protocol_instance *pi;
2422 
2423 	pi = scmi_devres_protocol_instance_get(sdev, protocol_id);
2424 	if (IS_ERR(pi))
2425 		return PTR_ERR(pi);
2426 
2427 	return 0;
2428 }
2429 
2430 static int scmi_devm_protocol_match(struct device *dev, void *res, void *data)
2431 {
2432 	struct scmi_protocol_devres *dres = res;
2433 
2434 	if (WARN_ON(!dres || !data))
2435 		return 0;
2436 
2437 	return dres->protocol_id == *((u8 *)data);
2438 }
2439 
2440 /**
2441  * scmi_devm_protocol_put  - Devres managed put protocol operations and handle
2442  * @sdev: A reference to an scmi_device whose embedded struct device is to
2443  *	  be used for devres accounting.
2444  * @protocol_id: The protocol being requested.
2445  *
2446  * Explicitly release a protocol hold previously obtained calling the above
2447  * @scmi_devm_protocol_get.
2448  */
2449 static void scmi_devm_protocol_put(struct scmi_device *sdev, u8 protocol_id)
2450 {
2451 	int ret;
2452 
2453 	ret = devres_release(&sdev->dev, scmi_devm_release_protocol,
2454 			     scmi_devm_protocol_match, &protocol_id);
2455 	WARN_ON(ret);
2456 }
2457 
2458 /**
2459  * scmi_is_transport_atomic  - Method to check if underlying transport for an
2460  * SCMI instance is configured as atomic.
2461  *
2462  * @handle: A reference to the SCMI platform instance.
2463  * @atomic_threshold: An optional return value for the system wide currently
2464  *		      configured threshold for atomic operations.
2465  *
2466  * Return: True if transport is configured as atomic
2467  */
2468 static bool scmi_is_transport_atomic(const struct scmi_handle *handle,
2469 				     unsigned int *atomic_threshold)
2470 {
2471 	bool ret;
2472 	struct scmi_info *info = handle_to_scmi_info(handle);
2473 
2474 	ret = info->desc->atomic_enabled &&
2475 		is_transport_polling_capable(info->desc);
2476 	if (ret && atomic_threshold)
2477 		*atomic_threshold = info->desc->atomic_threshold;
2478 
2479 	return ret;
2480 }
2481 
2482 /**
2483  * scmi_handle_get() - Get the SCMI handle for a device
2484  *
2485  * @dev: pointer to device for which we want SCMI handle
2486  *
2487  * NOTE: The function does not track individual clients of the framework
2488  * and is expected to be maintained by caller of SCMI protocol library.
2489  * scmi_handle_put must be balanced with successful scmi_handle_get
2490  *
2491  * Return: pointer to handle if successful, NULL on error
2492  */
2493 static struct scmi_handle *scmi_handle_get(struct device *dev)
2494 {
2495 	struct list_head *p;
2496 	struct scmi_info *info;
2497 	struct scmi_handle *handle = NULL;
2498 
2499 	mutex_lock(&scmi_list_mutex);
2500 	list_for_each(p, &scmi_list) {
2501 		info = list_entry(p, struct scmi_info, node);
2502 		if (dev->parent == info->dev) {
2503 			info->users++;
2504 			handle = &info->handle;
2505 			break;
2506 		}
2507 	}
2508 	mutex_unlock(&scmi_list_mutex);
2509 
2510 	return handle;
2511 }
2512 
2513 /**
2514  * scmi_handle_put() - Release the handle acquired by scmi_handle_get
2515  *
2516  * @handle: handle acquired by scmi_handle_get
2517  *
2518  * NOTE: The function does not track individual clients of the framework
2519  * and is expected to be maintained by caller of SCMI protocol library.
2520  * scmi_handle_put must be balanced with successful scmi_handle_get
2521  *
2522  * Return: 0 is successfully released
2523  *	if null was passed, it returns -EINVAL;
2524  */
2525 static int scmi_handle_put(const struct scmi_handle *handle)
2526 {
2527 	struct scmi_info *info;
2528 
2529 	if (!handle)
2530 		return -EINVAL;
2531 
2532 	info = handle_to_scmi_info(handle);
2533 	mutex_lock(&scmi_list_mutex);
2534 	if (!WARN_ON(!info->users))
2535 		info->users--;
2536 	mutex_unlock(&scmi_list_mutex);
2537 
2538 	return 0;
2539 }
2540 
2541 static void scmi_device_link_add(struct device *consumer,
2542 				 struct device *supplier)
2543 {
2544 	struct device_link *link;
2545 
2546 	link = device_link_add(consumer, supplier, DL_FLAG_AUTOREMOVE_CONSUMER);
2547 
2548 	WARN_ON(!link);
2549 }
2550 
2551 static void scmi_set_handle(struct scmi_device *scmi_dev)
2552 {
2553 	scmi_dev->handle = scmi_handle_get(&scmi_dev->dev);
2554 	if (scmi_dev->handle)
2555 		scmi_device_link_add(&scmi_dev->dev, scmi_dev->handle->dev);
2556 }
2557 
2558 static int __scmi_xfer_info_init(struct scmi_info *sinfo,
2559 				 struct scmi_xfers_info *info)
2560 {
2561 	int i;
2562 	struct scmi_xfer *xfer;
2563 	struct device *dev = sinfo->dev;
2564 	const struct scmi_desc *desc = sinfo->desc;
2565 
2566 	/* Pre-allocated messages, no more than what hdr.seq can support */
2567 	if (WARN_ON(!info->max_msg || info->max_msg > MSG_TOKEN_MAX)) {
2568 		dev_err(dev,
2569 			"Invalid maximum messages %d, not in range [1 - %lu]\n",
2570 			info->max_msg, MSG_TOKEN_MAX);
2571 		return -EINVAL;
2572 	}
2573 
2574 	hash_init(info->pending_xfers);
2575 
2576 	/* Allocate a bitmask sized to hold MSG_TOKEN_MAX tokens */
2577 	info->xfer_alloc_table = devm_bitmap_zalloc(dev, MSG_TOKEN_MAX,
2578 						    GFP_KERNEL);
2579 	if (!info->xfer_alloc_table)
2580 		return -ENOMEM;
2581 
2582 	/*
2583 	 * Preallocate a number of xfers equal to max inflight messages,
2584 	 * pre-initialize the buffer pointer to pre-allocated buffers and
2585 	 * attach all of them to the free list
2586 	 */
2587 	INIT_HLIST_HEAD(&info->free_xfers);
2588 	for (i = 0; i < info->max_msg; i++) {
2589 		xfer = devm_kzalloc(dev, sizeof(*xfer), GFP_KERNEL);
2590 		if (!xfer)
2591 			return -ENOMEM;
2592 
2593 		xfer->rx.buf = devm_kcalloc(dev, sizeof(u8), desc->max_msg_size,
2594 					    GFP_KERNEL);
2595 		if (!xfer->rx.buf)
2596 			return -ENOMEM;
2597 
2598 		xfer->tx.buf = xfer->rx.buf;
2599 		init_completion(&xfer->done);
2600 		spin_lock_init(&xfer->lock);
2601 
2602 		/* Add initialized xfer to the free list */
2603 		hlist_add_head(&xfer->node, &info->free_xfers);
2604 	}
2605 
2606 	spin_lock_init(&info->xfer_lock);
2607 
2608 	return 0;
2609 }
2610 
2611 static int scmi_channels_max_msg_configure(struct scmi_info *sinfo)
2612 {
2613 	const struct scmi_desc *desc = sinfo->desc;
2614 
2615 	if (!desc->ops->get_max_msg) {
2616 		sinfo->tx_minfo.max_msg = desc->max_msg;
2617 		sinfo->rx_minfo.max_msg = desc->max_msg;
2618 	} else {
2619 		struct scmi_chan_info *base_cinfo;
2620 
2621 		base_cinfo = idr_find(&sinfo->tx_idr, SCMI_PROTOCOL_BASE);
2622 		if (!base_cinfo)
2623 			return -EINVAL;
2624 		sinfo->tx_minfo.max_msg = desc->ops->get_max_msg(base_cinfo);
2625 
2626 		/* RX channel is optional so can be skipped */
2627 		base_cinfo = idr_find(&sinfo->rx_idr, SCMI_PROTOCOL_BASE);
2628 		if (base_cinfo)
2629 			sinfo->rx_minfo.max_msg =
2630 				desc->ops->get_max_msg(base_cinfo);
2631 	}
2632 
2633 	return 0;
2634 }
2635 
2636 static int scmi_xfer_info_init(struct scmi_info *sinfo)
2637 {
2638 	int ret;
2639 
2640 	ret = scmi_channels_max_msg_configure(sinfo);
2641 	if (ret)
2642 		return ret;
2643 
2644 	ret = __scmi_xfer_info_init(sinfo, &sinfo->tx_minfo);
2645 	if (!ret && !idr_is_empty(&sinfo->rx_idr))
2646 		ret = __scmi_xfer_info_init(sinfo, &sinfo->rx_minfo);
2647 
2648 	return ret;
2649 }
2650 
2651 static int scmi_chan_setup(struct scmi_info *info, struct device_node *of_node,
2652 			   int prot_id, bool tx)
2653 {
2654 	int ret, idx;
2655 	char name[32];
2656 	struct scmi_chan_info *cinfo;
2657 	struct idr *idr;
2658 	struct scmi_device *tdev = NULL;
2659 
2660 	/* Transmit channel is first entry i.e. index 0 */
2661 	idx = tx ? 0 : 1;
2662 	idr = tx ? &info->tx_idr : &info->rx_idr;
2663 
2664 	if (!info->desc->ops->chan_available(of_node, idx)) {
2665 		cinfo = idr_find(idr, SCMI_PROTOCOL_BASE);
2666 		if (unlikely(!cinfo)) /* Possible only if platform has no Rx */
2667 			return -EINVAL;
2668 		goto idr_alloc;
2669 	}
2670 
2671 	cinfo = devm_kzalloc(info->dev, sizeof(*cinfo), GFP_KERNEL);
2672 	if (!cinfo)
2673 		return -ENOMEM;
2674 
2675 	cinfo->is_p2a = !tx;
2676 	cinfo->rx_timeout_ms = info->desc->max_rx_timeout_ms;
2677 	cinfo->max_msg_size = info->desc->max_msg_size;
2678 
2679 	/* Create a unique name for this transport device */
2680 	snprintf(name, 32, "__scmi_transport_device_%s_%02X",
2681 		 idx ? "rx" : "tx", prot_id);
2682 	/* Create a uniquely named, dedicated transport device for this chan */
2683 	tdev = scmi_device_create(of_node, info->dev, prot_id, name);
2684 	if (!tdev) {
2685 		dev_err(info->dev,
2686 			"failed to create transport device (%s)\n", name);
2687 		devm_kfree(info->dev, cinfo);
2688 		return -EINVAL;
2689 	}
2690 	of_node_get(of_node);
2691 
2692 	cinfo->id = prot_id;
2693 	cinfo->dev = &tdev->dev;
2694 	ret = info->desc->ops->chan_setup(cinfo, info->dev, tx);
2695 	if (ret) {
2696 		of_node_put(of_node);
2697 		scmi_device_destroy(info->dev, prot_id, name);
2698 		devm_kfree(info->dev, cinfo);
2699 		return ret;
2700 	}
2701 
2702 	if (tx && is_polling_required(cinfo, info->desc)) {
2703 		if (is_transport_polling_capable(info->desc))
2704 			dev_info(&tdev->dev,
2705 				 "Enabled polling mode TX channel - prot_id:%d\n",
2706 				 prot_id);
2707 		else
2708 			dev_warn(&tdev->dev,
2709 				 "Polling mode NOT supported by transport.\n");
2710 	}
2711 
2712 idr_alloc:
2713 	ret = idr_alloc(idr, cinfo, prot_id, prot_id + 1, GFP_KERNEL);
2714 	if (ret != prot_id) {
2715 		dev_err(info->dev,
2716 			"unable to allocate SCMI idr slot err %d\n", ret);
2717 		/* Destroy channel and device only if created by this call. */
2718 		if (tdev) {
2719 			of_node_put(of_node);
2720 			scmi_device_destroy(info->dev, prot_id, name);
2721 			devm_kfree(info->dev, cinfo);
2722 		}
2723 		return ret;
2724 	}
2725 
2726 	cinfo->handle = &info->handle;
2727 	return 0;
2728 }
2729 
2730 static inline int
2731 scmi_txrx_setup(struct scmi_info *info, struct device_node *of_node,
2732 		int prot_id)
2733 {
2734 	int ret = scmi_chan_setup(info, of_node, prot_id, true);
2735 
2736 	if (!ret) {
2737 		/* Rx is optional, report only memory errors */
2738 		ret = scmi_chan_setup(info, of_node, prot_id, false);
2739 		if (ret && ret != -ENOMEM)
2740 			ret = 0;
2741 	}
2742 
2743 	if (ret)
2744 		dev_err(info->dev,
2745 			"failed to setup channel for protocol:0x%X\n", prot_id);
2746 
2747 	return ret;
2748 }
2749 
2750 /**
2751  * scmi_channels_setup  - Helper to initialize all required channels
2752  *
2753  * @info: The SCMI instance descriptor.
2754  *
2755  * Initialize all the channels found described in the DT against the underlying
2756  * configured transport using custom defined dedicated devices instead of
2757  * borrowing devices from the SCMI drivers; this way channels are initialized
2758  * upfront during core SCMI stack probing and are no more coupled with SCMI
2759  * devices used by SCMI drivers.
2760  *
2761  * Note that, even though a pair of TX/RX channels is associated to each
2762  * protocol defined in the DT, a distinct freshly initialized channel is
2763  * created only if the DT node for the protocol at hand describes a dedicated
2764  * channel: in all the other cases the common BASE protocol channel is reused.
2765  *
2766  * Return: 0 on Success
2767  */
2768 static int scmi_channels_setup(struct scmi_info *info)
2769 {
2770 	int ret;
2771 	struct device_node *top_np = info->dev->of_node;
2772 
2773 	/* Initialize a common generic channel at first */
2774 	ret = scmi_txrx_setup(info, top_np, SCMI_PROTOCOL_BASE);
2775 	if (ret)
2776 		return ret;
2777 
2778 	for_each_available_child_of_node_scoped(top_np, child) {
2779 		u32 prot_id;
2780 
2781 		if (of_property_read_u32(child, "reg", &prot_id))
2782 			continue;
2783 
2784 		if (!FIELD_FIT(MSG_PROTOCOL_ID_MASK, prot_id))
2785 			dev_err(info->dev,
2786 				"Out of range protocol %d\n", prot_id);
2787 
2788 		ret = scmi_txrx_setup(info, child, prot_id);
2789 		if (ret)
2790 			return ret;
2791 	}
2792 
2793 	return 0;
2794 }
2795 
2796 static int scmi_chan_destroy(int id, void *p, void *idr)
2797 {
2798 	struct scmi_chan_info *cinfo = p;
2799 
2800 	if (cinfo->dev) {
2801 		struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
2802 		struct scmi_device *sdev = to_scmi_dev(cinfo->dev);
2803 
2804 		of_node_put(cinfo->dev->of_node);
2805 		scmi_device_destroy(info->dev, id, sdev->name);
2806 		cinfo->dev = NULL;
2807 	}
2808 
2809 	idr_remove(idr, id);
2810 
2811 	return 0;
2812 }
2813 
2814 static void scmi_cleanup_channels(struct scmi_info *info, struct idr *idr)
2815 {
2816 	/* At first free all channels at the transport layer ... */
2817 	idr_for_each(idr, info->desc->ops->chan_free, idr);
2818 
2819 	/* ...then destroy all underlying devices */
2820 	idr_for_each(idr, scmi_chan_destroy, idr);
2821 
2822 	idr_destroy(idr);
2823 }
2824 
2825 static void scmi_cleanup_txrx_channels(struct scmi_info *info)
2826 {
2827 	scmi_cleanup_channels(info, &info->tx_idr);
2828 
2829 	scmi_cleanup_channels(info, &info->rx_idr);
2830 }
2831 
2832 static int scmi_bus_notifier(struct notifier_block *nb,
2833 			     unsigned long action, void *data)
2834 {
2835 	struct scmi_info *info = bus_nb_to_scmi_info(nb);
2836 	struct scmi_device *sdev = to_scmi_dev(data);
2837 
2838 	/* Skip transport devices and devices of different SCMI instances */
2839 	if (!strncmp(sdev->name, "__scmi_transport_device", 23) ||
2840 	    sdev->dev.parent != info->dev)
2841 		return NOTIFY_DONE;
2842 
2843 	switch (action) {
2844 	case BUS_NOTIFY_BIND_DRIVER:
2845 		/* setup handle now as the transport is ready */
2846 		scmi_set_handle(sdev);
2847 		break;
2848 	case BUS_NOTIFY_UNBOUND_DRIVER:
2849 		scmi_handle_put(sdev->handle);
2850 		sdev->handle = NULL;
2851 		break;
2852 	default:
2853 		return NOTIFY_DONE;
2854 	}
2855 
2856 	dev_dbg(info->dev, "Device %s (%s) is now %s\n", dev_name(&sdev->dev),
2857 		sdev->name, action == BUS_NOTIFY_BIND_DRIVER ?
2858 		"about to be BOUND." : "UNBOUND.");
2859 
2860 	return NOTIFY_OK;
2861 }
2862 
2863 static int scmi_device_request_notifier(struct notifier_block *nb,
2864 					unsigned long action, void *data)
2865 {
2866 	struct device_node *np;
2867 	struct scmi_device_id *id_table = data;
2868 	struct scmi_info *info = req_nb_to_scmi_info(nb);
2869 
2870 	np = idr_find(&info->active_protocols, id_table->protocol_id);
2871 	if (!np)
2872 		return NOTIFY_DONE;
2873 
2874 	dev_dbg(info->dev, "%sRequested device (%s) for protocol 0x%x\n",
2875 		action == SCMI_BUS_NOTIFY_DEVICE_REQUEST ? "" : "UN-",
2876 		id_table->name, id_table->protocol_id);
2877 
2878 	switch (action) {
2879 	case SCMI_BUS_NOTIFY_DEVICE_REQUEST:
2880 		scmi_create_protocol_devices(np, info, id_table->protocol_id,
2881 					     id_table->name);
2882 		break;
2883 	case SCMI_BUS_NOTIFY_DEVICE_UNREQUEST:
2884 		scmi_destroy_protocol_devices(info, id_table->protocol_id,
2885 					      id_table->name);
2886 		break;
2887 	default:
2888 		return NOTIFY_DONE;
2889 	}
2890 
2891 	return NOTIFY_OK;
2892 }
2893 
2894 static const char * const dbg_counter_strs[] = {
2895 	"sent_ok",
2896 	"sent_fail",
2897 	"sent_fail_polling_unsupported",
2898 	"sent_fail_channel_not_found",
2899 	"response_ok",
2900 	"notification_ok",
2901 	"delayed_response_ok",
2902 	"xfers_response_timeout",
2903 	"xfers_response_polled_timeout",
2904 	"response_polled_ok",
2905 	"err_msg_unexpected",
2906 	"err_msg_invalid",
2907 	"err_msg_nomem",
2908 	"err_protocol",
2909 };
2910 
2911 static ssize_t reset_all_on_write(struct file *filp, const char __user *buf,
2912 				  size_t count, loff_t *ppos)
2913 {
2914 	struct scmi_debug_info *dbg = filp->private_data;
2915 
2916 	for (int i = 0; i < SCMI_DEBUG_COUNTERS_LAST; i++)
2917 		atomic_set(&dbg->counters[i], 0);
2918 
2919 	return count;
2920 }
2921 
2922 static const struct file_operations fops_reset_counts = {
2923 	.owner = THIS_MODULE,
2924 	.open = simple_open,
2925 	.write = reset_all_on_write,
2926 };
2927 
2928 static void scmi_debugfs_counters_setup(struct scmi_debug_info *dbg,
2929 					struct dentry *trans)
2930 {
2931 	struct dentry *counters;
2932 	int idx;
2933 
2934 	counters = debugfs_create_dir("counters", trans);
2935 
2936 	for (idx = 0; idx < SCMI_DEBUG_COUNTERS_LAST; idx++)
2937 		debugfs_create_atomic_t(dbg_counter_strs[idx], 0600, counters,
2938 					&dbg->counters[idx]);
2939 
2940 	debugfs_create_file("reset", 0200, counters, dbg, &fops_reset_counts);
2941 }
2942 
2943 static void scmi_debugfs_common_cleanup(void *d)
2944 {
2945 	struct scmi_debug_info *dbg = d;
2946 
2947 	if (!dbg)
2948 		return;
2949 
2950 	debugfs_remove_recursive(dbg->top_dentry);
2951 	kfree(dbg->name);
2952 	kfree(dbg->type);
2953 }
2954 
2955 static struct scmi_debug_info *scmi_debugfs_common_setup(struct scmi_info *info)
2956 {
2957 	char top_dir[16];
2958 	struct dentry *trans, *top_dentry;
2959 	struct scmi_debug_info *dbg;
2960 	const char *c_ptr = NULL;
2961 
2962 	dbg = devm_kzalloc(info->dev, sizeof(*dbg), GFP_KERNEL);
2963 	if (!dbg)
2964 		return NULL;
2965 
2966 	dbg->name = kstrdup(of_node_full_name(info->dev->of_node), GFP_KERNEL);
2967 	if (!dbg->name) {
2968 		devm_kfree(info->dev, dbg);
2969 		return NULL;
2970 	}
2971 
2972 	of_property_read_string(info->dev->of_node, "compatible", &c_ptr);
2973 	dbg->type = kstrdup(c_ptr, GFP_KERNEL);
2974 	if (!dbg->type) {
2975 		kfree(dbg->name);
2976 		devm_kfree(info->dev, dbg);
2977 		return NULL;
2978 	}
2979 
2980 	snprintf(top_dir, 16, "%d", info->id);
2981 	top_dentry = debugfs_create_dir(top_dir, scmi_top_dentry);
2982 	trans = debugfs_create_dir("transport", top_dentry);
2983 
2984 	dbg->is_atomic = info->desc->atomic_enabled &&
2985 				is_transport_polling_capable(info->desc);
2986 
2987 	debugfs_create_str("instance_name", 0400, top_dentry,
2988 			   (char **)&dbg->name);
2989 
2990 	debugfs_create_u32("atomic_threshold_us", 0400, top_dentry,
2991 			   (u32 *)&info->desc->atomic_threshold);
2992 
2993 	debugfs_create_str("type", 0400, trans, (char **)&dbg->type);
2994 
2995 	debugfs_create_bool("is_atomic", 0400, trans, &dbg->is_atomic);
2996 
2997 	debugfs_create_u32("max_rx_timeout_ms", 0400, trans,
2998 			   (u32 *)&info->desc->max_rx_timeout_ms);
2999 
3000 	debugfs_create_u32("max_msg_size", 0400, trans,
3001 			   (u32 *)&info->desc->max_msg_size);
3002 
3003 	debugfs_create_u32("tx_max_msg", 0400, trans,
3004 			   (u32 *)&info->tx_minfo.max_msg);
3005 
3006 	debugfs_create_u32("rx_max_msg", 0400, trans,
3007 			   (u32 *)&info->rx_minfo.max_msg);
3008 
3009 	if (IS_ENABLED(CONFIG_ARM_SCMI_DEBUG_COUNTERS))
3010 		scmi_debugfs_counters_setup(dbg, trans);
3011 
3012 	dbg->top_dentry = top_dentry;
3013 
3014 	if (devm_add_action_or_reset(info->dev,
3015 				     scmi_debugfs_common_cleanup, dbg))
3016 		return NULL;
3017 
3018 	return dbg;
3019 }
3020 
3021 static int scmi_debugfs_raw_mode_setup(struct scmi_info *info)
3022 {
3023 	int id, num_chans = 0, ret = 0;
3024 	struct scmi_chan_info *cinfo;
3025 	u8 channels[SCMI_MAX_CHANNELS] = {};
3026 	DECLARE_BITMAP(protos, SCMI_MAX_CHANNELS) = {};
3027 
3028 	if (!info->dbg)
3029 		return -EINVAL;
3030 
3031 	/* Enumerate all channels to collect their ids */
3032 	idr_for_each_entry(&info->tx_idr, cinfo, id) {
3033 		/*
3034 		 * Cannot happen, but be defensive.
3035 		 * Zero as num_chans is ok, warn and carry on.
3036 		 */
3037 		if (num_chans >= SCMI_MAX_CHANNELS || !cinfo) {
3038 			dev_warn(info->dev,
3039 				 "SCMI RAW - Error enumerating channels\n");
3040 			break;
3041 		}
3042 
3043 		if (!test_bit(cinfo->id, protos)) {
3044 			channels[num_chans++] = cinfo->id;
3045 			set_bit(cinfo->id, protos);
3046 		}
3047 	}
3048 
3049 	info->raw = scmi_raw_mode_init(&info->handle, info->dbg->top_dentry,
3050 				       info->id, channels, num_chans,
3051 				       info->desc, info->tx_minfo.max_msg);
3052 	if (IS_ERR(info->raw)) {
3053 		dev_err(info->dev, "Failed to initialize SCMI RAW Mode !\n");
3054 		ret = PTR_ERR(info->raw);
3055 		info->raw = NULL;
3056 	}
3057 
3058 	return ret;
3059 }
3060 
3061 static const struct scmi_desc *scmi_transport_setup(struct device *dev)
3062 {
3063 	struct scmi_transport *trans;
3064 	int ret;
3065 
3066 	trans = dev_get_platdata(dev);
3067 	if (!trans || !trans->supplier || !trans->core_ops)
3068 		return NULL;
3069 
3070 	if (!device_link_add(dev, trans->supplier, DL_FLAG_AUTOREMOVE_CONSUMER)) {
3071 		dev_err(dev,
3072 			"Adding link to supplier transport device failed\n");
3073 		return NULL;
3074 	}
3075 
3076 	/* Provide core transport ops */
3077 	*trans->core_ops = &scmi_trans_core_ops;
3078 
3079 	dev_info(dev, "Using %s\n", dev_driver_string(trans->supplier));
3080 
3081 	ret = of_property_read_u32(dev->of_node, "arm,max-rx-timeout-ms",
3082 				   &trans->desc.max_rx_timeout_ms);
3083 	if (ret && ret != -EINVAL)
3084 		dev_err(dev, "Malformed arm,max-rx-timeout-ms DT property.\n");
3085 
3086 	ret = of_property_read_u32(dev->of_node, "arm,max-msg-size",
3087 				   &trans->desc.max_msg_size);
3088 	if (ret && ret != -EINVAL)
3089 		dev_err(dev, "Malformed arm,max-msg-size DT property.\n");
3090 
3091 	ret = of_property_read_u32(dev->of_node, "arm,max-msg",
3092 				   &trans->desc.max_msg);
3093 	if (ret && ret != -EINVAL)
3094 		dev_err(dev, "Malformed arm,max-msg DT property.\n");
3095 
3096 	dev_info(dev,
3097 		 "SCMI max-rx-timeout: %dms / max-msg-size: %dbytes / max-msg: %d\n",
3098 		 trans->desc.max_rx_timeout_ms, trans->desc.max_msg_size,
3099 		 trans->desc.max_msg);
3100 
3101 	/* System wide atomic threshold for atomic ops .. if any */
3102 	if (!of_property_read_u32(dev->of_node, "atomic-threshold-us",
3103 				  &trans->desc.atomic_threshold))
3104 		dev_info(dev,
3105 			 "SCMI System wide atomic threshold set to %u us\n",
3106 			 trans->desc.atomic_threshold);
3107 
3108 	return &trans->desc;
3109 }
3110 
3111 static int scmi_probe(struct platform_device *pdev)
3112 {
3113 	int ret;
3114 	char *err_str = "probe failure\n";
3115 	struct scmi_handle *handle;
3116 	const struct scmi_desc *desc;
3117 	struct scmi_info *info;
3118 	bool coex = IS_ENABLED(CONFIG_ARM_SCMI_RAW_MODE_SUPPORT_COEX);
3119 	struct device *dev = &pdev->dev;
3120 	struct device_node *child, *np = dev->of_node;
3121 
3122 	desc = scmi_transport_setup(dev);
3123 	if (!desc) {
3124 		err_str = "transport invalid\n";
3125 		ret = -EINVAL;
3126 		goto out_err;
3127 	}
3128 
3129 	info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
3130 	if (!info)
3131 		return -ENOMEM;
3132 
3133 	info->id = ida_alloc_min(&scmi_id, 0, GFP_KERNEL);
3134 	if (info->id < 0)
3135 		return info->id;
3136 
3137 	info->dev = dev;
3138 	info->desc = desc;
3139 	info->bus_nb.notifier_call = scmi_bus_notifier;
3140 	info->dev_req_nb.notifier_call = scmi_device_request_notifier;
3141 	INIT_LIST_HEAD(&info->node);
3142 	idr_init(&info->protocols);
3143 	mutex_init(&info->protocols_mtx);
3144 	idr_init(&info->active_protocols);
3145 	mutex_init(&info->devreq_mtx);
3146 
3147 	platform_set_drvdata(pdev, info);
3148 	idr_init(&info->tx_idr);
3149 	idr_init(&info->rx_idr);
3150 
3151 	handle = &info->handle;
3152 	handle->dev = info->dev;
3153 	handle->version = &info->version;
3154 	handle->devm_protocol_acquire = scmi_devm_protocol_acquire;
3155 	handle->devm_protocol_get = scmi_devm_protocol_get;
3156 	handle->devm_protocol_put = scmi_devm_protocol_put;
3157 	handle->is_transport_atomic = scmi_is_transport_atomic;
3158 
3159 	/* Setup all channels described in the DT at first */
3160 	ret = scmi_channels_setup(info);
3161 	if (ret) {
3162 		err_str = "failed to setup channels\n";
3163 		goto clear_ida;
3164 	}
3165 
3166 	ret = bus_register_notifier(&scmi_bus_type, &info->bus_nb);
3167 	if (ret) {
3168 		err_str = "failed to register bus notifier\n";
3169 		goto clear_txrx_setup;
3170 	}
3171 
3172 	ret = blocking_notifier_chain_register(&scmi_requested_devices_nh,
3173 					       &info->dev_req_nb);
3174 	if (ret) {
3175 		err_str = "failed to register device notifier\n";
3176 		goto clear_bus_notifier;
3177 	}
3178 
3179 	ret = scmi_xfer_info_init(info);
3180 	if (ret) {
3181 		err_str = "failed to init xfers pool\n";
3182 		goto clear_dev_req_notifier;
3183 	}
3184 
3185 	if (scmi_top_dentry) {
3186 		info->dbg = scmi_debugfs_common_setup(info);
3187 		if (!info->dbg)
3188 			dev_warn(dev, "Failed to setup SCMI debugfs.\n");
3189 
3190 		if (IS_ENABLED(CONFIG_ARM_SCMI_RAW_MODE_SUPPORT)) {
3191 			ret = scmi_debugfs_raw_mode_setup(info);
3192 			if (!coex) {
3193 				if (ret)
3194 					goto clear_dev_req_notifier;
3195 
3196 				/* Bail out anyway when coex disabled. */
3197 				return 0;
3198 			}
3199 
3200 			/* Coex enabled, carry on in any case. */
3201 			dev_info(dev, "SCMI RAW Mode COEX enabled !\n");
3202 		}
3203 	}
3204 
3205 	if (scmi_notification_init(handle))
3206 		dev_err(dev, "SCMI Notifications NOT available.\n");
3207 
3208 	if (info->desc->atomic_enabled &&
3209 	    !is_transport_polling_capable(info->desc))
3210 		dev_err(dev,
3211 			"Transport is not polling capable. Atomic mode not supported.\n");
3212 
3213 	/*
3214 	 * Trigger SCMI Base protocol initialization.
3215 	 * It's mandatory and won't be ever released/deinit until the
3216 	 * SCMI stack is shutdown/unloaded as a whole.
3217 	 */
3218 	ret = scmi_protocol_acquire(handle, SCMI_PROTOCOL_BASE);
3219 	if (ret) {
3220 		err_str = "unable to communicate with SCMI\n";
3221 		if (coex) {
3222 			dev_err(dev, "%s", err_str);
3223 			return 0;
3224 		}
3225 		goto notification_exit;
3226 	}
3227 
3228 	mutex_lock(&scmi_list_mutex);
3229 	list_add_tail(&info->node, &scmi_list);
3230 	mutex_unlock(&scmi_list_mutex);
3231 
3232 	for_each_available_child_of_node(np, child) {
3233 		u32 prot_id;
3234 
3235 		if (of_property_read_u32(child, "reg", &prot_id))
3236 			continue;
3237 
3238 		if (!FIELD_FIT(MSG_PROTOCOL_ID_MASK, prot_id))
3239 			dev_err(dev, "Out of range protocol %d\n", prot_id);
3240 
3241 		if (!scmi_is_protocol_implemented(handle, prot_id)) {
3242 			dev_err(dev, "SCMI protocol %d not implemented\n",
3243 				prot_id);
3244 			continue;
3245 		}
3246 
3247 		/*
3248 		 * Save this valid DT protocol descriptor amongst
3249 		 * @active_protocols for this SCMI instance/
3250 		 */
3251 		ret = idr_alloc(&info->active_protocols, child,
3252 				prot_id, prot_id + 1, GFP_KERNEL);
3253 		if (ret != prot_id) {
3254 			dev_err(dev, "SCMI protocol %d already activated. Skip\n",
3255 				prot_id);
3256 			continue;
3257 		}
3258 
3259 		of_node_get(child);
3260 		scmi_create_protocol_devices(child, info, prot_id, NULL);
3261 	}
3262 
3263 	return 0;
3264 
3265 notification_exit:
3266 	if (IS_ENABLED(CONFIG_ARM_SCMI_RAW_MODE_SUPPORT))
3267 		scmi_raw_mode_cleanup(info->raw);
3268 	scmi_notification_exit(&info->handle);
3269 clear_dev_req_notifier:
3270 	blocking_notifier_chain_unregister(&scmi_requested_devices_nh,
3271 					   &info->dev_req_nb);
3272 clear_bus_notifier:
3273 	bus_unregister_notifier(&scmi_bus_type, &info->bus_nb);
3274 clear_txrx_setup:
3275 	scmi_cleanup_txrx_channels(info);
3276 clear_ida:
3277 	ida_free(&scmi_id, info->id);
3278 
3279 out_err:
3280 	return dev_err_probe(dev, ret, "%s", err_str);
3281 }
3282 
3283 static void scmi_remove(struct platform_device *pdev)
3284 {
3285 	int id;
3286 	struct scmi_info *info = platform_get_drvdata(pdev);
3287 	struct device_node *child;
3288 
3289 	if (IS_ENABLED(CONFIG_ARM_SCMI_RAW_MODE_SUPPORT))
3290 		scmi_raw_mode_cleanup(info->raw);
3291 
3292 	mutex_lock(&scmi_list_mutex);
3293 	if (info->users)
3294 		dev_warn(&pdev->dev,
3295 			 "Still active SCMI users will be forcibly unbound.\n");
3296 	list_del(&info->node);
3297 	mutex_unlock(&scmi_list_mutex);
3298 
3299 	scmi_notification_exit(&info->handle);
3300 
3301 	mutex_lock(&info->protocols_mtx);
3302 	idr_destroy(&info->protocols);
3303 	mutex_unlock(&info->protocols_mtx);
3304 
3305 	idr_for_each_entry(&info->active_protocols, child, id)
3306 		of_node_put(child);
3307 	idr_destroy(&info->active_protocols);
3308 
3309 	blocking_notifier_chain_unregister(&scmi_requested_devices_nh,
3310 					   &info->dev_req_nb);
3311 	bus_unregister_notifier(&scmi_bus_type, &info->bus_nb);
3312 
3313 	/* Safe to free channels since no more users */
3314 	scmi_cleanup_txrx_channels(info);
3315 
3316 	ida_free(&scmi_id, info->id);
3317 }
3318 
3319 static ssize_t protocol_version_show(struct device *dev,
3320 				     struct device_attribute *attr, char *buf)
3321 {
3322 	struct scmi_info *info = dev_get_drvdata(dev);
3323 
3324 	return sprintf(buf, "%u.%u\n", info->version.major_ver,
3325 		       info->version.minor_ver);
3326 }
3327 static DEVICE_ATTR_RO(protocol_version);
3328 
3329 static ssize_t firmware_version_show(struct device *dev,
3330 				     struct device_attribute *attr, char *buf)
3331 {
3332 	struct scmi_info *info = dev_get_drvdata(dev);
3333 
3334 	return sprintf(buf, "0x%x\n", info->version.impl_ver);
3335 }
3336 static DEVICE_ATTR_RO(firmware_version);
3337 
3338 static ssize_t vendor_id_show(struct device *dev,
3339 			      struct device_attribute *attr, char *buf)
3340 {
3341 	struct scmi_info *info = dev_get_drvdata(dev);
3342 
3343 	return sprintf(buf, "%s\n", info->version.vendor_id);
3344 }
3345 static DEVICE_ATTR_RO(vendor_id);
3346 
3347 static ssize_t sub_vendor_id_show(struct device *dev,
3348 				  struct device_attribute *attr, char *buf)
3349 {
3350 	struct scmi_info *info = dev_get_drvdata(dev);
3351 
3352 	return sprintf(buf, "%s\n", info->version.sub_vendor_id);
3353 }
3354 static DEVICE_ATTR_RO(sub_vendor_id);
3355 
3356 static struct attribute *versions_attrs[] = {
3357 	&dev_attr_firmware_version.attr,
3358 	&dev_attr_protocol_version.attr,
3359 	&dev_attr_vendor_id.attr,
3360 	&dev_attr_sub_vendor_id.attr,
3361 	NULL,
3362 };
3363 ATTRIBUTE_GROUPS(versions);
3364 
3365 static struct platform_driver scmi_driver = {
3366 	.driver = {
3367 		   .name = "arm-scmi",
3368 		   .suppress_bind_attrs = true,
3369 		   .dev_groups = versions_groups,
3370 		   },
3371 	.probe = scmi_probe,
3372 	.remove = scmi_remove,
3373 };
3374 
3375 static struct dentry *scmi_debugfs_init(void)
3376 {
3377 	struct dentry *d;
3378 
3379 	d = debugfs_create_dir("scmi", NULL);
3380 	if (IS_ERR(d)) {
3381 		pr_err("Could NOT create SCMI top dentry.\n");
3382 		return NULL;
3383 	}
3384 
3385 	return d;
3386 }
3387 
3388 static int __init scmi_driver_init(void)
3389 {
3390 	/* Bail out if no SCMI transport was configured */
3391 	if (WARN_ON(!IS_ENABLED(CONFIG_ARM_SCMI_HAVE_TRANSPORT)))
3392 		return -EINVAL;
3393 
3394 	if (IS_ENABLED(CONFIG_ARM_SCMI_HAVE_SHMEM))
3395 		scmi_trans_core_ops.shmem = scmi_shared_mem_operations_get();
3396 
3397 	if (IS_ENABLED(CONFIG_ARM_SCMI_HAVE_MSG))
3398 		scmi_trans_core_ops.msg = scmi_message_operations_get();
3399 
3400 	if (IS_ENABLED(CONFIG_ARM_SCMI_NEED_DEBUGFS))
3401 		scmi_top_dentry = scmi_debugfs_init();
3402 
3403 	scmi_base_register();
3404 
3405 	scmi_clock_register();
3406 	scmi_perf_register();
3407 	scmi_power_register();
3408 	scmi_reset_register();
3409 	scmi_sensors_register();
3410 	scmi_voltage_register();
3411 	scmi_system_register();
3412 	scmi_powercap_register();
3413 	scmi_pinctrl_register();
3414 
3415 	return platform_driver_register(&scmi_driver);
3416 }
3417 module_init(scmi_driver_init);
3418 
3419 static void __exit scmi_driver_exit(void)
3420 {
3421 	scmi_base_unregister();
3422 
3423 	scmi_clock_unregister();
3424 	scmi_perf_unregister();
3425 	scmi_power_unregister();
3426 	scmi_reset_unregister();
3427 	scmi_sensors_unregister();
3428 	scmi_voltage_unregister();
3429 	scmi_system_unregister();
3430 	scmi_powercap_unregister();
3431 	scmi_pinctrl_unregister();
3432 
3433 	platform_driver_unregister(&scmi_driver);
3434 
3435 	debugfs_remove_recursive(scmi_top_dentry);
3436 }
3437 module_exit(scmi_driver_exit);
3438 
3439 MODULE_ALIAS("platform:arm-scmi");
3440 MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
3441 MODULE_DESCRIPTION("ARM SCMI protocol driver");
3442 MODULE_LICENSE("GPL v2");
3443