-
Notifications
You must be signed in to change notification settings - Fork 33
/
controller.c
2807 lines (2421 loc) · 84.4 KB
/
controller.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-2.0+
/*
* Main SSAM/SSH controller structure and functionality.
*
* Copyright (C) 2019-2022 Maximilian Luz <luzmaximilian@gmail.com>
*/
#include <linux/acpi.h>
#include <linux/atomic.h>
#include <linux/completion.h>
#include <linux/gpio/consumer.h>
#include <linux/interrupt.h>
#include <linux/kref.h>
#include <linux/limits.h>
#include <linux/list.h>
#include <linux/lockdep.h>
#include <linux/mutex.h>
#include <linux/rculist.h>
#include <linux/rbtree.h>
#include <linux/rwsem.h>
#include <linux/serdev.h>
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/srcu.h>
#include <linux/types.h>
#include <linux/workqueue.h>
#include <linux/surface_aggregator/controller.h>
#include <linux/surface_aggregator/serial_hub.h>
#include "controller.h"
#include "ssh_msgb.h"
#include "ssh_request_layer.h"
#include "trace.h"
/* -- Safe counters. -------------------------------------------------------- */
/**
* ssh_seq_reset() - Reset/initialize sequence ID counter.
* @c: The counter to reset.
*/
static void ssh_seq_reset(struct ssh_seq_counter *c)
{
WRITE_ONCE(c->value, 0);
}
/**
* ssh_seq_next() - Get next sequence ID.
* @c: The counter providing the sequence IDs.
*
* Return: Returns the next sequence ID of the counter.
*/
static u8 ssh_seq_next(struct ssh_seq_counter *c)
{
u8 old = READ_ONCE(c->value);
u8 new = old + 1;
u8 ret;
while (unlikely((ret = cmpxchg(&c->value, old, new)) != old)) {
old = ret;
new = old + 1;
}
return old;
}
/**
* ssh_rqid_reset() - Reset/initialize request ID counter.
* @c: The counter to reset.
*/
static void ssh_rqid_reset(struct ssh_rqid_counter *c)
{
WRITE_ONCE(c->value, 0);
}
/**
* ssh_rqid_next() - Get next request ID.
* @c: The counter providing the request IDs.
*
* Return: Returns the next request ID of the counter, skipping any reserved
* request IDs.
*/
static u16 ssh_rqid_next(struct ssh_rqid_counter *c)
{
u16 old = READ_ONCE(c->value);
u16 new = ssh_rqid_next_valid(old);
u16 ret;
while (unlikely((ret = cmpxchg(&c->value, old, new)) != old)) {
old = ret;
new = ssh_rqid_next_valid(old);
}
return old;
}
/* -- Event notifier/callbacks. --------------------------------------------- */
/*
* The notifier system is based on linux/notifier.h, specifically the SRCU
* implementation. The difference to that is, that some bits of the notifier
* call return value can be tracked across multiple calls. This is done so
* that handling of events can be tracked and a warning can be issued in case
* an event goes unhandled. The idea of that warning is that it should help
* discover and identify new/currently unimplemented features.
*/
/**
* ssam_event_matches_notifier() - Test if an event matches a notifier.
* @n: The event notifier to test against.
* @event: The event to test.
*
* Return: Returns %true if the given event matches the given notifier
* according to the rules set in the notifier's event mask, %false otherwise.
*/
static bool ssam_event_matches_notifier(const struct ssam_event_notifier *n,
const struct ssam_event *event)
{
bool match = n->event.id.target_category == event->target_category;
if (n->event.mask & SSAM_EVENT_MASK_TARGET)
match &= n->event.reg.target_id == event->target_id;
if (n->event.mask & SSAM_EVENT_MASK_INSTANCE)
match &= n->event.id.instance == event->instance_id;
return match;
}
/**
* ssam_nfblk_call_chain() - Call event notifier callbacks of the given chain.
* @nh: The notifier head for which the notifier callbacks should be called.
* @event: The event data provided to the callbacks.
*
* Call all registered notifier callbacks in order of their priority until
* either no notifier is left or a notifier returns a value with the
* %SSAM_NOTIF_STOP bit set. Note that this bit is automatically set via
* ssam_notifier_from_errno() on any non-zero error value.
*
* Return: Returns the notifier status value, which contains the notifier
* status bits (%SSAM_NOTIF_HANDLED and %SSAM_NOTIF_STOP) as well as a
* potential error value returned from the last executed notifier callback.
* Use ssam_notifier_to_errno() to convert this value to the original error
* value.
*/
static int ssam_nfblk_call_chain(struct ssam_nf_head *nh, struct ssam_event *event)
{
struct ssam_event_notifier *nf;
int ret = 0, idx;
idx = srcu_read_lock(&nh->srcu);
list_for_each_entry_rcu(nf, &nh->head, base.node,
srcu_read_lock_held(&nh->srcu)) {
if (ssam_event_matches_notifier(nf, event)) {
ret = (ret & SSAM_NOTIF_STATE_MASK) | nf->base.fn(nf, event);
if (ret & SSAM_NOTIF_STOP)
break;
}
}
srcu_read_unlock(&nh->srcu, idx);
return ret;
}
/**
* ssam_nfblk_insert() - Insert a new notifier block into the given notifier
* list.
* @nh: The notifier head into which the block should be inserted.
* @nb: The notifier block to add.
*
* Note: This function must be synchronized by the caller with respect to other
* insert, find, and/or remove calls by holding ``struct ssam_nf.lock``.
*
* Return: Returns zero on success, %-EEXIST if the notifier block has already
* been registered.
*/
static int ssam_nfblk_insert(struct ssam_nf_head *nh, struct ssam_notifier_block *nb)
{
struct ssam_notifier_block *p;
struct list_head *h;
/* Runs under lock, no need for RCU variant. */
list_for_each(h, &nh->head) {
p = list_entry(h, struct ssam_notifier_block, node);
if (unlikely(p == nb)) {
WARN(1, "double register detected");
return -EEXIST;
}
if (nb->priority > p->priority)
break;
}
list_add_tail_rcu(&nb->node, h);
return 0;
}
/**
* ssam_nfblk_find() - Check if a notifier block is registered on the given
* notifier head.
* list.
* @nh: The notifier head on which to search.
* @nb: The notifier block to search for.
*
* Note: This function must be synchronized by the caller with respect to other
* insert, find, and/or remove calls by holding ``struct ssam_nf.lock``.
*
* Return: Returns true if the given notifier block is registered on the given
* notifier head, false otherwise.
*/
static bool ssam_nfblk_find(struct ssam_nf_head *nh, struct ssam_notifier_block *nb)
{
struct ssam_notifier_block *p;
/* Runs under lock, no need for RCU variant. */
list_for_each_entry(p, &nh->head, node) {
if (p == nb)
return true;
}
return false;
}
/**
* ssam_nfblk_remove() - Remove a notifier block from its notifier list.
* @nb: The notifier block to be removed.
*
* Note: This function must be synchronized by the caller with respect to
* other insert, find, and/or remove calls by holding ``struct ssam_nf.lock``.
* Furthermore, the caller _must_ ensure SRCU synchronization by calling
* synchronize_srcu() with ``nh->srcu`` after leaving the critical section, to
* ensure that the removed notifier block is not in use any more.
*/
static void ssam_nfblk_remove(struct ssam_notifier_block *nb)
{
list_del_rcu(&nb->node);
}
/**
* ssam_nf_head_init() - Initialize the given notifier head.
* @nh: The notifier head to initialize.
*/
static int ssam_nf_head_init(struct ssam_nf_head *nh)
{
int status;
status = init_srcu_struct(&nh->srcu);
if (status)
return status;
INIT_LIST_HEAD(&nh->head);
return 0;
}
/**
* ssam_nf_head_destroy() - Deinitialize the given notifier head.
* @nh: The notifier head to deinitialize.
*/
static void ssam_nf_head_destroy(struct ssam_nf_head *nh)
{
cleanup_srcu_struct(&nh->srcu);
}
/* -- Event/notification registry. ------------------------------------------ */
/**
* struct ssam_nf_refcount_key - Key used for event activation reference
* counting.
* @reg: The registry via which the event is enabled/disabled.
* @id: The ID uniquely describing the event.
*/
struct ssam_nf_refcount_key {
struct ssam_event_registry reg;
struct ssam_event_id id;
};
/**
* struct ssam_nf_refcount_entry - RB-tree entry for reference counting event
* activations.
* @node: The node of this entry in the rb-tree.
* @key: The key of the event.
* @refcount: The reference-count of the event.
* @flags: The flags used when enabling the event.
*/
struct ssam_nf_refcount_entry {
struct rb_node node;
struct ssam_nf_refcount_key key;
int refcount;
u8 flags;
};
/**
* ssam_nf_refcount_inc() - Increment reference-/activation-count of the given
* event.
* @nf: The notifier system reference.
* @reg: The registry used to enable/disable the event.
* @id: The event ID.
*
* Increments the reference-/activation-count associated with the specified
* event type/ID, allocating a new entry for this event ID if necessary. A
* newly allocated entry will have a refcount of one.
*
* Note: ``nf->lock`` must be held when calling this function.
*
* Return: Returns the refcount entry on success. Returns an error pointer
* with %-ENOSPC if there have already been %INT_MAX events of the specified
* ID and type registered, or %-ENOMEM if the entry could not be allocated.
*/
static struct ssam_nf_refcount_entry *
ssam_nf_refcount_inc(struct ssam_nf *nf, struct ssam_event_registry reg,
struct ssam_event_id id)
{
struct ssam_nf_refcount_entry *entry;
struct ssam_nf_refcount_key key;
struct rb_node **link = &nf->refcount.rb_node;
struct rb_node *parent = NULL;
int cmp;
lockdep_assert_held(&nf->lock);
key.reg = reg;
key.id = id;
while (*link) {
entry = rb_entry(*link, struct ssam_nf_refcount_entry, node);
parent = *link;
cmp = memcmp(&key, &entry->key, sizeof(key));
if (cmp < 0) {
link = &(*link)->rb_left;
} else if (cmp > 0) {
link = &(*link)->rb_right;
} else if (entry->refcount < INT_MAX) {
entry->refcount++;
return entry;
} else {
WARN_ON(1);
return ERR_PTR(-ENOSPC);
}
}
entry = kzalloc(sizeof(*entry), GFP_KERNEL);
if (!entry)
return ERR_PTR(-ENOMEM);
entry->key = key;
entry->refcount = 1;
rb_link_node(&entry->node, parent, link);
rb_insert_color(&entry->node, &nf->refcount);
return entry;
}
/**
* ssam_nf_refcount_dec() - Decrement reference-/activation-count of the given
* event.
* @nf: The notifier system reference.
* @reg: The registry used to enable/disable the event.
* @id: The event ID.
*
* Decrements the reference-/activation-count of the specified event,
* returning its entry. If the returned entry has a refcount of zero, the
* caller is responsible for freeing it using kfree().
*
* Note: ``nf->lock`` must be held when calling this function.
*
* Return: Returns the refcount entry on success or %NULL if the entry has not
* been found.
*/
static struct ssam_nf_refcount_entry *
ssam_nf_refcount_dec(struct ssam_nf *nf, struct ssam_event_registry reg,
struct ssam_event_id id)
{
struct ssam_nf_refcount_entry *entry;
struct ssam_nf_refcount_key key;
struct rb_node *node = nf->refcount.rb_node;
int cmp;
lockdep_assert_held(&nf->lock);
key.reg = reg;
key.id = id;
while (node) {
entry = rb_entry(node, struct ssam_nf_refcount_entry, node);
cmp = memcmp(&key, &entry->key, sizeof(key));
if (cmp < 0) {
node = node->rb_left;
} else if (cmp > 0) {
node = node->rb_right;
} else {
entry->refcount--;
if (entry->refcount == 0)
rb_erase(&entry->node, &nf->refcount);
return entry;
}
}
return NULL;
}
/**
* ssam_nf_refcount_dec_free() - Decrement reference-/activation-count of the
* given event and free its entry if the reference count reaches zero.
* @nf: The notifier system reference.
* @reg: The registry used to enable/disable the event.
* @id: The event ID.
*
* Decrements the reference-/activation-count of the specified event, freeing
* its entry if it reaches zero.
*
* Note: ``nf->lock`` must be held when calling this function.
*/
static void ssam_nf_refcount_dec_free(struct ssam_nf *nf,
struct ssam_event_registry reg,
struct ssam_event_id id)
{
struct ssam_nf_refcount_entry *entry;
lockdep_assert_held(&nf->lock);
entry = ssam_nf_refcount_dec(nf, reg, id);
if (entry && entry->refcount == 0)
kfree(entry);
}
/**
* ssam_nf_refcount_empty() - Test if the notification system has any
* enabled/active events.
* @nf: The notification system.
*/
static bool ssam_nf_refcount_empty(struct ssam_nf *nf)
{
return RB_EMPTY_ROOT(&nf->refcount);
}
/**
* ssam_nf_call() - Call notification callbacks for the provided event.
* @nf: The notifier system
* @dev: The associated device, only used for logging.
* @rqid: The request ID of the event.
* @event: The event provided to the callbacks.
*
* Execute registered callbacks in order of their priority until either no
* callback is left or a callback returns a value with the %SSAM_NOTIF_STOP
* bit set. Note that this bit is set automatically when converting non-zero
* error values via ssam_notifier_from_errno() to notifier values.
*
* Also note that any callback that could handle an event should return a value
* with bit %SSAM_NOTIF_HANDLED set, indicating that the event does not go
* unhandled/ignored. In case no registered callback could handle an event,
* this function will emit a warning.
*
* In case a callback failed, this function will emit an error message.
*/
static void ssam_nf_call(struct ssam_nf *nf, struct device *dev, u16 rqid,
struct ssam_event *event)
{
struct ssam_nf_head *nf_head;
int status, nf_ret;
if (!ssh_rqid_is_event(rqid)) {
dev_warn(dev, "event: unsupported rqid: %#06x\n", rqid);
return;
}
nf_head = &nf->head[ssh_rqid_to_event(rqid)];
nf_ret = ssam_nfblk_call_chain(nf_head, event);
status = ssam_notifier_to_errno(nf_ret);
if (status < 0) {
dev_err(dev,
"event: error handling event: %d (tc: %#04x, tid: %#04x, cid: %#04x, iid: %#04x)\n",
status, event->target_category, event->target_id,
event->command_id, event->instance_id);
} else if (!(nf_ret & SSAM_NOTIF_HANDLED)) {
dev_warn(dev,
"event: unhandled event (rqid: %#04x, tc: %#04x, tid: %#04x, cid: %#04x, iid: %#04x)\n",
rqid, event->target_category, event->target_id,
event->command_id, event->instance_id);
}
}
/**
* ssam_nf_init() - Initialize the notifier system.
* @nf: The notifier system to initialize.
*/
static int ssam_nf_init(struct ssam_nf *nf)
{
int i, status;
for (i = 0; i < SSH_NUM_EVENTS; i++) {
status = ssam_nf_head_init(&nf->head[i]);
if (status)
break;
}
if (status) {
while (i--)
ssam_nf_head_destroy(&nf->head[i]);
return status;
}
mutex_init(&nf->lock);
return 0;
}
/**
* ssam_nf_destroy() - Deinitialize the notifier system.
* @nf: The notifier system to deinitialize.
*/
static void ssam_nf_destroy(struct ssam_nf *nf)
{
int i;
for (i = 0; i < SSH_NUM_EVENTS; i++)
ssam_nf_head_destroy(&nf->head[i]);
mutex_destroy(&nf->lock);
}
/* -- Event/async request completion system. -------------------------------- */
#define SSAM_CPLT_WQ_NAME "ssam_cpltq"
/*
* SSAM_CPLT_WQ_BATCH - Maximum number of event item completions executed per
* work execution. Used to prevent livelocking of the workqueue. Value chosen
* via educated guess, may be adjusted.
*/
#define SSAM_CPLT_WQ_BATCH 10
/*
* SSAM_EVENT_ITEM_CACHE_PAYLOAD_LEN - Maximum payload length for a cached
* &struct ssam_event_item.
*
* This length has been chosen to be accommodate standard touchpad and
* keyboard input events. Events with larger payloads will be allocated
* separately.
*/
#define SSAM_EVENT_ITEM_CACHE_PAYLOAD_LEN 32
static struct kmem_cache *ssam_event_item_cache;
/**
* ssam_event_item_cache_init() - Initialize the event item cache.
*/
int ssam_event_item_cache_init(void)
{
const unsigned int size = sizeof(struct ssam_event_item)
+ SSAM_EVENT_ITEM_CACHE_PAYLOAD_LEN;
const unsigned int align = __alignof__(struct ssam_event_item);
struct kmem_cache *cache;
cache = kmem_cache_create("ssam_event_item", size, align, 0, NULL);
if (!cache)
return -ENOMEM;
ssam_event_item_cache = cache;
return 0;
}
/**
* ssam_event_item_cache_destroy() - Deinitialize the event item cache.
*/
void ssam_event_item_cache_destroy(void)
{
kmem_cache_destroy(ssam_event_item_cache);
ssam_event_item_cache = NULL;
}
static void __ssam_event_item_free_cached(struct ssam_event_item *item)
{
kmem_cache_free(ssam_event_item_cache, item);
}
static void __ssam_event_item_free_generic(struct ssam_event_item *item)
{
kfree(item);
}
/**
* ssam_event_item_free() - Free the provided event item.
* @item: The event item to free.
*/
static void ssam_event_item_free(struct ssam_event_item *item)
{
trace_ssam_event_item_free(item);
item->ops.free(item);
}
/**
* ssam_event_item_alloc() - Allocate an event item with the given payload size.
* @len: The event payload length.
* @flags: The flags used for allocation.
*
* Allocate an event item with the given payload size, preferring allocation
* from the event item cache if the payload is small enough (i.e. smaller than
* %SSAM_EVENT_ITEM_CACHE_PAYLOAD_LEN). Sets the item operations and payload
* length values. The item free callback (``ops.free``) should not be
* overwritten after this call.
*
* Return: Returns the newly allocated event item.
*/
static struct ssam_event_item *ssam_event_item_alloc(size_t len, gfp_t flags)
{
struct ssam_event_item *item;
if (len <= SSAM_EVENT_ITEM_CACHE_PAYLOAD_LEN) {
item = kmem_cache_alloc(ssam_event_item_cache, flags);
if (!item)
return NULL;
item->ops.free = __ssam_event_item_free_cached;
} else {
item = kzalloc(struct_size(item, event.data, len), flags);
if (!item)
return NULL;
item->ops.free = __ssam_event_item_free_generic;
}
item->event.length = len;
trace_ssam_event_item_alloc(item, len);
return item;
}
/**
* ssam_event_queue_push() - Push an event item to the event queue.
* @q: The event queue.
* @item: The item to add.
*/
static void ssam_event_queue_push(struct ssam_event_queue *q,
struct ssam_event_item *item)
{
spin_lock(&q->lock);
list_add_tail(&item->node, &q->head);
spin_unlock(&q->lock);
}
/**
* ssam_event_queue_pop() - Pop the next event item from the event queue.
* @q: The event queue.
*
* Returns and removes the next event item from the queue. Returns %NULL If
* there is no event item left.
*/
static struct ssam_event_item *ssam_event_queue_pop(struct ssam_event_queue *q)
{
struct ssam_event_item *item;
spin_lock(&q->lock);
item = list_first_entry_or_null(&q->head, struct ssam_event_item, node);
if (item)
list_del(&item->node);
spin_unlock(&q->lock);
return item;
}
/**
* ssam_event_queue_is_empty() - Check if the event queue is empty.
* @q: The event queue.
*/
static bool ssam_event_queue_is_empty(struct ssam_event_queue *q)
{
bool empty;
spin_lock(&q->lock);
empty = list_empty(&q->head);
spin_unlock(&q->lock);
return empty;
}
/**
* ssam_cplt_get_event_queue() - Get the event queue for the given parameters.
* @cplt: The completion system on which to look for the queue.
* @tid: The target ID of the queue.
* @rqid: The request ID representing the event ID for which to get the queue.
*
* Return: Returns the event queue corresponding to the event type described
* by the given parameters. If the request ID does not represent an event,
* this function returns %NULL. If the target ID is not supported, this
* function will fall back to the default target ID (``tid = 1``).
*/
static
struct ssam_event_queue *ssam_cplt_get_event_queue(struct ssam_cplt *cplt,
u8 tid, u16 rqid)
{
u16 event = ssh_rqid_to_event(rqid);
u16 tidx = ssh_tid_to_index(tid);
if (!ssh_rqid_is_event(rqid)) {
dev_err(cplt->dev, "event: unsupported request ID: %#06x\n", rqid);
return NULL;
}
if (!ssh_tid_is_valid(tid)) {
dev_warn(cplt->dev, "event: unsupported target ID: %u\n", tid);
tidx = 0;
}
return &cplt->event.target[tidx].queue[event];
}
/**
* ssam_cplt_submit() - Submit a work item to the completion system workqueue.
* @cplt: The completion system.
* @work: The work item to submit.
*/
static bool ssam_cplt_submit(struct ssam_cplt *cplt, struct work_struct *work)
{
return queue_work(cplt->wq, work);
}
/**
* ssam_cplt_submit_event() - Submit an event to the completion system.
* @cplt: The completion system.
* @item: The event item to submit.
*
* Submits the event to the completion system by queuing it on the event item
* queue and queuing the respective event queue work item on the completion
* workqueue, which will eventually complete the event.
*
* Return: Returns zero on success, %-EINVAL if there is no event queue that
* can handle the given event item.
*/
static int ssam_cplt_submit_event(struct ssam_cplt *cplt,
struct ssam_event_item *item)
{
struct ssam_event_queue *evq;
evq = ssam_cplt_get_event_queue(cplt, item->event.target_id, item->rqid);
if (!evq)
return -EINVAL;
ssam_event_queue_push(evq, item);
ssam_cplt_submit(cplt, &evq->work);
return 0;
}
/**
* ssam_cplt_flush() - Flush the completion system.
* @cplt: The completion system.
*
* Flush the completion system by waiting until all currently submitted work
* items have been completed.
*
* Note: This function does not guarantee that all events will have been
* handled once this call terminates. In case of a larger number of
* to-be-completed events, the event queue work function may re-schedule its
* work item, which this flush operation will ignore.
*
* This operation is only intended to, during normal operation prior to
* shutdown, try to complete most events and requests to get them out of the
* system while the system is still fully operational. It does not aim to
* provide any guarantee that all of them have been handled.
*/
static void ssam_cplt_flush(struct ssam_cplt *cplt)
{
flush_workqueue(cplt->wq);
}
static void ssam_event_queue_work_fn(struct work_struct *work)
{
struct ssam_event_queue *queue;
struct ssam_event_item *item;
struct ssam_nf *nf;
struct device *dev;
unsigned int iterations = SSAM_CPLT_WQ_BATCH;
queue = container_of(work, struct ssam_event_queue, work);
nf = &queue->cplt->event.notif;
dev = queue->cplt->dev;
/* Limit number of processed events to avoid livelocking. */
do {
item = ssam_event_queue_pop(queue);
if (!item)
return;
ssam_nf_call(nf, dev, item->rqid, &item->event);
ssam_event_item_free(item);
} while (--iterations);
if (!ssam_event_queue_is_empty(queue))
ssam_cplt_submit(queue->cplt, &queue->work);
}
/**
* ssam_event_queue_init() - Initialize an event queue.
* @cplt: The completion system on which the queue resides.
* @evq: The event queue to initialize.
*/
static void ssam_event_queue_init(struct ssam_cplt *cplt,
struct ssam_event_queue *evq)
{
evq->cplt = cplt;
spin_lock_init(&evq->lock);
INIT_LIST_HEAD(&evq->head);
INIT_WORK(&evq->work, ssam_event_queue_work_fn);
}
/**
* ssam_cplt_init() - Initialize completion system.
* @cplt: The completion system to initialize.
* @dev: The device used for logging.
*/
static int ssam_cplt_init(struct ssam_cplt *cplt, struct device *dev)
{
struct ssam_event_target *target;
int status, c, i;
cplt->dev = dev;
cplt->wq = alloc_workqueue(SSAM_CPLT_WQ_NAME, WQ_UNBOUND | WQ_MEM_RECLAIM, 0);
if (!cplt->wq)
return -ENOMEM;
for (c = 0; c < ARRAY_SIZE(cplt->event.target); c++) {
target = &cplt->event.target[c];
for (i = 0; i < ARRAY_SIZE(target->queue); i++)
ssam_event_queue_init(cplt, &target->queue[i]);
}
status = ssam_nf_init(&cplt->event.notif);
if (status)
destroy_workqueue(cplt->wq);
return status;
}
/**
* ssam_cplt_destroy() - Deinitialize the completion system.
* @cplt: The completion system to deinitialize.
*
* Deinitialize the given completion system and ensure that all pending, i.e.
* yet-to-be-completed, event items and requests have been handled.
*/
static void ssam_cplt_destroy(struct ssam_cplt *cplt)
{
/*
* Note: destroy_workqueue ensures that all currently queued work will
* be fully completed and the workqueue drained. This means that this
* call will inherently also free any queued ssam_event_items, thus we
* don't have to take care of that here explicitly.
*/
destroy_workqueue(cplt->wq);
ssam_nf_destroy(&cplt->event.notif);
}
/* -- Main SSAM device structures. ------------------------------------------ */
/**
* ssam_controller_device() - Get the &struct device associated with this
* controller.
* @c: The controller for which to get the device.
*
* Return: Returns the &struct device associated with this controller,
* providing its lower-level transport.
*/
struct device *ssam_controller_device(struct ssam_controller *c)
{
return ssh_rtl_get_device(&c->rtl);
}
EXPORT_SYMBOL_GPL(ssam_controller_device);
static void __ssam_controller_release(struct kref *kref)
{
struct ssam_controller *ctrl = to_ssam_controller(kref, kref);
/*
* The lock-call here is to satisfy lockdep. At this point we really
* expect this to be the last remaining reference to the controller.
* Anything else is a bug.
*/
ssam_controller_lock(ctrl);
ssam_controller_destroy(ctrl);
ssam_controller_unlock(ctrl);
kfree(ctrl);
}
/**
* ssam_controller_get() - Increment reference count of controller.
* @c: The controller.
*
* Return: Returns the controller provided as input.
*/
struct ssam_controller *ssam_controller_get(struct ssam_controller *c)
{
if (c)
kref_get(&c->kref);
return c;
}
EXPORT_SYMBOL_GPL(ssam_controller_get);
/**
* ssam_controller_put() - Decrement reference count of controller.
* @c: The controller.
*/
void ssam_controller_put(struct ssam_controller *c)
{
if (c)
kref_put(&c->kref, __ssam_controller_release);
}
EXPORT_SYMBOL_GPL(ssam_controller_put);
/**
* ssam_controller_statelock() - Lock the controller against state transitions.
* @c: The controller to lock.
*
* Lock the controller against state transitions. Holding this lock guarantees
* that the controller will not transition between states, i.e. if the
* controller is in state "started", when this lock has been acquired, it will
* remain in this state at least until the lock has been released.
*
* Multiple clients may concurrently hold this lock. In other words: The
* ``statelock`` functions represent the read-lock part of a r/w-semaphore.
* Actions causing state transitions of the controller must be executed while
* holding the write-part of this r/w-semaphore (see ssam_controller_lock()
* and ssam_controller_unlock() for that).
*
* See ssam_controller_stateunlock() for the corresponding unlock function.
*/
void ssam_controller_statelock(struct ssam_controller *c)
{
down_read(&c->lock);
}
EXPORT_SYMBOL_GPL(ssam_controller_statelock);
/**
* ssam_controller_stateunlock() - Unlock controller state transitions.
* @c: The controller to unlock.
*
* See ssam_controller_statelock() for the corresponding lock function.
*/
void ssam_controller_stateunlock(struct ssam_controller *c)
{
up_read(&c->lock);
}
EXPORT_SYMBOL_GPL(ssam_controller_stateunlock);
/**
* ssam_controller_lock() - Acquire the main controller lock.
* @c: The controller to lock.
*
* This lock must be held for any state transitions, including transition to
* suspend/resumed states and during shutdown. See ssam_controller_statelock()
* for more details on controller locking.
*
* See ssam_controller_unlock() for the corresponding unlock function.
*/
void ssam_controller_lock(struct ssam_controller *c)
{
down_write(&c->lock);
}
/*
* ssam_controller_unlock() - Release the main controller lock.
* @c: The controller to unlock.
*
* See ssam_controller_lock() for the corresponding lock function.
*/
void ssam_controller_unlock(struct ssam_controller *c)
{
up_write(&c->lock);
}
static void ssam_handle_event(struct ssh_rtl *rtl,
const struct ssh_command *cmd,
const struct ssam_span *data)
{
struct ssam_controller *ctrl = to_ssam_controller(rtl, rtl);
struct ssam_event_item *item;
item = ssam_event_item_alloc(data->len, GFP_KERNEL);
if (!item)
return;
item->rqid = get_unaligned_le16(&cmd->rqid);
item->event.target_category = cmd->tc;
item->event.target_id = cmd->sid;
item->event.command_id = cmd->cid;
item->event.instance_id = cmd->iid;
memcpy(&item->event.data[0], data->ptr, data->len);