aboutsummaryrefslogtreecommitdiffstats
blob: 062f733f9e3eb0af1a09377bb77bb80c5296a7e5 (plain) (blame)
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
/*
 * Copyright (C) 2012 Texas Instruments Incorporated
 * Authors: Reece Pollack <reece@theptrgroup.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation version 2.
 *
 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
 * kind, whether express or implied; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <linux/io.h>
#include <linux/clk.h>
#include <linux/slab.h>
#include <linux/delay.h>
#include <linux/types.h>
#include <linux/module.h>
#include <linux/delay.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/of_address.h>
#include <linux/firmware.h>
#include <linux/spinlock.h>
#include <linux/interrupt.h>
#include <linux/dmaengine.h>
#include <linux/dma-mapping.h>
#include <linux/scatterlist.h>
#include <linux/byteorder/generic.h>
#include <linux/platform_device.h>
#include <linux/keystone-dma.h>
#include <linux/errqueue.h>

#include "keystone_net.h"

#define	QOS_TXHOOK_ORDER	20

struct qos_channel {
	const char		*tx_chan_name;
	u32			 tx_queue_depth;
	struct netcp_tx_pipe	 tx_pipe;
	struct kobject		 kobj;
};

struct qos_device {
	struct netcp_device		*netcp_device;
	struct device			*dev;
	struct device_node		*node;
	u32				 multi_if;
	struct kobject			 kobj;
};

struct qos_intf {
	struct qos_device		*qdev;
	struct net_device		*ndev;
	struct device			*dev;
	struct kobject			 kobj;
	int				 num_channels;
	int				 max_channels;
	struct qos_channel		 channels[1];
	/* NB: channels is allocated dynamically */
};

/*
 *  Sysfs stuff related to QoS TX Channels
 */
#define kobj_to_qchan(kobj) container_of(kobj, struct qos_channel, kobj)

static ssize_t qos_txchan_show(struct kobject *kobj,
			struct kobj_attribute *attr,
			char *buf)
{
	struct qos_channel *qos_chan = kobj_to_qchan(kobj);
	return snprintf(buf, PAGE_SIZE, "%s\n", qos_chan->tx_chan_name);
}

struct kobj_attribute qos_txchan_attr =
	__ATTR(tx-channel, S_IRUGO, qos_txchan_show, NULL);


static ssize_t qos_txdepth_show(struct kobject *kobj,
			struct kobj_attribute *attr,
			char *buf)
{
	struct qos_channel *qos_chan = kobj_to_qchan(kobj);
	return snprintf(buf, PAGE_SIZE, "%u\n", qos_chan->tx_queue_depth);
}

static ssize_t qos_txdepth_store(struct kobject *kobj,
			struct kobj_attribute *attr,
			const char *buf, size_t count)
{
	struct qos_channel *qos_chan = kobj_to_qchan(kobj);
	struct netcp_tx_pipe *tx_pipe = &qos_chan->tx_pipe;
	unsigned int val;

	if (kstrtouint(buf, 0, &val) < 0)
		return -EINVAL;

	qos_chan->tx_queue_depth = val;
	tx_pipe->dma_queue_depth = val;

	return count;
}

struct kobj_attribute qos_txdepth_attr =
	__ATTR(tx_queue_depth, S_IRUGO | S_IWUSR,
		qos_txdepth_show, qos_txdepth_store);

static struct attribute *qos_txchan_attrs[] = {
		&qos_txchan_attr.attr,
		&qos_txdepth_attr.attr,
		NULL
};

static struct kobj_type qos_chan_ktype = {
	.sysfs_ops = &kobj_sysfs_ops,
	.default_attrs = qos_txchan_attrs,
};

/* Sysfs stuff related to QoS Interfaces */
static struct kobj_type qos_intf_ktype = {
	.sysfs_ops = &kobj_sysfs_ops,
};

/* Sysfs stuff related to QoS Instances */
#define	kobj_to_qdev(kobj) container_of(kobj, struct qos_device, kobj)
static ssize_t qos_multi_if_show(struct kobject *kobj,
			struct kobj_attribute *attr,
			char *buf)
{
	struct qos_device *qos_dev = kobj_to_qdev(kobj);
	return snprintf(buf, PAGE_SIZE, "%u\n", qos_dev->multi_if);
}

struct kobj_attribute qos_multi_if_attr =
	__ATTR(multi-interface, S_IRUGO, qos_multi_if_show, NULL);

static struct attribute *qos_inst_attrs[] = {
		&qos_multi_if_attr.attr,
		NULL
};

static struct kobj_type qos_inst_ktype = {
	.sysfs_ops = &kobj_sysfs_ops,
	.default_attrs = qos_inst_attrs,
};


static int qos_tx_hook(int order, void *data, struct netcp_packet *p_info)
{
	struct qos_intf *qos_intf = data;
	struct sk_buff *skb = p_info->skb;

	dev_dbg(qos_intf->dev,
		"priority: %u, queue_mapping: %04x\n",
		skb->priority, skb_get_queue_mapping(skb));

	if (skb->queue_mapping < qos_intf->num_channels)
		p_info->tx_pipe =
			&qos_intf->channels[skb->queue_mapping].tx_pipe;
	else {
		dev_warn(qos_intf->dev,
			"queue mapping (%d) >= num chans (%d) QoS bypassed\n",
			 skb_get_queue_mapping(skb), qos_intf->num_channels);
	}

	return 0;
}


static int qos_close(void *intf_priv, struct net_device *ndev)
{
	struct qos_intf *qos_intf = intf_priv;
	struct netcp_priv *netcp_priv = netdev_priv(ndev);
	int i;

	netcp_unregister_txhook(netcp_priv, QOS_TXHOOK_ORDER, qos_tx_hook,
				qos_intf);

	for (i = 0; i < qos_intf->num_channels; ++i) {
		struct qos_channel *qchan = &qos_intf->channels[i];

		netcp_txpipe_close(&qchan->tx_pipe);
	}

	return 0;
}

static int qos_open(void *intf_priv, struct net_device *ndev)
{
	struct qos_intf *qos_intf = intf_priv;
	struct netcp_priv *netcp_priv = netdev_priv(ndev);
	int ret;
	int i;

	/* Open the QoS input queues */
	for (i = 0; i < qos_intf->num_channels; ++i) {
		struct qos_channel *qchan = &qos_intf->channels[i];

		ret = netcp_txpipe_open(&qchan->tx_pipe);
		if (ret)
			goto fail;
	}

	netcp_register_txhook(netcp_priv, QOS_TXHOOK_ORDER, qos_tx_hook,
			      intf_priv);

	return 0;

fail:
	qos_close(intf_priv, ndev);
	return ret;
}

static int init_channel(struct qos_intf *qos_intf,
			int index,
			struct device_node *node)
{
	struct qos_channel *qchan = &qos_intf->channels[index];
	int ret;

	ret = of_property_read_string(node, "tx-channel", &qchan->tx_chan_name);
	if (ret < 0) {
		dev_err(qos_intf->dev,
			"missing tx-channel parameter, err %d\n", ret);
		qchan->tx_chan_name = "qos";
	}
	dev_dbg(qos_intf->dev, "tx-channel \"%s\"\n", qchan->tx_chan_name);

	ret = of_property_read_u32(node, "tx_queue_depth",
				   &qchan->tx_queue_depth);
	if (ret < 0) {
		dev_err(qos_intf->dev,
			"missing tx_queue_depth parameter, err %d\n", ret);
		qchan->tx_queue_depth = 16;
	}
	dev_dbg(qos_intf->dev, "tx_queue_depth %u\n", qchan->tx_queue_depth);

	/* Create the per-channel entry and attributes */
	ret = kobject_init_and_add(&qchan->kobj, &qos_chan_ktype,
			kobject_get(&qos_intf->kobj), node->name);
	if (ret) {
		dev_err(qos_intf->dev,
			"failed to create %s/%s/%s sysfs entry\n",
			qos_intf->qdev->kobj.name, qos_intf->kobj.name,
			node->name);
		kobject_put(&qchan->kobj);
		kobject_put(&qos_intf->kobj);
		return ret;
	}

	return 0;
}

static int qos_attach(void *inst_priv, struct net_device *ndev,
		      void **intf_priv)
{
	struct netcp_priv *netcp = netdev_priv(ndev);
	struct qos_device *qos_dev = inst_priv;
	struct qos_intf *qos_intf;
	struct device_node *interface, *channel;
	char node_name[24];
	int i, ret;
	int max_channels, size;

	max_channels = netcp->ndev->num_tx_queues;
	size = sizeof(struct qos_intf) +
			((max_channels - 1) * sizeof(struct qos_channel));
	qos_intf = devm_kzalloc(qos_dev->dev, size, GFP_KERNEL);
	if (!qos_intf) {
		dev_err(qos_dev->dev,
			"qos interface memory allocation failed\n");
		return -ENOMEM;
	}
	qos_intf->max_channels = max_channels;

	qos_intf->qdev = qos_dev;
	qos_intf->ndev = ndev;
	qos_intf->dev = qos_dev->dev;

	snprintf(node_name, sizeof(node_name), "interface-%d",
		 (qos_dev->multi_if) ? (netcp->cpsw_port - 1) : 0);

	*intf_priv = qos_intf;

	interface = of_get_child_by_name(qos_dev->node, node_name);
	if (!interface) {
		dev_err(qos_intf->dev,
			"could not find interface %d node in device tree\n",
			(netcp->cpsw_port - 1));
		ret = -ENODEV;
		goto exit;
	}

	/* Create the per-interface sysfs entry */
	ret = kobject_init_and_add(&qos_intf->kobj, &qos_intf_ktype,
			kobject_get(&qos_intf->qdev->kobj), node_name);
	if (ret) {
		dev_err(qos_intf->dev, "failed to create %s/%s sysfs entry\n",
			qos_intf->qdev->kobj.name, node_name);
		kobject_put(&qos_intf->kobj);
		kobject_put(&qos_intf->qdev->kobj);
		goto exit;
	}

	qos_intf->num_channels = 0;
	for_each_child_of_node(interface, channel) {
		if (qos_intf->num_channels >= max_channels) {
			dev_err(qos_intf->dev,
				"too many QoS input channels defined\n");
			break;
		}
		init_channel(qos_intf, qos_intf->num_channels, channel);
		++qos_intf->num_channels;
	}

	of_node_put(interface);

	/* Initialize the QoS input queues */
	for (i = 0; i < qos_intf->num_channels; ++i) {
		struct qos_channel *qchan = &qos_intf->channels[i];

		netcp_txpipe_init(&qchan->tx_pipe, netdev_priv(ndev),
				  qchan->tx_chan_name,
				  qchan->tx_queue_depth);

		qchan->tx_pipe.dma_psflags = netcp->cpsw_port;
	}

	return 0;
exit:
	devm_kfree(qos_dev->dev, qos_intf);
	return ret;
}

static int qos_release(void *intf_priv)
{
	struct qos_intf *qos_intf = intf_priv;

	kfree(qos_intf);

	return 0;
}

static int qos_remove(struct netcp_device *netcp_device, void *inst_priv)
{
	struct qos_device *qos_dev = inst_priv;

	kfree(qos_dev);

	return 0;
}

static int qos_probe(struct netcp_device *netcp_device,
		    struct device *dev,
		    struct device_node *node,
		    void **inst_priv)
{
	struct qos_device *qos_dev;
	int ret = 0;

	qos_dev = devm_kzalloc(dev, sizeof(struct qos_device), GFP_KERNEL);
	if (!qos_dev) {
		dev_err(dev, "memory allocation failed\n");
		return -ENOMEM;
	}
	*inst_priv = qos_dev;

	if (!node) {
		dev_err(dev, "device tree info unavailable\n");
		ret = -ENODEV;
		goto exit;
	}

	qos_dev->netcp_device = netcp_device;
	qos_dev->dev = dev;
	qos_dev->node = node;

	if (of_find_property(node, "multi-interface", NULL))
		qos_dev->multi_if = 1;

	/* Create the per-instance sysfs entry */
	ret = kobject_init_and_add(&qos_dev->kobj, &qos_inst_ktype,
		kobject_get(&dev->kobj), node->name);
	if (ret) {
		dev_err(dev, "failed to create %s sysfs entry\n",
			node->name);
		kobject_put(&qos_dev->kobj);
		kobject_put(&dev->kobj);
		goto exit;
	}

	return 0;

exit:
	qos_remove(netcp_device, qos_dev);
	*inst_priv = NULL;
	return ret;
}


static struct netcp_module qos_module = {
	.name		= "keystone-qos",
	.owner		= THIS_MODULE,
	.probe		= qos_probe,
	.open		= qos_open,
	.close		= qos_close,
	.remove		= qos_remove,
	.attach		= qos_attach,
	.release	= qos_release,
};

static int __init keystone_qos_init(void)
{
	return netcp_register_module(&qos_module);
}
module_init(keystone_qos_init);

static void __exit keystone_qos_exit(void)
{
	netcp_unregister_module(&qos_module);
}
module_exit(keystone_qos_exit);

MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("Reece Pollack <reece@theptrgroup.com");
MODULE_DESCRIPTION("Quality of Service driver for Keystone devices");