-
Notifications
You must be signed in to change notification settings - Fork 0
/
mhi_main.c
executable file
·1332 lines (1233 loc) · 40.3 KB
/
mhi_main.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
/* Copyright (c) 2013-2014, The Linux Foundation. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 and
* only version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include "mhi_sys.h"
#include "mhi.h"
#include "mhi_hwio.h"
#include "mhi_macros.h"
extern struct pci_driver mhi_pcie_driver;
int mhi_init_pcie_device(mhi_pcie_dev_info *mhi_pcie_dev)
{
int ret_val = 0;
long int sleep_time = 100000;
struct pci_dev *pcie_device =
(struct pci_dev *)mhi_pcie_dev->pcie_device;
/* Enable the device */
do {
ret_val = pci_enable_device(mhi_pcie_dev->pcie_device);
if (0 != ret_val) {
mhi_log(MHI_MSG_ERROR,
"Failed to enable pcie device ret_val %d\n",
ret_val);
mhi_log(MHI_MSG_ERROR,
"Sleeping for ~ %li uS, and retrying.\n",
sleep_time);
usleep(sleep_time);
}
} while (ret_val != 0);
mhi_log(MHI_MSG_INFO, "Successfully enabled pcie device.\n");
mhi_pcie_dev->core.bar0_base =
(uintptr_t)ioremap_nocache(pci_resource_start(pcie_device, 0),
pci_resource_len(pcie_device, 0));
mhi_pcie_dev->core.bar0_end = mhi_pcie_dev->core.bar0_base +
pci_resource_len(pcie_device, 0);
mhi_pcie_dev->core.bar2_base =
(uintptr_t)ioremap_nocache(pci_resource_start(pcie_device, 2),
pci_resource_len(pcie_device, 2));
mhi_pcie_dev->core.bar2_end = mhi_pcie_dev->core.bar2_base +
pci_resource_len(pcie_device, 2);
if (0 == mhi_pcie_dev->core.bar0_base) {
mhi_log(MHI_MSG_ERROR,
"Failed to register for pcie resources\n");
goto mhi_pcie_read_ep_config_err;
}
mhi_log(MHI_MSG_INFO, "Device BAR0 address is at 0x%llx\n",
mhi_pcie_dev->core.bar0_base);
ret_val = pci_request_region(pcie_device, 0, mhi_pcie_driver.name);
if (ret_val)
mhi_log(MHI_MSG_ERROR, "Could not request BAR0 region\n");
mhi_pcie_dev->core.manufact_id = pcie_device->vendor;
mhi_pcie_dev->core.dev_id = pcie_device->device;
if (mhi_pcie_dev->core.manufact_id != MHI_PCIE_VENDOR_ID ||
mhi_pcie_dev->core.dev_id != MHI_PCIE_DEVICE_ID) {
mhi_log(MHI_MSG_ERROR, "Incorrect device/manufacturer ID\n");
goto mhi_device_list_error;
}
/* We need to ensure that the link is stable before we kick off MHI */
return 0;
mhi_device_list_error:
pci_disable_device(pcie_device);
mhi_pcie_read_ep_config_err:
return -EIO;
}
static void mhi_move_interrupts(mhi_device_ctxt *mhi_dev_ctxt, u32 cpu)
{
u32 irq_to_affin = 0;
MHI_GET_EVENT_RING_INFO(EVENT_RING_MSI_VEC,
mhi_dev_ctxt->ev_ring_props[IPA_IN_EV_RING], irq_to_affin);
irq_to_affin += mhi_dev_ctxt->dev_props->irq_base;
irq_set_affinity(irq_to_affin, get_cpu_mask(cpu));
MHI_GET_EVENT_RING_INFO(EVENT_RING_MSI_VEC,
mhi_dev_ctxt->ev_ring_props[IPA_OUT_EV_RING], irq_to_affin);
irq_to_affin += mhi_dev_ctxt->dev_props->irq_base;
irq_set_affinity(irq_to_affin, get_cpu_mask(cpu));
}
int mhi_cpu_notifier_cb(struct notifier_block *nfb, unsigned long action,
void *hcpu)
{
u32 cpu = (u32)hcpu;
mhi_device_ctxt *mhi_dev_ctxt = container_of(nfb,
mhi_device_ctxt,
mhi_cpu_notifier);
if (NULL == mhi_dev_ctxt)
return NOTIFY_BAD;
switch(action) {
case CPU_ONLINE:
if (cpu > 0)
mhi_move_interrupts(mhi_dev_ctxt, cpu);
break;
case CPU_DEAD:
cpu = NR_CPUS - 1;
while(cpu > 0) {
if (cpu_online(cpu)) {
mhi_move_interrupts(mhi_dev_ctxt, cpu);
break;
}
cpu--;
}
break;
default:
break;
}
return NOTIFY_OK;
}
int mhi_init_gpios(mhi_pcie_dev_info *mhi_pcie_dev)
{
int ret_val = 0;
struct device *dev = &mhi_pcie_dev->pcie_device->dev;
struct device_node *np;
np = dev->of_node;
mhi_log(MHI_MSG_VERBOSE,
"Attempting to grab DEVICE_WAKE gpio\n");
if (!of_find_property(np, "mhi-device-wake-gpio", NULL))
mhi_log(MHI_MSG_CRITICAL,
"Could not find device wake gpio prop, in dt.\n");
else
mhi_log(MHI_MSG_VERBOSE, "Found gpio as a property in DT\n");
ret_val = of_get_named_gpio(np, "mhi-device-wake-gpio", 0);
switch (ret_val) {
case -EPROBE_DEFER:
mhi_log(MHI_MSG_VERBOSE, "DT is not ready\n");
return ret_val;
break;
case 0:
mhi_log(MHI_MSG_CRITICAL,
"Could not get gpio from device tree!\n");
return -EIO;
break;
default:
mhi_pcie_dev->core.device_wake_gpio = ret_val;
mhi_log(MHI_MSG_CRITICAL,
"Got DEVICE_WAKE GPIO nr 0x%x from device tree\n",
mhi_pcie_dev->core.device_wake_gpio);
break;
}
ret_val = gpio_request(mhi_pcie_dev->core.device_wake_gpio, "mhi");
if (ret_val) {
mhi_log(MHI_MSG_VERBOSE,
"Could not obtain device WAKE gpio\n");
}
mhi_log(MHI_MSG_VERBOSE,
"Attempting to set output direction to DEVICE_WAKE gpio\n");
/* This GPIO must never sleep as it can be set in timer ctxt */
gpio_set_value_cansleep(mhi_pcie_dev->core.device_wake_gpio, 0);
if (ret_val)
mhi_log(MHI_MSG_VERBOSE,
"Could not set GPIO to not sleep!\n");
ret_val = gpio_direction_output(mhi_pcie_dev->core.device_wake_gpio, 1);
if (ret_val) {
mhi_log(MHI_MSG_VERBOSE,
"Failed to set output direction of DEVICE_WAKE gpio\n");
goto mhi_gpio_dir_err;
}
return 0;
mhi_gpio_dir_err:
gpio_free(mhi_pcie_dev->core.device_wake_gpio);
return -EIO;
}
MHI_STATUS mhi_open_channel(mhi_client_handle **client_handle,
MHI_CLIENT_CHANNEL chan, s32 device_index,
mhi_client_info_t *client_info, void *UserData)
{
MHI_STATUS ret_val = MHI_STATUS_SUCCESS;
mhi_control_seg *mhi_ctrl_seg = NULL;
if (!VALID_CHAN_NR(chan)) {
ret_val = MHI_STATUS_INVALID_CHAN_ERR;
goto error_handle;
}
if (NULL == client_handle || device_index < 0 ||
device_index >= mhi_devices.nr_of_devices) {
ret_val = MHI_STATUS_ERROR;
goto error_handle;
}
mhi_log(MHI_MSG_INFO,
"Opened channel 0x%x for client\n", chan);
atomic_inc(&mhi_devices.device_list[device_index].ref_count);
*client_handle = kmalloc(sizeof(mhi_client_handle), GFP_KERNEL);
if (NULL == *client_handle) {
ret_val = MHI_STATUS_ALLOC_ERROR;
goto error_handle;
}
memset(*client_handle, 0, sizeof(mhi_client_handle));
(*client_handle)->chan = chan;
(*client_handle)->mhi_dev_ctxt =
mhi_devices.device_list[device_index].mhi_ctxt;
mhi_ctrl_seg = (*client_handle)->mhi_dev_ctxt->mhi_ctrl_seg;
(*client_handle)->mhi_dev_ctxt->client_handle_list[chan] =
*client_handle;
if (NULL != client_info)
(*client_handle)->client_info = *client_info;
(*client_handle)->user_data = UserData;
(*client_handle)->event_ring_index =
mhi_ctrl_seg->mhi_cc_list[chan].mhi_event_ring_index;
init_completion(&(*client_handle)->chan_close_complete);
(*client_handle)->msi_vec =
mhi_ctrl_seg->mhi_ec_list[
(*client_handle)->event_ring_index].mhi_msi_vector;
if (client_info->cb_mod != 0)
(*client_handle)->cb_mod = client_info->cb_mod;
else
(*client_handle)->cb_mod = 1;
if (MHI_CLIENT_IP_HW_0_OUT == chan)
(*client_handle)->intmod_t = 10;
if (MHI_CLIENT_IP_HW_0_IN == chan)
(*client_handle)->intmod_t = 10;
mhi_log(MHI_MSG_VERBOSE,
"Successfuly started chan 0x%x\n", chan);
error_handle:
return ret_val;
}
void mhi_close_channel(mhi_client_handle *mhi_handle)
{
u32 index = 0;
u32 chan = 0;
if (NULL == mhi_handle)
return;
chan = mhi_handle->chan;
mhi_log(MHI_MSG_INFO, "Client attempting to close chan 0x%x\n", chan);
index = mhi_handle->device_index;
mhi_log(MHI_MSG_INFO, "Chan 0x%x confirmed closed.\n", chan);
mhi_handle->mhi_dev_ctxt->client_handle_list[mhi_handle->chan] = NULL;
atomic_dec(&(mhi_devices.device_list[index].ref_count));
kfree(mhi_handle);
}
void ring_ev_db(mhi_device_ctxt *mhi_dev_ctxt, u32 event_ring_index)
{
mhi_ring *event_ctxt = NULL;
u64 db_value = 0;
event_ctxt =
&mhi_dev_ctxt->mhi_local_event_ctxt[event_ring_index];
db_value = mhi_v2p_addr(mhi_dev_ctxt->mhi_ctrl_seg_info,
(uintptr_t)event_ctxt->wp);
MHI_WRITE_DB(mhi_dev_ctxt, mhi_dev_ctxt->event_db_addr,
event_ring_index, db_value);
}
/**
* @brief Add elements to event ring for the device to use
*
* @param mhi device context
*
* @return MHI_STATUS
*/
MHI_STATUS mhi_add_elements_to_event_rings(mhi_device_ctxt *mhi_dev_ctxt,
STATE_TRANSITION new_state)
{
MHI_STATUS ret_val = MHI_STATUS_SUCCESS;
MHI_EVENT_RING_STATE event_ring_state = MHI_EVENT_RING_UINIT;
switch (new_state) {
case STATE_TRANSITION_READY:
MHI_GET_EVENT_RING_INFO(EVENT_RING_STATE_FIELD,
mhi_dev_ctxt->ev_ring_props[PRIMARY_EVENT_RING],
event_ring_state);
if (MHI_EVENT_RING_UINIT == event_ring_state) {
ret_val = mhi_init_event_ring(mhi_dev_ctxt,
EV_EL_PER_RING,
mhi_dev_ctxt->alloced_ev_rings[PRIMARY_EVENT_RING]);
if (MHI_STATUS_SUCCESS != ret_val) {
mhi_log(MHI_MSG_ERROR,
"Failed to add ev el on event ring\n");
return MHI_STATUS_ERROR;
}
MHI_SET_EVENT_RING_INFO(EVENT_RING_STATE_FIELD,
mhi_dev_ctxt->ev_ring_props[PRIMARY_EVENT_RING],
MHI_EVENT_RING_INIT);
}
mhi_log(MHI_MSG_ERROR,
"Event ring initialized ringing, EV DB to resume\n");
ring_ev_db(mhi_dev_ctxt,
mhi_dev_ctxt->alloced_ev_rings[PRIMARY_EVENT_RING]);
break;
case STATE_TRANSITION_AMSS:
MHI_GET_EVENT_RING_INFO(EVENT_RING_STATE_FIELD,
mhi_dev_ctxt->ev_ring_props[IPA_OUT_EV_RING],
event_ring_state);
if (MHI_EVENT_RING_UINIT == event_ring_state) {
ret_val = mhi_init_event_ring(mhi_dev_ctxt,
EV_EL_PER_RING,
mhi_dev_ctxt->alloced_ev_rings[IPA_OUT_EV_RING]);
if (MHI_STATUS_SUCCESS != ret_val) {
mhi_log(MHI_MSG_ERROR,
"Failed to add ev el on event ring\n");
return MHI_STATUS_ERROR;
}
ret_val = mhi_init_event_ring(mhi_dev_ctxt,
EV_EL_PER_RING,
mhi_dev_ctxt->alloced_ev_rings[IPA_IN_EV_RING]);
if (MHI_STATUS_SUCCESS != ret_val) {
mhi_log(MHI_MSG_ERROR,
"Failed to add ev el on event ring\n");
return MHI_STATUS_ERROR;
}
MHI_SET_EVENT_RING_INFO(EVENT_RING_STATE_FIELD,
mhi_dev_ctxt->ev_ring_props[IPA_OUT_EV_RING],
MHI_EVENT_RING_INIT);
MHI_SET_EVENT_RING_INFO(EVENT_RING_STATE_FIELD,
mhi_dev_ctxt->ev_ring_props[IPA_IN_EV_RING],
MHI_EVENT_RING_INIT);
}
ring_ev_db(mhi_dev_ctxt,
mhi_dev_ctxt->alloced_ev_rings[SOFTWARE_EV_RING]);
ring_ev_db(mhi_dev_ctxt,
mhi_dev_ctxt->alloced_ev_rings[IPA_OUT_EV_RING]);
ring_ev_db(mhi_dev_ctxt,
mhi_dev_ctxt->alloced_ev_rings[IPA_IN_EV_RING]);
break;
default:
mhi_log(MHI_MSG_ERROR,
"Unrecognized event stage, %d\n", new_state);
ret_val = MHI_STATUS_ERROR;
break;
}
return ret_val;
}
/**
* @brief Add available TRBs to an IN channel
*
* @param device mhi device context
* @param chan mhi channel number
*
* @return MHI_STATUS
*/
/**
* @brief Function for sending data on an outbound channel.
* This function only sends on TRE's worth of
* data and may chain the TRE as specified by the caller.
*
* @param device [IN ] Pointer to mhi context used to send the TRE
* @param chan [IN ] Channel number to send the TRE on
* @param buf [IN ] Physical address of buffer to be linked to descriptor
* @param buf_len [IN ] Length of buffer, which will be populated in the TRE
* @param chain [IN ] Specification on whether this TRE should be chained
*
* @return MHI_STATUS
*/
MHI_STATUS mhi_queue_xfer(mhi_client_handle *client_handle,
uintptr_t buf, size_t buf_len, u32 chain, u32 eob)
{
mhi_xfer_pkt *pkt_loc;
MHI_STATUS ret_val;
MHI_CLIENT_CHANNEL chan;
mhi_device_ctxt *mhi_dev_ctxt;
unsigned long flags;
uintptr_t trb_index;
if (NULL == client_handle || !VALID_CHAN_NR(client_handle->chan) ||
0 == buf || chain >= MHI_TRE_CHAIN_LIMIT || 0 == buf_len) {
mhi_log(MHI_MSG_CRITICAL, "Bad input args\n");
return MHI_STATUS_ERROR;
}
MHI_ASSERT(VALID_BUF(buf, buf_len),
"Client buffer is of invalid length\n");
mhi_dev_ctxt = client_handle->mhi_dev_ctxt;
chan = client_handle->chan;
/* Bump up the vote for pending data */
read_lock_irqsave(&mhi_dev_ctxt->xfer_lock, flags);
atomic_inc(&mhi_dev_ctxt->flags.data_pending);
mhi_dev_ctxt->counters.m1_m0++;
if (mhi_dev_ctxt->flags.link_up)
mhi_assert_device_wake(mhi_dev_ctxt);
read_unlock_irqrestore(&mhi_dev_ctxt->xfer_lock, flags);
/* Add the TRB to the correct transfer ring */
ret_val = ctxt_add_element(&mhi_dev_ctxt->mhi_local_chan_ctxt[chan],
(void *)&pkt_loc);
if (unlikely(MHI_STATUS_SUCCESS != ret_val)) {
mhi_log(MHI_MSG_INFO, "Failed to insert trb in xfer ring\n");
goto error;
}
pkt_loc->data_tx_pkt.buffer_ptr = buf;
get_element_index(&mhi_dev_ctxt->mhi_local_chan_ctxt[chan],
pkt_loc, &trb_index);
if (likely(0 != client_handle->intmod_t))
MHI_TRB_SET_INFO(TX_TRB_BEI, pkt_loc, 1);
else
MHI_TRB_SET_INFO(TX_TRB_BEI, pkt_loc, 0);
MHI_TRB_SET_INFO(TX_TRB_IEOT, pkt_loc, 1);
MHI_TRB_SET_INFO(TX_TRB_CHAIN, pkt_loc, chain);
MHI_TRB_SET_INFO(TX_TRB_IEOB, pkt_loc, eob);
MHI_TRB_SET_INFO(TX_TRB_TYPE, pkt_loc, MHI_PKT_TYPE_TRANSFER);
MHI_TX_TRB_SET_LEN(TX_TRB_LEN, pkt_loc, buf_len);
if (chan % 2 == 0) {
atomic_inc(&mhi_dev_ctxt->counters.outbound_acks);
mhi_log(MHI_MSG_VERBOSE,
"Queued outbound pkt. Pending Acks %d\n",
atomic_read(&mhi_dev_ctxt->counters.outbound_acks));
}
mhi_notify_device(mhi_dev_ctxt, chan);
atomic_dec(&mhi_dev_ctxt->flags.data_pending);
return MHI_STATUS_SUCCESS;
error:
atomic_dec(&mhi_dev_ctxt->flags.data_pending);
return ret_val;
}
MHI_STATUS mhi_notify_device(mhi_device_ctxt *mhi_dev_ctxt, u32 chan)
{
unsigned long flags = 0;
u64 db_value;
mhi_chan_ctxt *chan_ctxt;
chan_ctxt = &mhi_dev_ctxt->mhi_ctrl_seg->mhi_cc_list[chan];
spin_lock_irqsave(&mhi_dev_ctxt->db_write_lock[chan], flags);
if (likely(((MHI_STATE_M0 == mhi_dev_ctxt->mhi_state) ||
(MHI_STATE_M1 == mhi_dev_ctxt->mhi_state)) &&
(chan_ctxt->mhi_chan_state != MHI_CHAN_STATE_ERROR) &&
!mhi_dev_ctxt->flags.pending_M3)) {
mhi_dev_ctxt->mhi_chan_db_order[chan]++;
db_value = mhi_v2p_addr(mhi_dev_ctxt->mhi_ctrl_seg_info,
(uintptr_t)mhi_dev_ctxt->mhi_local_chan_ctxt[chan].wp);
if (IS_HARDWARE_CHANNEL(chan) && (chan % 2)) {
if ((mhi_dev_ctxt->mhi_chan_cntr[chan].pkts_xferd %
MHI_XFER_DB_INTERVAL) == 0) {
MHI_WRITE_DB(mhi_dev_ctxt,
mhi_dev_ctxt->channel_db_addr,
chan, db_value);
}
} else {
MHI_WRITE_DB(mhi_dev_ctxt,
mhi_dev_ctxt->channel_db_addr,
chan, db_value);
}
} else {
mhi_log(MHI_MSG_VERBOSE,
"Triggering wakeup due to pending data MHI state %d, Chan state %d\n",
mhi_dev_ctxt->mhi_state, chan_ctxt->mhi_chan_state);
if (mhi_dev_ctxt->flags.pending_M3 ||
mhi_dev_ctxt->mhi_state == MHI_STATE_M3) {
mhi_wake_dev_from_m3(mhi_dev_ctxt);
}
}
spin_unlock_irqrestore(&mhi_dev_ctxt->db_write_lock[chan], flags);
/* If there are no clients still sending we can trigger our
* inactivity timer */
return MHI_STATUS_SUCCESS;
}
MHI_STATUS mhi_wake_dev_from_m3(mhi_device_ctxt *mhi_dev_ctxt)
{
u32 r;
if (!atomic_cmpxchg(&mhi_dev_ctxt->flags.m0_work_enabled, 0, 1)) {
mhi_log(MHI_MSG_INFO,
"Initiating M0 work...\n");
if (atomic_read(&mhi_dev_ctxt->flags.pending_resume)) {
mhi_log(MHI_MSG_INFO,
"Resume is pending, quitting ...\n");
atomic_set(&mhi_dev_ctxt->flags.m0_work_enabled, 0);
__pm_stay_awake(&mhi_dev_ctxt->wake_lock);
__pm_relax(&mhi_dev_ctxt->wake_lock);
return MHI_STATUS_SUCCESS;
}
r = queue_work(mhi_dev_ctxt->work_queue,
&mhi_dev_ctxt->m0_work);
if (!r)
mhi_log(MHI_MSG_CRITICAL,
"Failed to start M0 work.\n");
} else {
mhi_log(MHI_MSG_VERBOSE,
"M0 work pending.\n");
}
return MHI_STATUS_SUCCESS;
}
/**
* @brief Function used to send a command TRE to the mhi device.
*
* @param device [IN ] Specify the mhi dev context to which to send the command
* @param cmd [IN ] Enum specifying which command to send to device
* @param chan [in ] Channel number for which this command is intended,
* not applicable for all commands
*
* @return MHI_STATUS
*/
MHI_STATUS mhi_send_cmd(mhi_device_ctxt *mhi_dev_ctxt,
MHI_COMMAND cmd, u32 chan)
{
u64 db_value = 0;
mhi_cmd_pkt *cmd_pkt = NULL;
MHI_CHAN_STATE from_state = MHI_CHAN_STATE_DISABLED;
MHI_CHAN_STATE to_state = MHI_CHAN_STATE_DISABLED;
MHI_PKT_TYPE ring_el_type = MHI_PKT_TYPE_NOOP_CMD;
struct mutex *cmd_mutex = NULL;
struct mutex *chan_mutex = NULL;
if (chan >= MHI_MAX_CHANNELS ||
cmd >= MHI_COMMAND_MAX_NR || NULL == mhi_dev_ctxt) {
mhi_log(MHI_MSG_ERROR,
"Invalid channel id, received id: 0x%x", chan);
goto error_general;
}
mhi_assert_device_wake(mhi_dev_ctxt);
/*If there is a cmd pending a device confirmation, do not send anymore
for this channel */
if (MHI_CMD_PENDING == mhi_dev_ctxt->mhi_chan_pend_cmd_ack[chan])
return MHI_STATUS_CMD_PENDING;
from_state =
mhi_dev_ctxt->mhi_ctrl_seg->mhi_cc_list[chan].mhi_chan_state;
switch (cmd) {
case MHI_COMMAND_NOOP:
{
ring_el_type = MHI_PKT_TYPE_NOOP_CMD;
break;
}
case MHI_COMMAND_RESET_CHAN:
{
to_state = MHI_CHAN_STATE_DISABLED;
ring_el_type = MHI_PKT_TYPE_RESET_CHAN_CMD;
break;
}
case MHI_COMMAND_START_CHAN:
{
switch (from_state) {
case MHI_CHAN_STATE_ENABLED:
case MHI_CHAN_STATE_STOP:
to_state = MHI_CHAN_STATE_RUNNING;
break;
default:
mhi_log(MHI_MSG_ERROR,
"Invalid state transition for "
"cmd 0x%x, from_state 0x%x\n",
cmd, from_state);
goto error_general;
}
ring_el_type = MHI_PKT_TYPE_START_CHAN_CMD;
break;
}
case MHI_COMMAND_STOP_CHAN:
{
switch (from_state) {
case MHI_CHAN_STATE_RUNNING:
case MHI_CHAN_STATE_SUSPENDED:
to_state = MHI_CHAN_STATE_STOP;
break;
default:
mhi_log(MHI_MSG_ERROR,
"Invalid state transition for "
"cmd 0x%x, from_state 0x%x\n",
cmd, from_state);
goto error_general;
}
ring_el_type = MHI_PKT_TYPE_STOP_CHAN_CMD;
break;
}
default:
mhi_log(MHI_MSG_ERROR, "Bad command received\n");
}
cmd_mutex = &mhi_dev_ctxt->mhi_cmd_mutex_list[PRIMARY_CMD_RING];
mutex_lock(cmd_mutex);
if (MHI_STATUS_SUCCESS !=
ctxt_add_element(mhi_dev_ctxt->mhi_local_cmd_ctxt,
(void *)&cmd_pkt)) {
mhi_log(MHI_MSG_ERROR, "Failed to insert element\n");
goto error_general;
}
chan_mutex = &mhi_dev_ctxt->mhi_chan_mutex[chan];
if (MHI_COMMAND_NOOP != cmd) {
mutex_lock(chan_mutex);
MHI_TRB_SET_INFO(CMD_TRB_TYPE, cmd_pkt, ring_el_type);
MHI_TRB_SET_INFO(CMD_TRB_CHID, cmd_pkt, chan);
mutex_unlock(chan_mutex);
}
db_value = mhi_v2p_addr(mhi_dev_ctxt->mhi_ctrl_seg_info,
(uintptr_t)mhi_dev_ctxt->mhi_local_cmd_ctxt->wp);
mhi_dev_ctxt->mhi_chan_pend_cmd_ack[chan] = MHI_CMD_PENDING;
if (MHI_STATE_M0 == mhi_dev_ctxt->mhi_state ||
MHI_STATE_M1 == mhi_dev_ctxt->mhi_state) {
mhi_dev_ctxt->cmd_ring_order++;
MHI_WRITE_DB(mhi_dev_ctxt, mhi_dev_ctxt->cmd_db_addr, 0, db_value);
}
mhi_log(MHI_MSG_VERBOSE, "Sent command 0x%x for chan 0x%x\n", cmd, chan);
mutex_unlock(&mhi_dev_ctxt->mhi_cmd_mutex_list[PRIMARY_CMD_RING]);
return MHI_STATUS_SUCCESS;
error_general:
mutex_unlock(&mhi_dev_ctxt->mhi_cmd_mutex_list[PRIMARY_CMD_RING]);
return MHI_STATUS_ERROR;
}
/**
* @brief Thread which handles inbound data for MHI clients.
* This thread will invoke thecallback for the mhi clients to
* inform thme of data availability.
*
* The thread monitors the MHI state variable to know if it should
* continue processing, * or stop.
*
* @param ctxt void pointer to a device context
*/
MHI_STATUS parse_xfer_event(mhi_device_ctxt *ctxt, mhi_event_pkt *event)
{
mhi_device_ctxt *mhi_dev_ctxt = (mhi_device_ctxt *)ctxt;
mhi_result *result;
u32 chan = MHI_MAX_CHANNELS;
u16 xfer_len;
uintptr_t phy_ev_trb_loc;
mhi_xfer_pkt *local_ev_trb_loc;
mhi_client_handle *client_handle;
mhi_xfer_pkt *local_trb_loc;
mhi_chan_ctxt *chan_ctxt;
u32 nr_trb_to_parse;
u32 i = 0;
switch (MHI_EV_READ_CODE(EV_TRB_CODE, event)) {
case MHI_EVENT_CC_EOB:
mhi_log(MHI_MSG_VERBOSE, "IEOB condition detected\n");
case MHI_EVENT_CC_OVERFLOW:
mhi_log(MHI_MSG_VERBOSE, "Overflow condition detected\n");
case MHI_EVENT_CC_EOT:
{
void *trb_data_loc;
u32 ieot_flag;
MHI_STATUS ret_val;
mhi_ring *local_chan_ctxt;
chan = MHI_EV_READ_CHID(EV_CHID, event);
local_chan_ctxt =
&mhi_dev_ctxt->mhi_local_chan_ctxt[chan];
phy_ev_trb_loc = MHI_EV_READ_PTR(EV_PTR, event);
if (unlikely(!VALID_CHAN_NR(chan))) {
mhi_log(MHI_MSG_ERROR, "Bad ring id.\n");
break;
}
chan_ctxt = &mhi_dev_ctxt->mhi_ctrl_seg->mhi_cc_list[chan];
ret_val = validate_xfer_el_addr(chan_ctxt,
phy_ev_trb_loc);
if (unlikely(MHI_STATUS_SUCCESS != ret_val)) {
mhi_log(MHI_MSG_ERROR, "Bad event trb ptr.\n");
break;
}
/* Get the TRB this event points to*/
local_ev_trb_loc =
(mhi_xfer_pkt *)mhi_p2v_addr(
mhi_dev_ctxt->mhi_ctrl_seg_info,
phy_ev_trb_loc);
local_trb_loc = (mhi_xfer_pkt *)local_chan_ctxt->rp;
ret_val = get_nr_enclosed_el(local_chan_ctxt,
local_trb_loc,
local_ev_trb_loc,
&nr_trb_to_parse);
if (unlikely(MHI_STATUS_SUCCESS != ret_val)) {
mhi_log(MHI_MSG_CRITICAL,
"Failed to get nr available trbs ret: %d.\n",
ret_val);
return MHI_STATUS_ERROR;
}
do {
u64 phy_buf_loc;
MHI_TRB_GET_INFO(TX_TRB_IEOT, local_trb_loc, ieot_flag);
phy_buf_loc = local_trb_loc->data_tx_pkt.buffer_ptr;
trb_data_loc = (void *)(uintptr_t)phy_buf_loc;
if (chan % 2)
xfer_len = MHI_EV_READ_LEN(EV_LEN, event);
else
xfer_len = MHI_TX_TRB_GET_LEN(TX_TRB_LEN,
local_trb_loc);
if (!VALID_BUF(trb_data_loc, xfer_len)) {
mhi_log(MHI_MSG_CRITICAL,
"Bad buffer ptr: %p.\n",
trb_data_loc);
return MHI_STATUS_ERROR;
}
client_handle = mhi_dev_ctxt->client_handle_list[chan];
if (NULL != client_handle) {
client_handle->pkt_count++;
result = &client_handle->result;
result->payload_buf = trb_data_loc;
result->bytes_xferd = xfer_len;
result->user_data = client_handle->user_data;
}
if (chan % 2) {
parse_inbound(mhi_dev_ctxt, chan,
local_ev_trb_loc, xfer_len);
} else {
parse_outbound(mhi_dev_ctxt, chan,
local_ev_trb_loc, xfer_len);
}
mhi_dev_ctxt->mhi_chan_cntr[chan].pkts_xferd++;
if (local_trb_loc ==
(mhi_xfer_pkt *)local_chan_ctxt->rp) {
mhi_log(MHI_MSG_CRITICAL,
"Done. Processed until: %p.\n",
trb_data_loc);
break;
} else {
local_trb_loc =
(mhi_xfer_pkt *)local_chan_ctxt->rp;
}
i++;
} while (i <= nr_trb_to_parse);
break;
} /* CC_EOT */
case MHI_EVENT_CC_OOB:
case MHI_EVENT_CC_DB_MODE:
{
mhi_ring *chan_ctxt = NULL;
u64 db_value = 0;
mhi_dev_ctxt->uldl_enabled = 1;
chan = MHI_EV_READ_CHID(EV_CHID, event);
mhi_dev_ctxt->db_mode[chan] = 1;
chan_ctxt =
&mhi_dev_ctxt->mhi_local_chan_ctxt[chan];
mhi_log(MHI_MSG_INFO, "OOB Detected chan %d.\n", chan);
if (chan_ctxt->wp != chan_ctxt->rp) {
db_value = mhi_v2p_addr(mhi_dev_ctxt->mhi_ctrl_seg_info,
(uintptr_t)chan_ctxt->wp);
MHI_WRITE_DB(mhi_dev_ctxt, mhi_dev_ctxt->channel_db_addr, chan,
db_value);
}
client_handle = mhi_dev_ctxt->client_handle_list[chan];
if (NULL != client_handle) {
result->transaction_status = MHI_STATUS_DEVICE_NOT_READY;
}
break;
}
default:
{
mhi_log(MHI_MSG_ERROR,
"Unknown TX completion.\n");
break;
}
} /*switch(MHI_EV_READ_CODE(EV_TRB_CODE,event)) */
return 0;
}
MHI_STATUS recycle_trb_and_ring(mhi_device_ctxt *mhi_dev_ctxt,
mhi_ring *ring,
MHI_RING_TYPE ring_type,
u32 ring_index)
{
MHI_STATUS ret_val = MHI_STATUS_ERROR;
u64 db_value = 0;
void *removed_element = NULL;
void *added_element = NULL;
/* TODO This will not cover us for ring_index out of
* bounds for cmd or event channels */
if (NULL == mhi_dev_ctxt || NULL == ring ||
ring_type > (MHI_RING_TYPE_MAX - 1) ||
ring_index > (MHI_MAX_CHANNELS - 1)) {
mhi_log(MHI_MSG_ERROR, "Bad input params\n");
return ret_val;
}
ret_val = ctxt_del_element(ring, &removed_element);
if (MHI_STATUS_SUCCESS != ret_val) {
mhi_log(MHI_MSG_ERROR, "Could not remove element from ring\n");
return MHI_STATUS_ERROR;
}
ret_val = ctxt_add_element(ring, &added_element);
if (MHI_STATUS_SUCCESS != ret_val)
mhi_log(MHI_MSG_ERROR, "Could not add element to ring\n");
db_value = mhi_v2p_addr(mhi_dev_ctxt->mhi_ctrl_seg_info,
(uintptr_t)ring->wp);
if (MHI_STATUS_SUCCESS != ret_val)
return ret_val;
if (MHI_RING_TYPE_XFER_RING == ring_type) {
mhi_xfer_pkt *removed_xfer_pkt =
(mhi_xfer_pkt *)removed_element;
mhi_xfer_pkt *added_xfer_pkt =
(mhi_xfer_pkt *)added_element;
added_xfer_pkt->data_tx_pkt =
*(mhi_tx_pkt *)removed_xfer_pkt;
if ((IS_HARDWARE_CHANNEL(ring_index)) &&
(mhi_dev_ctxt->counters.m0_m3 > 0)) {
unsigned long flags = 0;
mhi_chan_ctxt *chan_ctxt;
chan_ctxt = &mhi_dev_ctxt->mhi_ctrl_seg->mhi_cc_list[ring_index];
mhi_log(MHI_MSG_VERBOSE,
"Updating chan ctxt ring %d\n", ring_index);
spin_lock_irqsave(&mhi_dev_ctxt->db_write_lock[ring_index], flags);
mhi_dev_ctxt->mhi_chan_db_order[ring_index] = 1;
db_value = mhi_v2p_addr(mhi_dev_ctxt->mhi_ctrl_seg_info,
(uintptr_t)mhi_dev_ctxt->mhi_local_chan_ctxt[ring_index].wp);
chan_ctxt->mhi_trb_write_ptr = db_value;
spin_unlock_irqrestore(&mhi_dev_ctxt->db_write_lock[ring_index], flags);
}
} else if (MHI_RING_TYPE_EVENT_RING == ring_type &&
mhi_dev_ctxt->counters.m0_m3 > 0 &&
IS_HARDWARE_CHANNEL(ring_index)) {
spinlock_t *lock = NULL;
unsigned long flags = 0;
mhi_log(MHI_MSG_VERBOSE, "Updating ev ctxt ring %d\n", ring_index);
lock = &mhi_dev_ctxt->mhi_ev_spinlock_list[ring_index];
spin_lock_irqsave(lock, flags);
mhi_dev_ctxt->mhi_ev_db_order[ring_index] = 1;
mhi_dev_ctxt->mhi_ctrl_seg->mhi_ec_list[ring_index].mhi_event_write_ptr = db_value;
mhi_dev_ctxt->ev_counter[ring_index]++;
spin_unlock_irqrestore(lock, flags);
}
atomic_inc(&mhi_dev_ctxt->flags.data_pending);
/* Asserting Device Wake here, will imediately wake mdm */
if ((MHI_STATE_M0 == mhi_dev_ctxt->mhi_state ||
MHI_STATE_M1 == mhi_dev_ctxt->mhi_state) &&
mhi_dev_ctxt->flags.link_up) {
switch (ring_type) {
case MHI_RING_TYPE_CMD_RING:
{
struct mutex *cmd_mutex = NULL;
cmd_mutex =
&mhi_dev_ctxt->mhi_cmd_mutex_list[PRIMARY_CMD_RING];
mutex_lock(cmd_mutex);
mhi_dev_ctxt->cmd_ring_order = 1;
MHI_WRITE_DB(mhi_dev_ctxt, mhi_dev_ctxt->cmd_db_addr,
ring_index, db_value);
mutex_unlock(cmd_mutex);
break;
}
case MHI_RING_TYPE_EVENT_RING:
{
spinlock_t *lock = NULL;
unsigned long flags = 0;
lock = &mhi_dev_ctxt->mhi_ev_spinlock_list[ring_index];
spin_lock_irqsave(lock, flags);
mhi_dev_ctxt->mhi_ev_db_order[ring_index] = 1;
if ((mhi_dev_ctxt->ev_counter[ring_index] %
MHI_EV_DB_INTERVAL) == 0) {
MHI_WRITE_DB(mhi_dev_ctxt, mhi_dev_ctxt->event_db_addr,
ring_index, db_value);
}
mhi_dev_ctxt->ev_counter[ring_index]++;
spin_unlock_irqrestore(lock, flags);
break;
}
case MHI_RING_TYPE_XFER_RING:
{
unsigned long flags = 0;
spin_lock_irqsave(&mhi_dev_ctxt->db_write_lock[ring_index], flags);
mhi_dev_ctxt->mhi_chan_db_order[ring_index] = 1;
MHI_WRITE_DB(mhi_dev_ctxt, mhi_dev_ctxt->channel_db_addr,
ring_index, db_value);
spin_unlock_irqrestore(&mhi_dev_ctxt->db_write_lock[ring_index],
flags);
break;
}
default:
mhi_log(MHI_MSG_ERROR, "Bad ring type\n");
}
} else {
mhi_log(MHI_MSG_ERROR,
"Cannot ring DB state %d link %d, ring type %d, index %d db 0x%lx\n",
mhi_dev_ctxt->mhi_state,
mhi_dev_ctxt->flags.link_up,
ring_type,
ring_index,
(uintptr_t)db_value);
mhi_dev_ctxt->counters.failed_recycle[ring_type]++;
}
atomic_dec(&mhi_dev_ctxt->flags.data_pending);
return ret_val;
}
MHI_STATUS mhi_change_chan_state(mhi_device_ctxt *mhi_dev_ctxt, u32 chan_id,
MHI_CHAN_STATE new_state)
{
struct mutex *chan_mutex = &mhi_dev_ctxt->mhi_chan_mutex[chan_id];
if (chan_id > (MHI_MAX_CHANNELS - 1) || NULL == mhi_dev_ctxt ||
new_state > MHI_CHAN_STATE_LIMIT) {
mhi_log(MHI_MSG_ERROR, "Bad input parameters\n");
return MHI_STATUS_ERROR;
}
mutex_lock(chan_mutex);
/* Set the new state of the channel context */
mhi_dev_ctxt->mhi_ctrl_seg->mhi_cc_list[chan_id].mhi_chan_state =
MHI_CHAN_STATE_ENABLED;
mutex_unlock(chan_mutex);
return MHI_STATUS_SUCCESS;
}
MHI_STATUS parse_cmd_event(mhi_device_ctxt *mhi_dev_ctxt, mhi_event_pkt *ev_pkt)
{
MHI_STATUS ret_val = MHI_STATUS_SUCCESS;
mhi_cmd_pkt *cmd_pkt = NULL;
uintptr_t phy_trb_loc = 0;
if (NULL != ev_pkt)
phy_trb_loc = (uintptr_t)MHI_EV_READ_PTR(EV_PTR,
ev_pkt);
else
return MHI_STATUS_ERROR;
cmd_pkt = (mhi_cmd_pkt *)mhi_p2v_addr(mhi_dev_ctxt->mhi_ctrl_seg_info,
phy_trb_loc);
mhi_log(MHI_MSG_INFO, "Received CMD completion event\n");
switch (MHI_EV_READ_CODE(EV_TRB_CODE, ev_pkt)) {
/* Command completion was successful */
case MHI_EVENT_CC_SUCCESS:
{
u32 chan;
MHI_TRB_GET_INFO(CMD_TRB_CHID, cmd_pkt, chan);
switch (MHI_TRB_READ_INFO(CMD_TRB_TYPE, cmd_pkt)) {
case MHI_PKT_TYPE_NOOP_CMD:
mhi_log(MHI_MSG_INFO, "Processed NOOP cmd event\n");
break;
case MHI_PKT_TYPE_RESET_CHAN_CMD:
if (MHI_STATUS_SUCCESS != reset_chan_cmd(mhi_dev_ctxt,
cmd_pkt))
mhi_log(MHI_MSG_INFO,
"Failed to process reset cmd\n");
break;
case MHI_PKT_TYPE_STOP_CHAN_CMD:
{
mhi_log(MHI_MSG_INFO, "Processed cmd stop event\n");
if (MHI_STATUS_SUCCESS != ret_val) {
mhi_log(MHI_MSG_INFO,
"Failed to set chan state\n");
return MHI_STATUS_ERROR;
}
break;
}
case MHI_PKT_TYPE_START_CHAN_CMD:
{
if (MHI_STATUS_SUCCESS != start_chan_cmd(mhi_dev_ctxt,
cmd_pkt))
mhi_log(MHI_MSG_INFO,
"Failed to process reset cmd\n");
atomic_dec(&mhi_dev_ctxt->start_cmd_pending_ack);
wake_up_interruptible(mhi_dev_ctxt->chan_start_complete);
break;
}
default:
mhi_log(MHI_MSG_INFO,
"Bad cmd type 0x%x\n",
MHI_TRB_READ_INFO(CMD_TRB_TYPE, cmd_pkt));
break;
}
mhi_log(MHI_MSG_INFO, "CMD completion indicated successful\n");
mhi_dev_ctxt->mhi_chan_pend_cmd_ack[chan] = MHI_CMD_NOT_PENDING;
break;
}
default:
mhi_log(MHI_MSG_INFO, "Unhandled mhi completion code\n");
break;
}
mhi_log(MHI_MSG_INFO, "Recycling cmd event\n");
ctxt_del_element(mhi_dev_ctxt->mhi_local_cmd_ctxt, NULL);
return MHI_STATUS_SUCCESS;
}
MHI_STATUS reset_chan_cmd(mhi_device_ctxt *mhi_dev_ctxt, mhi_cmd_pkt *cmd_pkt)
{
u32 chan = 0;
MHI_STATUS ret_val = MHI_STATUS_SUCCESS;