2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
6 * Copyright (C) 1999-2008 Silicon Graphics, Inc. All rights reserved.
10 * Cross Partition Network Interface (XPNET) support
12 * XPNET provides a virtual network layered on top of the Cross
13 * Partition communication layer.
15 * XPNET provides direct point-to-point and broadcast-like support
16 * for an ethernet-like device. The ethernet broadcast medium is
17 * replaced with a point-to-point message structure which passes
18 * pointers to a DMA-capable block that a remote partition should
19 * retrieve and pass to the upper level networking layer.
23 #include <linux/module.h>
24 #include <linux/types.h>
25 #include <linux/kernel.h>
26 #include <linux/init.h>
27 #include <linux/ioport.h>
28 #include <linux/netdevice.h>
29 #include <linux/etherdevice.h>
30 #include <linux/delay.h>
31 #include <linux/ethtool.h>
32 #include <linux/mii.h>
33 #include <linux/smp.h>
34 #include <linux/string.h>
35 #include <asm/sn/io.h>
36 #include <asm/sn/sn_sal.h>
37 #include <asm/atomic.h>
41 * The message payload transferred by XPC.
43 * buf_pa is the physical address where the DMA should pull from.
45 * NOTE: for performance reasons, buf_pa should _ALWAYS_ begin on a
46 * cacheline boundary. To accomplish this, we record the number of
47 * bytes from the beginning of the first cacheline to the first useful
48 * byte of the skb (leadin_ignore) and the number of bytes from the
49 * last useful byte of the skb to the end of the last cacheline
52 * size is the number of bytes to transfer which includes the skb->len
53 * (useful bytes of the senders skb) plus the leadin and tailout
55 struct xpnet_message {
56 u16 version; /* Version for this message */
57 u16 embedded_bytes; /* #of bytes embedded in XPC message */
58 u32 magic; /* Special number indicating this is xpnet */
59 u64 buf_pa; /* phys address of buffer to retrieve */
60 u32 size; /* #of bytes in buffer */
61 u8 leadin_ignore; /* #of bytes to ignore at the beginning */
62 u8 tailout_ignore; /* #of bytes to ignore at the end */
63 unsigned char data; /* body of small packets */
67 * Determine the size of our message, the cacheline aligned size,
68 * and then the number of message will request from XPC.
70 * XPC expects each message to exist in an individual cacheline.
72 #define XPNET_MSG_SIZE (L1_CACHE_BYTES - XPC_MSG_PAYLOAD_OFFSET)
73 #define XPNET_MSG_DATA_MAX \
74 (XPNET_MSG_SIZE - (u64)(&((struct xpnet_message *)0)->data))
75 #define XPNET_MSG_ALIGNED_SIZE (L1_CACHE_ALIGN(XPNET_MSG_SIZE))
76 #define XPNET_MSG_NENTRIES (PAGE_SIZE / XPNET_MSG_ALIGNED_SIZE)
78 #define XPNET_MAX_KTHREADS (XPNET_MSG_NENTRIES + 1)
79 #define XPNET_MAX_IDLE_KTHREADS (XPNET_MSG_NENTRIES + 1)
82 * Version number of XPNET implementation. XPNET can always talk to versions
83 * with same major #, and never talk to versions with a different version.
85 #define _XPNET_VERSION(_major, _minor) (((_major) << 4) | (_minor))
86 #define XPNET_VERSION_MAJOR(_v) ((_v) >> 4)
87 #define XPNET_VERSION_MINOR(_v) ((_v) & 0xf)
89 #define XPNET_VERSION _XPNET_VERSION(1, 0) /* version 1.0 */
90 #define XPNET_VERSION_EMBED _XPNET_VERSION(1, 1) /* version 1.1 */
91 #define XPNET_MAGIC 0x88786984 /* "XNET" */
93 #define XPNET_VALID_MSG(_m) \
94 ((XPNET_VERSION_MAJOR(_m->version) == XPNET_VERSION_MAJOR(XPNET_VERSION)) \
95 && (msg->magic == XPNET_MAGIC))
97 #define XPNET_DEVICE_NAME "xp0"
100 * When messages are queued with xpc_send_notify, a kmalloc'd buffer
101 * of the following type is passed as a notification cookie. When the
102 * notification function is called, we use the cookie to decide
103 * whether all outstanding message sends have completed. The skb can
106 struct xpnet_pending_msg {
107 struct list_head free_list;
112 /* driver specific structure pointed to by the device structure */
113 struct xpnet_dev_private {
114 struct net_device_stats stats;
117 struct net_device *xpnet_device;
120 * When we are notified of other partitions activating, we add them to
121 * our bitmask of partitions to which we broadcast.
123 static u64 xpnet_broadcast_partitions;
125 static DEFINE_SPINLOCK(xpnet_broadcast_lock);
128 * Since the Block Transfer Engine (BTE) is being used for the transfer
129 * and it relies upon cache-line size transfers, we need to reserve at
130 * least one cache-line for head and tail alignment. The BTE is
131 * limited to 8MB transfers.
133 * Testing has shown that changing MTU to greater than 64KB has no effect
134 * on TCP as the two sides negotiate a Max Segment Size that is limited
135 * to 64K. Other protocols May use packets greater than this, but for
136 * now, the default is 64KB.
138 #define XPNET_MAX_MTU (0x800000UL - L1_CACHE_BYTES)
139 /* 32KB has been determined to be the ideal */
140 #define XPNET_DEF_MTU (0x8000UL)
143 * The partition id is encapsulated in the MAC address. The following
144 * define locates the octet the partid is in.
146 #define XPNET_PARTID_OCTET 1
147 #define XPNET_LICENSE_OCTET 2
150 * Define the XPNET debug device structure that is to be used with dev_dbg(),
151 * dev_err(), dev_warn(), and dev_info().
153 struct device_driver xpnet_dbg_name = {
157 struct device xpnet_dbg_subname = {
158 .bus_id = {0}, /* set to "" */
159 .driver = &xpnet_dbg_name
162 struct device *xpnet = &xpnet_dbg_subname;
165 * Packet was recevied by XPC and forwarded to us.
168 xpnet_receive(short partid, int channel, struct xpnet_message *msg)
172 struct xpnet_dev_private *priv =
173 (struct xpnet_dev_private *)xpnet_device->priv;
175 if (!XPNET_VALID_MSG(msg)) {
177 * Packet with a different XPC version. Ignore.
179 xpc_received(partid, channel, (void *)msg);
181 priv->stats.rx_errors++;
185 dev_dbg(xpnet, "received 0x%lx, %d, %d, %d\n", msg->buf_pa, msg->size,
186 msg->leadin_ignore, msg->tailout_ignore);
188 /* reserve an extra cache line */
189 skb = dev_alloc_skb(msg->size + L1_CACHE_BYTES);
191 dev_err(xpnet, "failed on dev_alloc_skb(%d)\n",
192 msg->size + L1_CACHE_BYTES);
194 xpc_received(partid, channel, (void *)msg);
196 priv->stats.rx_errors++;
202 * The allocated skb has some reserved space.
203 * In order to use xp_remote_memcpy(), we need to get the
204 * skb->data pointer moved forward.
206 skb_reserve(skb, (L1_CACHE_BYTES - ((u64)skb->data &
207 (L1_CACHE_BYTES - 1)) +
208 msg->leadin_ignore));
211 * Update the tail pointer to indicate data actually
214 skb_put(skb, (msg->size - msg->leadin_ignore - msg->tailout_ignore));
217 * Move the data over from the other side.
219 if ((XPNET_VERSION_MINOR(msg->version) == 1) &&
220 (msg->embedded_bytes != 0)) {
221 dev_dbg(xpnet, "copying embedded message. memcpy(0x%p, 0x%p, "
222 "%lu)\n", skb->data, &msg->data,
223 (size_t)msg->embedded_bytes);
225 skb_copy_to_linear_data(skb, &msg->data,
226 (size_t)msg->embedded_bytes);
228 dev_dbg(xpnet, "transferring buffer to the skb->data area;\n\t"
229 "xp_remote_memcpy(0x%p, 0x%p, %hu)\n", (void *)
230 ((u64)skb->data & ~(L1_CACHE_BYTES - 1)),
231 (void *)msg->buf_pa, msg->size);
233 ret = xp_remote_memcpy((void *)((u64)skb->data &
234 ~(L1_CACHE_BYTES - 1)),
235 (void *)msg->buf_pa, msg->size);
237 if (ret != xpSuccess) {
239 * >>> Need better way of cleaning skb. Currently skb
240 * >>> appears in_use and we can't just call
243 dev_err(xpnet, "xp_remote_memcpy(0x%p, 0x%p, 0x%hx) "
244 "returned error=0x%x\n", (void *)
245 ((u64)skb->data & ~(L1_CACHE_BYTES - 1)),
246 (void *)msg->buf_pa, msg->size, ret);
248 xpc_received(partid, channel, (void *)msg);
250 priv->stats.rx_errors++;
256 dev_dbg(xpnet, "<skb->head=0x%p skb->data=0x%p skb->tail=0x%p "
257 "skb->end=0x%p skb->len=%d\n", (void *)skb->head,
258 (void *)skb->data, skb_tail_pointer(skb), skb_end_pointer(skb),
261 skb->protocol = eth_type_trans(skb, xpnet_device);
262 skb->ip_summed = CHECKSUM_UNNECESSARY;
264 dev_dbg(xpnet, "passing skb to network layer\n"
265 KERN_DEBUG "\tskb->head=0x%p skb->data=0x%p skb->tail=0x%p "
266 "skb->end=0x%p skb->len=%d\n",
267 (void *)skb->head, (void *)skb->data, skb_tail_pointer(skb),
268 skb_end_pointer(skb), skb->len);
270 xpnet_device->last_rx = jiffies;
271 priv->stats.rx_packets++;
272 priv->stats.rx_bytes += skb->len + ETH_HLEN;
275 xpc_received(partid, channel, (void *)msg);
279 * This is the handler which XPC calls during any sort of change in
280 * state or message reception on a connection.
283 xpnet_connection_activity(enum xp_retval reason, short partid, int channel,
284 void *data, void *key)
288 DBUG_ON(partid < 0 || partid >= xp_max_npartitions);
289 DBUG_ON(channel != XPC_NET_CHANNEL);
292 case xpMsgReceived: /* message received */
293 DBUG_ON(data == NULL);
295 xpnet_receive(partid, channel, (struct xpnet_message *)data);
298 case xpConnected: /* connection completed to a partition */
299 spin_lock_bh(&xpnet_broadcast_lock);
300 xpnet_broadcast_partitions |= 1UL << (partid - 1);
301 bp = xpnet_broadcast_partitions;
302 spin_unlock_bh(&xpnet_broadcast_lock);
304 netif_carrier_on(xpnet_device);
306 dev_dbg(xpnet, "%s connection created to partition %d; "
307 "xpnet_broadcast_partitions=0x%lx\n",
308 xpnet_device->name, partid, bp);
312 spin_lock_bh(&xpnet_broadcast_lock);
313 xpnet_broadcast_partitions &= ~(1UL << (partid - 1));
314 bp = xpnet_broadcast_partitions;
315 spin_unlock_bh(&xpnet_broadcast_lock);
318 netif_carrier_off(xpnet_device);
320 dev_dbg(xpnet, "%s disconnected from partition %d; "
321 "xpnet_broadcast_partitions=0x%lx\n",
322 xpnet_device->name, partid, bp);
329 xpnet_dev_open(struct net_device *dev)
333 dev_dbg(xpnet, "calling xpc_connect(%d, 0x%p, NULL, %ld, %ld, %ld, "
334 "%ld)\n", XPC_NET_CHANNEL, xpnet_connection_activity,
335 XPNET_MSG_SIZE, XPNET_MSG_NENTRIES, XPNET_MAX_KTHREADS,
336 XPNET_MAX_IDLE_KTHREADS);
338 ret = xpc_connect(XPC_NET_CHANNEL, xpnet_connection_activity, NULL,
339 XPNET_MSG_SIZE, XPNET_MSG_NENTRIES,
340 XPNET_MAX_KTHREADS, XPNET_MAX_IDLE_KTHREADS);
341 if (ret != xpSuccess) {
342 dev_err(xpnet, "ifconfig up of %s failed on XPC connect, "
343 "ret=%d\n", dev->name, ret);
348 dev_dbg(xpnet, "ifconfig up of %s; XPC connected\n", dev->name);
354 xpnet_dev_stop(struct net_device *dev)
356 xpc_disconnect(XPC_NET_CHANNEL);
358 dev_dbg(xpnet, "ifconfig down of %s; XPC disconnected\n", dev->name);
364 xpnet_dev_change_mtu(struct net_device *dev, int new_mtu)
366 /* 68 comes from min TCP+IP+MAC header */
367 if ((new_mtu < 68) || (new_mtu > XPNET_MAX_MTU)) {
368 dev_err(xpnet, "ifconfig %s mtu %d failed; value must be "
369 "between 68 and %ld\n", dev->name, new_mtu,
375 dev_dbg(xpnet, "ifconfig %s mtu set to %d\n", dev->name, new_mtu);
380 * Required for the net_device structure.
383 xpnet_dev_set_config(struct net_device *dev, struct ifmap *new_map)
389 * Return statistics to the caller.
391 static struct net_device_stats *
392 xpnet_dev_get_stats(struct net_device *dev)
394 struct xpnet_dev_private *priv;
396 priv = (struct xpnet_dev_private *)dev->priv;
402 * Notification that the other end has received the message and
403 * DMA'd the skb information. At this point, they are done with
404 * our side. When all recipients are done processing, we
405 * release the skb and then release our pending message structure.
408 xpnet_send_completed(enum xp_retval reason, short partid, int channel,
411 struct xpnet_pending_msg *queued_msg = (struct xpnet_pending_msg *)__qm;
413 DBUG_ON(queued_msg == NULL);
415 dev_dbg(xpnet, "message to %d notified with reason %d\n",
418 if (atomic_dec_return(&queued_msg->use_count) == 0) {
419 dev_dbg(xpnet, "all acks for skb->head=-x%p\n",
420 (void *)queued_msg->skb->head);
422 dev_kfree_skb_any(queued_msg->skb);
428 * Network layer has formatted a packet (skb) and is ready to place it
429 * "on the wire". Prepare and send an xpnet_message to all partitions
430 * which have connected with us and are targets of this packet.
432 * MAC-NOTE: For the XPNET driver, the MAC address contains the
433 * destination partition_id. If the destination partition id word
434 * is 0xff, this packet is to broadcast to all partitions.
437 xpnet_dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
439 struct xpnet_pending_msg *queued_msg;
441 struct xpnet_message *msg;
442 u64 start_addr, end_addr;
446 struct xpnet_dev_private *priv;
449 priv = (struct xpnet_dev_private *)dev->priv;
451 dev_dbg(xpnet, ">skb->head=0x%p skb->data=0x%p skb->tail=0x%p "
452 "skb->end=0x%p skb->len=%d\n", (void *)skb->head,
453 (void *)skb->data, skb_tail_pointer(skb), skb_end_pointer(skb),
457 * The xpnet_pending_msg tracks how many outstanding
458 * xpc_send_notifies are relying on this skb. When none
459 * remain, release the skb.
461 queued_msg = kmalloc(sizeof(struct xpnet_pending_msg), GFP_ATOMIC);
462 if (queued_msg == NULL) {
463 dev_warn(xpnet, "failed to kmalloc %ld bytes; dropping "
464 "packet\n", sizeof(struct xpnet_pending_msg));
466 priv->stats.tx_errors++;
471 /* get the beginning of the first cacheline and end of last */
472 start_addr = ((u64)skb->data & ~(L1_CACHE_BYTES - 1));
473 end_addr = L1_CACHE_ALIGN((u64)skb_tail_pointer(skb));
475 /* calculate how many bytes to embed in the XPC message */
477 if (unlikely(skb->len <= XPNET_MSG_DATA_MAX)) {
478 /* skb->data does fit so embed */
479 embedded_bytes = skb->len;
483 * Since the send occurs asynchronously, we set the count to one
484 * and begin sending. Any sends that happen to complete before
485 * we are done sending will not free the skb. We will be left
486 * with that task during exit. This also handles the case of
487 * a packet destined for a partition which is no longer up.
489 atomic_set(&queued_msg->use_count, 1);
490 queued_msg->skb = skb;
492 second_mac_octet = skb->data[XPNET_PARTID_OCTET];
493 if (second_mac_octet == 0xff) {
494 /* we are being asked to broadcast to all partitions */
495 dp = xpnet_broadcast_partitions;
496 } else if (second_mac_octet != 0) {
497 dp = xpnet_broadcast_partitions &
498 (1UL << (second_mac_octet - 1));
500 /* 0 is an invalid partid. Ignore */
503 dev_dbg(xpnet, "destination Partitions mask (dp) = 0x%lx\n", dp);
506 * If we wanted to allow promiscuous mode to work like an
507 * unswitched network, this would be a good point to OR in a
508 * mask of partitions which should be receiving all packets.
514 for (dest_partid = 0; dp && dest_partid < xp_max_npartitions;
517 if (!(dp & (1UL << (dest_partid - 1)))) {
518 /* not destined for this partition */
522 /* remove this partition from the destinations mask */
523 dp &= ~(1UL << (dest_partid - 1));
525 /* found a partition to send to */
527 ret = xpc_allocate(dest_partid, XPC_NET_CHANNEL,
528 XPC_NOWAIT, (void **)&msg);
529 if (unlikely(ret != xpSuccess))
532 msg->embedded_bytes = embedded_bytes;
533 if (unlikely(embedded_bytes != 0)) {
534 msg->version = XPNET_VERSION_EMBED;
535 dev_dbg(xpnet, "calling memcpy(0x%p, 0x%p, 0x%lx)\n",
536 &msg->data, skb->data, (size_t)embedded_bytes);
537 skb_copy_from_linear_data(skb, &msg->data,
538 (size_t)embedded_bytes);
540 msg->version = XPNET_VERSION;
542 msg->magic = XPNET_MAGIC;
543 msg->size = end_addr - start_addr;
544 msg->leadin_ignore = (u64)skb->data - start_addr;
545 msg->tailout_ignore = end_addr - (u64)skb_tail_pointer(skb);
546 msg->buf_pa = __pa(start_addr);
548 dev_dbg(xpnet, "sending XPC message to %d:%d\n"
549 KERN_DEBUG "msg->buf_pa=0x%lx, msg->size=%u, "
550 "msg->leadin_ignore=%u, msg->tailout_ignore=%u\n",
551 dest_partid, XPC_NET_CHANNEL, msg->buf_pa, msg->size,
552 msg->leadin_ignore, msg->tailout_ignore);
554 atomic_inc(&queued_msg->use_count);
556 ret = xpc_send_notify(dest_partid, XPC_NET_CHANNEL, msg,
557 xpnet_send_completed, queued_msg);
558 if (unlikely(ret != xpSuccess)) {
559 atomic_dec(&queued_msg->use_count);
564 if (atomic_dec_return(&queued_msg->use_count) == 0) {
565 dev_dbg(xpnet, "no partitions to receive packet destined for "
566 "%d\n", dest_partid);
572 priv->stats.tx_packets++;
573 priv->stats.tx_bytes += skb->len;
579 * Deal with transmit timeouts coming from the network layer.
582 xpnet_dev_tx_timeout(struct net_device *dev)
584 struct xpnet_dev_private *priv;
586 priv = (struct xpnet_dev_private *)dev->priv;
588 priv->stats.tx_errors++;
597 int result = -ENOMEM;
599 if (!ia64_platform_is("sn2"))
602 dev_info(xpnet, "registering network device %s\n", XPNET_DEVICE_NAME);
605 * use ether_setup() to init the majority of our device
606 * structure and then override the necessary pieces.
608 xpnet_device = alloc_netdev(sizeof(struct xpnet_dev_private),
609 XPNET_DEVICE_NAME, ether_setup);
610 if (xpnet_device == NULL)
613 netif_carrier_off(xpnet_device);
615 xpnet_device->mtu = XPNET_DEF_MTU;
616 xpnet_device->change_mtu = xpnet_dev_change_mtu;
617 xpnet_device->open = xpnet_dev_open;
618 xpnet_device->get_stats = xpnet_dev_get_stats;
619 xpnet_device->stop = xpnet_dev_stop;
620 xpnet_device->hard_start_xmit = xpnet_dev_hard_start_xmit;
621 xpnet_device->tx_timeout = xpnet_dev_tx_timeout;
622 xpnet_device->set_config = xpnet_dev_set_config;
625 * Multicast assumes the LSB of the first octet is set for multicast
626 * MAC addresses. We chose the first octet of the MAC to be unlikely
627 * to collide with any vendor's officially issued MAC.
629 xpnet_device->dev_addr[0] = 0xfe;
630 xpnet_device->dev_addr[XPNET_PARTID_OCTET] = sn_partition_id;
631 license_num = sn_partition_serial_number_val();
632 for (i = 3; i >= 0; i--) {
633 xpnet_device->dev_addr[XPNET_LICENSE_OCTET + i] =
635 license_num = license_num >> 8;
639 * ether_setup() sets this to a multicast device. We are
640 * really not supporting multicast at this time.
642 xpnet_device->flags &= ~IFF_MULTICAST;
645 * No need to checksum as it is a DMA transfer. The BTE will
646 * report an error if the data is not retrievable and the
647 * packet will be dropped.
649 xpnet_device->features = NETIF_F_NO_CSUM;
651 result = register_netdev(xpnet_device);
653 free_netdev(xpnet_device);
658 module_init(xpnet_init);
663 dev_info(xpnet, "unregistering network device %s\n",
664 xpnet_device[0].name);
666 unregister_netdev(xpnet_device);
668 free_netdev(xpnet_device);
671 module_exit(xpnet_exit);
673 MODULE_AUTHOR("Silicon Graphics, Inc.");
674 MODULE_DESCRIPTION("Cross Partition Network adapter (XPNET)");
675 MODULE_LICENSE("GPL");