aboutsummaryrefslogtreecommitdiff
path: root/sys/dev/cxgb/ulp/toecore/toedev.c
blob: 01a7d902a5635dc8518729f453a2539fde59e6d4 (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

/**************************************************************************

Copyright (c) 2007, Chelsio Inc.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

 1. Redistributions of source code must retain the above copyright notice,
    this list of conditions and the following disclaimer.

 2. Neither the name of the Chelsio Corporation nor the names of its
    contributors may be used to endorse or promote products derived from
    this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.

***************************************************************************/

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/bus.h>
#include <sys/module.h>
#include <sys/queue.h>
#include <sys/mbuf.h>
#include <sys/proc.h>

#include <sys/socket.h>
#include <sys/sockio.h>

#include <net/bpf.h>
#include <net/ethernet.h>
#include <net/if.h>
#include <net/route.h>


/*
 * XXX 
 */
#include <cxgb_include.h>
#include <ulp/toecore/cxgb_toedev.h>

static struct mtx offload_db_lock;
static TAILQ_HEAD(, toedev) offload_dev_list;
static TAILQ_HEAD(, tom_info) offload_module_list;

/*
 * Returns the entry in the given table with the given offload id, or NULL
 * if the id is not found.
 */
static const struct offload_id *
id_find(unsigned int id, const struct offload_id *table)
{
	for ( ; table->id; ++table)
		if (table->id == id)
			return table;
	return NULL;
}

/*
 * Returns true if an offload device is presently attached to an offload module.
 */
static inline int
is_attached(const struct toedev *dev)
{
	return dev->tod_offload_mod != NULL;
}

/*
 * Try to attach a new offload device to an existing TCP offload module that
 * can handle the device's offload id.  Returns 0 if it succeeds.
 *
 * Must be called with the offload_db_lock held.
 */
static int
offload_attach(struct toedev *dev)
{
	struct tom_info *t;

	TAILQ_FOREACH(t, &offload_module_list, entry) {
		const struct offload_id *entry;

		entry = id_find(dev->tod_ttid, t->ti_id_table);
		if (entry && t->ti_attach(dev, entry) == 0) {
			dev->tod_offload_mod = t;
			return 0;
		}
	}
	return (ENOPROTOOPT);
}

/**
 * register_tom - register a TCP Offload Module (TOM)
 * @t: the offload module to register
 *
 * Register a TCP Offload Module (TOM).
 */
int
register_tom(struct tom_info *t)
{
	mtx_lock(&offload_db_lock);
	toedev_registration_count++;
	TAILQ_INSERT_HEAD(&offload_module_list, t, entry);
	mtx_unlock(&offload_db_lock);
	return 0;
}

/**
 * unregister_tom - unregister a TCP Offload Module (TOM)
 * @t: the offload module to register
 *
 * Unregister a TCP Offload Module (TOM).  Note that this does not affect any
 * TOE devices to which the TOM is already attached.
 */
int
unregister_tom(struct tom_info *t)
{
	mtx_lock(&offload_db_lock);
	TAILQ_REMOVE(&offload_module_list, t, entry);
	mtx_unlock(&offload_db_lock);
	return 0;
}

/*
 * Find an offload device by name.  Must be called with offload_db_lock held.
 */
static struct toedev *
__find_offload_dev_by_name(const char *name)
{
	struct toedev *dev;

	TAILQ_FOREACH(dev, &offload_dev_list, entry) {
		if (!strncmp(dev->tod_name, name, TOENAMSIZ))
			return dev;
	}
	return NULL;
}

/*
 * Returns true if an offload device is already registered.
 * Must be called with the offload_db_lock held.
 */
static int
is_registered(const struct toedev *dev)
{
	struct toedev *d;

	TAILQ_FOREACH(d, &offload_dev_list, entry) {
		if (d == dev)
			return 1;
	}
	return 0;
}

/*
 * Finalize the name of an offload device by assigning values to any format
 * strings in its name.
 */
static int
assign_name(struct toedev *dev, const char *name, int limit)
{
	int i;

	for (i = 0; i < limit; ++i) {
		char s[TOENAMSIZ];

		if (snprintf(s, sizeof(s), name, i) >= sizeof(s))
			return -1;                  /* name too long */
		if (!__find_offload_dev_by_name(s)) {
			strcpy(dev->tod_name, s);
			return 0;
		}
	}
	return -1;
}

/**
 * register_toedev - register a TOE device
 * @dev: the device
 * @name: a name template for the device
 *
 * Register a TOE device and try to attach an appropriate TCP offload module
 * to it.  @name is a template that may contain at most one %d format
 * specifier.
 */
int
register_toedev(struct toedev *dev, const char *name)
{
	int ret;
	const char *p;

	/*
	 * Validate the name template.  Only one %d allowed and name must be
	 * a valid filename so it can appear in sysfs.
	 */
	if (!name || !*name || !strcmp(name, ".") || !strcmp(name, "..") ||
	    strchr(name, '/'))
		return EINVAL;

	p = strchr(name, '%');
	if (p && (p[1] != 'd' || strchr(p + 2, '%')))
		return EINVAL;

	mtx_lock(&offload_db_lock);
	if (is_registered(dev)) {  /* device already registered */
		ret = EEXIST;
		goto out;
	}

	if ((ret = assign_name(dev, name, 32)) != 0)
		goto out;

	dev->tod_offload_mod = NULL;
	TAILQ_INSERT_TAIL(&offload_dev_list, dev, entry);
out:
	mtx_unlock(&offload_db_lock);
	return ret;
}

/**
 * unregister_toedev - unregister a TOE device
 * @dev: the device
 *
 * Unregister a TOE device.  The device must not be attached to an offload
 * module.
 */
int
unregister_toedev(struct toedev *dev)
{
	int ret = 0;

	mtx_lock(&offload_db_lock);
	if (!is_registered(dev)) {
		ret = ENODEV;
		goto out;
	}
	if (is_attached(dev)) {
		ret = EBUSY;
		goto out;
	}
	TAILQ_REMOVE(&offload_dev_list, dev, entry);
out:
	mtx_unlock(&offload_db_lock);
	return ret;
}

/**
 * activate_offload - activate an offload device
 * @dev: the device
 *
 * Activate an offload device by locating an appropriate registered offload
 * module.  If no module is found the operation fails and may be retried at
 * a later time.
 */
int
activate_offload(struct toedev *dev)
{
	int ret = 0;

	mtx_lock(&offload_db_lock);
	if (!is_registered(dev))
		ret = ENODEV;
	else if (!is_attached(dev))
		ret = offload_attach(dev);
	mtx_unlock(&offload_db_lock);
	return ret;
}

/**
 * toe_send - send a packet to a TOE device
 * @dev: the device
 * @m: the packet
 *
 * Sends an mbuf to a TOE driver after dealing with any active network taps.
 */
int
toe_send(struct toedev *dev, struct mbuf *m)
{
	int r;

	critical_enter(); /* XXX neccessary? */
	r = dev->tod_send(dev, m);
	critical_exit();
	if (r)
		BPF_MTAP(dev->tod_lldev, m);
	return r;
}

/**
 * toe_receive_mbuf - process n received TOE packets
 * @dev: the toe device
 * @m: an array of offload packets
 * @n: the number of offload packets
 *
 * Process an array of ingress offload packets.  Each packet is forwarded
 * to any active network taps and then passed to the toe device's receive
 * method.  We optimize passing packets to the receive method by passing
 * it the whole array at once except when there are active taps.
 */
int
toe_receive_mbuf(struct toedev *dev, struct mbuf **m, int n)
{
	if (__predict_true(!bpf_peers_present(dev->tod_lldev->if_bpf)))
		return dev->tod_recv(dev, m, n);

	for ( ; n; n--, m++) {
		m[0]->m_pkthdr.rcvif = dev->tod_lldev;
		BPF_MTAP(dev->tod_lldev, m[0]);
		dev->tod_recv(dev, m, 1);
	}
	return 0;
}

static inline int
ifnet_is_offload(const struct ifnet *ifp)
{
	return (ifp->if_flags & IFCAP_TOE);
}

void
toe_arp_update(struct rtentry *rt)
{
	struct ifnet *ifp = rt->rt_ifp;

	if (ifp && ifnet_is_offload(ifp)) {
		struct toedev *tdev = TOEDEV(ifp);

		if (tdev && tdev->tod_arp_update)
			tdev->tod_arp_update(tdev, rt);
	}
}

/**
 * offload_get_phys_egress - find the physical egress device
 * @root_dev: the root device anchoring the search
 * @so: the socket used to determine egress port in bonding mode
 * @context: in bonding mode, indicates a connection set up or failover
 *
 * Given a root network device it returns the physical egress device that is a
 * descendant of the root device.  The root device may be either a physical
 * device, in which case it is the device returned, or a virtual device, such
 * as a VLAN or bonding device.  In case of a bonding device the search
 * considers the decisions of the bonding device given its mode to locate the
 * correct egress device.
 */
struct ifnet *
offload_get_phys_egress(struct ifnet *root_dev, struct socket *so, int context)
{

#if 0
	while (root_dev && ifnet_is_offload(root_dev)) {
		if (root_dev->tod_priv_flags & IFF_802_1Q_VLAN)
			root_dev = VLAN_DEV_INFO(root_dev)->real_dev;
		else if (root_dev->tod_flags & IFF_MASTER)
			root_dev = toe_bond_get_slave(root_dev, sk, context);
		else
			break;
	}
#endif
	return root_dev;
}

static int
toecore_load(module_t mod, int cmd, void *arg)
{
	int err = 0;

	switch (cmd) {
	case MOD_LOAD:
		mtx_init(&offload_db_lock, "toedev lock", NULL, MTX_DEF);
		TAILQ_INIT(&offload_dev_list);
		TAILQ_INIT(&offload_module_list);
		break;
	case MOD_QUIESCE:
		break;
	case MOD_UNLOAD:
		mtx_lock(&offload_db_lock);
		if (!TAILQ_EMPTY(&offload_dev_list) ||
		    !TAILQ_EMPTY(&offload_module_list)) {
			err = EBUSY;
			mtx_unlock(&offload_db_lock);
			break;
		}
		mtx_unlock(&offload_db_lock);
		mtx_destroy(&offload_db_lock);
		break;
	case MOD_SHUTDOWN:
		break;
	default:
		err = EOPNOTSUPP;
		break;
	}

	return (err);
}


static moduledata_t mod_data= {
	"toecore",
	toecore_load,
	0
};

MODULE_VERSION(toecore, 1);
DECLARE_MODULE(toecore, mod_data, SI_SUB_EXEC, SI_ORDER_ANY);