aboutsummaryrefslogtreecommitdiff
path: root/drivers/platform/msm/ipa/ipa_v3/ethernet/ipa_eth_offload.c
blob: 77d92d9ae82f85bb97e1003279db297a6e846afa (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
/* Copyright (c) 2019 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 "ipa_eth_i.h"

static LIST_HEAD(ipa_eth_offload_drivers);
static DEFINE_MUTEX(ipa_eth_offload_drivers_lock);

static struct dentry *ipa_eth_offload_debugfs;

static void ipa_eth_offload_debugfs_cleanup(void)
{
	debugfs_remove_recursive(ipa_eth_offload_debugfs);
}

static int ipa_eth_offload_debugfs_init(struct dentry *dbgfs_root)
{
	if (!dbgfs_root)
		return 0;

	ipa_eth_offload_debugfs = debugfs_create_dir("offload", dbgfs_root);
	if (IS_ERR_OR_NULL(ipa_eth_offload_debugfs)) {
		int rc = ipa_eth_offload_debugfs ?
			PTR_ERR(ipa_eth_offload_debugfs) : -EFAULT;

		ipa_eth_offload_debugfs = NULL;
		return rc;
	}

	return 0;
}

int ipa_eth_offload_init(struct ipa_eth_device *eth_dev)
{
	int rc;

	rc = eth_dev->od->ops->init_tx(eth_dev);
	if (rc) {
		ipa_eth_dev_err(eth_dev, "Failed to init offload for tx");
		return rc;
	}

	rc = eth_dev->od->ops->init_rx(eth_dev);
	if (rc) {
		ipa_eth_dev_err(eth_dev, "Failed to init offload for rx");
		(void) eth_dev->od->ops->deinit_tx(eth_dev);
		return rc;
	}

	return 0;
}

int ipa_eth_offload_deinit(struct ipa_eth_device *eth_dev)
{
	int rc_rx, rc_tx;

	rc_rx = eth_dev->od->ops->deinit_rx(eth_dev);
	if (rc_rx)
		ipa_eth_dev_err(eth_dev, "Failed to deinit offload for rx");

	rc_tx = eth_dev->od->ops->deinit_tx(eth_dev);
	if (rc_tx)
		ipa_eth_dev_err(eth_dev, "Failed to deinit offload for tx");

	return rc_rx || rc_tx;
}

int ipa_eth_offload_start(struct ipa_eth_device *eth_dev)
{
	int rc;

	rc = eth_dev->od->ops->start_tx(eth_dev);
	if (rc) {
		ipa_eth_dev_err(eth_dev, "Failed to start offload for tx");
		return rc;
	}

	rc = eth_dev->od->ops->start_rx(eth_dev);
	if (rc) {
		ipa_eth_dev_err(eth_dev, "Failed to start offload for rx");
		(void) eth_dev->od->ops->stop_tx(eth_dev);
		return rc;
	}

	return 0;
}

int ipa_eth_offload_stop(struct ipa_eth_device *eth_dev)
{
	int rc_rx, rc_tx;

	rc_rx = eth_dev->od->ops->stop_rx(eth_dev);
	if (rc_rx)
		ipa_eth_dev_err(eth_dev, "Failed to stop offload for rx");

	rc_tx = eth_dev->od->ops->stop_tx(eth_dev);
	if (rc_tx)
		ipa_eth_dev_err(eth_dev, "Failed to stop offload for tx");

	return rc_rx || rc_tx;
}

static int __try_pair_device(struct ipa_eth_device *eth_dev,
			   struct ipa_eth_offload_driver *od)
{
	int rc = od->ops->pair(eth_dev);

	if (rc) {
		ipa_eth_dev_dbg(eth_dev,
			"Offload driver %s passed up paring with %s",
			od->name, eth_dev->nd->name);
		return rc;
	}

	ipa_eth_dev_log(eth_dev,
		"Offload driver %s is paired with device from %s",
		od->name, eth_dev->nd->name);

	eth_dev->od = od;

	ipa_eth_dev_log(eth_dev,
		"Offload driver %s successfully paired with device from %s",
		od->name, eth_dev->nd->name);

	return 0;
}

static int try_pair_device(struct ipa_eth_device *eth_dev,
			   struct ipa_eth_offload_driver *od)
{
	if (od->bus != eth_dev->dev->bus) {
		ipa_eth_dev_dbg(eth_dev,
			"Offload driver %s is not a bus match for %s",
			od->name, eth_dev->nd->name);

		return -ENOTSUPP;
	}

	return __try_pair_device(eth_dev, od);
}

int ipa_eth_offload_pair_device(struct ipa_eth_device *eth_dev)
{
	struct ipa_eth_offload_driver *od;

	if (ipa_eth_offload_device_paired(eth_dev))
		return 0;

	list_for_each_entry(od, &ipa_eth_offload_drivers, driver_list) {
		if (!try_pair_device(eth_dev, od))
			return 0;
	}

	return -ENODEV;
}

void ipa_eth_offload_unpair_device(struct ipa_eth_device *eth_dev)
{
	struct ipa_eth_offload_driver *od = eth_dev->od;

	if (!ipa_eth_offload_device_paired(eth_dev))
		return;

	eth_dev->od = NULL;

	od->ops->unpair(eth_dev);
}

int ipa_eth_offload_register_driver(struct ipa_eth_offload_driver *od)
{
	if (!od->bus) {
		ipa_eth_err("Bus info missing for offload driver %s", od->name);
		return -EINVAL;
	}

	if (!od->ops || !od->ops->pair  || !od->ops->unpair) {
		ipa_eth_err("Pair ops missing for offload driver %s", od->name);
		return -EINVAL;
	}

	if (!od->debugfs && ipa_eth_offload_debugfs) {
		od->debugfs =
			debugfs_create_dir(od->name, ipa_eth_offload_debugfs);
		if (IS_ERR_OR_NULL(od->debugfs)) {
			int rc = od->debugfs ? PTR_ERR(od->debugfs) : -EFAULT;

			od->debugfs = NULL;
			return rc;
		}
	}

	mutex_lock(&ipa_eth_offload_drivers_lock);
	list_add(&od->driver_list, &ipa_eth_offload_drivers);
	mutex_unlock(&ipa_eth_offload_drivers_lock);

	return 0;
}

void ipa_eth_offload_unregister_driver(struct ipa_eth_offload_driver *od)
{
	debugfs_remove_recursive(od->debugfs);

	mutex_lock(&ipa_eth_offload_drivers_lock);
	list_del(&od->driver_list);
	mutex_unlock(&ipa_eth_offload_drivers_lock);
}

int ipa_eth_offload_save_regs(struct ipa_eth_device *eth_dev)
{
	struct ipa_eth_offload_driver *od = eth_dev->od;

	if (od && od->ops->save_regs)
		return eth_dev->od->ops->save_regs(eth_dev, NULL, NULL);

	return 0;
}

int ipa_eth_offload_modinit(struct dentry *dbgfs_root)
{
	int rc;

	rc = ipa_eth_offload_debugfs_init(dbgfs_root);
	if (rc) {
		ipa_eth_err("Failed to init offload module debugfs");
		return rc;
	}

	ipa_eth_log("Offload sub-system offload module init is complete");

	return 0;
}

void ipa_eth_offload_modexit(void)
{
	ipa_eth_log("De-initing offload sub-system offload module");
	ipa_eth_offload_debugfs_cleanup();
}