summaryrefslogtreecommitdiff
path: root/drivers/platform/msm/ipa/ipa_bridge.c
blob: a54be264cd2c75aa1908b4ce24bfb53cae2164f4 (plain)
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
/* Copyright (c) 2012-2013, 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 <linux/delay.h>
#include <linux/ratelimit.h>
#include <soc/msm/smem.h>
#include "ipa_i.h"

/*
 * EP0 (teth)
 * A2_BAM(1)->(12)DMA_BAM->DMA_BAM(13)->(6)IPA_BAM->IPA_BAM(10)->USB_BAM(0)
 * A2_BAM(0)<-(15)DMA_BAM<-DMA_BAM(14)<-(7)IPA_BAM<-IPA_BAM(11)<-USB_BAM(1)
 *
 * EP2 (emb)
 * A2_BAM(5)->(16)DMA_BAM->DMA_BAM(17)->(8)IPA_BAM->
 * A2_BAM(4)<-(19)DMA_BAM<-DMA_BAM(18)<-(9)IPA_BAM<-
 */

#define A2_TETHERED_PIPE_UL      0
#define DMA_A2_TETHERED_PIPE_UL  15
#define DMA_IPA_TETHERED_PIPE_UL 14
#define A2_TETHERED_PIPE_DL      1
#define DMA_A2_TETHERED_PIPE_DL  12
#define DMA_IPA_TETHERED_PIPE_DL 13

#define A2_EMBEDDED_PIPE_UL      4
#define DMA_A2_EMBEDDED_PIPE_UL  19
#define DMA_IPA_EMBEDDED_PIPE_UL 18
#define A2_EMBEDDED_PIPE_DL      5
#define DMA_A2_EMBEDDED_PIPE_DL  16
#define DMA_IPA_EMBEDDED_PIPE_DL 17

#define IPA_SMEM_PIPE_MEM_SZ 32768

#define IPA_UL_DATA_FIFO_SZ 0xc00
#define IPA_UL_DESC_FIFO_SZ 0x530
#define IPA_DL_DATA_FIFO_SZ 0x2400
#define IPA_DL_DESC_FIFO_SZ 0x8a0
#define IPA_DL_EMB_DATA_FIFO_SZ 0x1800
#define IPA_DL_EMB_DESC_FIFO_SZ 0x4e8

#define IPA_SMEM_UL_DATA_FIFO_OFST 0
#define IPA_SMEM_UL_DESC_FIFO_OFST 0xc00
#define IPA_SMEM_DL_DATA_FIFO_OFST 0x1130
#define IPA_SMEM_DL_DESC_FIFO_OFST 0x3530
#define IPA_SMEM_UL_EMB_DATA_FIFO_OFST 0x3dd0
#define IPA_SMEM_UL_EMB_DESC_FIFO_OFST 0x49d0

#define IPA_OCIMEM_DL_A2_DATA_FIFO_OFST 0
#define IPA_OCIMEM_DL_A2_DESC_FIFO_OFST (IPA_OCIMEM_DL_A2_DATA_FIFO_OFST + \
		IPA_DL_EMB_DATA_FIFO_SZ)
#define IPA_OCIMEM_DL_IPA_DATA_FIFO_OFST (IPA_OCIMEM_DL_A2_DESC_FIFO_OFST + \
		IPA_DL_EMB_DESC_FIFO_SZ)
#define IPA_OCIMEM_DL_IPA_DESC_FIFO_OFST (IPA_OCIMEM_DL_IPA_DATA_FIFO_OFST + \
		IPA_DL_EMB_DATA_FIFO_SZ)

enum ipa_pipe_type {
	IPA_DL_FROM_A2,
	IPA_DL_TO_IPA,
	IPA_UL_FROM_IPA,
	IPA_UL_TO_A2,
	IPA_PIPE_TYPE_MAX
};

struct ipa_bridge_pipe_context {
	struct sps_pipe *pipe;
	bool ipa_facing;
	bool valid;
};

struct ipa_bridge_context {
	struct ipa_bridge_pipe_context pipe[IPA_PIPE_TYPE_MAX];
	enum ipa_bridge_type type;
};

static struct ipa_bridge_context bridge[IPA_BRIDGE_TYPE_MAX];

static void ipa_get_dma_pipe_num(enum ipa_bridge_dir dir,
		enum ipa_bridge_type type, int *a2, int *ipa)
{
	if (type == IPA_BRIDGE_TYPE_TETHERED) {
		if (dir == IPA_BRIDGE_DIR_UL) {
			*a2 = DMA_A2_TETHERED_PIPE_UL;
			*ipa = DMA_IPA_TETHERED_PIPE_UL;
		} else {
			*a2 = DMA_A2_TETHERED_PIPE_DL;
			*ipa = DMA_IPA_TETHERED_PIPE_DL;
		}
	} else {
		if (dir == IPA_BRIDGE_DIR_UL) {
			*a2 = DMA_A2_EMBEDDED_PIPE_UL;
			*ipa = DMA_IPA_EMBEDDED_PIPE_UL;
		} else {
			*a2 = DMA_A2_EMBEDDED_PIPE_DL;
			*ipa = DMA_IPA_EMBEDDED_PIPE_DL;
		}
	}
}

static int ipa_get_desc_fifo_sz(enum ipa_bridge_dir dir,
		enum ipa_bridge_type type)
{
	int sz;

	if (type == IPA_BRIDGE_TYPE_TETHERED) {
		if (dir == IPA_BRIDGE_DIR_UL)
			sz = IPA_UL_DESC_FIFO_SZ;
		else
			sz = IPA_DL_DESC_FIFO_SZ;
	} else {
		if (dir == IPA_BRIDGE_DIR_UL)
			sz = IPA_UL_DESC_FIFO_SZ;
		else
			sz = IPA_DL_EMB_DESC_FIFO_SZ;
	}

	return sz;
}

static int ipa_get_data_fifo_sz(enum ipa_bridge_dir dir,
		enum ipa_bridge_type type)
{
	int sz;

	if (type == IPA_BRIDGE_TYPE_TETHERED) {
		if (dir == IPA_BRIDGE_DIR_UL)
			sz = IPA_UL_DATA_FIFO_SZ;
		else
			sz = IPA_DL_DATA_FIFO_SZ;
	} else {
		if (dir == IPA_BRIDGE_DIR_UL)
			sz = IPA_UL_DATA_FIFO_SZ;
		else
			sz = IPA_DL_EMB_DATA_FIFO_SZ;
	}

	return sz;
}

static int ipa_get_a2_pipe_num(enum ipa_bridge_dir dir,
		enum ipa_bridge_type type)
{
	int ep;

	if (type == IPA_BRIDGE_TYPE_TETHERED) {
		if (dir == IPA_BRIDGE_DIR_UL)
			ep = A2_TETHERED_PIPE_UL;
		else
			ep = A2_TETHERED_PIPE_DL;
	} else {
		if (dir == IPA_BRIDGE_DIR_UL)
			ep = A2_EMBEDDED_PIPE_UL;
		else
			ep = A2_EMBEDDED_PIPE_DL;
	}

	return ep;
}

int ipa_setup_ipa_dma_fifos(enum ipa_bridge_dir dir,
		enum ipa_bridge_type type,
		struct sps_mem_buffer *desc,
		struct sps_mem_buffer *data)
{
	int ret;

	ret = sps_setup_bam2bam_fifo(data,
			IPA_OCIMEM_DL_IPA_DATA_FIFO_OFST,
			ipa_get_data_fifo_sz(dir, type), 1);
	if (ret) {
		IPAERR("DAFIFO setup fail %d dir %d type %d\n",
				ret, dir, type);
		return ret;
	}

	ret = sps_setup_bam2bam_fifo(desc,
			IPA_OCIMEM_DL_IPA_DESC_FIFO_OFST,
			ipa_get_desc_fifo_sz(dir, type), 1);
	if (ret) {
		IPAERR("DEFIFO setup fail %d dir %d type %d\n",
				ret, dir, type);
		return ret;
	}

	IPADBG("dir=%d type=%d Dpa=%x Dsz=%u Dva=%p dpa=%x dsz=%u dva=%p\n",
			dir, type, data->phys_base, data->size, data->base,
			desc->phys_base, desc->size, desc->base);

	return 0;
}

int ipa_setup_a2_dma_fifos(enum ipa_bridge_dir dir,
		enum ipa_bridge_type type,
		struct sps_mem_buffer *desc,
		struct sps_mem_buffer *data)
{
	int ret;

	if (type == IPA_BRIDGE_TYPE_TETHERED) {
		if (dir == IPA_BRIDGE_DIR_UL) {
			desc->base = ipa_ctx->smem_pipe_mem +
				IPA_SMEM_UL_DESC_FIFO_OFST;
			desc->phys_base = smem_virt_to_phys(desc->base);
			desc->size = ipa_get_desc_fifo_sz(dir, type);
			data->base = ipa_ctx->smem_pipe_mem +
				IPA_SMEM_UL_DATA_FIFO_OFST;
			data->phys_base = smem_virt_to_phys(data->base);
			data->size = ipa_get_data_fifo_sz(dir, type);
		} else {
			desc->base = ipa_ctx->smem_pipe_mem +
				IPA_SMEM_DL_DESC_FIFO_OFST;
			desc->phys_base = smem_virt_to_phys(desc->base);
			desc->size = ipa_get_desc_fifo_sz(dir, type);
			data->base = ipa_ctx->smem_pipe_mem +
				IPA_SMEM_DL_DATA_FIFO_OFST;
			data->phys_base = smem_virt_to_phys(data->base);
			data->size = ipa_get_data_fifo_sz(dir, type);
		}
	} else {
		if (dir == IPA_BRIDGE_DIR_UL) {
			desc->base = ipa_ctx->smem_pipe_mem +
				IPA_SMEM_UL_EMB_DESC_FIFO_OFST;
			desc->phys_base = smem_virt_to_phys(desc->base);
			desc->size = ipa_get_desc_fifo_sz(dir, type);
			data->base = ipa_ctx->smem_pipe_mem +
				IPA_SMEM_UL_EMB_DATA_FIFO_OFST;
			data->phys_base = smem_virt_to_phys(data->base);
			data->size = ipa_get_data_fifo_sz(dir, type);
		} else {
			ret = sps_setup_bam2bam_fifo(data,
					IPA_OCIMEM_DL_A2_DATA_FIFO_OFST,
					ipa_get_data_fifo_sz(dir, type), 1);
			if (ret) {
				IPAERR("DAFIFO setup fail %d dir %d type %d\n",
						ret, dir, type);
				return ret;
			}

			ret = sps_setup_bam2bam_fifo(desc,
					IPA_OCIMEM_DL_A2_DESC_FIFO_OFST,
					ipa_get_desc_fifo_sz(dir, type), 1);
			if (ret) {
				IPAERR("DEFIFO setup fail %d dir %d type %d\n",
						ret, dir, type);
				return ret;
			}
		}
	}

	IPADBG("dir=%d type=%d Dpa=%x Dsz=%u Dva=%p dpa=%x dsz=%u dva=%p\n",
			dir, type, data->phys_base, data->size, data->base,
			desc->phys_base, desc->size, desc->base);

	return 0;
}

static int setup_dma_bam_bridge(enum ipa_bridge_dir dir,
			       enum ipa_bridge_type type,
			       struct ipa_sys_connect_params *props,
			       u32 *clnt_hdl)
{
	struct ipa_connect_params ipa_in_params;
	struct ipa_sps_params sps_out_params;
	int dma_a2_pipe;
	int dma_ipa_pipe;
	struct sps_pipe *pipe;
	struct sps_pipe *pipe_a2;
	struct sps_connect _connection;
	struct sps_connect *connection = &_connection;
	struct a2_mux_pipe_connection pipe_conn = {0};
	enum a2_mux_pipe_direction pipe_dir;
	u32 dma_hdl = sps_dma_get_bam_handle();
	u32 a2_hdl;
	u32 pa;
	int ret;

	memset(&ipa_in_params, 0, sizeof(ipa_in_params));
	memset(&sps_out_params, 0, sizeof(sps_out_params));

	pipe_dir = (dir == IPA_BRIDGE_DIR_UL) ? IPA_TO_A2 : A2_TO_IPA;

	ret = ipa_get_a2_mux_pipe_info(pipe_dir, &pipe_conn);
	if (ret) {
		IPAERR("ipa_get_a2_mux_pipe_info failed dir=%d type=%d\n",
				dir, type);
		goto fail_get_a2_prop;
	}

	pa = (dir == IPA_BRIDGE_DIR_UL) ? pipe_conn.dst_phy_addr :
					  pipe_conn.src_phy_addr;

	ret = sps_phy2h(pa, &a2_hdl);
	if (ret) {
		IPAERR("sps_phy2h failed (A2 BAM) %d dir=%d type=%d\n",
				ret, dir, type);
		goto fail_get_a2_prop;
	}

	ipa_get_dma_pipe_num(dir, type, &dma_a2_pipe, &dma_ipa_pipe);

	ipa_in_params.ipa_ep_cfg = props->ipa_ep_cfg;
	ipa_in_params.client = props->client;
	ipa_in_params.client_bam_hdl = dma_hdl;
	ipa_in_params.client_ep_idx = dma_ipa_pipe;
	ipa_in_params.priv = props->priv;
	ipa_in_params.notify = props->notify;
	ipa_in_params.desc_fifo_sz = ipa_get_desc_fifo_sz(dir, type);
	ipa_in_params.data_fifo_sz = ipa_get_data_fifo_sz(dir, type);

	if (type == IPA_BRIDGE_TYPE_EMBEDDED && dir == IPA_BRIDGE_DIR_DL) {
		if (ipa_setup_ipa_dma_fifos(dir, type, &ipa_in_params.desc,
					&ipa_in_params.data)) {
			IPAERR("fail to setup IPA-DMA FIFOs dir=%d type=%d\n",
					dir, type);
			goto fail_get_a2_prop;
		}
	}

	if (ipa_connect(&ipa_in_params, &sps_out_params, clnt_hdl)) {
		IPAERR("ipa connect failed dir=%d type=%d\n", dir, type);
		goto fail_get_a2_prop;
	}

	pipe = sps_alloc_endpoint();
	if (pipe == NULL) {
		IPAERR("sps_alloc_endpoint failed dir=%d type=%d\n", dir, type);
		ret = -ENOMEM;
		goto fail_sps_alloc;
	}

	memset(&_connection, 0, sizeof(_connection));
	ret = sps_get_config(pipe, connection);
	if (ret) {
		IPAERR("sps_get_config failed %d dir=%d type=%d\n", ret, dir,
				type);
		goto fail_sps_get_config;
	}

	if (dir == IPA_BRIDGE_DIR_DL) {
		connection->mode = SPS_MODE_SRC;
		connection->source = dma_hdl;
		connection->destination = sps_out_params.ipa_bam_hdl;
		connection->src_pipe_index = dma_ipa_pipe;
		connection->dest_pipe_index = sps_out_params.ipa_ep_idx;
	} else {
		connection->mode = SPS_MODE_DEST;
		connection->source = sps_out_params.ipa_bam_hdl;
		connection->destination = dma_hdl;
		connection->src_pipe_index = sps_out_params.ipa_ep_idx;
		connection->dest_pipe_index = dma_ipa_pipe;
	}

	connection->event_thresh = IPA_EVENT_THRESHOLD;
	connection->data = sps_out_params.data;
	connection->desc = sps_out_params.desc;
	connection->options = SPS_O_AUTO_ENABLE;

	ret = sps_connect(pipe, connection);
	if (ret) {
		IPAERR("sps_connect failed %d dir=%d type=%d\n", ret, dir,
				type);
		goto fail_sps_get_config;
	}

	if (dir == IPA_BRIDGE_DIR_DL) {
		bridge[type].pipe[IPA_DL_TO_IPA].pipe = pipe;
		bridge[type].pipe[IPA_DL_TO_IPA].ipa_facing = true;
		bridge[type].pipe[IPA_DL_TO_IPA].valid = true;
	} else {
		bridge[type].pipe[IPA_UL_FROM_IPA].pipe = pipe;
		bridge[type].pipe[IPA_UL_FROM_IPA].ipa_facing = true;
		bridge[type].pipe[IPA_UL_FROM_IPA].valid = true;
	}

	IPADBG("dir=%d type=%d (ipa) src(0x%x:%u)->dst(0x%x:%u)\n", dir, type,
			connection->source, connection->src_pipe_index,
			connection->destination, connection->dest_pipe_index);

	pipe_a2 = sps_alloc_endpoint();
	if (pipe_a2 == NULL) {
		IPAERR("sps_alloc_endpoint failed2 dir=%d type=%d\n", dir,
				type);
		ret = -ENOMEM;
		goto fail_sps_alloc_a2;
	}

	memset(&_connection, 0, sizeof(_connection));
	ret = sps_get_config(pipe_a2, connection);
	if (ret) {
		IPAERR("sps_get_config failed2 %d dir=%d type=%d\n", ret, dir,
				type);
		goto fail_sps_get_config_a2;
	}

	if (dir == IPA_BRIDGE_DIR_DL) {
		connection->mode = SPS_MODE_DEST;
		connection->source = a2_hdl;
		connection->destination = dma_hdl;
		connection->src_pipe_index = ipa_get_a2_pipe_num(dir, type);
		connection->dest_pipe_index = dma_a2_pipe;
	} else {
		connection->mode = SPS_MODE_SRC;
		connection->source = dma_hdl;
		connection->destination = a2_hdl;
		connection->src_pipe_index = dma_a2_pipe;
		connection->dest_pipe_index = ipa_get_a2_pipe_num(dir, type);
	}

	connection->event_thresh = IPA_EVENT_THRESHOLD;

	if (ipa_setup_a2_dma_fifos(dir, type, &connection->desc,
				&connection->data)) {
		IPAERR("fail to setup A2-DMA FIFOs dir=%d type=%d\n",
				dir, type);
		goto fail_sps_get_config_a2;
	}

	connection->options = SPS_O_AUTO_ENABLE;

	ret = sps_connect(pipe_a2, connection);
	if (ret) {
		IPAERR("sps_connect failed2 %d dir=%d type=%d\n", ret, dir,
				type);
		goto fail_sps_get_config_a2;
	}

	if (dir == IPA_BRIDGE_DIR_DL) {
		bridge[type].pipe[IPA_DL_FROM_A2].pipe = pipe_a2;
		bridge[type].pipe[IPA_DL_FROM_A2].valid = true;
	} else {
		bridge[type].pipe[IPA_UL_TO_A2].pipe = pipe_a2;
		bridge[type].pipe[IPA_UL_TO_A2].valid = true;
	}

	IPADBG("dir=%d type=%d (a2) src(0x%x:%u)->dst(0x%x:%u)\n", dir, type,
			connection->source, connection->src_pipe_index,
			connection->destination, connection->dest_pipe_index);

	return 0;

fail_sps_get_config_a2:
	sps_free_endpoint(pipe_a2);
fail_sps_alloc_a2:
	sps_disconnect(pipe);
fail_sps_get_config:
	sps_free_endpoint(pipe);
fail_sps_alloc:
	ipa_disconnect(*clnt_hdl);
fail_get_a2_prop:
	return ret;
}

/**
 * ipa_bridge_init()
 *
 * Return codes: 0: success, -ENOMEM: failure
 */
int ipa_bridge_init(void)
{
	int i;

	ipa_ctx->smem_pipe_mem = smem_alloc(SMEM_BAM_PIPE_MEMORY,
			IPA_SMEM_PIPE_MEM_SZ,
			0,
			SMEM_ANY_HOST_FLAG);
	if (!ipa_ctx->smem_pipe_mem) {
		IPAERR("smem alloc failed\n");
		return -ENOMEM;
	}
	IPADBG("smem_pipe_mem = %p\n", ipa_ctx->smem_pipe_mem);

	for (i = 0; i < IPA_BRIDGE_TYPE_MAX; i++)
		bridge[i].type = i;

	return 0;
}

/**
 * ipa_bridge_setup() - setup SW bridge leg
 * @dir: downlink or uplink (from air interface perspective)
 * @type: tethered or embedded bridge
 * @props: bridge leg properties (EP config, callbacks, etc)
 * @clnt_hdl: [out] handle of IPA EP belonging to bridge leg
 *
 * NOTE: IT IS CALLER'S RESPONSIBILITY TO ENSURE BAMs ARE
 * OPERATIONAL AS LONG AS BRIDGE REMAINS UP
 *
 * Return codes:
 * 0: success
 * various negative error codes on errors
 */
int ipa_bridge_setup(enum ipa_bridge_dir dir, enum ipa_bridge_type type,
		     struct ipa_sys_connect_params *props, u32 *clnt_hdl)
{
	int ret;

	if (props == NULL || clnt_hdl == NULL ||
	    type >= IPA_BRIDGE_TYPE_MAX || dir >= IPA_BRIDGE_DIR_MAX ||
	    props->client >= IPA_CLIENT_MAX) {
		IPAERR("Bad param props=%p clnt_hdl=%p type=%d dir=%d\n",
		       props, clnt_hdl, type, dir);
		return -EINVAL;
	}

	ipa_inc_client_enable_clks();

	if (setup_dma_bam_bridge(dir, type, props, clnt_hdl)) {
		IPAERR("fail to setup SYS pipe to IPA dir=%d type=%d\n",
		       dir, type);
		ret = -EINVAL;
		goto bail_ipa;
	}

	return 0;

bail_ipa:
	ipa_dec_client_disable_clks();
	return ret;
}
EXPORT_SYMBOL(ipa_bridge_setup);

/**
 * ipa_bridge_teardown() - teardown SW bridge leg
 * @dir: downlink or uplink (from air interface perspective)
 * @type: tethered or embedded bridge
 * @clnt_hdl: handle of IPA EP
 *
 * Return codes:
 * 0: success
 * various negative error codes on errors
 */
int ipa_bridge_teardown(enum ipa_bridge_dir dir, enum ipa_bridge_type type,
			u32 clnt_hdl)
{
	struct ipa_bridge_pipe_context *sys;
	int lo;
	int hi;

	if (dir >= IPA_BRIDGE_DIR_MAX || type >= IPA_BRIDGE_TYPE_MAX ||
	    clnt_hdl >= IPA_NUM_PIPES || ipa_ctx->ep[clnt_hdl].valid == 0) {
		IPAERR("Bad param dir=%d type=%d\n", dir, type);
		return -EINVAL;
	}

	if (dir == IPA_BRIDGE_DIR_UL) {
		lo = IPA_UL_FROM_IPA;
		hi = IPA_UL_TO_A2;
	} else {
		lo = IPA_DL_FROM_A2;
		hi = IPA_DL_TO_IPA;
	}

	for (; lo <= hi; lo++) {
		sys = &bridge[type].pipe[lo];
		if (sys->valid) {
			if (sys->ipa_facing)
				ipa_disconnect(clnt_hdl);
			sps_disconnect(sys->pipe);
			sps_free_endpoint(sys->pipe);
			sys->valid = false;
		}
	}

	memset(&ipa_ctx->ep[clnt_hdl], 0, sizeof(struct ipa_ep_context));

	ipa_dec_client_disable_clks();

	return 0;
}
EXPORT_SYMBOL(ipa_bridge_teardown);

bool ipa_emb_ul_pipes_empty(void)
{
	struct sps_pipe *emb_ipa_ul =
		ipa_ctx->ep[ipa_get_ep_mapping(
			IPA_CLIENT_APPS_LAN_WAN_PROD)].ep_hdl;
	struct sps_pipe *emb_ipa_to_dma =
		bridge[IPA_BRIDGE_TYPE_EMBEDDED].pipe[IPA_UL_FROM_IPA].pipe;
	struct sps_pipe *emb_dma_to_a2 =
		bridge[IPA_BRIDGE_TYPE_EMBEDDED].pipe[IPA_UL_TO_A2].pipe;
	u32 emb_ipa_ul_empty;
	u32 emb_ipa_to_dma_empty;
	u32 emb_dma_to_a2_empty;

	if (sps_is_pipe_empty(emb_ipa_ul, &emb_ipa_ul_empty)) {
		IPAERR("emb_ip_ul pipe empty check fail\n");
		return false;
	}

	if (sps_is_pipe_empty(emb_ipa_to_dma, &emb_ipa_to_dma_empty)) {
		IPAERR("emb_ipa_to_dma pipe empty check fail\n");
		return false;
	}

	if (sps_is_pipe_empty(emb_dma_to_a2, &emb_dma_to_a2_empty)) {
		IPAERR("emb_dma_to_a2 pipe empty check fail\n");
		return false;
	}

	return emb_ipa_ul_empty && emb_ipa_to_dma_empty && emb_dma_to_a2_empty;
}
EXPORT_SYMBOL(ipa_emb_ul_pipes_empty);