sgi-xp: setup the notify GRU message queue
[linux-2.6/zen-sources.git] / drivers / misc / sgi-xp / xpnet.c
blob71513b3af7088e5f1ec115f5c08ff0c16039db04
1 /*
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
4 * for more details.
6 * Copyright (C) 1999-2008 Silicon Graphics, Inc. All rights reserved.
7 */
9 /*
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/netdevice.h>
25 #include <linux/etherdevice.h>
26 #include "xp.h"
29 * The message payload transferred by XPC.
31 * buf_pa is the physical address where the DMA should pull from.
33 * NOTE: for performance reasons, buf_pa should _ALWAYS_ begin on a
34 * cacheline boundary. To accomplish this, we record the number of
35 * bytes from the beginning of the first cacheline to the first useful
36 * byte of the skb (leadin_ignore) and the number of bytes from the
37 * last useful byte of the skb to the end of the last cacheline
38 * (tailout_ignore).
40 * size is the number of bytes to transfer which includes the skb->len
41 * (useful bytes of the senders skb) plus the leadin and tailout
43 struct xpnet_message {
44 u16 version; /* Version for this message */
45 u16 embedded_bytes; /* #of bytes embedded in XPC message */
46 u32 magic; /* Special number indicating this is xpnet */
47 unsigned long buf_pa; /* phys address of buffer to retrieve */
48 u32 size; /* #of bytes in buffer */
49 u8 leadin_ignore; /* #of bytes to ignore at the beginning */
50 u8 tailout_ignore; /* #of bytes to ignore at the end */
51 unsigned char data; /* body of small packets */
55 * Determine the size of our message, the cacheline aligned size,
56 * and then the number of message will request from XPC.
58 * XPC expects each message to exist in an individual cacheline.
60 #define XPNET_MSG_SIZE XPC_MSG_PAYLOAD_MAX_SIZE
61 #define XPNET_MSG_DATA_MAX \
62 (XPNET_MSG_SIZE - offsetof(struct xpnet_message, data))
63 #define XPNET_MSG_NENTRIES (PAGE_SIZE / XPC_MSG_MAX_SIZE)
65 #define XPNET_MAX_KTHREADS (XPNET_MSG_NENTRIES + 1)
66 #define XPNET_MAX_IDLE_KTHREADS (XPNET_MSG_NENTRIES + 1)
69 * Version number of XPNET implementation. XPNET can always talk to versions
70 * with same major #, and never talk to versions with a different version.
72 #define _XPNET_VERSION(_major, _minor) (((_major) << 4) | (_minor))
73 #define XPNET_VERSION_MAJOR(_v) ((_v) >> 4)
74 #define XPNET_VERSION_MINOR(_v) ((_v) & 0xf)
76 #define XPNET_VERSION _XPNET_VERSION(1, 0) /* version 1.0 */
77 #define XPNET_VERSION_EMBED _XPNET_VERSION(1, 1) /* version 1.1 */
78 #define XPNET_MAGIC 0x88786984 /* "XNET" */
80 #define XPNET_VALID_MSG(_m) \
81 ((XPNET_VERSION_MAJOR(_m->version) == XPNET_VERSION_MAJOR(XPNET_VERSION)) \
82 && (msg->magic == XPNET_MAGIC))
84 #define XPNET_DEVICE_NAME "xp0"
87 * When messages are queued with xpc_send_notify, a kmalloc'd buffer
88 * of the following type is passed as a notification cookie. When the
89 * notification function is called, we use the cookie to decide
90 * whether all outstanding message sends have completed. The skb can
91 * then be released.
93 struct xpnet_pending_msg {
94 struct sk_buff *skb;
95 atomic_t use_count;
98 /* driver specific structure pointed to by the device structure */
99 struct xpnet_dev_private {
100 struct net_device_stats stats;
103 struct net_device *xpnet_device;
106 * When we are notified of other partitions activating, we add them to
107 * our bitmask of partitions to which we broadcast.
109 static unsigned long *xpnet_broadcast_partitions;
110 /* protect above */
111 static DEFINE_SPINLOCK(xpnet_broadcast_lock);
114 * Since the Block Transfer Engine (BTE) is being used for the transfer
115 * and it relies upon cache-line size transfers, we need to reserve at
116 * least one cache-line for head and tail alignment. The BTE is
117 * limited to 8MB transfers.
119 * Testing has shown that changing MTU to greater than 64KB has no effect
120 * on TCP as the two sides negotiate a Max Segment Size that is limited
121 * to 64K. Other protocols May use packets greater than this, but for
122 * now, the default is 64KB.
124 #define XPNET_MAX_MTU (0x800000UL - L1_CACHE_BYTES)
125 /* 32KB has been determined to be the ideal */
126 #define XPNET_DEF_MTU (0x8000UL)
129 * The partid is encapsulated in the MAC address beginning in the following
130 * octet and it consists of two octets.
132 #define XPNET_PARTID_OCTET 2
134 /* Define the XPNET debug device structures to be used with dev_dbg() et al */
136 struct device_driver xpnet_dbg_name = {
137 .name = "xpnet"
140 struct device xpnet_dbg_subname = {
141 .bus_id = {0}, /* set to "" */
142 .driver = &xpnet_dbg_name
145 struct device *xpnet = &xpnet_dbg_subname;
148 * Packet was recevied by XPC and forwarded to us.
150 static void
151 xpnet_receive(short partid, int channel, struct xpnet_message *msg)
153 struct sk_buff *skb;
154 void *dst;
155 enum xp_retval ret;
156 struct xpnet_dev_private *priv =
157 (struct xpnet_dev_private *)xpnet_device->priv;
159 if (!XPNET_VALID_MSG(msg)) {
161 * Packet with a different XPC version. Ignore.
163 xpc_received(partid, channel, (void *)msg);
165 priv->stats.rx_errors++;
167 return;
169 dev_dbg(xpnet, "received 0x%lx, %d, %d, %d\n", msg->buf_pa, msg->size,
170 msg->leadin_ignore, msg->tailout_ignore);
172 /* reserve an extra cache line */
173 skb = dev_alloc_skb(msg->size + L1_CACHE_BYTES);
174 if (!skb) {
175 dev_err(xpnet, "failed on dev_alloc_skb(%d)\n",
176 msg->size + L1_CACHE_BYTES);
178 xpc_received(partid, channel, (void *)msg);
180 priv->stats.rx_errors++;
182 return;
186 * The allocated skb has some reserved space.
187 * In order to use xp_remote_memcpy(), we need to get the
188 * skb->data pointer moved forward.
190 skb_reserve(skb, (L1_CACHE_BYTES - ((u64)skb->data &
191 (L1_CACHE_BYTES - 1)) +
192 msg->leadin_ignore));
195 * Update the tail pointer to indicate data actually
196 * transferred.
198 skb_put(skb, (msg->size - msg->leadin_ignore - msg->tailout_ignore));
201 * Move the data over from the other side.
203 if ((XPNET_VERSION_MINOR(msg->version) == 1) &&
204 (msg->embedded_bytes != 0)) {
205 dev_dbg(xpnet, "copying embedded message. memcpy(0x%p, 0x%p, "
206 "%lu)\n", skb->data, &msg->data,
207 (size_t)msg->embedded_bytes);
209 skb_copy_to_linear_data(skb, &msg->data,
210 (size_t)msg->embedded_bytes);
211 } else {
212 dst = (void *)((u64)skb->data & ~(L1_CACHE_BYTES - 1));
213 dev_dbg(xpnet, "transferring buffer to the skb->data area;\n\t"
214 "xp_remote_memcpy(0x%p, 0x%p, %hu)\n", dst,
215 (void *)msg->buf_pa, msg->size);
217 ret = xp_remote_memcpy(xp_pa(dst), msg->buf_pa, msg->size);
218 if (ret != xpSuccess) {
220 * !!! Need better way of cleaning skb. Currently skb
221 * !!! appears in_use and we can't just call
222 * !!! dev_kfree_skb.
224 dev_err(xpnet, "xp_remote_memcpy(0x%p, 0x%p, 0x%hx) "
225 "returned error=0x%x\n", dst,
226 (void *)msg->buf_pa, msg->size, ret);
228 xpc_received(partid, channel, (void *)msg);
230 priv->stats.rx_errors++;
232 return;
236 dev_dbg(xpnet, "<skb->head=0x%p skb->data=0x%p skb->tail=0x%p "
237 "skb->end=0x%p skb->len=%d\n", (void *)skb->head,
238 (void *)skb->data, skb_tail_pointer(skb), skb_end_pointer(skb),
239 skb->len);
241 skb->protocol = eth_type_trans(skb, xpnet_device);
242 skb->ip_summed = CHECKSUM_UNNECESSARY;
244 dev_dbg(xpnet, "passing skb to network layer\n"
245 KERN_DEBUG "\tskb->head=0x%p skb->data=0x%p skb->tail=0x%p "
246 "skb->end=0x%p skb->len=%d\n",
247 (void *)skb->head, (void *)skb->data, skb_tail_pointer(skb),
248 skb_end_pointer(skb), skb->len);
250 xpnet_device->last_rx = jiffies;
251 priv->stats.rx_packets++;
252 priv->stats.rx_bytes += skb->len + ETH_HLEN;
254 netif_rx_ni(skb);
255 xpc_received(partid, channel, (void *)msg);
259 * This is the handler which XPC calls during any sort of change in
260 * state or message reception on a connection.
262 static void
263 xpnet_connection_activity(enum xp_retval reason, short partid, int channel,
264 void *data, void *key)
266 DBUG_ON(partid < 0 || partid >= xp_max_npartitions);
267 DBUG_ON(channel != XPC_NET_CHANNEL);
269 switch (reason) {
270 case xpMsgReceived: /* message received */
271 DBUG_ON(data == NULL);
273 xpnet_receive(partid, channel, (struct xpnet_message *)data);
274 break;
276 case xpConnected: /* connection completed to a partition */
277 spin_lock_bh(&xpnet_broadcast_lock);
278 __set_bit(partid, xpnet_broadcast_partitions);
279 spin_unlock_bh(&xpnet_broadcast_lock);
281 netif_carrier_on(xpnet_device);
283 dev_dbg(xpnet, "%s connected to partition %d\n",
284 xpnet_device->name, partid);
285 break;
287 default:
288 spin_lock_bh(&xpnet_broadcast_lock);
289 __clear_bit(partid, xpnet_broadcast_partitions);
290 spin_unlock_bh(&xpnet_broadcast_lock);
292 if (bitmap_empty((unsigned long *)xpnet_broadcast_partitions,
293 xp_max_npartitions)) {
294 netif_carrier_off(xpnet_device);
297 dev_dbg(xpnet, "%s disconnected from partition %d\n",
298 xpnet_device->name, partid);
299 break;
303 static int
304 xpnet_dev_open(struct net_device *dev)
306 enum xp_retval ret;
308 dev_dbg(xpnet, "calling xpc_connect(%d, 0x%p, NULL, %ld, %ld, %ld, "
309 "%ld)\n", XPC_NET_CHANNEL, xpnet_connection_activity,
310 (unsigned long)XPNET_MSG_SIZE,
311 (unsigned long)XPNET_MSG_NENTRIES,
312 (unsigned long)XPNET_MAX_KTHREADS,
313 (unsigned long)XPNET_MAX_IDLE_KTHREADS);
315 ret = xpc_connect(XPC_NET_CHANNEL, xpnet_connection_activity, NULL,
316 XPNET_MSG_SIZE, XPNET_MSG_NENTRIES,
317 XPNET_MAX_KTHREADS, XPNET_MAX_IDLE_KTHREADS);
318 if (ret != xpSuccess) {
319 dev_err(xpnet, "ifconfig up of %s failed on XPC connect, "
320 "ret=%d\n", dev->name, ret);
322 return -ENOMEM;
325 dev_dbg(xpnet, "ifconfig up of %s; XPC connected\n", dev->name);
327 return 0;
330 static int
331 xpnet_dev_stop(struct net_device *dev)
333 xpc_disconnect(XPC_NET_CHANNEL);
335 dev_dbg(xpnet, "ifconfig down of %s; XPC disconnected\n", dev->name);
337 return 0;
340 static int
341 xpnet_dev_change_mtu(struct net_device *dev, int new_mtu)
343 /* 68 comes from min TCP+IP+MAC header */
344 if ((new_mtu < 68) || (new_mtu > XPNET_MAX_MTU)) {
345 dev_err(xpnet, "ifconfig %s mtu %d failed; value must be "
346 "between 68 and %ld\n", dev->name, new_mtu,
347 XPNET_MAX_MTU);
348 return -EINVAL;
351 dev->mtu = new_mtu;
352 dev_dbg(xpnet, "ifconfig %s mtu set to %d\n", dev->name, new_mtu);
353 return 0;
357 * Required for the net_device structure.
359 static int
360 xpnet_dev_set_config(struct net_device *dev, struct ifmap *new_map)
362 return 0;
366 * Return statistics to the caller.
368 static struct net_device_stats *
369 xpnet_dev_get_stats(struct net_device *dev)
371 struct xpnet_dev_private *priv;
373 priv = (struct xpnet_dev_private *)dev->priv;
375 return &priv->stats;
379 * Notification that the other end has received the message and
380 * DMA'd the skb information. At this point, they are done with
381 * our side. When all recipients are done processing, we
382 * release the skb and then release our pending message structure.
384 static void
385 xpnet_send_completed(enum xp_retval reason, short partid, int channel,
386 void *__qm)
388 struct xpnet_pending_msg *queued_msg = (struct xpnet_pending_msg *)__qm;
390 DBUG_ON(queued_msg == NULL);
392 dev_dbg(xpnet, "message to %d notified with reason %d\n",
393 partid, reason);
395 if (atomic_dec_return(&queued_msg->use_count) == 0) {
396 dev_dbg(xpnet, "all acks for skb->head=-x%p\n",
397 (void *)queued_msg->skb->head);
399 dev_kfree_skb_any(queued_msg->skb);
400 kfree(queued_msg);
404 static void
405 xpnet_send(struct sk_buff *skb, struct xpnet_pending_msg *queued_msg,
406 u64 start_addr, u64 end_addr, u16 embedded_bytes, int dest_partid)
408 u8 msg_buffer[XPNET_MSG_SIZE];
409 struct xpnet_message *msg = (struct xpnet_message *)&msg_buffer;
410 u16 msg_size = sizeof(struct xpnet_message);
411 enum xp_retval ret;
413 msg->embedded_bytes = embedded_bytes;
414 if (unlikely(embedded_bytes != 0)) {
415 msg->version = XPNET_VERSION_EMBED;
416 dev_dbg(xpnet, "calling memcpy(0x%p, 0x%p, 0x%lx)\n",
417 &msg->data, skb->data, (size_t)embedded_bytes);
418 skb_copy_from_linear_data(skb, &msg->data,
419 (size_t)embedded_bytes);
420 msg_size += embedded_bytes - 1;
421 } else {
422 msg->version = XPNET_VERSION;
424 msg->magic = XPNET_MAGIC;
425 msg->size = end_addr - start_addr;
426 msg->leadin_ignore = (u64)skb->data - start_addr;
427 msg->tailout_ignore = end_addr - (u64)skb_tail_pointer(skb);
428 msg->buf_pa = xp_pa((void *)start_addr);
430 dev_dbg(xpnet, "sending XPC message to %d:%d\n"
431 KERN_DEBUG "msg->buf_pa=0x%lx, msg->size=%u, "
432 "msg->leadin_ignore=%u, msg->tailout_ignore=%u\n",
433 dest_partid, XPC_NET_CHANNEL, msg->buf_pa, msg->size,
434 msg->leadin_ignore, msg->tailout_ignore);
436 atomic_inc(&queued_msg->use_count);
438 ret = xpc_send_notify(dest_partid, XPC_NET_CHANNEL, XPC_NOWAIT, msg,
439 msg_size, xpnet_send_completed, queued_msg);
440 if (unlikely(ret != xpSuccess))
441 atomic_dec(&queued_msg->use_count);
445 * Network layer has formatted a packet (skb) and is ready to place it
446 * "on the wire". Prepare and send an xpnet_message to all partitions
447 * which have connected with us and are targets of this packet.
449 * MAC-NOTE: For the XPNET driver, the MAC address contains the
450 * destination partid. If the destination partid octets are 0xffff,
451 * this packet is to be broadcast to all connected partitions.
453 static int
454 xpnet_dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
456 struct xpnet_pending_msg *queued_msg;
457 u64 start_addr, end_addr;
458 short dest_partid;
459 struct xpnet_dev_private *priv = (struct xpnet_dev_private *)dev->priv;
460 u16 embedded_bytes = 0;
462 dev_dbg(xpnet, ">skb->head=0x%p skb->data=0x%p skb->tail=0x%p "
463 "skb->end=0x%p skb->len=%d\n", (void *)skb->head,
464 (void *)skb->data, skb_tail_pointer(skb), skb_end_pointer(skb),
465 skb->len);
467 if (skb->data[0] == 0x33) {
468 dev_kfree_skb(skb);
469 return 0; /* nothing needed to be done */
473 * The xpnet_pending_msg tracks how many outstanding
474 * xpc_send_notifies are relying on this skb. When none
475 * remain, release the skb.
477 queued_msg = kmalloc(sizeof(struct xpnet_pending_msg), GFP_ATOMIC);
478 if (queued_msg == NULL) {
479 dev_warn(xpnet, "failed to kmalloc %ld bytes; dropping "
480 "packet\n", sizeof(struct xpnet_pending_msg));
482 priv->stats.tx_errors++;
483 return -ENOMEM;
486 /* get the beginning of the first cacheline and end of last */
487 start_addr = ((u64)skb->data & ~(L1_CACHE_BYTES - 1));
488 end_addr = L1_CACHE_ALIGN((u64)skb_tail_pointer(skb));
490 /* calculate how many bytes to embed in the XPC message */
491 if (unlikely(skb->len <= XPNET_MSG_DATA_MAX)) {
492 /* skb->data does fit so embed */
493 embedded_bytes = skb->len;
497 * Since the send occurs asynchronously, we set the count to one
498 * and begin sending. Any sends that happen to complete before
499 * we are done sending will not free the skb. We will be left
500 * with that task during exit. This also handles the case of
501 * a packet destined for a partition which is no longer up.
503 atomic_set(&queued_msg->use_count, 1);
504 queued_msg->skb = skb;
506 if (skb->data[0] == 0xff) {
507 /* we are being asked to broadcast to all partitions */
508 for_each_bit(dest_partid, xpnet_broadcast_partitions,
509 xp_max_npartitions) {
511 xpnet_send(skb, queued_msg, start_addr, end_addr,
512 embedded_bytes, dest_partid);
514 } else {
515 dest_partid = (short)skb->data[XPNET_PARTID_OCTET + 1];
516 dest_partid |= (short)skb->data[XPNET_PARTID_OCTET + 0] << 8;
518 if (dest_partid >= 0 &&
519 dest_partid < xp_max_npartitions &&
520 test_bit(dest_partid, xpnet_broadcast_partitions) != 0) {
522 xpnet_send(skb, queued_msg, start_addr, end_addr,
523 embedded_bytes, dest_partid);
527 if (atomic_dec_return(&queued_msg->use_count) == 0) {
528 dev_kfree_skb(skb);
529 kfree(queued_msg);
532 priv->stats.tx_packets++;
533 priv->stats.tx_bytes += skb->len;
535 return 0;
539 * Deal with transmit timeouts coming from the network layer.
541 static void
542 xpnet_dev_tx_timeout(struct net_device *dev)
544 struct xpnet_dev_private *priv;
546 priv = (struct xpnet_dev_private *)dev->priv;
548 priv->stats.tx_errors++;
549 return;
552 static int __init
553 xpnet_init(void)
555 int result;
557 if (!is_shub() && !is_uv())
558 return -ENODEV;
560 dev_info(xpnet, "registering network device %s\n", XPNET_DEVICE_NAME);
562 xpnet_broadcast_partitions = kzalloc(BITS_TO_LONGS(xp_max_npartitions) *
563 sizeof(long), GFP_KERNEL);
564 if (xpnet_broadcast_partitions == NULL)
565 return -ENOMEM;
568 * use ether_setup() to init the majority of our device
569 * structure and then override the necessary pieces.
571 xpnet_device = alloc_netdev(sizeof(struct xpnet_dev_private),
572 XPNET_DEVICE_NAME, ether_setup);
573 if (xpnet_device == NULL) {
574 kfree(xpnet_broadcast_partitions);
575 return -ENOMEM;
578 netif_carrier_off(xpnet_device);
580 xpnet_device->mtu = XPNET_DEF_MTU;
581 xpnet_device->change_mtu = xpnet_dev_change_mtu;
582 xpnet_device->open = xpnet_dev_open;
583 xpnet_device->get_stats = xpnet_dev_get_stats;
584 xpnet_device->stop = xpnet_dev_stop;
585 xpnet_device->hard_start_xmit = xpnet_dev_hard_start_xmit;
586 xpnet_device->tx_timeout = xpnet_dev_tx_timeout;
587 xpnet_device->set_config = xpnet_dev_set_config;
590 * Multicast assumes the LSB of the first octet is set for multicast
591 * MAC addresses. We chose the first octet of the MAC to be unlikely
592 * to collide with any vendor's officially issued MAC.
594 xpnet_device->dev_addr[0] = 0x02; /* locally administered, no OUI */
596 xpnet_device->dev_addr[XPNET_PARTID_OCTET + 1] = xp_partition_id;
597 xpnet_device->dev_addr[XPNET_PARTID_OCTET + 0] = (xp_partition_id >> 8);
600 * ether_setup() sets this to a multicast device. We are
601 * really not supporting multicast at this time.
603 xpnet_device->flags &= ~IFF_MULTICAST;
606 * No need to checksum as it is a DMA transfer. The BTE will
607 * report an error if the data is not retrievable and the
608 * packet will be dropped.
610 xpnet_device->features = NETIF_F_NO_CSUM;
612 result = register_netdev(xpnet_device);
613 if (result != 0) {
614 free_netdev(xpnet_device);
615 kfree(xpnet_broadcast_partitions);
618 return result;
621 module_init(xpnet_init);
623 static void __exit
624 xpnet_exit(void)
626 dev_info(xpnet, "unregistering network device %s\n",
627 xpnet_device[0].name);
629 unregister_netdev(xpnet_device);
630 free_netdev(xpnet_device);
631 kfree(xpnet_broadcast_partitions);
634 module_exit(xpnet_exit);
636 MODULE_AUTHOR("Silicon Graphics, Inc.");
637 MODULE_DESCRIPTION("Cross Partition Network adapter (XPNET)");
638 MODULE_LICENSE("GPL");