1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Main SSAM/SSH controller structure and functionality.
4  *
5  * Copyright (C) 2019-2020 Maximilian Luz <luzmaximilian@gmail.com>
6  */
7 
8 #include <linux/acpi.h>
9 #include <linux/atomic.h>
10 #include <linux/completion.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/interrupt.h>
13 #include <linux/kref.h>
14 #include <linux/limits.h>
15 #include <linux/list.h>
16 #include <linux/lockdep.h>
17 #include <linux/mutex.h>
18 #include <linux/rculist.h>
19 #include <linux/rbtree.h>
20 #include <linux/rwsem.h>
21 #include <linux/serdev.h>
22 #include <linux/slab.h>
23 #include <linux/spinlock.h>
24 #include <linux/srcu.h>
25 #include <linux/types.h>
26 #include <linux/workqueue.h>
27 
28 #include <linux/surface_aggregator/controller.h>
29 #include <linux/surface_aggregator/serial_hub.h>
30 
31 #include "controller.h"
32 #include "ssh_msgb.h"
33 #include "ssh_request_layer.h"
34 
35 
36 /* -- Safe counters. -------------------------------------------------------- */
37 
38 /**
39  * ssh_seq_reset() - Reset/initialize sequence ID counter.
40  * @c: The counter to reset.
41  */
42 static void ssh_seq_reset(struct ssh_seq_counter *c)
43 {
44 	WRITE_ONCE(c->value, 0);
45 }
46 
47 /**
48  * ssh_seq_next() - Get next sequence ID.
49  * @c: The counter providing the sequence IDs.
50  *
51  * Return: Returns the next sequence ID of the counter.
52  */
53 static u8 ssh_seq_next(struct ssh_seq_counter *c)
54 {
55 	u8 old = READ_ONCE(c->value);
56 	u8 new = old + 1;
57 	u8 ret;
58 
59 	while (unlikely((ret = cmpxchg(&c->value, old, new)) != old)) {
60 		old = ret;
61 		new = old + 1;
62 	}
63 
64 	return old;
65 }
66 
67 /**
68  * ssh_rqid_reset() - Reset/initialize request ID counter.
69  * @c: The counter to reset.
70  */
71 static void ssh_rqid_reset(struct ssh_rqid_counter *c)
72 {
73 	WRITE_ONCE(c->value, 0);
74 }
75 
76 /**
77  * ssh_rqid_next() - Get next request ID.
78  * @c: The counter providing the request IDs.
79  *
80  * Return: Returns the next request ID of the counter, skipping any reserved
81  * request IDs.
82  */
83 static u16 ssh_rqid_next(struct ssh_rqid_counter *c)
84 {
85 	u16 old = READ_ONCE(c->value);
86 	u16 new = ssh_rqid_next_valid(old);
87 	u16 ret;
88 
89 	while (unlikely((ret = cmpxchg(&c->value, old, new)) != old)) {
90 		old = ret;
91 		new = ssh_rqid_next_valid(old);
92 	}
93 
94 	return old;
95 }
96 
97 
98 /* -- Event notifier/callbacks. --------------------------------------------- */
99 /*
100  * The notifier system is based on linux/notifier.h, specifically the SRCU
101  * implementation. The difference to that is, that some bits of the notifier
102  * call return value can be tracked across multiple calls. This is done so
103  * that handling of events can be tracked and a warning can be issued in case
104  * an event goes unhandled. The idea of that warning is that it should help
105  * discover and identify new/currently unimplemented features.
106  */
107 
108 /**
109  * ssam_event_matches_notifier() - Test if an event matches a notifier.
110  * @n: The event notifier to test against.
111  * @event: The event to test.
112  *
113  * Return: Returns %true if the given event matches the given notifier
114  * according to the rules set in the notifier's event mask, %false otherwise.
115  */
116 static bool ssam_event_matches_notifier(const struct ssam_event_notifier *n,
117 					const struct ssam_event *event)
118 {
119 	bool match = n->event.id.target_category == event->target_category;
120 
121 	if (n->event.mask & SSAM_EVENT_MASK_TARGET)
122 		match &= n->event.reg.target_id == event->target_id;
123 
124 	if (n->event.mask & SSAM_EVENT_MASK_INSTANCE)
125 		match &= n->event.id.instance == event->instance_id;
126 
127 	return match;
128 }
129 
130 /**
131  * ssam_nfblk_call_chain() - Call event notifier callbacks of the given chain.
132  * @nh:    The notifier head for which the notifier callbacks should be called.
133  * @event: The event data provided to the callbacks.
134  *
135  * Call all registered notifier callbacks in order of their priority until
136  * either no notifier is left or a notifier returns a value with the
137  * %SSAM_NOTIF_STOP bit set. Note that this bit is automatically set via
138  * ssam_notifier_from_errno() on any non-zero error value.
139  *
140  * Return: Returns the notifier status value, which contains the notifier
141  * status bits (%SSAM_NOTIF_HANDLED and %SSAM_NOTIF_STOP) as well as a
142  * potential error value returned from the last executed notifier callback.
143  * Use ssam_notifier_to_errno() to convert this value to the original error
144  * value.
145  */
146 static int ssam_nfblk_call_chain(struct ssam_nf_head *nh, struct ssam_event *event)
147 {
148 	struct ssam_event_notifier *nf;
149 	int ret = 0, idx;
150 
151 	idx = srcu_read_lock(&nh->srcu);
152 
153 	list_for_each_entry_rcu(nf, &nh->head, base.node,
154 				srcu_read_lock_held(&nh->srcu)) {
155 		if (ssam_event_matches_notifier(nf, event)) {
156 			ret = (ret & SSAM_NOTIF_STATE_MASK) | nf->base.fn(nf, event);
157 			if (ret & SSAM_NOTIF_STOP)
158 				break;
159 		}
160 	}
161 
162 	srcu_read_unlock(&nh->srcu, idx);
163 	return ret;
164 }
165 
166 /**
167  * ssam_nfblk_insert() - Insert a new notifier block into the given notifier
168  * list.
169  * @nh: The notifier head into which the block should be inserted.
170  * @nb: The notifier block to add.
171  *
172  * Note: This function must be synchronized by the caller with respect to other
173  * insert, find, and/or remove calls by holding ``struct ssam_nf.lock``.
174  *
175  * Return: Returns zero on success, %-EEXIST if the notifier block has already
176  * been registered.
177  */
178 static int ssam_nfblk_insert(struct ssam_nf_head *nh, struct ssam_notifier_block *nb)
179 {
180 	struct ssam_notifier_block *p;
181 	struct list_head *h;
182 
183 	/* Runs under lock, no need for RCU variant. */
184 	list_for_each(h, &nh->head) {
185 		p = list_entry(h, struct ssam_notifier_block, node);
186 
187 		if (unlikely(p == nb)) {
188 			WARN(1, "double register detected");
189 			return -EEXIST;
190 		}
191 
192 		if (nb->priority > p->priority)
193 			break;
194 	}
195 
196 	list_add_tail_rcu(&nb->node, h);
197 	return 0;
198 }
199 
200 /**
201  * ssam_nfblk_find() - Check if a notifier block is registered on the given
202  * notifier head.
203  * list.
204  * @nh: The notifier head on which to search.
205  * @nb: The notifier block to search for.
206  *
207  * Note: This function must be synchronized by the caller with respect to other
208  * insert, find, and/or remove calls by holding ``struct ssam_nf.lock``.
209  *
210  * Return: Returns true if the given notifier block is registered on the given
211  * notifier head, false otherwise.
212  */
213 static bool ssam_nfblk_find(struct ssam_nf_head *nh, struct ssam_notifier_block *nb)
214 {
215 	struct ssam_notifier_block *p;
216 
217 	/* Runs under lock, no need for RCU variant. */
218 	list_for_each_entry(p, &nh->head, node) {
219 		if (p == nb)
220 			return true;
221 	}
222 
223 	return false;
224 }
225 
226 /**
227  * ssam_nfblk_remove() - Remove a notifier block from its notifier list.
228  * @nb: The notifier block to be removed.
229  *
230  * Note: This function must be synchronized by the caller with respect to
231  * other insert, find, and/or remove calls by holding ``struct ssam_nf.lock``.
232  * Furthermore, the caller _must_ ensure SRCU synchronization by calling
233  * synchronize_srcu() with ``nh->srcu`` after leaving the critical section, to
234  * ensure that the removed notifier block is not in use any more.
235  */
236 static void ssam_nfblk_remove(struct ssam_notifier_block *nb)
237 {
238 	list_del_rcu(&nb->node);
239 }
240 
241 /**
242  * ssam_nf_head_init() - Initialize the given notifier head.
243  * @nh: The notifier head to initialize.
244  */
245 static int ssam_nf_head_init(struct ssam_nf_head *nh)
246 {
247 	int status;
248 
249 	status = init_srcu_struct(&nh->srcu);
250 	if (status)
251 		return status;
252 
253 	INIT_LIST_HEAD(&nh->head);
254 	return 0;
255 }
256 
257 /**
258  * ssam_nf_head_destroy() - Deinitialize the given notifier head.
259  * @nh: The notifier head to deinitialize.
260  */
261 static void ssam_nf_head_destroy(struct ssam_nf_head *nh)
262 {
263 	cleanup_srcu_struct(&nh->srcu);
264 }
265 
266 
267 /* -- Event/notification registry. ------------------------------------------ */
268 
269 /**
270  * struct ssam_nf_refcount_key - Key used for event activation reference
271  * counting.
272  * @reg: The registry via which the event is enabled/disabled.
273  * @id:  The ID uniquely describing the event.
274  */
275 struct ssam_nf_refcount_key {
276 	struct ssam_event_registry reg;
277 	struct ssam_event_id id;
278 };
279 
280 /**
281  * struct ssam_nf_refcount_entry - RB-tree entry for reference counting event
282  * activations.
283  * @node:     The node of this entry in the rb-tree.
284  * @key:      The key of the event.
285  * @refcount: The reference-count of the event.
286  * @flags:    The flags used when enabling the event.
287  */
288 struct ssam_nf_refcount_entry {
289 	struct rb_node node;
290 	struct ssam_nf_refcount_key key;
291 	int refcount;
292 	u8 flags;
293 };
294 
295 /**
296  * ssam_nf_refcount_inc() - Increment reference-/activation-count of the given
297  * event.
298  * @nf:  The notifier system reference.
299  * @reg: The registry used to enable/disable the event.
300  * @id:  The event ID.
301  *
302  * Increments the reference-/activation-count associated with the specified
303  * event type/ID, allocating a new entry for this event ID if necessary. A
304  * newly allocated entry will have a refcount of one.
305  *
306  * Note: ``nf->lock`` must be held when calling this function.
307  *
308  * Return: Returns the refcount entry on success. Returns an error pointer
309  * with %-ENOSPC if there have already been %INT_MAX events of the specified
310  * ID and type registered, or %-ENOMEM if the entry could not be allocated.
311  */
312 static struct ssam_nf_refcount_entry *
313 ssam_nf_refcount_inc(struct ssam_nf *nf, struct ssam_event_registry reg,
314 		     struct ssam_event_id id)
315 {
316 	struct ssam_nf_refcount_entry *entry;
317 	struct ssam_nf_refcount_key key;
318 	struct rb_node **link = &nf->refcount.rb_node;
319 	struct rb_node *parent = NULL;
320 	int cmp;
321 
322 	lockdep_assert_held(&nf->lock);
323 
324 	key.reg = reg;
325 	key.id = id;
326 
327 	while (*link) {
328 		entry = rb_entry(*link, struct ssam_nf_refcount_entry, node);
329 		parent = *link;
330 
331 		cmp = memcmp(&key, &entry->key, sizeof(key));
332 		if (cmp < 0) {
333 			link = &(*link)->rb_left;
334 		} else if (cmp > 0) {
335 			link = &(*link)->rb_right;
336 		} else if (entry->refcount < INT_MAX) {
337 			entry->refcount++;
338 			return entry;
339 		} else {
340 			WARN_ON(1);
341 			return ERR_PTR(-ENOSPC);
342 		}
343 	}
344 
345 	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
346 	if (!entry)
347 		return ERR_PTR(-ENOMEM);
348 
349 	entry->key = key;
350 	entry->refcount = 1;
351 
352 	rb_link_node(&entry->node, parent, link);
353 	rb_insert_color(&entry->node, &nf->refcount);
354 
355 	return entry;
356 }
357 
358 /**
359  * ssam_nf_refcount_dec() - Decrement reference-/activation-count of the given
360  * event.
361  * @nf:  The notifier system reference.
362  * @reg: The registry used to enable/disable the event.
363  * @id:  The event ID.
364  *
365  * Decrements the reference-/activation-count of the specified event,
366  * returning its entry. If the returned entry has a refcount of zero, the
367  * caller is responsible for freeing it using kfree().
368  *
369  * Note: ``nf->lock`` must be held when calling this function.
370  *
371  * Return: Returns the refcount entry on success or %NULL if the entry has not
372  * been found.
373  */
374 static struct ssam_nf_refcount_entry *
375 ssam_nf_refcount_dec(struct ssam_nf *nf, struct ssam_event_registry reg,
376 		     struct ssam_event_id id)
377 {
378 	struct ssam_nf_refcount_entry *entry;
379 	struct ssam_nf_refcount_key key;
380 	struct rb_node *node = nf->refcount.rb_node;
381 	int cmp;
382 
383 	lockdep_assert_held(&nf->lock);
384 
385 	key.reg = reg;
386 	key.id = id;
387 
388 	while (node) {
389 		entry = rb_entry(node, struct ssam_nf_refcount_entry, node);
390 
391 		cmp = memcmp(&key, &entry->key, sizeof(key));
392 		if (cmp < 0) {
393 			node = node->rb_left;
394 		} else if (cmp > 0) {
395 			node = node->rb_right;
396 		} else {
397 			entry->refcount--;
398 			if (entry->refcount == 0)
399 				rb_erase(&entry->node, &nf->refcount);
400 
401 			return entry;
402 		}
403 	}
404 
405 	return NULL;
406 }
407 
408 /**
409  * ssam_nf_refcount_empty() - Test if the notification system has any
410  * enabled/active events.
411  * @nf: The notification system.
412  */
413 static bool ssam_nf_refcount_empty(struct ssam_nf *nf)
414 {
415 	return RB_EMPTY_ROOT(&nf->refcount);
416 }
417 
418 /**
419  * ssam_nf_call() - Call notification callbacks for the provided event.
420  * @nf:    The notifier system
421  * @dev:   The associated device, only used for logging.
422  * @rqid:  The request ID of the event.
423  * @event: The event provided to the callbacks.
424  *
425  * Execute registered callbacks in order of their priority until either no
426  * callback is left or a callback returns a value with the %SSAM_NOTIF_STOP
427  * bit set. Note that this bit is set automatically when converting non-zero
428  * error values via ssam_notifier_from_errno() to notifier values.
429  *
430  * Also note that any callback that could handle an event should return a value
431  * with bit %SSAM_NOTIF_HANDLED set, indicating that the event does not go
432  * unhandled/ignored. In case no registered callback could handle an event,
433  * this function will emit a warning.
434  *
435  * In case a callback failed, this function will emit an error message.
436  */
437 static void ssam_nf_call(struct ssam_nf *nf, struct device *dev, u16 rqid,
438 			 struct ssam_event *event)
439 {
440 	struct ssam_nf_head *nf_head;
441 	int status, nf_ret;
442 
443 	if (!ssh_rqid_is_event(rqid)) {
444 		dev_warn(dev, "event: unsupported rqid: %#06x\n", rqid);
445 		return;
446 	}
447 
448 	nf_head = &nf->head[ssh_rqid_to_event(rqid)];
449 	nf_ret = ssam_nfblk_call_chain(nf_head, event);
450 	status = ssam_notifier_to_errno(nf_ret);
451 
452 	if (status < 0) {
453 		dev_err(dev,
454 			"event: error handling event: %d (tc: %#04x, tid: %#04x, cid: %#04x, iid: %#04x)\n",
455 			status, event->target_category, event->target_id,
456 			event->command_id, event->instance_id);
457 	} else if (!(nf_ret & SSAM_NOTIF_HANDLED)) {
458 		dev_warn(dev,
459 			 "event: unhandled event (rqid: %#04x, tc: %#04x, tid: %#04x, cid: %#04x, iid: %#04x)\n",
460 			 rqid, event->target_category, event->target_id,
461 			 event->command_id, event->instance_id);
462 	}
463 }
464 
465 /**
466  * ssam_nf_init() - Initialize the notifier system.
467  * @nf: The notifier system to initialize.
468  */
469 static int ssam_nf_init(struct ssam_nf *nf)
470 {
471 	int i, status;
472 
473 	for (i = 0; i < SSH_NUM_EVENTS; i++) {
474 		status = ssam_nf_head_init(&nf->head[i]);
475 		if (status)
476 			break;
477 	}
478 
479 	if (status) {
480 		while (i--)
481 			ssam_nf_head_destroy(&nf->head[i]);
482 
483 		return status;
484 	}
485 
486 	mutex_init(&nf->lock);
487 	return 0;
488 }
489 
490 /**
491  * ssam_nf_destroy() - Deinitialize the notifier system.
492  * @nf: The notifier system to deinitialize.
493  */
494 static void ssam_nf_destroy(struct ssam_nf *nf)
495 {
496 	int i;
497 
498 	for (i = 0; i < SSH_NUM_EVENTS; i++)
499 		ssam_nf_head_destroy(&nf->head[i]);
500 
501 	mutex_destroy(&nf->lock);
502 }
503 
504 
505 /* -- Event/async request completion system. -------------------------------- */
506 
507 #define SSAM_CPLT_WQ_NAME	"ssam_cpltq"
508 
509 /*
510  * SSAM_CPLT_WQ_BATCH - Maximum number of event item completions executed per
511  * work execution. Used to prevent livelocking of the workqueue. Value chosen
512  * via educated guess, may be adjusted.
513  */
514 #define SSAM_CPLT_WQ_BATCH	10
515 
516 /*
517  * SSAM_EVENT_ITEM_CACHE_PAYLOAD_LEN - Maximum payload length for a cached
518  * &struct ssam_event_item.
519  *
520  * This length has been chosen to be accommodate standard touchpad and
521  * keyboard input events. Events with larger payloads will be allocated
522  * separately.
523  */
524 #define SSAM_EVENT_ITEM_CACHE_PAYLOAD_LEN	32
525 
526 static struct kmem_cache *ssam_event_item_cache;
527 
528 /**
529  * ssam_event_item_cache_init() - Initialize the event item cache.
530  */
531 int ssam_event_item_cache_init(void)
532 {
533 	const unsigned int size = sizeof(struct ssam_event_item)
534 				  + SSAM_EVENT_ITEM_CACHE_PAYLOAD_LEN;
535 	const unsigned int align = __alignof__(struct ssam_event_item);
536 	struct kmem_cache *cache;
537 
538 	cache = kmem_cache_create("ssam_event_item", size, align, 0, NULL);
539 	if (!cache)
540 		return -ENOMEM;
541 
542 	ssam_event_item_cache = cache;
543 	return 0;
544 }
545 
546 /**
547  * ssam_event_item_cache_destroy() - Deinitialize the event item cache.
548  */
549 void ssam_event_item_cache_destroy(void)
550 {
551 	kmem_cache_destroy(ssam_event_item_cache);
552 	ssam_event_item_cache = NULL;
553 }
554 
555 static void __ssam_event_item_free_cached(struct ssam_event_item *item)
556 {
557 	kmem_cache_free(ssam_event_item_cache, item);
558 }
559 
560 static void __ssam_event_item_free_generic(struct ssam_event_item *item)
561 {
562 	kfree(item);
563 }
564 
565 /**
566  * ssam_event_item_free() - Free the provided event item.
567  * @item: The event item to free.
568  */
569 static void ssam_event_item_free(struct ssam_event_item *item)
570 {
571 	item->ops.free(item);
572 }
573 
574 /**
575  * ssam_event_item_alloc() - Allocate an event item with the given payload size.
576  * @len:   The event payload length.
577  * @flags: The flags used for allocation.
578  *
579  * Allocate an event item with the given payload size, preferring allocation
580  * from the event item cache if the payload is small enough (i.e. smaller than
581  * %SSAM_EVENT_ITEM_CACHE_PAYLOAD_LEN). Sets the item operations and payload
582  * length values. The item free callback (``ops.free``) should not be
583  * overwritten after this call.
584  *
585  * Return: Returns the newly allocated event item.
586  */
587 static struct ssam_event_item *ssam_event_item_alloc(size_t len, gfp_t flags)
588 {
589 	struct ssam_event_item *item;
590 
591 	if (len <= SSAM_EVENT_ITEM_CACHE_PAYLOAD_LEN) {
592 		item = kmem_cache_alloc(ssam_event_item_cache, flags);
593 		if (!item)
594 			return NULL;
595 
596 		item->ops.free = __ssam_event_item_free_cached;
597 	} else {
598 		item = kzalloc(struct_size(item, event.data, len), flags);
599 		if (!item)
600 			return NULL;
601 
602 		item->ops.free = __ssam_event_item_free_generic;
603 	}
604 
605 	item->event.length = len;
606 	return item;
607 }
608 
609 /**
610  * ssam_event_queue_push() - Push an event item to the event queue.
611  * @q:    The event queue.
612  * @item: The item to add.
613  */
614 static void ssam_event_queue_push(struct ssam_event_queue *q,
615 				  struct ssam_event_item *item)
616 {
617 	spin_lock(&q->lock);
618 	list_add_tail(&item->node, &q->head);
619 	spin_unlock(&q->lock);
620 }
621 
622 /**
623  * ssam_event_queue_pop() - Pop the next event item from the event queue.
624  * @q: The event queue.
625  *
626  * Returns and removes the next event item from the queue. Returns %NULL If
627  * there is no event item left.
628  */
629 static struct ssam_event_item *ssam_event_queue_pop(struct ssam_event_queue *q)
630 {
631 	struct ssam_event_item *item;
632 
633 	spin_lock(&q->lock);
634 	item = list_first_entry_or_null(&q->head, struct ssam_event_item, node);
635 	if (item)
636 		list_del(&item->node);
637 	spin_unlock(&q->lock);
638 
639 	return item;
640 }
641 
642 /**
643  * ssam_event_queue_is_empty() - Check if the event queue is empty.
644  * @q: The event queue.
645  */
646 static bool ssam_event_queue_is_empty(struct ssam_event_queue *q)
647 {
648 	bool empty;
649 
650 	spin_lock(&q->lock);
651 	empty = list_empty(&q->head);
652 	spin_unlock(&q->lock);
653 
654 	return empty;
655 }
656 
657 /**
658  * ssam_cplt_get_event_queue() - Get the event queue for the given parameters.
659  * @cplt: The completion system on which to look for the queue.
660  * @tid:  The target ID of the queue.
661  * @rqid: The request ID representing the event ID for which to get the queue.
662  *
663  * Return: Returns the event queue corresponding to the event type described
664  * by the given parameters. If the request ID does not represent an event,
665  * this function returns %NULL. If the target ID is not supported, this
666  * function will fall back to the default target ID (``tid = 1``).
667  */
668 static
669 struct ssam_event_queue *ssam_cplt_get_event_queue(struct ssam_cplt *cplt,
670 						   u8 tid, u16 rqid)
671 {
672 	u16 event = ssh_rqid_to_event(rqid);
673 	u16 tidx = ssh_tid_to_index(tid);
674 
675 	if (!ssh_rqid_is_event(rqid)) {
676 		dev_err(cplt->dev, "event: unsupported request ID: %#06x\n", rqid);
677 		return NULL;
678 	}
679 
680 	if (!ssh_tid_is_valid(tid)) {
681 		dev_warn(cplt->dev, "event: unsupported target ID: %u\n", tid);
682 		tidx = 0;
683 	}
684 
685 	return &cplt->event.target[tidx].queue[event];
686 }
687 
688 /**
689  * ssam_cplt_submit() - Submit a work item to the completion system workqueue.
690  * @cplt: The completion system.
691  * @work: The work item to submit.
692  */
693 static bool ssam_cplt_submit(struct ssam_cplt *cplt, struct work_struct *work)
694 {
695 	return queue_work(cplt->wq, work);
696 }
697 
698 /**
699  * ssam_cplt_submit_event() - Submit an event to the completion system.
700  * @cplt: The completion system.
701  * @item: The event item to submit.
702  *
703  * Submits the event to the completion system by queuing it on the event item
704  * queue and queuing the respective event queue work item on the completion
705  * workqueue, which will eventually complete the event.
706  *
707  * Return: Returns zero on success, %-EINVAL if there is no event queue that
708  * can handle the given event item.
709  */
710 static int ssam_cplt_submit_event(struct ssam_cplt *cplt,
711 				  struct ssam_event_item *item)
712 {
713 	struct ssam_event_queue *evq;
714 
715 	evq = ssam_cplt_get_event_queue(cplt, item->event.target_id, item->rqid);
716 	if (!evq)
717 		return -EINVAL;
718 
719 	ssam_event_queue_push(evq, item);
720 	ssam_cplt_submit(cplt, &evq->work);
721 	return 0;
722 }
723 
724 /**
725  * ssam_cplt_flush() - Flush the completion system.
726  * @cplt: The completion system.
727  *
728  * Flush the completion system by waiting until all currently submitted work
729  * items have been completed.
730  *
731  * Note: This function does not guarantee that all events will have been
732  * handled once this call terminates. In case of a larger number of
733  * to-be-completed events, the event queue work function may re-schedule its
734  * work item, which this flush operation will ignore.
735  *
736  * This operation is only intended to, during normal operation prior to
737  * shutdown, try to complete most events and requests to get them out of the
738  * system while the system is still fully operational. It does not aim to
739  * provide any guarantee that all of them have been handled.
740  */
741 static void ssam_cplt_flush(struct ssam_cplt *cplt)
742 {
743 	flush_workqueue(cplt->wq);
744 }
745 
746 static void ssam_event_queue_work_fn(struct work_struct *work)
747 {
748 	struct ssam_event_queue *queue;
749 	struct ssam_event_item *item;
750 	struct ssam_nf *nf;
751 	struct device *dev;
752 	unsigned int iterations = SSAM_CPLT_WQ_BATCH;
753 
754 	queue = container_of(work, struct ssam_event_queue, work);
755 	nf = &queue->cplt->event.notif;
756 	dev = queue->cplt->dev;
757 
758 	/* Limit number of processed events to avoid livelocking. */
759 	do {
760 		item = ssam_event_queue_pop(queue);
761 		if (!item)
762 			return;
763 
764 		ssam_nf_call(nf, dev, item->rqid, &item->event);
765 		ssam_event_item_free(item);
766 	} while (--iterations);
767 
768 	if (!ssam_event_queue_is_empty(queue))
769 		ssam_cplt_submit(queue->cplt, &queue->work);
770 }
771 
772 /**
773  * ssam_event_queue_init() - Initialize an event queue.
774  * @cplt: The completion system on which the queue resides.
775  * @evq:  The event queue to initialize.
776  */
777 static void ssam_event_queue_init(struct ssam_cplt *cplt,
778 				  struct ssam_event_queue *evq)
779 {
780 	evq->cplt = cplt;
781 	spin_lock_init(&evq->lock);
782 	INIT_LIST_HEAD(&evq->head);
783 	INIT_WORK(&evq->work, ssam_event_queue_work_fn);
784 }
785 
786 /**
787  * ssam_cplt_init() - Initialize completion system.
788  * @cplt: The completion system to initialize.
789  * @dev:  The device used for logging.
790  */
791 static int ssam_cplt_init(struct ssam_cplt *cplt, struct device *dev)
792 {
793 	struct ssam_event_target *target;
794 	int status, c, i;
795 
796 	cplt->dev = dev;
797 
798 	cplt->wq = create_workqueue(SSAM_CPLT_WQ_NAME);
799 	if (!cplt->wq)
800 		return -ENOMEM;
801 
802 	for (c = 0; c < ARRAY_SIZE(cplt->event.target); c++) {
803 		target = &cplt->event.target[c];
804 
805 		for (i = 0; i < ARRAY_SIZE(target->queue); i++)
806 			ssam_event_queue_init(cplt, &target->queue[i]);
807 	}
808 
809 	status = ssam_nf_init(&cplt->event.notif);
810 	if (status)
811 		destroy_workqueue(cplt->wq);
812 
813 	return status;
814 }
815 
816 /**
817  * ssam_cplt_destroy() - Deinitialize the completion system.
818  * @cplt: The completion system to deinitialize.
819  *
820  * Deinitialize the given completion system and ensure that all pending, i.e.
821  * yet-to-be-completed, event items and requests have been handled.
822  */
823 static void ssam_cplt_destroy(struct ssam_cplt *cplt)
824 {
825 	/*
826 	 * Note: destroy_workqueue ensures that all currently queued work will
827 	 * be fully completed and the workqueue drained. This means that this
828 	 * call will inherently also free any queued ssam_event_items, thus we
829 	 * don't have to take care of that here explicitly.
830 	 */
831 	destroy_workqueue(cplt->wq);
832 	ssam_nf_destroy(&cplt->event.notif);
833 }
834 
835 
836 /* -- Main SSAM device structures. ------------------------------------------ */
837 
838 /**
839  * ssam_controller_device() - Get the &struct device associated with this
840  * controller.
841  * @c: The controller for which to get the device.
842  *
843  * Return: Returns the &struct device associated with this controller,
844  * providing its lower-level transport.
845  */
846 struct device *ssam_controller_device(struct ssam_controller *c)
847 {
848 	return ssh_rtl_get_device(&c->rtl);
849 }
850 EXPORT_SYMBOL_GPL(ssam_controller_device);
851 
852 static void __ssam_controller_release(struct kref *kref)
853 {
854 	struct ssam_controller *ctrl = to_ssam_controller(kref, kref);
855 
856 	/*
857 	 * The lock-call here is to satisfy lockdep. At this point we really
858 	 * expect this to be the last remaining reference to the controller.
859 	 * Anything else is a bug.
860 	 */
861 	ssam_controller_lock(ctrl);
862 	ssam_controller_destroy(ctrl);
863 	ssam_controller_unlock(ctrl);
864 
865 	kfree(ctrl);
866 }
867 
868 /**
869  * ssam_controller_get() - Increment reference count of controller.
870  * @c: The controller.
871  *
872  * Return: Returns the controller provided as input.
873  */
874 struct ssam_controller *ssam_controller_get(struct ssam_controller *c)
875 {
876 	if (c)
877 		kref_get(&c->kref);
878 	return c;
879 }
880 EXPORT_SYMBOL_GPL(ssam_controller_get);
881 
882 /**
883  * ssam_controller_put() - Decrement reference count of controller.
884  * @c: The controller.
885  */
886 void ssam_controller_put(struct ssam_controller *c)
887 {
888 	if (c)
889 		kref_put(&c->kref, __ssam_controller_release);
890 }
891 EXPORT_SYMBOL_GPL(ssam_controller_put);
892 
893 /**
894  * ssam_controller_statelock() - Lock the controller against state transitions.
895  * @c: The controller to lock.
896  *
897  * Lock the controller against state transitions. Holding this lock guarantees
898  * that the controller will not transition between states, i.e. if the
899  * controller is in state "started", when this lock has been acquired, it will
900  * remain in this state at least until the lock has been released.
901  *
902  * Multiple clients may concurrently hold this lock. In other words: The
903  * ``statelock`` functions represent the read-lock part of a r/w-semaphore.
904  * Actions causing state transitions of the controller must be executed while
905  * holding the write-part of this r/w-semaphore (see ssam_controller_lock()
906  * and ssam_controller_unlock() for that).
907  *
908  * See ssam_controller_stateunlock() for the corresponding unlock function.
909  */
910 void ssam_controller_statelock(struct ssam_controller *c)
911 {
912 	down_read(&c->lock);
913 }
914 EXPORT_SYMBOL_GPL(ssam_controller_statelock);
915 
916 /**
917  * ssam_controller_stateunlock() - Unlock controller state transitions.
918  * @c: The controller to unlock.
919  *
920  * See ssam_controller_statelock() for the corresponding lock function.
921  */
922 void ssam_controller_stateunlock(struct ssam_controller *c)
923 {
924 	up_read(&c->lock);
925 }
926 EXPORT_SYMBOL_GPL(ssam_controller_stateunlock);
927 
928 /**
929  * ssam_controller_lock() - Acquire the main controller lock.
930  * @c: The controller to lock.
931  *
932  * This lock must be held for any state transitions, including transition to
933  * suspend/resumed states and during shutdown. See ssam_controller_statelock()
934  * for more details on controller locking.
935  *
936  * See ssam_controller_unlock() for the corresponding unlock function.
937  */
938 void ssam_controller_lock(struct ssam_controller *c)
939 {
940 	down_write(&c->lock);
941 }
942 
943 /*
944  * ssam_controller_unlock() - Release the main controller lock.
945  * @c: The controller to unlock.
946  *
947  * See ssam_controller_lock() for the corresponding lock function.
948  */
949 void ssam_controller_unlock(struct ssam_controller *c)
950 {
951 	up_write(&c->lock);
952 }
953 
954 static void ssam_handle_event(struct ssh_rtl *rtl,
955 			      const struct ssh_command *cmd,
956 			      const struct ssam_span *data)
957 {
958 	struct ssam_controller *ctrl = to_ssam_controller(rtl, rtl);
959 	struct ssam_event_item *item;
960 
961 	item = ssam_event_item_alloc(data->len, GFP_KERNEL);
962 	if (!item)
963 		return;
964 
965 	item->rqid = get_unaligned_le16(&cmd->rqid);
966 	item->event.target_category = cmd->tc;
967 	item->event.target_id = cmd->tid_in;
968 	item->event.command_id = cmd->cid;
969 	item->event.instance_id = cmd->iid;
970 	memcpy(&item->event.data[0], data->ptr, data->len);
971 
972 	if (WARN_ON(ssam_cplt_submit_event(&ctrl->cplt, item)))
973 		ssam_event_item_free(item);
974 }
975 
976 static const struct ssh_rtl_ops ssam_rtl_ops = {
977 	.handle_event = ssam_handle_event,
978 };
979 
980 static bool ssam_notifier_is_empty(struct ssam_controller *ctrl);
981 static void ssam_notifier_unregister_all(struct ssam_controller *ctrl);
982 
983 #define SSAM_SSH_DSM_REVISION	0
984 
985 /* d5e383e1-d892-4a76-89fc-f6aaae7ed5b5 */
986 static const guid_t SSAM_SSH_DSM_GUID =
987 	GUID_INIT(0xd5e383e1, 0xd892, 0x4a76,
988 		  0x89, 0xfc, 0xf6, 0xaa, 0xae, 0x7e, 0xd5, 0xb5);
989 
990 enum ssh_dsm_fn {
991 	SSH_DSM_FN_SSH_POWER_PROFILE             = 0x05,
992 	SSH_DSM_FN_SCREEN_ON_SLEEP_IDLE_TIMEOUT  = 0x06,
993 	SSH_DSM_FN_SCREEN_OFF_SLEEP_IDLE_TIMEOUT = 0x07,
994 	SSH_DSM_FN_D3_CLOSES_HANDLE              = 0x08,
995 	SSH_DSM_FN_SSH_BUFFER_SIZE               = 0x09,
996 };
997 
998 static int ssam_dsm_get_functions(acpi_handle handle, u64 *funcs)
999 {
1000 	union acpi_object *obj;
1001 	u64 mask = 0;
1002 	int i;
1003 
1004 	*funcs = 0;
1005 
1006 	/*
1007 	 * The _DSM function is only present on newer models. It is not
1008 	 * present on 5th and 6th generation devices (i.e. up to and including
1009 	 * Surface Pro 6, Surface Laptop 2, Surface Book 2).
1010 	 *
1011 	 * If the _DSM is not present, indicate that no function is supported.
1012 	 * This will result in default values being set.
1013 	 */
1014 	if (!acpi_has_method(handle, "_DSM"))
1015 		return 0;
1016 
1017 	obj = acpi_evaluate_dsm_typed(handle, &SSAM_SSH_DSM_GUID,
1018 				      SSAM_SSH_DSM_REVISION, 0, NULL,
1019 				      ACPI_TYPE_BUFFER);
1020 	if (!obj)
1021 		return -EIO;
1022 
1023 	for (i = 0; i < obj->buffer.length && i < 8; i++)
1024 		mask |= (((u64)obj->buffer.pointer[i]) << (i * 8));
1025 
1026 	if (mask & BIT(0))
1027 		*funcs = mask;
1028 
1029 	ACPI_FREE(obj);
1030 	return 0;
1031 }
1032 
1033 static int ssam_dsm_load_u32(acpi_handle handle, u64 funcs, u64 func, u32 *ret)
1034 {
1035 	union acpi_object *obj;
1036 	u64 val;
1037 
1038 	if (!(funcs & BIT(func)))
1039 		return 0; /* Not supported, leave *ret at its default value */
1040 
1041 	obj = acpi_evaluate_dsm_typed(handle, &SSAM_SSH_DSM_GUID,
1042 				      SSAM_SSH_DSM_REVISION, func, NULL,
1043 				      ACPI_TYPE_INTEGER);
1044 	if (!obj)
1045 		return -EIO;
1046 
1047 	val = obj->integer.value;
1048 	ACPI_FREE(obj);
1049 
1050 	if (val > U32_MAX)
1051 		return -ERANGE;
1052 
1053 	*ret = val;
1054 	return 0;
1055 }
1056 
1057 /**
1058  * ssam_controller_caps_load_from_acpi() - Load controller capabilities from
1059  * ACPI _DSM.
1060  * @handle: The handle of the ACPI controller/SSH device.
1061  * @caps:   Where to store the capabilities in.
1062  *
1063  * Initializes the given controller capabilities with default values, then
1064  * checks and, if the respective _DSM functions are available, loads the
1065  * actual capabilities from the _DSM.
1066  *
1067  * Return: Returns zero on success, a negative error code on failure.
1068  */
1069 static
1070 int ssam_controller_caps_load_from_acpi(acpi_handle handle,
1071 					struct ssam_controller_caps *caps)
1072 {
1073 	u32 d3_closes_handle = false;
1074 	u64 funcs;
1075 	int status;
1076 
1077 	/* Set defaults. */
1078 	caps->ssh_power_profile = U32_MAX;
1079 	caps->screen_on_sleep_idle_timeout = U32_MAX;
1080 	caps->screen_off_sleep_idle_timeout = U32_MAX;
1081 	caps->d3_closes_handle = false;
1082 	caps->ssh_buffer_size = U32_MAX;
1083 
1084 	/* Pre-load supported DSM functions. */
1085 	status = ssam_dsm_get_functions(handle, &funcs);
1086 	if (status)
1087 		return status;
1088 
1089 	/* Load actual values from ACPI, if present. */
1090 	status = ssam_dsm_load_u32(handle, funcs, SSH_DSM_FN_SSH_POWER_PROFILE,
1091 				   &caps->ssh_power_profile);
1092 	if (status)
1093 		return status;
1094 
1095 	status = ssam_dsm_load_u32(handle, funcs,
1096 				   SSH_DSM_FN_SCREEN_ON_SLEEP_IDLE_TIMEOUT,
1097 				   &caps->screen_on_sleep_idle_timeout);
1098 	if (status)
1099 		return status;
1100 
1101 	status = ssam_dsm_load_u32(handle, funcs,
1102 				   SSH_DSM_FN_SCREEN_OFF_SLEEP_IDLE_TIMEOUT,
1103 				   &caps->screen_off_sleep_idle_timeout);
1104 	if (status)
1105 		return status;
1106 
1107 	status = ssam_dsm_load_u32(handle, funcs, SSH_DSM_FN_D3_CLOSES_HANDLE,
1108 				   &d3_closes_handle);
1109 	if (status)
1110 		return status;
1111 
1112 	caps->d3_closes_handle = !!d3_closes_handle;
1113 
1114 	status = ssam_dsm_load_u32(handle, funcs, SSH_DSM_FN_SSH_BUFFER_SIZE,
1115 				   &caps->ssh_buffer_size);
1116 	if (status)
1117 		return status;
1118 
1119 	return 0;
1120 }
1121 
1122 /**
1123  * ssam_controller_init() - Initialize SSAM controller.
1124  * @ctrl:   The controller to initialize.
1125  * @serdev: The serial device representing the underlying data transport.
1126  *
1127  * Initializes the given controller. Does neither start receiver nor
1128  * transmitter threads. After this call, the controller has to be hooked up to
1129  * the serdev core separately via &struct serdev_device_ops, relaying calls to
1130  * ssam_controller_receive_buf() and ssam_controller_write_wakeup(). Once the
1131  * controller has been hooked up, transmitter and receiver threads may be
1132  * started via ssam_controller_start(). These setup steps need to be completed
1133  * before controller can be used for requests.
1134  */
1135 int ssam_controller_init(struct ssam_controller *ctrl,
1136 			 struct serdev_device *serdev)
1137 {
1138 	acpi_handle handle = ACPI_HANDLE(&serdev->dev);
1139 	int status;
1140 
1141 	init_rwsem(&ctrl->lock);
1142 	kref_init(&ctrl->kref);
1143 
1144 	status = ssam_controller_caps_load_from_acpi(handle, &ctrl->caps);
1145 	if (status)
1146 		return status;
1147 
1148 	dev_dbg(&serdev->dev,
1149 		"device capabilities:\n"
1150 		"  ssh_power_profile:             %u\n"
1151 		"  ssh_buffer_size:               %u\n"
1152 		"  screen_on_sleep_idle_timeout:  %u\n"
1153 		"  screen_off_sleep_idle_timeout: %u\n"
1154 		"  d3_closes_handle:              %u\n",
1155 		ctrl->caps.ssh_power_profile,
1156 		ctrl->caps.ssh_buffer_size,
1157 		ctrl->caps.screen_on_sleep_idle_timeout,
1158 		ctrl->caps.screen_off_sleep_idle_timeout,
1159 		ctrl->caps.d3_closes_handle);
1160 
1161 	ssh_seq_reset(&ctrl->counter.seq);
1162 	ssh_rqid_reset(&ctrl->counter.rqid);
1163 
1164 	/* Initialize event/request completion system. */
1165 	status = ssam_cplt_init(&ctrl->cplt, &serdev->dev);
1166 	if (status)
1167 		return status;
1168 
1169 	/* Initialize request and packet transport layers. */
1170 	status = ssh_rtl_init(&ctrl->rtl, serdev, &ssam_rtl_ops);
1171 	if (status) {
1172 		ssam_cplt_destroy(&ctrl->cplt);
1173 		return status;
1174 	}
1175 
1176 	/*
1177 	 * Set state via write_once even though we expect to be in an
1178 	 * exclusive context, due to smoke-testing in
1179 	 * ssam_request_sync_submit().
1180 	 */
1181 	WRITE_ONCE(ctrl->state, SSAM_CONTROLLER_INITIALIZED);
1182 	return 0;
1183 }
1184 
1185 /**
1186  * ssam_controller_start() - Start the receiver and transmitter threads of the
1187  * controller.
1188  * @ctrl: The controller.
1189  *
1190  * Note: When this function is called, the controller should be properly
1191  * hooked up to the serdev core via &struct serdev_device_ops. Please refer
1192  * to ssam_controller_init() for more details on controller initialization.
1193  *
1194  * This function must be called with the main controller lock held (i.e. by
1195  * calling ssam_controller_lock()).
1196  */
1197 int ssam_controller_start(struct ssam_controller *ctrl)
1198 {
1199 	int status;
1200 
1201 	lockdep_assert_held_write(&ctrl->lock);
1202 
1203 	if (ctrl->state != SSAM_CONTROLLER_INITIALIZED)
1204 		return -EINVAL;
1205 
1206 	status = ssh_rtl_start(&ctrl->rtl);
1207 	if (status)
1208 		return status;
1209 
1210 	/*
1211 	 * Set state via write_once even though we expect to be locked/in an
1212 	 * exclusive context, due to smoke-testing in
1213 	 * ssam_request_sync_submit().
1214 	 */
1215 	WRITE_ONCE(ctrl->state, SSAM_CONTROLLER_STARTED);
1216 	return 0;
1217 }
1218 
1219 /*
1220  * SSAM_CTRL_SHUTDOWN_FLUSH_TIMEOUT - Timeout for flushing requests during
1221  * shutdown.
1222  *
1223  * Chosen to be larger than one full request timeout, including packets timing
1224  * out. This value should give ample time to complete any outstanding requests
1225  * during normal operation and account for the odd package timeout.
1226  */
1227 #define SSAM_CTRL_SHUTDOWN_FLUSH_TIMEOUT	msecs_to_jiffies(5000)
1228 
1229 /**
1230  * ssam_controller_shutdown() - Shut down the controller.
1231  * @ctrl: The controller.
1232  *
1233  * Shuts down the controller by flushing all pending requests and stopping the
1234  * transmitter and receiver threads. All requests submitted after this call
1235  * will fail with %-ESHUTDOWN. While it is discouraged to do so, this function
1236  * is safe to use in parallel with ongoing request submission.
1237  *
1238  * In the course of this shutdown procedure, all currently registered
1239  * notifiers will be unregistered. It is, however, strongly recommended to not
1240  * rely on this behavior, and instead the party registering the notifier
1241  * should unregister it before the controller gets shut down, e.g. via the
1242  * SSAM bus which guarantees client devices to be removed before a shutdown.
1243  *
1244  * Note that events may still be pending after this call, but, due to the
1245  * notifiers being unregistered, these events will be dropped when the
1246  * controller is subsequently destroyed via ssam_controller_destroy().
1247  *
1248  * This function must be called with the main controller lock held (i.e. by
1249  * calling ssam_controller_lock()).
1250  */
1251 void ssam_controller_shutdown(struct ssam_controller *ctrl)
1252 {
1253 	enum ssam_controller_state s = ctrl->state;
1254 	int status;
1255 
1256 	lockdep_assert_held_write(&ctrl->lock);
1257 
1258 	if (s == SSAM_CONTROLLER_UNINITIALIZED || s == SSAM_CONTROLLER_STOPPED)
1259 		return;
1260 
1261 	/*
1262 	 * Try to flush pending events and requests while everything still
1263 	 * works. Note: There may still be packets and/or requests in the
1264 	 * system after this call (e.g. via control packets submitted by the
1265 	 * packet transport layer or flush timeout / failure, ...). Those will
1266 	 * be handled with the ssh_rtl_shutdown() call below.
1267 	 */
1268 	status = ssh_rtl_flush(&ctrl->rtl, SSAM_CTRL_SHUTDOWN_FLUSH_TIMEOUT);
1269 	if (status) {
1270 		ssam_err(ctrl, "failed to flush request transport layer: %d\n",
1271 			 status);
1272 	}
1273 
1274 	/* Try to flush all currently completing requests and events. */
1275 	ssam_cplt_flush(&ctrl->cplt);
1276 
1277 	/*
1278 	 * We expect all notifiers to have been removed by the respective client
1279 	 * driver that set them up at this point. If this warning occurs, some
1280 	 * client driver has not done that...
1281 	 */
1282 	WARN_ON(!ssam_notifier_is_empty(ctrl));
1283 
1284 	/*
1285 	 * Nevertheless, we should still take care of drivers that don't behave
1286 	 * well. Thus disable all enabled events, unregister all notifiers.
1287 	 */
1288 	ssam_notifier_unregister_all(ctrl);
1289 
1290 	/*
1291 	 * Cancel remaining requests. Ensure no new ones can be queued and stop
1292 	 * threads.
1293 	 */
1294 	ssh_rtl_shutdown(&ctrl->rtl);
1295 
1296 	/*
1297 	 * Set state via write_once even though we expect to be locked/in an
1298 	 * exclusive context, due to smoke-testing in
1299 	 * ssam_request_sync_submit().
1300 	 */
1301 	WRITE_ONCE(ctrl->state, SSAM_CONTROLLER_STOPPED);
1302 	ctrl->rtl.ptl.serdev = NULL;
1303 }
1304 
1305 /**
1306  * ssam_controller_destroy() - Destroy the controller and free its resources.
1307  * @ctrl: The controller.
1308  *
1309  * Ensures that all resources associated with the controller get freed. This
1310  * function should only be called after the controller has been stopped via
1311  * ssam_controller_shutdown(). In general, this function should not be called
1312  * directly. The only valid place to call this function directly is during
1313  * initialization, before the controller has been fully initialized and passed
1314  * to other processes. This function is called automatically when the
1315  * reference count of the controller reaches zero.
1316  *
1317  * This function must be called with the main controller lock held (i.e. by
1318  * calling ssam_controller_lock()).
1319  */
1320 void ssam_controller_destroy(struct ssam_controller *ctrl)
1321 {
1322 	lockdep_assert_held_write(&ctrl->lock);
1323 
1324 	if (ctrl->state == SSAM_CONTROLLER_UNINITIALIZED)
1325 		return;
1326 
1327 	WARN_ON(ctrl->state != SSAM_CONTROLLER_STOPPED);
1328 
1329 	/*
1330 	 * Note: New events could still have been received after the previous
1331 	 * flush in ssam_controller_shutdown, before the request transport layer
1332 	 * has been shut down. At this point, after the shutdown, we can be sure
1333 	 * that no new events will be queued. The call to ssam_cplt_destroy will
1334 	 * ensure that those remaining are being completed and freed.
1335 	 */
1336 
1337 	/* Actually free resources. */
1338 	ssam_cplt_destroy(&ctrl->cplt);
1339 	ssh_rtl_destroy(&ctrl->rtl);
1340 
1341 	/*
1342 	 * Set state via write_once even though we expect to be locked/in an
1343 	 * exclusive context, due to smoke-testing in
1344 	 * ssam_request_sync_submit().
1345 	 */
1346 	WRITE_ONCE(ctrl->state, SSAM_CONTROLLER_UNINITIALIZED);
1347 }
1348 
1349 /**
1350  * ssam_controller_suspend() - Suspend the controller.
1351  * @ctrl: The controller to suspend.
1352  *
1353  * Marks the controller as suspended. Note that display-off and D0-exit
1354  * notifications have to be sent manually before transitioning the controller
1355  * into the suspended state via this function.
1356  *
1357  * See ssam_controller_resume() for the corresponding resume function.
1358  *
1359  * Return: Returns %-EINVAL if the controller is currently not in the
1360  * "started" state.
1361  */
1362 int ssam_controller_suspend(struct ssam_controller *ctrl)
1363 {
1364 	ssam_controller_lock(ctrl);
1365 
1366 	if (ctrl->state != SSAM_CONTROLLER_STARTED) {
1367 		ssam_controller_unlock(ctrl);
1368 		return -EINVAL;
1369 	}
1370 
1371 	ssam_dbg(ctrl, "pm: suspending controller\n");
1372 
1373 	/*
1374 	 * Set state via write_once even though we're locked, due to
1375 	 * smoke-testing in ssam_request_sync_submit().
1376 	 */
1377 	WRITE_ONCE(ctrl->state, SSAM_CONTROLLER_SUSPENDED);
1378 
1379 	ssam_controller_unlock(ctrl);
1380 	return 0;
1381 }
1382 
1383 /**
1384  * ssam_controller_resume() - Resume the controller from suspend.
1385  * @ctrl: The controller to resume.
1386  *
1387  * Resume the controller from the suspended state it was put into via
1388  * ssam_controller_suspend(). This function does not issue display-on and
1389  * D0-entry notifications. If required, those have to be sent manually after
1390  * this call.
1391  *
1392  * Return: Returns %-EINVAL if the controller is currently not suspended.
1393  */
1394 int ssam_controller_resume(struct ssam_controller *ctrl)
1395 {
1396 	ssam_controller_lock(ctrl);
1397 
1398 	if (ctrl->state != SSAM_CONTROLLER_SUSPENDED) {
1399 		ssam_controller_unlock(ctrl);
1400 		return -EINVAL;
1401 	}
1402 
1403 	ssam_dbg(ctrl, "pm: resuming controller\n");
1404 
1405 	/*
1406 	 * Set state via write_once even though we're locked, due to
1407 	 * smoke-testing in ssam_request_sync_submit().
1408 	 */
1409 	WRITE_ONCE(ctrl->state, SSAM_CONTROLLER_STARTED);
1410 
1411 	ssam_controller_unlock(ctrl);
1412 	return 0;
1413 }
1414 
1415 
1416 /* -- Top-level request interface ------------------------------------------- */
1417 
1418 /**
1419  * ssam_request_write_data() - Construct and write SAM request message to
1420  * buffer.
1421  * @buf:  The buffer to write the data to.
1422  * @ctrl: The controller via which the request will be sent.
1423  * @spec: The request data and specification.
1424  *
1425  * Constructs a SAM/SSH request message and writes it to the provided buffer.
1426  * The request and transport counters, specifically RQID and SEQ, will be set
1427  * in this call. These counters are obtained from the controller. It is thus
1428  * only valid to send the resulting message via the controller specified here.
1429  *
1430  * For calculation of the required buffer size, refer to the
1431  * SSH_COMMAND_MESSAGE_LENGTH() macro.
1432  *
1433  * Return: Returns the number of bytes used in the buffer on success. Returns
1434  * %-EINVAL if the payload length provided in the request specification is too
1435  * large (larger than %SSH_COMMAND_MAX_PAYLOAD_SIZE) or if the provided buffer
1436  * is too small.
1437  */
1438 ssize_t ssam_request_write_data(struct ssam_span *buf,
1439 				struct ssam_controller *ctrl,
1440 				const struct ssam_request *spec)
1441 {
1442 	struct msgbuf msgb;
1443 	u16 rqid;
1444 	u8 seq;
1445 
1446 	if (spec->length > SSH_COMMAND_MAX_PAYLOAD_SIZE)
1447 		return -EINVAL;
1448 
1449 	if (SSH_COMMAND_MESSAGE_LENGTH(spec->length) > buf->len)
1450 		return -EINVAL;
1451 
1452 	msgb_init(&msgb, buf->ptr, buf->len);
1453 	seq = ssh_seq_next(&ctrl->counter.seq);
1454 	rqid = ssh_rqid_next(&ctrl->counter.rqid);
1455 	msgb_push_cmd(&msgb, seq, rqid, spec);
1456 
1457 	return msgb_bytes_used(&msgb);
1458 }
1459 EXPORT_SYMBOL_GPL(ssam_request_write_data);
1460 
1461 static void ssam_request_sync_complete(struct ssh_request *rqst,
1462 				       const struct ssh_command *cmd,
1463 				       const struct ssam_span *data, int status)
1464 {
1465 	struct ssh_rtl *rtl = ssh_request_rtl(rqst);
1466 	struct ssam_request_sync *r;
1467 
1468 	r = container_of(rqst, struct ssam_request_sync, base);
1469 	r->status = status;
1470 
1471 	if (r->resp)
1472 		r->resp->length = 0;
1473 
1474 	if (status) {
1475 		rtl_dbg_cond(rtl, "rsp: request failed: %d\n", status);
1476 		return;
1477 	}
1478 
1479 	if (!data)	/* Handle requests without a response. */
1480 		return;
1481 
1482 	if (!r->resp || !r->resp->pointer) {
1483 		if (data->len)
1484 			rtl_warn(rtl, "rsp: no response buffer provided, dropping data\n");
1485 		return;
1486 	}
1487 
1488 	if (data->len > r->resp->capacity) {
1489 		rtl_err(rtl,
1490 			"rsp: response buffer too small, capacity: %zu bytes, got: %zu bytes\n",
1491 			r->resp->capacity, data->len);
1492 		r->status = -ENOSPC;
1493 		return;
1494 	}
1495 
1496 	r->resp->length = data->len;
1497 	memcpy(r->resp->pointer, data->ptr, data->len);
1498 }
1499 
1500 static void ssam_request_sync_release(struct ssh_request *rqst)
1501 {
1502 	complete_all(&container_of(rqst, struct ssam_request_sync, base)->comp);
1503 }
1504 
1505 static const struct ssh_request_ops ssam_request_sync_ops = {
1506 	.release = ssam_request_sync_release,
1507 	.complete = ssam_request_sync_complete,
1508 };
1509 
1510 /**
1511  * ssam_request_sync_alloc() - Allocate a synchronous request.
1512  * @payload_len: The length of the request payload.
1513  * @flags:       Flags used for allocation.
1514  * @rqst:        Where to store the pointer to the allocated request.
1515  * @buffer:      Where to store the buffer descriptor for the message buffer of
1516  *               the request.
1517  *
1518  * Allocates a synchronous request with corresponding message buffer. The
1519  * request still needs to be initialized ssam_request_sync_init() before
1520  * it can be submitted, and the message buffer data must still be set to the
1521  * returned buffer via ssam_request_sync_set_data() after it has been filled,
1522  * if need be with adjusted message length.
1523  *
1524  * After use, the request and its corresponding message buffer should be freed
1525  * via ssam_request_sync_free(). The buffer must not be freed separately.
1526  *
1527  * Return: Returns zero on success, %-ENOMEM if the request could not be
1528  * allocated.
1529  */
1530 int ssam_request_sync_alloc(size_t payload_len, gfp_t flags,
1531 			    struct ssam_request_sync **rqst,
1532 			    struct ssam_span *buffer)
1533 {
1534 	size_t msglen = SSH_COMMAND_MESSAGE_LENGTH(payload_len);
1535 
1536 	*rqst = kzalloc(sizeof(**rqst) + msglen, flags);
1537 	if (!*rqst)
1538 		return -ENOMEM;
1539 
1540 	buffer->ptr = (u8 *)(*rqst + 1);
1541 	buffer->len = msglen;
1542 
1543 	return 0;
1544 }
1545 EXPORT_SYMBOL_GPL(ssam_request_sync_alloc);
1546 
1547 /**
1548  * ssam_request_sync_free() - Free a synchronous request.
1549  * @rqst: The request to be freed.
1550  *
1551  * Free a synchronous request and its corresponding buffer allocated with
1552  * ssam_request_sync_alloc(). Do not use for requests allocated on the stack
1553  * or via any other function.
1554  *
1555  * Warning: The caller must ensure that the request is not in use any more.
1556  * I.e. the caller must ensure that it has the only reference to the request
1557  * and the request is not currently pending. This means that the caller has
1558  * either never submitted the request, request submission has failed, or the
1559  * caller has waited until the submitted request has been completed via
1560  * ssam_request_sync_wait().
1561  */
1562 void ssam_request_sync_free(struct ssam_request_sync *rqst)
1563 {
1564 	kfree(rqst);
1565 }
1566 EXPORT_SYMBOL_GPL(ssam_request_sync_free);
1567 
1568 /**
1569  * ssam_request_sync_init() - Initialize a synchronous request struct.
1570  * @rqst:  The request to initialize.
1571  * @flags: The request flags.
1572  *
1573  * Initializes the given request struct. Does not initialize the request
1574  * message data. This has to be done explicitly after this call via
1575  * ssam_request_sync_set_data() and the actual message data has to be written
1576  * via ssam_request_write_data().
1577  *
1578  * Return: Returns zero on success or %-EINVAL if the given flags are invalid.
1579  */
1580 int ssam_request_sync_init(struct ssam_request_sync *rqst,
1581 			   enum ssam_request_flags flags)
1582 {
1583 	int status;
1584 
1585 	status = ssh_request_init(&rqst->base, flags, &ssam_request_sync_ops);
1586 	if (status)
1587 		return status;
1588 
1589 	init_completion(&rqst->comp);
1590 	rqst->resp = NULL;
1591 	rqst->status = 0;
1592 
1593 	return 0;
1594 }
1595 EXPORT_SYMBOL_GPL(ssam_request_sync_init);
1596 
1597 /**
1598  * ssam_request_sync_submit() - Submit a synchronous request.
1599  * @ctrl: The controller with which to submit the request.
1600  * @rqst: The request to submit.
1601  *
1602  * Submit a synchronous request. The request has to be initialized and
1603  * properly set up, including response buffer (may be %NULL if no response is
1604  * expected) and command message data. This function does not wait for the
1605  * request to be completed.
1606  *
1607  * If this function succeeds, ssam_request_sync_wait() must be used to ensure
1608  * that the request has been completed before the response data can be
1609  * accessed and/or the request can be freed. On failure, the request may
1610  * immediately be freed.
1611  *
1612  * This function may only be used if the controller is active, i.e. has been
1613  * initialized and not suspended.
1614  */
1615 int ssam_request_sync_submit(struct ssam_controller *ctrl,
1616 			     struct ssam_request_sync *rqst)
1617 {
1618 	int status;
1619 
1620 	/*
1621 	 * This is only a superficial check. In general, the caller needs to
1622 	 * ensure that the controller is initialized and is not (and does not
1623 	 * get) suspended during use, i.e. until the request has been completed
1624 	 * (if _absolutely_ necessary, by use of ssam_controller_statelock/
1625 	 * ssam_controller_stateunlock, but something like ssam_client_link
1626 	 * should be preferred as this needs to last until the request has been
1627 	 * completed).
1628 	 *
1629 	 * Note that it is actually safe to use this function while the
1630 	 * controller is in the process of being shut down (as ssh_rtl_submit
1631 	 * is safe with regards to this), but it is generally discouraged to do
1632 	 * so.
1633 	 */
1634 	if (WARN_ON(READ_ONCE(ctrl->state) != SSAM_CONTROLLER_STARTED)) {
1635 		ssh_request_put(&rqst->base);
1636 		return -ENODEV;
1637 	}
1638 
1639 	status = ssh_rtl_submit(&ctrl->rtl, &rqst->base);
1640 	ssh_request_put(&rqst->base);
1641 
1642 	return status;
1643 }
1644 EXPORT_SYMBOL_GPL(ssam_request_sync_submit);
1645 
1646 /**
1647  * ssam_request_sync() - Execute a synchronous request.
1648  * @ctrl: The controller via which the request will be submitted.
1649  * @spec: The request specification and payload.
1650  * @rsp:  The response buffer.
1651  *
1652  * Allocates a synchronous request with its message data buffer on the heap
1653  * via ssam_request_sync_alloc(), fully initializes it via the provided
1654  * request specification, submits it, and finally waits for its completion
1655  * before freeing it and returning its status.
1656  *
1657  * Return: Returns the status of the request or any failure during setup.
1658  */
1659 int ssam_request_sync(struct ssam_controller *ctrl,
1660 		      const struct ssam_request *spec,
1661 		      struct ssam_response *rsp)
1662 {
1663 	struct ssam_request_sync *rqst;
1664 	struct ssam_span buf;
1665 	ssize_t len;
1666 	int status;
1667 
1668 	status = ssam_request_sync_alloc(spec->length, GFP_KERNEL, &rqst, &buf);
1669 	if (status)
1670 		return status;
1671 
1672 	status = ssam_request_sync_init(rqst, spec->flags);
1673 	if (status)
1674 		return status;
1675 
1676 	ssam_request_sync_set_resp(rqst, rsp);
1677 
1678 	len = ssam_request_write_data(&buf, ctrl, spec);
1679 	if (len < 0) {
1680 		ssam_request_sync_free(rqst);
1681 		return len;
1682 	}
1683 
1684 	ssam_request_sync_set_data(rqst, buf.ptr, len);
1685 
1686 	status = ssam_request_sync_submit(ctrl, rqst);
1687 	if (!status)
1688 		status = ssam_request_sync_wait(rqst);
1689 
1690 	ssam_request_sync_free(rqst);
1691 	return status;
1692 }
1693 EXPORT_SYMBOL_GPL(ssam_request_sync);
1694 
1695 /**
1696  * ssam_request_sync_with_buffer() - Execute a synchronous request with the
1697  * provided buffer as back-end for the message buffer.
1698  * @ctrl: The controller via which the request will be submitted.
1699  * @spec: The request specification and payload.
1700  * @rsp:  The response buffer.
1701  * @buf:  The buffer for the request message data.
1702  *
1703  * Allocates a synchronous request struct on the stack, fully initializes it
1704  * using the provided buffer as message data buffer, submits it, and then
1705  * waits for its completion before returning its status. The
1706  * SSH_COMMAND_MESSAGE_LENGTH() macro can be used to compute the required
1707  * message buffer size.
1708  *
1709  * This function does essentially the same as ssam_request_sync(), but instead
1710  * of dynamically allocating the request and message data buffer, it uses the
1711  * provided message data buffer and stores the (small) request struct on the
1712  * heap.
1713  *
1714  * Return: Returns the status of the request or any failure during setup.
1715  */
1716 int ssam_request_sync_with_buffer(struct ssam_controller *ctrl,
1717 				  const struct ssam_request *spec,
1718 				  struct ssam_response *rsp,
1719 				  struct ssam_span *buf)
1720 {
1721 	struct ssam_request_sync rqst;
1722 	ssize_t len;
1723 	int status;
1724 
1725 	status = ssam_request_sync_init(&rqst, spec->flags);
1726 	if (status)
1727 		return status;
1728 
1729 	ssam_request_sync_set_resp(&rqst, rsp);
1730 
1731 	len = ssam_request_write_data(buf, ctrl, spec);
1732 	if (len < 0)
1733 		return len;
1734 
1735 	ssam_request_sync_set_data(&rqst, buf->ptr, len);
1736 
1737 	status = ssam_request_sync_submit(ctrl, &rqst);
1738 	if (!status)
1739 		status = ssam_request_sync_wait(&rqst);
1740 
1741 	return status;
1742 }
1743 EXPORT_SYMBOL_GPL(ssam_request_sync_with_buffer);
1744 
1745 
1746 /* -- Internal SAM requests. ------------------------------------------------ */
1747 
1748 static SSAM_DEFINE_SYNC_REQUEST_R(ssam_ssh_get_firmware_version, __le32, {
1749 	.target_category = SSAM_SSH_TC_SAM,
1750 	.target_id       = 0x01,
1751 	.command_id      = 0x13,
1752 	.instance_id     = 0x00,
1753 });
1754 
1755 static SSAM_DEFINE_SYNC_REQUEST_R(ssam_ssh_notif_display_off, u8, {
1756 	.target_category = SSAM_SSH_TC_SAM,
1757 	.target_id       = 0x01,
1758 	.command_id      = 0x15,
1759 	.instance_id     = 0x00,
1760 });
1761 
1762 static SSAM_DEFINE_SYNC_REQUEST_R(ssam_ssh_notif_display_on, u8, {
1763 	.target_category = SSAM_SSH_TC_SAM,
1764 	.target_id       = 0x01,
1765 	.command_id      = 0x16,
1766 	.instance_id     = 0x00,
1767 });
1768 
1769 static SSAM_DEFINE_SYNC_REQUEST_R(ssam_ssh_notif_d0_exit, u8, {
1770 	.target_category = SSAM_SSH_TC_SAM,
1771 	.target_id       = 0x01,
1772 	.command_id      = 0x33,
1773 	.instance_id     = 0x00,
1774 });
1775 
1776 static SSAM_DEFINE_SYNC_REQUEST_R(ssam_ssh_notif_d0_entry, u8, {
1777 	.target_category = SSAM_SSH_TC_SAM,
1778 	.target_id       = 0x01,
1779 	.command_id      = 0x34,
1780 	.instance_id     = 0x00,
1781 });
1782 
1783 /**
1784  * struct ssh_notification_params - Command payload to enable/disable SSH
1785  * notifications.
1786  * @target_category: The target category for which notifications should be
1787  *                   enabled/disabled.
1788  * @flags:           Flags determining how notifications are being sent.
1789  * @request_id:      The request ID that is used to send these notifications.
1790  * @instance_id:     The specific instance in the given target category for
1791  *                   which notifications should be enabled.
1792  */
1793 struct ssh_notification_params {
1794 	u8 target_category;
1795 	u8 flags;
1796 	__le16 request_id;
1797 	u8 instance_id;
1798 } __packed;
1799 
1800 static_assert(sizeof(struct ssh_notification_params) == 5);
1801 
1802 static int __ssam_ssh_event_request(struct ssam_controller *ctrl,
1803 				    struct ssam_event_registry reg, u8 cid,
1804 				    struct ssam_event_id id, u8 flags)
1805 {
1806 	struct ssh_notification_params params;
1807 	struct ssam_request rqst;
1808 	struct ssam_response result;
1809 	int status;
1810 
1811 	u16 rqid = ssh_tc_to_rqid(id.target_category);
1812 	u8 buf = 0;
1813 
1814 	/* Only allow RQIDs that lie within the event spectrum. */
1815 	if (!ssh_rqid_is_event(rqid))
1816 		return -EINVAL;
1817 
1818 	params.target_category = id.target_category;
1819 	params.instance_id = id.instance;
1820 	params.flags = flags;
1821 	put_unaligned_le16(rqid, &params.request_id);
1822 
1823 	rqst.target_category = reg.target_category;
1824 	rqst.target_id = reg.target_id;
1825 	rqst.command_id = cid;
1826 	rqst.instance_id = 0x00;
1827 	rqst.flags = SSAM_REQUEST_HAS_RESPONSE;
1828 	rqst.length = sizeof(params);
1829 	rqst.payload = (u8 *)&params;
1830 
1831 	result.capacity = sizeof(buf);
1832 	result.length = 0;
1833 	result.pointer = &buf;
1834 
1835 	status = ssam_retry(ssam_request_sync_onstack, ctrl, &rqst, &result,
1836 			    sizeof(params));
1837 
1838 	return status < 0 ? status : buf;
1839 }
1840 
1841 /**
1842  * ssam_ssh_event_enable() - Enable SSH event.
1843  * @ctrl:  The controller for which to enable the event.
1844  * @reg:   The event registry describing what request to use for enabling and
1845  *         disabling the event.
1846  * @id:    The event identifier.
1847  * @flags: The event flags.
1848  *
1849  * Enables the specified event on the EC. This function does not manage
1850  * reference counting of enabled events and is basically only a wrapper for
1851  * the raw EC request. If the specified event is already enabled, the EC will
1852  * ignore this request.
1853  *
1854  * Return: Returns the status of the executed SAM request (zero on success and
1855  * negative on direct failure) or %-EPROTO if the request response indicates a
1856  * failure.
1857  */
1858 static int ssam_ssh_event_enable(struct ssam_controller *ctrl,
1859 				 struct ssam_event_registry reg,
1860 				 struct ssam_event_id id, u8 flags)
1861 {
1862 	int status;
1863 
1864 	status = __ssam_ssh_event_request(ctrl, reg, reg.cid_enable, id, flags);
1865 
1866 	if (status < 0 && status != -EINVAL) {
1867 		ssam_err(ctrl,
1868 			 "failed to enable event source (tc: %#04x, iid: %#04x, reg: %#04x)\n",
1869 			 id.target_category, id.instance, reg.target_category);
1870 	}
1871 
1872 	if (status > 0) {
1873 		ssam_err(ctrl,
1874 			 "unexpected result while enabling event source: %#04x (tc: %#04x, iid: %#04x, reg: %#04x)\n",
1875 			 status, id.target_category, id.instance, reg.target_category);
1876 		return -EPROTO;
1877 	}
1878 
1879 	return status;
1880 }
1881 
1882 /**
1883  * ssam_ssh_event_disable() - Disable SSH event.
1884  * @ctrl:  The controller for which to disable the event.
1885  * @reg:   The event registry describing what request to use for enabling and
1886  *         disabling the event (must be same as used when enabling the event).
1887  * @id:    The event identifier.
1888  * @flags: The event flags (likely ignored for disabling of events).
1889  *
1890  * Disables the specified event on the EC. This function does not manage
1891  * reference counting of enabled events and is basically only a wrapper for
1892  * the raw EC request. If the specified event is already disabled, the EC will
1893  * ignore this request.
1894  *
1895  * Return: Returns the status of the executed SAM request (zero on success and
1896  * negative on direct failure) or %-EPROTO if the request response indicates a
1897  * failure.
1898  */
1899 static int ssam_ssh_event_disable(struct ssam_controller *ctrl,
1900 				  struct ssam_event_registry reg,
1901 				  struct ssam_event_id id, u8 flags)
1902 {
1903 	int status;
1904 
1905 	status = __ssam_ssh_event_request(ctrl, reg, reg.cid_enable, id, flags);
1906 
1907 	if (status < 0 && status != -EINVAL) {
1908 		ssam_err(ctrl,
1909 			 "failed to disable event source (tc: %#04x, iid: %#04x, reg: %#04x)\n",
1910 			 id.target_category, id.instance, reg.target_category);
1911 	}
1912 
1913 	if (status > 0) {
1914 		ssam_err(ctrl,
1915 			 "unexpected result while disabling event source: %#04x (tc: %#04x, iid: %#04x, reg: %#04x)\n",
1916 			 status, id.target_category, id.instance, reg.target_category);
1917 		return -EPROTO;
1918 	}
1919 
1920 	return status;
1921 }
1922 
1923 
1924 /* -- Wrappers for internal SAM requests. ----------------------------------- */
1925 
1926 /**
1927  * ssam_get_firmware_version() - Get the SAM/EC firmware version.
1928  * @ctrl:    The controller.
1929  * @version: Where to store the version number.
1930  *
1931  * Return: Returns zero on success or the status of the executed SAM request
1932  * if that request failed.
1933  */
1934 int ssam_get_firmware_version(struct ssam_controller *ctrl, u32 *version)
1935 {
1936 	__le32 __version;
1937 	int status;
1938 
1939 	status = ssam_retry(ssam_ssh_get_firmware_version, ctrl, &__version);
1940 	if (status)
1941 		return status;
1942 
1943 	*version = le32_to_cpu(__version);
1944 	return 0;
1945 }
1946 
1947 /**
1948  * ssam_ctrl_notif_display_off() - Notify EC that the display has been turned
1949  * off.
1950  * @ctrl: The controller.
1951  *
1952  * Notify the EC that the display has been turned off and the driver may enter
1953  * a lower-power state. This will prevent events from being sent directly.
1954  * Rather, the EC signals an event by pulling the wakeup GPIO high for as long
1955  * as there are pending events. The events then need to be manually released,
1956  * one by one, via the GPIO callback request. All pending events accumulated
1957  * during this state can also be released by issuing the display-on
1958  * notification, e.g. via ssam_ctrl_notif_display_on(), which will also reset
1959  * the GPIO.
1960  *
1961  * On some devices, specifically ones with an integrated keyboard, the keyboard
1962  * backlight will be turned off by this call.
1963  *
1964  * This function will only send the display-off notification command if
1965  * display notifications are supported by the EC. Currently all known devices
1966  * support these notifications.
1967  *
1968  * Use ssam_ctrl_notif_display_on() to reverse the effects of this function.
1969  *
1970  * Return: Returns zero on success or if no request has been executed, the
1971  * status of the executed SAM request if that request failed, or %-EPROTO if
1972  * an unexpected response has been received.
1973  */
1974 int ssam_ctrl_notif_display_off(struct ssam_controller *ctrl)
1975 {
1976 	int status;
1977 	u8 response;
1978 
1979 	ssam_dbg(ctrl, "pm: notifying display off\n");
1980 
1981 	status = ssam_retry(ssam_ssh_notif_display_off, ctrl, &response);
1982 	if (status)
1983 		return status;
1984 
1985 	if (response != 0) {
1986 		ssam_err(ctrl, "unexpected response from display-off notification: %#04x\n",
1987 			 response);
1988 		return -EPROTO;
1989 	}
1990 
1991 	return 0;
1992 }
1993 
1994 /**
1995  * ssam_ctrl_notif_display_on() - Notify EC that the display has been turned on.
1996  * @ctrl: The controller.
1997  *
1998  * Notify the EC that the display has been turned back on and the driver has
1999  * exited its lower-power state. This notification is the counterpart to the
2000  * display-off notification sent via ssam_ctrl_notif_display_off() and will
2001  * reverse its effects, including resetting events to their default behavior.
2002  *
2003  * This function will only send the display-on notification command if display
2004  * notifications are supported by the EC. Currently all known devices support
2005  * these notifications.
2006  *
2007  * See ssam_ctrl_notif_display_off() for more details.
2008  *
2009  * Return: Returns zero on success or if no request has been executed, the
2010  * status of the executed SAM request if that request failed, or %-EPROTO if
2011  * an unexpected response has been received.
2012  */
2013 int ssam_ctrl_notif_display_on(struct ssam_controller *ctrl)
2014 {
2015 	int status;
2016 	u8 response;
2017 
2018 	ssam_dbg(ctrl, "pm: notifying display on\n");
2019 
2020 	status = ssam_retry(ssam_ssh_notif_display_on, ctrl, &response);
2021 	if (status)
2022 		return status;
2023 
2024 	if (response != 0) {
2025 		ssam_err(ctrl, "unexpected response from display-on notification: %#04x\n",
2026 			 response);
2027 		return -EPROTO;
2028 	}
2029 
2030 	return 0;
2031 }
2032 
2033 /**
2034  * ssam_ctrl_notif_d0_exit() - Notify EC that the driver/device exits the D0
2035  * power state.
2036  * @ctrl: The controller
2037  *
2038  * Notifies the EC that the driver prepares to exit the D0 power state in
2039  * favor of a lower-power state. Exact effects of this function related to the
2040  * EC are currently unknown.
2041  *
2042  * This function will only send the D0-exit notification command if D0-state
2043  * notifications are supported by the EC. Only newer Surface generations
2044  * support these notifications.
2045  *
2046  * Use ssam_ctrl_notif_d0_entry() to reverse the effects of this function.
2047  *
2048  * Return: Returns zero on success or if no request has been executed, the
2049  * status of the executed SAM request if that request failed, or %-EPROTO if
2050  * an unexpected response has been received.
2051  */
2052 int ssam_ctrl_notif_d0_exit(struct ssam_controller *ctrl)
2053 {
2054 	int status;
2055 	u8 response;
2056 
2057 	if (!ctrl->caps.d3_closes_handle)
2058 		return 0;
2059 
2060 	ssam_dbg(ctrl, "pm: notifying D0 exit\n");
2061 
2062 	status = ssam_retry(ssam_ssh_notif_d0_exit, ctrl, &response);
2063 	if (status)
2064 		return status;
2065 
2066 	if (response != 0) {
2067 		ssam_err(ctrl, "unexpected response from D0-exit notification: %#04x\n",
2068 			 response);
2069 		return -EPROTO;
2070 	}
2071 
2072 	return 0;
2073 }
2074 
2075 /**
2076  * ssam_ctrl_notif_d0_entry() - Notify EC that the driver/device enters the D0
2077  * power state.
2078  * @ctrl: The controller
2079  *
2080  * Notifies the EC that the driver has exited a lower-power state and entered
2081  * the D0 power state. Exact effects of this function related to the EC are
2082  * currently unknown.
2083  *
2084  * This function will only send the D0-entry notification command if D0-state
2085  * notifications are supported by the EC. Only newer Surface generations
2086  * support these notifications.
2087  *
2088  * See ssam_ctrl_notif_d0_exit() for more details.
2089  *
2090  * Return: Returns zero on success or if no request has been executed, the
2091  * status of the executed SAM request if that request failed, or %-EPROTO if
2092  * an unexpected response has been received.
2093  */
2094 int ssam_ctrl_notif_d0_entry(struct ssam_controller *ctrl)
2095 {
2096 	int status;
2097 	u8 response;
2098 
2099 	if (!ctrl->caps.d3_closes_handle)
2100 		return 0;
2101 
2102 	ssam_dbg(ctrl, "pm: notifying D0 entry\n");
2103 
2104 	status = ssam_retry(ssam_ssh_notif_d0_entry, ctrl, &response);
2105 	if (status)
2106 		return status;
2107 
2108 	if (response != 0) {
2109 		ssam_err(ctrl, "unexpected response from D0-entry notification: %#04x\n",
2110 			 response);
2111 		return -EPROTO;
2112 	}
2113 
2114 	return 0;
2115 }
2116 
2117 
2118 /* -- Top-level event registry interface. ----------------------------------- */
2119 
2120 /**
2121  * ssam_notifier_register() - Register an event notifier.
2122  * @ctrl: The controller to register the notifier on.
2123  * @n:    The event notifier to register.
2124  *
2125  * Register an event notifier and increment the usage counter of the
2126  * associated SAM event. If the event was previously not enabled, it will be
2127  * enabled during this call.
2128  *
2129  * Return: Returns zero on success, %-ENOSPC if there have already been
2130  * %INT_MAX notifiers for the event ID/type associated with the notifier block
2131  * registered, %-ENOMEM if the corresponding event entry could not be
2132  * allocated. If this is the first time that a notifier block is registered
2133  * for the specific associated event, returns the status of the event-enable
2134  * EC-command.
2135  */
2136 int ssam_notifier_register(struct ssam_controller *ctrl,
2137 			   struct ssam_event_notifier *n)
2138 {
2139 	u16 rqid = ssh_tc_to_rqid(n->event.id.target_category);
2140 	struct ssam_nf_refcount_entry *entry;
2141 	struct ssam_nf_head *nf_head;
2142 	struct ssam_nf *nf;
2143 	int status;
2144 
2145 	if (!ssh_rqid_is_event(rqid))
2146 		return -EINVAL;
2147 
2148 	nf = &ctrl->cplt.event.notif;
2149 	nf_head = &nf->head[ssh_rqid_to_event(rqid)];
2150 
2151 	mutex_lock(&nf->lock);
2152 
2153 	entry = ssam_nf_refcount_inc(nf, n->event.reg, n->event.id);
2154 	if (IS_ERR(entry)) {
2155 		mutex_unlock(&nf->lock);
2156 		return PTR_ERR(entry);
2157 	}
2158 
2159 	ssam_dbg(ctrl, "enabling event (reg: %#04x, tc: %#04x, iid: %#04x, rc: %d)\n",
2160 		 n->event.reg.target_category, n->event.id.target_category,
2161 		 n->event.id.instance, entry->refcount);
2162 
2163 	status = ssam_nfblk_insert(nf_head, &n->base);
2164 	if (status) {
2165 		entry = ssam_nf_refcount_dec(nf, n->event.reg, n->event.id);
2166 		if (entry->refcount == 0)
2167 			kfree(entry);
2168 
2169 		mutex_unlock(&nf->lock);
2170 		return status;
2171 	}
2172 
2173 	if (entry->refcount == 1) {
2174 		status = ssam_ssh_event_enable(ctrl, n->event.reg, n->event.id,
2175 					       n->event.flags);
2176 		if (status) {
2177 			ssam_nfblk_remove(&n->base);
2178 			kfree(ssam_nf_refcount_dec(nf, n->event.reg, n->event.id));
2179 			mutex_unlock(&nf->lock);
2180 			synchronize_srcu(&nf_head->srcu);
2181 			return status;
2182 		}
2183 
2184 		entry->flags = n->event.flags;
2185 
2186 	} else if (entry->flags != n->event.flags) {
2187 		ssam_warn(ctrl,
2188 			  "inconsistent flags when enabling event: got %#04x, expected %#04x (reg: %#04x, tc: %#04x, iid: %#04x)\n",
2189 			  n->event.flags, entry->flags, n->event.reg.target_category,
2190 			  n->event.id.target_category, n->event.id.instance);
2191 	}
2192 
2193 	mutex_unlock(&nf->lock);
2194 	return 0;
2195 }
2196 EXPORT_SYMBOL_GPL(ssam_notifier_register);
2197 
2198 /**
2199  * ssam_notifier_unregister() - Unregister an event notifier.
2200  * @ctrl: The controller the notifier has been registered on.
2201  * @n:    The event notifier to unregister.
2202  *
2203  * Unregister an event notifier and decrement the usage counter of the
2204  * associated SAM event. If the usage counter reaches zero, the event will be
2205  * disabled.
2206  *
2207  * Return: Returns zero on success, %-ENOENT if the given notifier block has
2208  * not been registered on the controller. If the given notifier block was the
2209  * last one associated with its specific event, returns the status of the
2210  * event-disable EC-command.
2211  */
2212 int ssam_notifier_unregister(struct ssam_controller *ctrl,
2213 			     struct ssam_event_notifier *n)
2214 {
2215 	u16 rqid = ssh_tc_to_rqid(n->event.id.target_category);
2216 	struct ssam_nf_refcount_entry *entry;
2217 	struct ssam_nf_head *nf_head;
2218 	struct ssam_nf *nf;
2219 	int status = 0;
2220 
2221 	if (!ssh_rqid_is_event(rqid))
2222 		return -EINVAL;
2223 
2224 	nf = &ctrl->cplt.event.notif;
2225 	nf_head = &nf->head[ssh_rqid_to_event(rqid)];
2226 
2227 	mutex_lock(&nf->lock);
2228 
2229 	if (!ssam_nfblk_find(nf_head, &n->base)) {
2230 		mutex_unlock(&nf->lock);
2231 		return -ENOENT;
2232 	}
2233 
2234 	entry = ssam_nf_refcount_dec(nf, n->event.reg, n->event.id);
2235 	if (WARN_ON(!entry)) {
2236 		/*
2237 		 * If this does not return an entry, there's a logic error
2238 		 * somewhere: The notifier block is registered, but the event
2239 		 * refcount entry is not there. Remove the notifier block
2240 		 * anyways.
2241 		 */
2242 		status = -ENOENT;
2243 		goto remove;
2244 	}
2245 
2246 	ssam_dbg(ctrl, "disabling event (reg: %#04x, tc: %#04x, iid: %#04x, rc: %d)\n",
2247 		 n->event.reg.target_category, n->event.id.target_category,
2248 		 n->event.id.instance, entry->refcount);
2249 
2250 	if (entry->flags != n->event.flags) {
2251 		ssam_warn(ctrl,
2252 			  "inconsistent flags when disabling event: got %#04x, expected %#04x (reg: %#04x, tc: %#04x, iid: %#04x)\n",
2253 			  n->event.flags, entry->flags, n->event.reg.target_category,
2254 			  n->event.id.target_category, n->event.id.instance);
2255 	}
2256 
2257 	if (entry->refcount == 0) {
2258 		status = ssam_ssh_event_disable(ctrl, n->event.reg, n->event.id,
2259 						n->event.flags);
2260 		kfree(entry);
2261 	}
2262 
2263 remove:
2264 	ssam_nfblk_remove(&n->base);
2265 	mutex_unlock(&nf->lock);
2266 	synchronize_srcu(&nf_head->srcu);
2267 
2268 	return status;
2269 }
2270 EXPORT_SYMBOL_GPL(ssam_notifier_unregister);
2271 
2272 /**
2273  * ssam_notifier_disable_registered() - Disable events for all registered
2274  * notifiers.
2275  * @ctrl: The controller for which to disable the notifiers/events.
2276  *
2277  * Disables events for all currently registered notifiers. In case of an error
2278  * (EC command failing), all previously disabled events will be restored and
2279  * the error code returned.
2280  *
2281  * This function is intended to disable all events prior to hibernation entry.
2282  * See ssam_notifier_restore_registered() to restore/re-enable all events
2283  * disabled with this function.
2284  *
2285  * Note that this function will not disable events for notifiers registered
2286  * after calling this function. It should thus be made sure that no new
2287  * notifiers are going to be added after this call and before the corresponding
2288  * call to ssam_notifier_restore_registered().
2289  *
2290  * Return: Returns zero on success. In case of failure returns the error code
2291  * returned by the failed EC command to disable an event.
2292  */
2293 int ssam_notifier_disable_registered(struct ssam_controller *ctrl)
2294 {
2295 	struct ssam_nf *nf = &ctrl->cplt.event.notif;
2296 	struct rb_node *n;
2297 	int status;
2298 
2299 	mutex_lock(&nf->lock);
2300 	for (n = rb_first(&nf->refcount); n; n = rb_next(n)) {
2301 		struct ssam_nf_refcount_entry *e;
2302 
2303 		e = rb_entry(n, struct ssam_nf_refcount_entry, node);
2304 		status = ssam_ssh_event_disable(ctrl, e->key.reg,
2305 						e->key.id, e->flags);
2306 		if (status)
2307 			goto err;
2308 	}
2309 	mutex_unlock(&nf->lock);
2310 
2311 	return 0;
2312 
2313 err:
2314 	for (n = rb_prev(n); n; n = rb_prev(n)) {
2315 		struct ssam_nf_refcount_entry *e;
2316 
2317 		e = rb_entry(n, struct ssam_nf_refcount_entry, node);
2318 		ssam_ssh_event_enable(ctrl, e->key.reg, e->key.id, e->flags);
2319 	}
2320 	mutex_unlock(&nf->lock);
2321 
2322 	return status;
2323 }
2324 
2325 /**
2326  * ssam_notifier_restore_registered() - Restore/re-enable events for all
2327  * registered notifiers.
2328  * @ctrl: The controller for which to restore the notifiers/events.
2329  *
2330  * Restores/re-enables all events for which notifiers have been registered on
2331  * the given controller. In case of a failure, the error is logged and the
2332  * function continues to try and enable the remaining events.
2333  *
2334  * This function is intended to restore/re-enable all registered events after
2335  * hibernation. See ssam_notifier_disable_registered() for the counter part
2336  * disabling the events and more details.
2337  */
2338 void ssam_notifier_restore_registered(struct ssam_controller *ctrl)
2339 {
2340 	struct ssam_nf *nf = &ctrl->cplt.event.notif;
2341 	struct rb_node *n;
2342 
2343 	mutex_lock(&nf->lock);
2344 	for (n = rb_first(&nf->refcount); n; n = rb_next(n)) {
2345 		struct ssam_nf_refcount_entry *e;
2346 
2347 		e = rb_entry(n, struct ssam_nf_refcount_entry, node);
2348 
2349 		/* Ignore errors, will get logged in call. */
2350 		ssam_ssh_event_enable(ctrl, e->key.reg, e->key.id, e->flags);
2351 	}
2352 	mutex_unlock(&nf->lock);
2353 }
2354 
2355 /**
2356  * ssam_notifier_is_empty() - Check if there are any registered notifiers.
2357  * @ctrl: The controller to check on.
2358  *
2359  * Return: Returns %true if there are currently no notifiers registered on the
2360  * controller, %false otherwise.
2361  */
2362 static bool ssam_notifier_is_empty(struct ssam_controller *ctrl)
2363 {
2364 	struct ssam_nf *nf = &ctrl->cplt.event.notif;
2365 	bool result;
2366 
2367 	mutex_lock(&nf->lock);
2368 	result = ssam_nf_refcount_empty(nf);
2369 	mutex_unlock(&nf->lock);
2370 
2371 	return result;
2372 }
2373 
2374 /**
2375  * ssam_notifier_unregister_all() - Unregister all currently registered
2376  * notifiers.
2377  * @ctrl: The controller to unregister the notifiers on.
2378  *
2379  * Unregisters all currently registered notifiers. This function is used to
2380  * ensure that all notifiers will be unregistered and associated
2381  * entries/resources freed when the controller is being shut down.
2382  */
2383 static void ssam_notifier_unregister_all(struct ssam_controller *ctrl)
2384 {
2385 	struct ssam_nf *nf = &ctrl->cplt.event.notif;
2386 	struct ssam_nf_refcount_entry *e, *n;
2387 
2388 	mutex_lock(&nf->lock);
2389 	rbtree_postorder_for_each_entry_safe(e, n, &nf->refcount, node) {
2390 		/* Ignore errors, will get logged in call. */
2391 		ssam_ssh_event_disable(ctrl, e->key.reg, e->key.id, e->flags);
2392 		kfree(e);
2393 	}
2394 	nf->refcount = RB_ROOT;
2395 	mutex_unlock(&nf->lock);
2396 }
2397 
2398 
2399 /* -- Wakeup IRQ. ----------------------------------------------------------- */
2400 
2401 static irqreturn_t ssam_irq_handle(int irq, void *dev_id)
2402 {
2403 	struct ssam_controller *ctrl = dev_id;
2404 
2405 	ssam_dbg(ctrl, "pm: wake irq triggered\n");
2406 
2407 	/*
2408 	 * Note: Proper wakeup detection is currently unimplemented.
2409 	 *       When the EC is in display-off or any other non-D0 state, it
2410 	 *       does not send events/notifications to the host. Instead it
2411 	 *       signals that there are events available via the wakeup IRQ.
2412 	 *       This driver is responsible for calling back to the EC to
2413 	 *       release these events one-by-one.
2414 	 *
2415 	 *       This IRQ should not cause a full system resume by its own.
2416 	 *       Instead, events should be handled by their respective subsystem
2417 	 *       drivers, which in turn should signal whether a full system
2418 	 *       resume should be performed.
2419 	 *
2420 	 * TODO: Send GPIO callback command repeatedly to EC until callback
2421 	 *       returns 0x00. Return flag of callback is "has more events".
2422 	 *       Each time the command is sent, one event is "released". Once
2423 	 *       all events have been released (return = 0x00), the GPIO is
2424 	 *       re-armed. Detect wakeup events during this process, go back to
2425 	 *       sleep if no wakeup event has been received.
2426 	 */
2427 
2428 	return IRQ_HANDLED;
2429 }
2430 
2431 /**
2432  * ssam_irq_setup() - Set up SAM EC wakeup-GPIO interrupt.
2433  * @ctrl: The controller for which the IRQ should be set up.
2434  *
2435  * Set up an IRQ for the wakeup-GPIO pin of the SAM EC. This IRQ can be used
2436  * to wake the device from a low power state.
2437  *
2438  * Note that this IRQ can only be triggered while the EC is in the display-off
2439  * state. In this state, events are not sent to the host in the usual way.
2440  * Instead the wakeup-GPIO gets pulled to "high" as long as there are pending
2441  * events and these events need to be released one-by-one via the GPIO
2442  * callback request, either until there are no events left and the GPIO is
2443  * reset, or all at once by transitioning the EC out of the display-off state,
2444  * which will also clear the GPIO.
2445  *
2446  * Not all events, however, should trigger a full system wakeup. Instead the
2447  * driver should, if necessary, inspect and forward each event to the
2448  * corresponding subsystem, which in turn should decide if the system needs to
2449  * be woken up. This logic has not been implemented yet, thus wakeup by this
2450  * IRQ should be disabled by default to avoid spurious wake-ups, caused, for
2451  * example, by the remaining battery percentage changing. Refer to comments in
2452  * this function and comments in the corresponding IRQ handler for more
2453  * details on how this should be implemented.
2454  *
2455  * See also ssam_ctrl_notif_display_off() and ssam_ctrl_notif_display_off()
2456  * for functions to transition the EC into and out of the display-off state as
2457  * well as more details on it.
2458  *
2459  * The IRQ is disabled by default and has to be enabled before it can wake up
2460  * the device from suspend via ssam_irq_arm_for_wakeup(). On teardown, the IRQ
2461  * should be freed via ssam_irq_free().
2462  */
2463 int ssam_irq_setup(struct ssam_controller *ctrl)
2464 {
2465 	struct device *dev = ssam_controller_device(ctrl);
2466 	struct gpio_desc *gpiod;
2467 	int irq;
2468 	int status;
2469 
2470 	/*
2471 	 * The actual GPIO interrupt is declared in ACPI as TRIGGER_HIGH.
2472 	 * However, the GPIO line only gets reset by sending the GPIO callback
2473 	 * command to SAM (or alternatively the display-on notification). As
2474 	 * proper handling for this interrupt is not implemented yet, leaving
2475 	 * the IRQ at TRIGGER_HIGH would cause an IRQ storm (as the callback
2476 	 * never gets sent and thus the line never gets reset). To avoid this,
2477 	 * mark the IRQ as TRIGGER_RISING for now, only creating a single
2478 	 * interrupt, and let the SAM resume callback during the controller
2479 	 * resume process clear it.
2480 	 */
2481 	const int irqf = IRQF_SHARED | IRQF_ONESHOT | IRQF_TRIGGER_RISING;
2482 
2483 	gpiod = gpiod_get(dev, "ssam_wakeup-int", GPIOD_ASIS);
2484 	if (IS_ERR(gpiod))
2485 		return PTR_ERR(gpiod);
2486 
2487 	irq = gpiod_to_irq(gpiod);
2488 	gpiod_put(gpiod);
2489 
2490 	if (irq < 0)
2491 		return irq;
2492 
2493 	status = request_threaded_irq(irq, NULL, ssam_irq_handle, irqf,
2494 				      "ssam_wakeup", ctrl);
2495 	if (status)
2496 		return status;
2497 
2498 	ctrl->irq.num = irq;
2499 	disable_irq(ctrl->irq.num);
2500 	return 0;
2501 }
2502 
2503 /**
2504  * ssam_irq_free() - Free SAM EC wakeup-GPIO interrupt.
2505  * @ctrl: The controller for which the IRQ should be freed.
2506  *
2507  * Free the wakeup-GPIO IRQ previously set-up via ssam_irq_setup().
2508  */
2509 void ssam_irq_free(struct ssam_controller *ctrl)
2510 {
2511 	free_irq(ctrl->irq.num, ctrl);
2512 	ctrl->irq.num = -1;
2513 }
2514 
2515 /**
2516  * ssam_irq_arm_for_wakeup() - Arm the EC IRQ for wakeup, if enabled.
2517  * @ctrl: The controller for which the IRQ should be armed.
2518  *
2519  * Sets up the IRQ so that it can be used to wake the device. Specifically,
2520  * this function enables the irq and then, if the device is allowed to wake up
2521  * the system, calls enable_irq_wake(). See ssam_irq_disarm_wakeup() for the
2522  * corresponding function to disable the IRQ.
2523  *
2524  * This function is intended to arm the IRQ before entering S2idle suspend.
2525  *
2526  * Note: calls to ssam_irq_arm_for_wakeup() and ssam_irq_disarm_wakeup() must
2527  * be balanced.
2528  */
2529 int ssam_irq_arm_for_wakeup(struct ssam_controller *ctrl)
2530 {
2531 	struct device *dev = ssam_controller_device(ctrl);
2532 	int status;
2533 
2534 	enable_irq(ctrl->irq.num);
2535 	if (device_may_wakeup(dev)) {
2536 		status = enable_irq_wake(ctrl->irq.num);
2537 		if (status) {
2538 			ssam_err(ctrl, "failed to enable wake IRQ: %d\n", status);
2539 			disable_irq(ctrl->irq.num);
2540 			return status;
2541 		}
2542 
2543 		ctrl->irq.wakeup_enabled = true;
2544 	} else {
2545 		ctrl->irq.wakeup_enabled = false;
2546 	}
2547 
2548 	return 0;
2549 }
2550 
2551 /**
2552  * ssam_irq_disarm_wakeup() - Disarm the wakeup IRQ.
2553  * @ctrl: The controller for which the IRQ should be disarmed.
2554  *
2555  * Disarm the IRQ previously set up for wake via ssam_irq_arm_for_wakeup().
2556  *
2557  * This function is intended to disarm the IRQ after exiting S2idle suspend.
2558  *
2559  * Note: calls to ssam_irq_arm_for_wakeup() and ssam_irq_disarm_wakeup() must
2560  * be balanced.
2561  */
2562 void ssam_irq_disarm_wakeup(struct ssam_controller *ctrl)
2563 {
2564 	int status;
2565 
2566 	if (ctrl->irq.wakeup_enabled) {
2567 		status = disable_irq_wake(ctrl->irq.num);
2568 		if (status)
2569 			ssam_err(ctrl, "failed to disable wake IRQ: %d\n", status);
2570 
2571 		ctrl->irq.wakeup_enabled = false;
2572 	}
2573 	disable_irq(ctrl->irq.num);
2574 }
2575