1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23#include <linux/slab.h>
24#include <linux/module.h>
25#include <linux/netdevice.h>
26#include <linux/etherdevice.h>
27#include "xp.h"
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44struct xpnet_message {
45 u16 version;
46 u16 embedded_bytes;
47 u32 magic;
48 unsigned long buf_pa;
49 u32 size;
50 u8 leadin_ignore;
51 u8 tailout_ignore;
52 unsigned char data;
53};
54
55
56
57
58
59
60
61#define XPNET_MSG_SIZE XPC_MSG_PAYLOAD_MAX_SIZE
62#define XPNET_MSG_DATA_MAX \
63 (XPNET_MSG_SIZE - offsetof(struct xpnet_message, data))
64#define XPNET_MSG_NENTRIES (PAGE_SIZE / XPC_MSG_MAX_SIZE)
65
66#define XPNET_MAX_KTHREADS (XPNET_MSG_NENTRIES + 1)
67#define XPNET_MAX_IDLE_KTHREADS (XPNET_MSG_NENTRIES + 1)
68
69
70
71
72
73#define _XPNET_VERSION(_major, _minor) (((_major) << 4) | (_minor))
74#define XPNET_VERSION_MAJOR(_v) ((_v) >> 4)
75#define XPNET_VERSION_MINOR(_v) ((_v) & 0xf)
76
77#define XPNET_VERSION _XPNET_VERSION(1, 0)
78#define XPNET_VERSION_EMBED _XPNET_VERSION(1, 1)
79#define XPNET_MAGIC 0x88786984
80
81#define XPNET_VALID_MSG(_m) \
82 ((XPNET_VERSION_MAJOR(_m->version) == XPNET_VERSION_MAJOR(XPNET_VERSION)) \
83 && (msg->magic == XPNET_MAGIC))
84
85#define XPNET_DEVICE_NAME "xp0"
86
87
88
89
90
91
92
93
94struct xpnet_pending_msg {
95 struct sk_buff *skb;
96 atomic_t use_count;
97};
98
99struct net_device *xpnet_device;
100
101
102
103
104
105static unsigned long *xpnet_broadcast_partitions;
106
107static DEFINE_SPINLOCK(xpnet_broadcast_lock);
108
109
110
111
112
113
114
115
116
117
118
119
120#define XPNET_MAX_MTU (0x800000UL - L1_CACHE_BYTES)
121
122#define XPNET_MIN_MTU 68
123
124#define XPNET_DEF_MTU (0x8000UL)
125
126
127
128
129
130#define XPNET_PARTID_OCTET 2
131
132
133
134struct device_driver xpnet_dbg_name = {
135 .name = "xpnet"
136};
137
138struct device xpnet_dbg_subname = {
139 .init_name = "",
140 .driver = &xpnet_dbg_name
141};
142
143struct device *xpnet = &xpnet_dbg_subname;
144
145
146
147
148static void
149xpnet_receive(short partid, int channel, struct xpnet_message *msg)
150{
151 struct sk_buff *skb;
152 void *dst;
153 enum xp_retval ret;
154
155 if (!XPNET_VALID_MSG(msg)) {
156
157
158
159 xpc_received(partid, channel, (void *)msg);
160
161 xpnet_device->stats.rx_errors++;
162
163 return;
164 }
165 dev_dbg(xpnet, "received 0x%lx, %d, %d, %d\n", msg->buf_pa, msg->size,
166 msg->leadin_ignore, msg->tailout_ignore);
167
168
169 skb = dev_alloc_skb(msg->size + L1_CACHE_BYTES);
170 if (!skb) {
171 dev_err(xpnet, "failed on dev_alloc_skb(%d)\n",
172 msg->size + L1_CACHE_BYTES);
173
174 xpc_received(partid, channel, (void *)msg);
175
176 xpnet_device->stats.rx_errors++;
177
178 return;
179 }
180
181
182
183
184
185
186 skb_reserve(skb, (L1_CACHE_BYTES - ((u64)skb->data &
187 (L1_CACHE_BYTES - 1)) +
188 msg->leadin_ignore));
189
190
191
192
193
194 skb_put(skb, (msg->size - msg->leadin_ignore - msg->tailout_ignore));
195
196
197
198
199 if ((XPNET_VERSION_MINOR(msg->version) == 1) &&
200 (msg->embedded_bytes != 0)) {
201 dev_dbg(xpnet, "copying embedded message. memcpy(0x%p, 0x%p, "
202 "%lu)\n", skb->data, &msg->data,
203 (size_t)msg->embedded_bytes);
204
205 skb_copy_to_linear_data(skb, &msg->data,
206 (size_t)msg->embedded_bytes);
207 } else {
208 dst = (void *)((u64)skb->data & ~(L1_CACHE_BYTES - 1));
209 dev_dbg(xpnet, "transferring buffer to the skb->data area;\n\t"
210 "xp_remote_memcpy(0x%p, 0x%p, %hu)\n", dst,
211 (void *)msg->buf_pa, msg->size);
212
213 ret = xp_remote_memcpy(xp_pa(dst), msg->buf_pa, msg->size);
214 if (ret != xpSuccess) {
215
216
217
218
219
220 dev_err(xpnet, "xp_remote_memcpy(0x%p, 0x%p, 0x%hx) "
221 "returned error=0x%x\n", dst,
222 (void *)msg->buf_pa, msg->size, ret);
223
224 xpc_received(partid, channel, (void *)msg);
225
226 xpnet_device->stats.rx_errors++;
227
228 return;
229 }
230 }
231
232 dev_dbg(xpnet, "<skb->head=0x%p skb->data=0x%p skb->tail=0x%p "
233 "skb->end=0x%p skb->len=%d\n", (void *)skb->head,
234 (void *)skb->data, skb_tail_pointer(skb), skb_end_pointer(skb),
235 skb->len);
236
237 skb->protocol = eth_type_trans(skb, xpnet_device);
238 skb->ip_summed = CHECKSUM_UNNECESSARY;
239
240 dev_dbg(xpnet, "passing skb to network layer\n"
241 "\tskb->head=0x%p skb->data=0x%p skb->tail=0x%p "
242 "skb->end=0x%p skb->len=%d\n",
243 (void *)skb->head, (void *)skb->data, skb_tail_pointer(skb),
244 skb_end_pointer(skb), skb->len);
245
246 xpnet_device->stats.rx_packets++;
247 xpnet_device->stats.rx_bytes += skb->len + ETH_HLEN;
248
249 netif_rx_ni(skb);
250 xpc_received(partid, channel, (void *)msg);
251}
252
253
254
255
256
257static void
258xpnet_connection_activity(enum xp_retval reason, short partid, int channel,
259 void *data, void *key)
260{
261 DBUG_ON(partid < 0 || partid >= xp_max_npartitions);
262 DBUG_ON(channel != XPC_NET_CHANNEL);
263
264 switch (reason) {
265 case xpMsgReceived:
266 DBUG_ON(data == NULL);
267
268 xpnet_receive(partid, channel, (struct xpnet_message *)data);
269 break;
270
271 case xpConnected:
272 spin_lock_bh(&xpnet_broadcast_lock);
273 __set_bit(partid, xpnet_broadcast_partitions);
274 spin_unlock_bh(&xpnet_broadcast_lock);
275
276 netif_carrier_on(xpnet_device);
277
278 dev_dbg(xpnet, "%s connected to partition %d\n",
279 xpnet_device->name, partid);
280 break;
281
282 default:
283 spin_lock_bh(&xpnet_broadcast_lock);
284 __clear_bit(partid, xpnet_broadcast_partitions);
285 spin_unlock_bh(&xpnet_broadcast_lock);
286
287 if (bitmap_empty((unsigned long *)xpnet_broadcast_partitions,
288 xp_max_npartitions)) {
289 netif_carrier_off(xpnet_device);
290 }
291
292 dev_dbg(xpnet, "%s disconnected from partition %d\n",
293 xpnet_device->name, partid);
294 break;
295 }
296}
297
298static int
299xpnet_dev_open(struct net_device *dev)
300{
301 enum xp_retval ret;
302
303 dev_dbg(xpnet, "calling xpc_connect(%d, 0x%p, NULL, %ld, %ld, %ld, "
304 "%ld)\n", XPC_NET_CHANNEL, xpnet_connection_activity,
305 (unsigned long)XPNET_MSG_SIZE,
306 (unsigned long)XPNET_MSG_NENTRIES,
307 (unsigned long)XPNET_MAX_KTHREADS,
308 (unsigned long)XPNET_MAX_IDLE_KTHREADS);
309
310 ret = xpc_connect(XPC_NET_CHANNEL, xpnet_connection_activity, NULL,
311 XPNET_MSG_SIZE, XPNET_MSG_NENTRIES,
312 XPNET_MAX_KTHREADS, XPNET_MAX_IDLE_KTHREADS);
313 if (ret != xpSuccess) {
314 dev_err(xpnet, "ifconfig up of %s failed on XPC connect, "
315 "ret=%d\n", dev->name, ret);
316
317 return -ENOMEM;
318 }
319
320 dev_dbg(xpnet, "ifconfig up of %s; XPC connected\n", dev->name);
321
322 return 0;
323}
324
325static int
326xpnet_dev_stop(struct net_device *dev)
327{
328 xpc_disconnect(XPC_NET_CHANNEL);
329
330 dev_dbg(xpnet, "ifconfig down of %s; XPC disconnected\n", dev->name);
331
332 return 0;
333}
334
335
336
337
338
339
340
341static void
342xpnet_send_completed(enum xp_retval reason, short partid, int channel,
343 void *__qm)
344{
345 struct xpnet_pending_msg *queued_msg = (struct xpnet_pending_msg *)__qm;
346
347 DBUG_ON(queued_msg == NULL);
348
349 dev_dbg(xpnet, "message to %d notified with reason %d\n",
350 partid, reason);
351
352 if (atomic_dec_return(&queued_msg->use_count) == 0) {
353 dev_dbg(xpnet, "all acks for skb->head=-x%p\n",
354 (void *)queued_msg->skb->head);
355
356 dev_kfree_skb_any(queued_msg->skb);
357 kfree(queued_msg);
358 }
359}
360
361static void
362xpnet_send(struct sk_buff *skb, struct xpnet_pending_msg *queued_msg,
363 u64 start_addr, u64 end_addr, u16 embedded_bytes, int dest_partid)
364{
365 u8 msg_buffer[XPNET_MSG_SIZE];
366 struct xpnet_message *msg = (struct xpnet_message *)&msg_buffer;
367 u16 msg_size = sizeof(struct xpnet_message);
368 enum xp_retval ret;
369
370 msg->embedded_bytes = embedded_bytes;
371 if (unlikely(embedded_bytes != 0)) {
372 msg->version = XPNET_VERSION_EMBED;
373 dev_dbg(xpnet, "calling memcpy(0x%p, 0x%p, 0x%lx)\n",
374 &msg->data, skb->data, (size_t)embedded_bytes);
375 skb_copy_from_linear_data(skb, &msg->data,
376 (size_t)embedded_bytes);
377 msg_size += embedded_bytes - 1;
378 } else {
379 msg->version = XPNET_VERSION;
380 }
381 msg->magic = XPNET_MAGIC;
382 msg->size = end_addr - start_addr;
383 msg->leadin_ignore = (u64)skb->data - start_addr;
384 msg->tailout_ignore = end_addr - (u64)skb_tail_pointer(skb);
385 msg->buf_pa = xp_pa((void *)start_addr);
386
387 dev_dbg(xpnet, "sending XPC message to %d:%d\n"
388 "msg->buf_pa=0x%lx, msg->size=%u, "
389 "msg->leadin_ignore=%u, msg->tailout_ignore=%u\n",
390 dest_partid, XPC_NET_CHANNEL, msg->buf_pa, msg->size,
391 msg->leadin_ignore, msg->tailout_ignore);
392
393 atomic_inc(&queued_msg->use_count);
394
395 ret = xpc_send_notify(dest_partid, XPC_NET_CHANNEL, XPC_NOWAIT, msg,
396 msg_size, xpnet_send_completed, queued_msg);
397 if (unlikely(ret != xpSuccess))
398 atomic_dec(&queued_msg->use_count);
399}
400
401
402
403
404
405
406
407
408
409
410static netdev_tx_t
411xpnet_dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
412{
413 struct xpnet_pending_msg *queued_msg;
414 u64 start_addr, end_addr;
415 short dest_partid;
416 u16 embedded_bytes = 0;
417
418 dev_dbg(xpnet, ">skb->head=0x%p skb->data=0x%p skb->tail=0x%p "
419 "skb->end=0x%p skb->len=%d\n", (void *)skb->head,
420 (void *)skb->data, skb_tail_pointer(skb), skb_end_pointer(skb),
421 skb->len);
422
423 if (skb->data[0] == 0x33) {
424 dev_kfree_skb(skb);
425 return NETDEV_TX_OK;
426 }
427
428
429
430
431
432
433 queued_msg = kmalloc(sizeof(struct xpnet_pending_msg), GFP_ATOMIC);
434 if (queued_msg == NULL) {
435 dev_warn(xpnet, "failed to kmalloc %ld bytes; dropping "
436 "packet\n", sizeof(struct xpnet_pending_msg));
437
438 dev->stats.tx_errors++;
439 dev_kfree_skb(skb);
440 return NETDEV_TX_OK;
441 }
442
443
444 start_addr = ((u64)skb->data & ~(L1_CACHE_BYTES - 1));
445 end_addr = L1_CACHE_ALIGN((u64)skb_tail_pointer(skb));
446
447
448 if (unlikely(skb->len <= XPNET_MSG_DATA_MAX)) {
449
450 embedded_bytes = skb->len;
451 }
452
453
454
455
456
457
458
459
460 atomic_set(&queued_msg->use_count, 1);
461 queued_msg->skb = skb;
462
463 if (skb->data[0] == 0xff) {
464
465 for_each_set_bit(dest_partid, xpnet_broadcast_partitions,
466 xp_max_npartitions) {
467
468 xpnet_send(skb, queued_msg, start_addr, end_addr,
469 embedded_bytes, dest_partid);
470 }
471 } else {
472 dest_partid = (short)skb->data[XPNET_PARTID_OCTET + 1];
473 dest_partid |= (short)skb->data[XPNET_PARTID_OCTET + 0] << 8;
474
475 if (dest_partid >= 0 &&
476 dest_partid < xp_max_npartitions &&
477 test_bit(dest_partid, xpnet_broadcast_partitions) != 0) {
478
479 xpnet_send(skb, queued_msg, start_addr, end_addr,
480 embedded_bytes, dest_partid);
481 }
482 }
483
484 dev->stats.tx_packets++;
485 dev->stats.tx_bytes += skb->len;
486
487 if (atomic_dec_return(&queued_msg->use_count) == 0) {
488 dev_kfree_skb(skb);
489 kfree(queued_msg);
490 }
491
492 return NETDEV_TX_OK;
493}
494
495
496
497
498static void
499xpnet_dev_tx_timeout(struct net_device *dev, unsigned int txqueue)
500{
501 dev->stats.tx_errors++;
502}
503
504static const struct net_device_ops xpnet_netdev_ops = {
505 .ndo_open = xpnet_dev_open,
506 .ndo_stop = xpnet_dev_stop,
507 .ndo_start_xmit = xpnet_dev_hard_start_xmit,
508 .ndo_tx_timeout = xpnet_dev_tx_timeout,
509 .ndo_set_mac_address = eth_mac_addr,
510 .ndo_validate_addr = eth_validate_addr,
511};
512
513static int __init
514xpnet_init(void)
515{
516 int result;
517
518 if (!is_uv())
519 return -ENODEV;
520
521 dev_info(xpnet, "registering network device %s\n", XPNET_DEVICE_NAME);
522
523 xpnet_broadcast_partitions = kcalloc(BITS_TO_LONGS(xp_max_npartitions),
524 sizeof(long),
525 GFP_KERNEL);
526 if (xpnet_broadcast_partitions == NULL)
527 return -ENOMEM;
528
529
530
531
532
533 xpnet_device = alloc_netdev(0, XPNET_DEVICE_NAME, NET_NAME_UNKNOWN,
534 ether_setup);
535 if (xpnet_device == NULL) {
536 kfree(xpnet_broadcast_partitions);
537 return -ENOMEM;
538 }
539
540 netif_carrier_off(xpnet_device);
541
542 xpnet_device->netdev_ops = &xpnet_netdev_ops;
543 xpnet_device->mtu = XPNET_DEF_MTU;
544 xpnet_device->min_mtu = XPNET_MIN_MTU;
545 xpnet_device->max_mtu = XPNET_MAX_MTU;
546
547
548
549
550
551
552 xpnet_device->dev_addr[0] = 0x02;
553
554 xpnet_device->dev_addr[XPNET_PARTID_OCTET + 1] = xp_partition_id;
555 xpnet_device->dev_addr[XPNET_PARTID_OCTET + 0] = (xp_partition_id >> 8);
556
557
558
559
560
561 xpnet_device->flags &= ~IFF_MULTICAST;
562
563
564
565
566
567
568 xpnet_device->features = NETIF_F_HW_CSUM;
569
570 result = register_netdev(xpnet_device);
571 if (result != 0) {
572 free_netdev(xpnet_device);
573 kfree(xpnet_broadcast_partitions);
574 }
575
576 return result;
577}
578
579module_init(xpnet_init);
580
581static void __exit
582xpnet_exit(void)
583{
584 dev_info(xpnet, "unregistering network device %s\n",
585 xpnet_device[0].name);
586
587 unregister_netdev(xpnet_device);
588 free_netdev(xpnet_device);
589 kfree(xpnet_broadcast_partitions);
590}
591
592module_exit(xpnet_exit);
593
594MODULE_AUTHOR("Silicon Graphics, Inc.");
595MODULE_DESCRIPTION("Cross Partition Network adapter (XPNET)");
596MODULE_LICENSE("GPL");
597