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
|
/* Copyright 2018 The Chromium OS Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "atomic.h"
#include "compile_time_macros.h"
#include "console.h"
#include "hbm.h"
#include "heci_client.h"
#include "ipc_heci.h"
#include "system_state.h"
#include "task.h"
#include "timer.h"
#include "util.h"
#define CPUTS(outstr) cputs(CC_LPC, outstr)
#define CPRINTS(format, args...) cprints(CC_LPC, format, ## args)
#define CPRINTF(format, args...) cprintf(CC_LPC, format, ## args)
struct heci_header {
uint8_t fw_addr;
uint8_t host_addr;
uint16_t length; /* [8:0] length, [14:9] reserved, [15] msg_complete */
} __packed;
#define HECI_MSG_CMPL_SHIFT 15
#define HECI_MSG_LENGTH_MASK 0x01FF
#define HECI_MSG_LENGTH(length) ((length) & HECI_MSG_LENGTH_MASK)
#define HECI_MSG_IS_COMPLETED(length) \
(!!((length) & (0x01 << HECI_MSG_CMPL_SHIFT)))
BUILD_ASSERT(HECI_IPC_PAYLOAD_SIZE ==
(IPC_MAX_PAYLOAD_SIZE - sizeof(struct heci_header)));
struct heci_msg {
struct heci_header hdr;
uint8_t payload[HECI_IPC_PAYLOAD_SIZE];
} __packed;
/* address for Host Bus */
#define HECI_HBM_ADDRESS 0
/* should be less than HECI_INVALID_HANDLE - 1 */
BUILD_ASSERT(HECI_MAX_NUM_OF_CLIENTS < 0x0FE);
struct heci_client_connect {
uint8_t is_connected; /* client is connected to host */
uint8_t host_addr; /* connected host address */
/* receiving message */
uint8_t ignore_rx_msg;
uint8_t rx_msg[HECI_MAX_MSG_SIZE];
size_t rx_msg_length;
uint32_t flow_ctrl_creds; /* flow control */
struct mutex lock; /* protects against 2 writers */
struct mutex cred_lock; /* protects flow ctrl */
int waiting_task;
};
struct heci_client_context {
const struct heci_client *client;
void *data; /* client specific data */
struct heci_client_connect connect; /* connection context */
struct ss_subsys_device ss_device; /* system state receiver device */
};
struct heci_bus_context {
ipc_handle_t ipc_handle; /* ipc handle for heci protocol */
int num_of_clients;
struct heci_client_context client_ctxs[HECI_MAX_NUM_OF_CLIENTS];
};
/* declare heci bus */
struct heci_bus_context heci_bus_ctx = {
.ipc_handle = IPC_INVALID_HANDLE,
};
/* host generates client fw address starting from 1 */
static inline struct heci_client_context *
heci_get_client_context(const uint8_t fw_addr)
{
return &heci_bus_ctx.client_ctxs[fw_addr - 1];
}
static inline struct heci_client_connect *
heci_get_client_connect(const uint8_t fw_addr)
{
struct heci_client_context *cli_ctx = heci_get_client_context(fw_addr);
return &cli_ctx->connect;
}
static inline int heci_is_client_connected(const uint8_t fw_addr)
{
struct heci_client_context *cli_ctx = heci_get_client_context(fw_addr);
return cli_ctx->connect.is_connected;
}
static inline int heci_is_valid_client_addr(const uint8_t fw_addr)
{
return fw_addr > 0 && fw_addr <= heci_bus_ctx.num_of_clients;
}
static inline int heci_is_valid_handle(const heci_handle_t handle)
{
return heci_is_valid_client_addr((uintptr_t)(handle));
}
#define TO_FW_ADDR(handle) ((uintptr_t)(handle))
#define TO_HECI_HANDLE(fw_addr) ((heci_handle_t)(uintptr_t)(fw_addr))
/* find heci device that contains this system state device in it */
#define ss_device_to_heci_client_context(ss_dev) \
((struct heci_client_context *)((void *)(ss_dev) - \
(void *)(&(((struct heci_client_context *)0)->ss_device))))
#define client_context_to_handle(cli_ctx) \
((heci_handle_t)((uint32_t)((cli_ctx) - &heci_bus_ctx.client_ctxs[0]) \
/ sizeof(heci_bus_ctx.client_ctxs[0]) + 1))
/*
* each heci device registered as system state device which gets
* system state(e.g. suspend/resume, portrait/landscape) events
* through system state subsystem from host
*/
static int heci_client_suspend(struct ss_subsys_device *ss_device)
{
struct heci_client_context *cli_ctx =
ss_device_to_heci_client_context(ss_device);
heci_handle_t handle = client_context_to_handle(cli_ctx);
if (cli_ctx->client->cbs->suspend)
cli_ctx->client->cbs->suspend(handle);
return EC_SUCCESS;
}
static int heci_client_resume(struct ss_subsys_device *ss_device)
{
struct heci_client_context *cli_ctx =
ss_device_to_heci_client_context(ss_device);
heci_handle_t handle = client_context_to_handle(cli_ctx);
if (cli_ctx->client->cbs->resume)
cli_ctx->client->cbs->resume(handle);
return EC_SUCCESS;
}
struct system_state_callbacks heci_ss_cbs = {
.suspend = heci_client_suspend,
.resume = heci_client_resume,
};
/*
* This function should be called only by HECI_CLIENT_ENTRY()
*/
heci_handle_t heci_register_client(const struct heci_client *client)
{
int ret;
struct heci_client_context *cli_ctx;
if (client == NULL || client->cbs == NULL)
return HECI_INVALID_HANDLE;
/*
* we don't need mutex here since this function is called by
* entry function which is serialized among heci clients.
*/
if (heci_bus_ctx.num_of_clients >= HECI_MAX_NUM_OF_CLIENTS)
return HECI_INVALID_HANDLE;
/* we only support 1 connection */
if (client->max_n_of_connections > 1)
return HECI_INVALID_HANDLE;
if (client->max_msg_size > HECI_MAX_MSG_SIZE)
return HECI_INVALID_HANDLE;
cli_ctx = &heci_bus_ctx.client_ctxs[heci_bus_ctx.num_of_clients++];
cli_ctx->client = client;
if (client->cbs->initialize) {
ret = client->cbs->initialize(
(heci_handle_t)heci_bus_ctx.num_of_clients);
if (ret) {
heci_bus_ctx.num_of_clients--;
return HECI_INVALID_HANDLE;
}
}
if (client->cbs->suspend || client->cbs->resume) {
cli_ctx->ss_device.cbs = &heci_ss_cbs;
ss_subsys_register_client(&cli_ctx->ss_device);
}
return (heci_handle_t)heci_bus_ctx.num_of_clients;
}
static void heci_build_hbm_header(struct heci_header *hdr, uint32_t length)
{
hdr->fw_addr = HECI_HBM_ADDRESS;
hdr->host_addr = HECI_HBM_ADDRESS;
hdr->length = length;
/* payload of hbm is less than IPC payload */
hdr->length |= (uint16_t)1 << HECI_MSG_CMPL_SHIFT;
}
static void heci_build_fixed_client_header(struct heci_header *hdr,
const uint8_t fw_addr,
const uint32_t length)
{
hdr->fw_addr = fw_addr;
hdr->host_addr = 0;
hdr->length = length;
/* Fixed client payload < IPC payload */
hdr->length |= (uint16_t)1 << HECI_MSG_CMPL_SHIFT;
}
static int heci_send_heci_msg_timestamp(struct heci_msg *msg,
uint32_t *timestamp)
{
int length, written;
if (heci_bus_ctx.ipc_handle == IPC_INVALID_HANDLE)
return -1;
length = sizeof(msg->hdr) + HECI_MSG_LENGTH(msg->hdr.length);
written = ipc_write_timestamp(heci_bus_ctx.ipc_handle, msg, length,
timestamp);
if (written != length) {
CPRINTF("%s error : len = %d err = %d\n", __func__,
(int)length, written);
return -EC_ERROR_UNKNOWN;
}
return EC_SUCCESS;
}
static int heci_send_heci_msg(struct heci_msg *msg)
{
return heci_send_heci_msg_timestamp(msg, NULL);
}
int heci_set_client_data(const heci_handle_t handle, void *data)
{
struct heci_client_context *cli_ctx;
const uint8_t fw_addr = TO_FW_ADDR(handle);
if (!heci_is_valid_handle(handle))
return -EC_ERROR_INVAL;
cli_ctx = heci_get_client_context(fw_addr);
cli_ctx->data = data;
return EC_SUCCESS;
}
void *heci_get_client_data(const heci_handle_t handle)
{
struct heci_client_context *cli_ctx;
const uint8_t fw_addr = TO_FW_ADDR(handle);
if (!heci_is_valid_handle(handle))
return NULL;
cli_ctx = heci_get_client_context(fw_addr);
return cli_ctx->data;
}
/*
* Waits for flow control credit that allows TX transactions
*
* Returns true if credit was acquired, otherwise false
*/
static int wait_for_flow_ctrl_cred(struct heci_client_connect *connect)
{
int need_to_wait;
do {
mutex_lock(&connect->cred_lock);
need_to_wait = !connect->flow_ctrl_creds;
if (need_to_wait) {
connect->waiting_task = task_get_current();
} else {
connect->flow_ctrl_creds = 0;
connect->waiting_task = 0;
}
mutex_unlock(&connect->cred_lock);
if (need_to_wait) {
/*
* A second is more than enough, otherwise if will
* probably never happen.
*/
int ev = task_wait_event_mask(TASK_EVENT_IPC_READY,
SECOND);
if (ev & TASK_EVENT_TIMER) {
/* Return false, not able to get credit */
return 0;
}
}
} while (need_to_wait);
/* We successfully got flow control credit */
return 1;
}
int heci_send_msg_timestamp(const heci_handle_t handle, uint8_t *buf,
const size_t buf_size, uint32_t *timestamp)
{
int buf_offset = 0, ret = 0, remain, payload_size;
struct heci_client_connect *connect;
struct heci_msg msg;
const uint8_t fw_addr = TO_FW_ADDR(handle);
if (!heci_is_valid_handle(handle))
return -EC_ERROR_INVAL;
if (buf_size > HECI_MAX_MSG_SIZE)
return -EC_ERROR_OVERFLOW;
connect = heci_get_client_connect(fw_addr);
mutex_lock(&connect->lock);
if (!heci_is_client_connected(fw_addr)) {
ret = -HECI_ERR_CLIENT_IS_NOT_CONNECTED;
goto err_locked;
}
if (!wait_for_flow_ctrl_cred(connect)) {
CPRINTF("no cred\n");
ret = -HECI_ERR_NO_CRED_FROM_CLIENT_IN_HOST;
goto err_locked;
}
msg.hdr.fw_addr = fw_addr;
msg.hdr.host_addr = connect->host_addr;
remain = buf_size;
while (remain) {
if (remain > HECI_IPC_PAYLOAD_SIZE) {
msg.hdr.length = HECI_IPC_PAYLOAD_SIZE;
payload_size = HECI_IPC_PAYLOAD_SIZE;
} else {
msg.hdr.length = remain;
/* set as last heci msg */
msg.hdr.length |= (uint16_t)1 << HECI_MSG_CMPL_SHIFT;
payload_size = remain;
}
memcpy(msg.payload, buf + buf_offset, payload_size);
heci_send_heci_msg_timestamp(&msg, timestamp);
remain -= payload_size;
buf_offset += payload_size;
}
mutex_unlock(&connect->lock);
return buf_size;
err_locked:
mutex_unlock(&connect->lock);
return ret;
}
int heci_send_msg(const heci_handle_t handle, uint8_t *buf,
const size_t buf_size)
{
return heci_send_msg_timestamp(handle, buf, buf_size, NULL);
}
int heci_send_msgs(const heci_handle_t handle,
const struct heci_msg_list *msg_list)
{
struct heci_msg_item *cur_item;
int total_size = 0;
int i, msg_cur_pos, buf_size, copy_size, msg_sent;
struct heci_client_connect *connect;
struct heci_msg msg;
const uint8_t fw_addr = TO_FW_ADDR(handle);
if (!heci_is_valid_handle(handle))
return -EC_ERROR_INVAL;
for (i = 0; i < msg_list->num_of_items; i++) {
if (!msg_list->items[i]->size || !msg_list->items[i]->buf)
return -EC_ERROR_INVAL;
total_size += msg_list->items[i]->size;
}
if (total_size > HECI_MAX_MSG_SIZE)
return -EC_ERROR_OVERFLOW;
if (msg_list->num_of_items > HECI_MAX_MSGS)
return -HECI_ERR_TOO_MANY_MSG_ITEMS;
connect = heci_get_client_connect(fw_addr);
mutex_lock(&connect->lock);
if (!heci_is_client_connected(fw_addr)) {
total_size = -HECI_ERR_CLIENT_IS_NOT_CONNECTED;
goto err_locked;
}
if (!wait_for_flow_ctrl_cred(connect)) {
CPRINTF("no cred\n");
total_size = -HECI_ERR_NO_CRED_FROM_CLIENT_IN_HOST;
goto err_locked;
}
msg.hdr.fw_addr = fw_addr;
msg.hdr.host_addr = connect->host_addr;
i = 1;
msg_cur_pos = 0;
buf_size = 0;
cur_item = msg_list->items[0];
msg_sent = 0;
while (1) {
/* get next item if current item is consumed */
if (msg_cur_pos == cur_item->size) {
/*
* break if no more item.
* if "msg" contains data to be sent
* it will be sent after break.
*/
if (i == msg_list->num_of_items)
break;
/* get next item and reset msg_cur_pos */
cur_item = msg_list->items[i++];
msg_cur_pos = 0;
}
/* send data in ipc buf if it's completely filled */
if (buf_size == HECI_IPC_PAYLOAD_SIZE) {
msg.hdr.length = buf_size;
msg_sent += buf_size;
/* no leftovers, send the last msg here */
if (msg_sent == total_size) {
msg.hdr.length |=
(uint16_t)1 << HECI_MSG_CMPL_SHIFT;
}
heci_send_heci_msg(&msg);
buf_size = 0;
}
/* fill ipc msg buffer */
if (cur_item->size - msg_cur_pos >
HECI_IPC_PAYLOAD_SIZE - buf_size) {
copy_size = HECI_IPC_PAYLOAD_SIZE - buf_size;
} else {
copy_size = cur_item->size - msg_cur_pos;
}
memcpy(msg.payload + buf_size, cur_item->buf + msg_cur_pos,
copy_size);
msg_cur_pos += copy_size;
buf_size += copy_size;
}
/* leftovers ? send last msg */
if (buf_size != 0) {
msg.hdr.length = buf_size;
msg.hdr.length |= (uint16_t)1 << HECI_MSG_CMPL_SHIFT;
heci_send_heci_msg(&msg);
}
err_locked:
mutex_unlock(&connect->lock);
return total_size;
}
/* For now, we only support fixed client payload size < IPC payload size */
int heci_send_fixed_client_msg(const uint8_t fw_addr, uint8_t *buf,
const size_t buf_size)
{
struct heci_msg msg;
heci_build_fixed_client_header(&msg.hdr, fw_addr, buf_size);
memcpy(msg.payload, buf, buf_size);
heci_send_heci_msg(&msg);
return EC_SUCCESS;
}
static int handle_version_req(struct hbm_version_req *ver_req)
{
struct hbm_version_res *ver_res;
struct heci_msg heci_msg;
struct hbm_i2h *i2h;
heci_build_hbm_header(&heci_msg.hdr,
sizeof(i2h->cmd) + sizeof(*ver_res));
i2h = (struct hbm_i2h *)heci_msg.payload;
i2h->cmd = HECI_BUS_MSG_VERSION_RESP;
ver_res = (struct hbm_version_res *)&i2h->data;
memset(ver_res, 0, sizeof(*ver_res));
ver_res->version.major = HBM_MAJOR_VERSION;
ver_res->version.minor = HBM_MINOR_VERSION;
if (ver_req->version.major == HBM_MAJOR_VERSION &&
ver_req->version.minor == HBM_MINOR_VERSION) {
ver_res->supported = 1;
} else {
ver_res->supported = 0;
}
heci_send_heci_msg(&heci_msg);
return EC_SUCCESS;
}
#define BITS_PER_BYTE 8
/* get number of bits for one element of "valid_addresses" array */
#define BITS_PER_ELEMENT \
(sizeof(((struct hbm_enum_res *)0)->valid_addresses[0]) * BITS_PER_BYTE)
static int handle_enum_req(struct hbm_enum_req *enum_req)
{
struct hbm_enum_res *enum_res;
struct heci_msg heci_msg;
struct hbm_i2h *i2h;
int i;
heci_build_hbm_header(&heci_msg.hdr,
sizeof(i2h->cmd) + sizeof(*enum_res));
i2h = (struct hbm_i2h *)heci_msg.payload;
i2h->cmd = HECI_BUS_MSG_HOST_ENUM_RESP;
enum_res = (struct hbm_enum_res *)&i2h->data;
memset(enum_res, 0, sizeof(*enum_res));
/*
* current host driver implementation doesn't allow to use bit0
* due to its fw address generation scheme.
* bit-0 set -> fw address "0", bit-1 set -> fw address "1"
* but fw address "0" is used for HECI bus management only.
* so the bitmap should not use bit-0
*/
for (i = 0; i < heci_bus_ctx.num_of_clients; i++) {
enum_res->valid_addresses[(i + 1) / BITS_PER_ELEMENT] |=
1 << ((i + 1) & (BITS_PER_ELEMENT - 1));
}
heci_send_heci_msg(&heci_msg);
return EC_SUCCESS;
}
static int handle_client_prop_req(struct hbm_client_prop_req *client_prop_req)
{
struct hbm_client_prop_res *client_prop_res;
struct heci_msg heci_msg;
struct hbm_i2h *i2h;
struct heci_client_context *client_ctx;
const struct heci_client *client;
heci_build_hbm_header(&heci_msg.hdr,
sizeof(i2h->cmd) + sizeof(*client_prop_res));
i2h = (struct hbm_i2h *)heci_msg.payload;
i2h->cmd = HECI_BUS_MSG_HOST_CLIENT_PROP_RESP;
client_prop_res = (struct hbm_client_prop_res *)&i2h->data;
memset(client_prop_res, 0, sizeof(*client_prop_res));
client_prop_res->address = client_prop_req->address;
if (!heci_is_valid_client_addr(client_prop_req->address)) {
client_prop_res->status = HECI_CONNECT_STATUS_CLIENT_NOT_FOUND;
} else {
struct hbm_client_properties *client_prop;
client_ctx = heci_get_client_context(client_prop_req->address);
client = client_ctx->client;
client_prop = &client_prop_res->client_prop;
client_prop->protocol_name = client->protocol_id;
client_prop->protocol_version = client->protocol_ver;
client_prop->max_number_of_connections =
client->max_n_of_connections;
client_prop->max_msg_length = client->max_msg_size;
client_prop->dma_hdr_len = client->dma_header_length;
client_prop->dma_hdr_len |= client->dma_enabled ?
CLIENT_DMA_ENABLE : 0;
}
heci_send_heci_msg(&heci_msg);
return EC_SUCCESS;
}
static int heci_send_flow_control(uint8_t fw_addr)
{
struct heci_client_connect *connect;
struct hbm_i2h *i2h;
struct hbm_flow_control *flow_ctrl;
struct heci_msg heci_msg;
connect = heci_get_client_connect(fw_addr);
heci_build_hbm_header(&heci_msg.hdr,
sizeof(i2h->cmd) + sizeof(*flow_ctrl));
i2h = (struct hbm_i2h *)heci_msg.payload;
i2h->cmd = HECI_BUS_MSG_FLOW_CONTROL;
flow_ctrl = (struct hbm_flow_control *)&i2h->data;
memset(flow_ctrl, 0, sizeof(*flow_ctrl));
flow_ctrl->fw_addr = fw_addr;
flow_ctrl->host_addr = connect->host_addr;
heci_send_heci_msg(&heci_msg);
return EC_SUCCESS;
}
static int handle_client_connect_req(
struct hbm_client_connect_req *client_connect_req)
{
struct hbm_client_connect_res *client_connect_res;
struct heci_msg heci_msg;
struct hbm_i2h *i2h;
struct heci_client_connect *connect;
heci_build_hbm_header(&heci_msg.hdr,
sizeof(i2h->cmd) + sizeof(*client_connect_res));
i2h = (struct hbm_i2h *)heci_msg.payload;
i2h->cmd = HECI_BUS_MSG_CLIENT_CONNECT_RESP;
client_connect_res = (struct hbm_client_connect_res *)&i2h->data;
memset(client_connect_res, 0, sizeof(*client_connect_res));
client_connect_res->fw_addr = client_connect_req->fw_addr;
client_connect_res->host_addr = client_connect_req->host_addr;
if (!heci_is_valid_client_addr(client_connect_req->fw_addr)) {
client_connect_res->status =
HECI_CONNECT_STATUS_CLIENT_NOT_FOUND;
} else {
connect = heci_get_client_connect(client_connect_req->fw_addr);
if (connect->is_connected) {
client_connect_res->status =
HECI_CONNECT_STATUS_ALREADY_EXISTS;
} else {
connect->is_connected = 1;
connect->host_addr = client_connect_req->host_addr;
}
}
heci_send_heci_msg(&heci_msg);
heci_send_flow_control(client_connect_req->fw_addr);
return EC_SUCCESS;
}
static int handle_flow_control_cmd(struct hbm_flow_control *flow_ctrl)
{
struct heci_client_connect *connect;
int waiting_task;
if (!heci_is_valid_client_addr(flow_ctrl->fw_addr))
return -1;
if (!heci_is_client_connected(flow_ctrl->fw_addr))
return -1;
connect = heci_get_client_connect(flow_ctrl->fw_addr);
mutex_lock(&connect->cred_lock);
connect->flow_ctrl_creds = 1;
waiting_task = connect->waiting_task;
mutex_unlock(&connect->cred_lock);
if (waiting_task)
task_set_event(waiting_task, TASK_EVENT_IPC_READY, 0);
return EC_SUCCESS;
}
static void heci_handle_client_msg(struct heci_msg *msg, size_t length)
{
struct heci_client_context *cli_ctx;
struct heci_client_connect *connect;
const struct heci_client_callbacks *cbs;
int payload_size;
if (!heci_is_valid_client_addr(msg->hdr.fw_addr))
return;
if (!heci_is_client_connected(msg->hdr.fw_addr))
return;
cli_ctx = heci_get_client_context(msg->hdr.fw_addr);
cbs = cli_ctx->client->cbs;
connect = &cli_ctx->connect;
payload_size = HECI_MSG_LENGTH(msg->hdr.length);
if (connect->is_connected &&
msg->hdr.host_addr == connect->host_addr) {
if (!connect->ignore_rx_msg &&
connect->rx_msg_length + payload_size > HECI_MAX_MSG_SIZE) {
connect->ignore_rx_msg = 1; /* too big. discard */
}
if (!connect->ignore_rx_msg) {
memcpy(connect->rx_msg + connect->rx_msg_length,
msg->payload, payload_size);
connect->rx_msg_length += payload_size;
}
if (HECI_MSG_IS_COMPLETED(msg->hdr.length)) {
if (!connect->ignore_rx_msg) {
cbs->new_msg_received(
TO_HECI_HANDLE(msg->hdr.fw_addr),
connect->rx_msg,
connect->rx_msg_length);
}
connect->rx_msg_length = 0;
connect->ignore_rx_msg = 0;
heci_send_flow_control(msg->hdr.fw_addr);
}
}
}
static int handle_client_disconnect_req(
struct hbm_client_disconnect_req *client_disconnect_req)
{
struct hbm_client_disconnect_res *client_disconnect_res;
struct heci_msg heci_msg;
struct hbm_i2h *i2h;
struct heci_client_context *cli_ctx;
struct heci_client_connect *connect;
const struct heci_client_callbacks *cbs;
uint8_t fw_addr;
CPRINTS("Got HECI disconnect request");
heci_build_hbm_header(&heci_msg.hdr, sizeof(i2h->cmd) +
sizeof(*client_disconnect_res));
i2h = (struct hbm_i2h *)heci_msg.payload;
i2h->cmd = HECI_BUS_MSG_CLIENT_DISCONNECT_RESP;
client_disconnect_res = (struct hbm_client_disconnect_res *)&i2h->data;
memset(client_disconnect_res, 0, sizeof(*client_disconnect_res));
fw_addr = client_disconnect_req->fw_addr;
client_disconnect_res->fw_addr = fw_addr;
client_disconnect_res->host_addr = client_disconnect_req->host_addr;
if (!heci_is_valid_client_addr(fw_addr) ||
!heci_is_client_connected(fw_addr)) {
client_disconnect_res->status =
HECI_CONNECT_STATUS_CLIENT_NOT_FOUND;
} else {
connect = heci_get_client_connect(fw_addr);
cli_ctx = heci_get_client_context(fw_addr);
cbs = cli_ctx->client->cbs;
mutex_lock(&connect->lock);
if (connect->is_connected) {
cbs->disconnected(TO_HECI_HANDLE(fw_addr));
connect->is_connected = 0;
}
mutex_unlock(&connect->lock);
}
heci_send_heci_msg(&heci_msg);
return EC_SUCCESS;
}
/* host stops due to version mismatch */
static int handle_host_stop_req(struct hbm_host_stop_req *host_stop_req)
{
struct hbm_host_stop_res *host_stop_res;
struct heci_msg heci_msg;
struct hbm_i2h *i2h;
heci_build_hbm_header(&heci_msg.hdr,
sizeof(i2h->cmd) + sizeof(*host_stop_res));
i2h = (struct hbm_i2h *)heci_msg.payload;
i2h->cmd = HECI_BUS_MSG_HOST_STOP_RESP;
host_stop_res = (struct hbm_host_stop_res *)&i2h->data;
memset(host_stop_res, 0, sizeof(*host_stop_res));
heci_send_heci_msg(&heci_msg);
return EC_SUCCESS;
}
static int is_hbm_validity(struct hbm_h2i *h2i, size_t length)
{
int valid_msg_len;
valid_msg_len = sizeof(h2i->cmd);
switch (h2i->cmd) {
case HECI_BUS_MSG_VERSION_REQ:
valid_msg_len += sizeof(struct hbm_version_req);
break;
case HECI_BUS_MSG_HOST_ENUM_REQ:
valid_msg_len += sizeof(struct hbm_enum_req);
break;
case HECI_BUS_MSG_HOST_CLIENT_PROP_REQ:
valid_msg_len += sizeof(struct hbm_client_prop_req);
break;
case HECI_BUS_MSG_CLIENT_CONNECT_REQ:
valid_msg_len += sizeof(struct hbm_client_connect_req);
break;
case HECI_BUS_MSG_FLOW_CONTROL:
valid_msg_len += sizeof(struct hbm_flow_control);
break;
case HECI_BUS_MSG_CLIENT_DISCONNECT_REQ:
valid_msg_len += sizeof(struct hbm_client_disconnect_req);
break;
case HECI_BUS_MSG_HOST_STOP_REQ:
valid_msg_len += sizeof(struct hbm_host_stop_req);
break;
/* TODO: DMA support for large data */
#if 0
case HECI_BUS_MSG_DMA_REQ:
valid_msg_len += sizeof(struct hbm_dma_req);
break;
case HECI_BUS_MSG_DMA_ALLOC_NOTIFY:
valid_msg_len += sizeof(struct hbm_dma_alloc_notify);
break;
case HECI_BUS_MSG_DMA_XFER_REQ: /* DMA transfer to FW */
valid_msg_len += sizeof(struct hbm_dma_xfer_req);
break;
case HECI_BUS_MSG_DMA_XFER_RESP: /* Ack for DMA transfer from FW */
valid_msg_len += sizeof(struct hbm_dma_xfer_resp);
break;
#endif
default:
break;
}
if (valid_msg_len != length) {
CPRINTF("invalid cmd(%d) valid : %d, cur : %d\n",
valid_msg_len, length);
/* TODO: invalid cmd. not sure to reply with error ? */
return 0;
}
return 1;
}
static void heci_handle_hbm(struct hbm_h2i *h2i, size_t length)
{
void *data = (void *)&h2i->data;
if (!is_hbm_validity(h2i, length))
return;
switch (h2i->cmd) {
case HECI_BUS_MSG_VERSION_REQ:
handle_version_req((struct hbm_version_req *)data);
break;
case HECI_BUS_MSG_HOST_ENUM_REQ:
handle_enum_req((struct hbm_enum_req *)data);
break;
case HECI_BUS_MSG_HOST_CLIENT_PROP_REQ:
handle_client_prop_req((struct hbm_client_prop_req *)data);
break;
case HECI_BUS_MSG_CLIENT_CONNECT_REQ:
handle_client_connect_req(
(struct hbm_client_connect_req *)data);
break;
case HECI_BUS_MSG_FLOW_CONTROL:
handle_flow_control_cmd((struct hbm_flow_control *)data);
break;
case HECI_BUS_MSG_CLIENT_DISCONNECT_REQ:
handle_client_disconnect_req(
(struct hbm_client_disconnect_req *)data);
break;
case HECI_BUS_MSG_HOST_STOP_REQ:
handle_host_stop_req((struct hbm_host_stop_req *)data);
break;
/* TODO: DMA transfer if data is too big >= ? KB */
#if 0
case HECI_BUS_MSG_DMA_REQ:
handle_dma_req((struct hbm_dma_req *)data);
break;
case HECI_BUS_MSG_DMA_ALLOC_NOTIFY:
handle_dma_alloc_notify((struct hbm_dma_alloc_notify *));
break;
case HECI_BUS_MSG_DMA_XFER_REQ: /* DMA transfer to FW */
handle_dma_xfer_req((struct hbm_dma_xfer_req *)data);
break;
case HECI_BUS_MSG_DMA_XFER_RESP: /* Ack for DMA transfer from FW */
handle_dma_xfer_resp((struct hbm_dma_xfer_resp *)data);
break;
#endif
default:
break;
}
}
static void heci_handle_heci_msg(struct heci_msg *heci_msg, size_t msg_length)
{
if (heci_msg->hdr.fw_addr) {
if (heci_msg->hdr.fw_addr == HECI_FIXED_SYSTEM_STATE_ADDR)
heci_handle_system_state_msg(
heci_msg->payload,
HECI_MSG_LENGTH(heci_msg->hdr.length)
);
else
heci_handle_client_msg(heci_msg, msg_length);
} else {
heci_handle_hbm((struct hbm_h2i *)heci_msg->payload,
HECI_MSG_LENGTH(heci_msg->hdr.length));
}
}
/* event flag for HECI msg */
#define EVENT_FLAG_BIT_HECI_MSG TASK_EVENT_CUSTOM(1)
void heci_rx_task(void)
{
int msg_len;
struct heci_msg heci_msg;
ipc_handle_t ipc_handle;
/* open IPC for HECI protocol */
heci_bus_ctx.ipc_handle = ipc_open(IPC_PEER_ID_HOST, IPC_PROTOCOL_HECI,
EVENT_FLAG_BIT_HECI_MSG);
ASSERT(heci_bus_ctx.ipc_handle != IPC_INVALID_HANDLE);
/* get ipc handle */
ipc_handle = heci_bus_ctx.ipc_handle;
while (1) {
/* task will be blocked here, waiting for event */
msg_len = ipc_read(ipc_handle, &heci_msg, sizeof(heci_msg), -1);
if (msg_len <= 0) {
CPRINTS("discard heci packet");
continue;
}
if (HECI_MSG_LENGTH(heci_msg.hdr.length) + sizeof(heci_msg.hdr)
== msg_len)
heci_handle_heci_msg(&heci_msg, msg_len);
else
CPRINTS("msg len mismatch.. discard..");
}
}
|