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