Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 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. |
| 5 | * |
Dean Nelson | 45d9ca4 | 2008-04-22 14:46:56 -0500 | [diff] [blame] | 6 | * Copyright (C) 1999-2008 Silicon Graphics, Inc. All rights reserved. |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 9 | /* |
| 10 | * Cross Partition Network Interface (XPNET) support |
| 11 | * |
| 12 | * XPNET provides a virtual network layered on top of the Cross |
| 13 | * Partition communication layer. |
| 14 | * |
| 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. |
| 20 | * |
| 21 | */ |
| 22 | |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 23 | #include <linux/module.h> |
| 24 | #include <linux/kernel.h> |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 25 | #include <linux/init.h> |
| 26 | #include <linux/ioport.h> |
| 27 | #include <linux/netdevice.h> |
| 28 | #include <linux/etherdevice.h> |
| 29 | #include <linux/delay.h> |
| 30 | #include <linux/ethtool.h> |
| 31 | #include <linux/mii.h> |
| 32 | #include <linux/smp.h> |
| 33 | #include <linux/string.h> |
| 34 | #include <asm/sn/bte.h> |
| 35 | #include <asm/sn/io.h> |
| 36 | #include <asm/sn/sn_sal.h> |
| 37 | #include <asm/types.h> |
| 38 | #include <asm/atomic.h> |
Dean Nelson | 45d9ca4 | 2008-04-22 14:46:56 -0500 | [diff] [blame] | 39 | #include "xp.h" |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 40 | |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 41 | /* |
| 42 | * The message payload transferred by XPC. |
| 43 | * |
| 44 | * buf_pa is the physical address where the DMA should pull from. |
| 45 | * |
| 46 | * NOTE: for performance reasons, buf_pa should _ALWAYS_ begin on a |
| 47 | * cacheline boundary. To accomplish this, we record the number of |
| 48 | * bytes from the beginning of the first cacheline to the first useful |
| 49 | * byte of the skb (leadin_ignore) and the number of bytes from the |
| 50 | * last useful byte of the skb to the end of the last cacheline |
| 51 | * (tailout_ignore). |
| 52 | * |
| 53 | * size is the number of bytes to transfer which includes the skb->len |
| 54 | * (useful bytes of the senders skb) plus the leadin and tailout |
| 55 | */ |
| 56 | struct xpnet_message { |
| 57 | u16 version; /* Version for this message */ |
| 58 | u16 embedded_bytes; /* #of bytes embedded in XPC message */ |
| 59 | u32 magic; /* Special number indicating this is xpnet */ |
| 60 | u64 buf_pa; /* phys address of buffer to retrieve */ |
| 61 | u32 size; /* #of bytes in buffer */ |
| 62 | u8 leadin_ignore; /* #of bytes to ignore at the beginning */ |
| 63 | u8 tailout_ignore; /* #of bytes to ignore at the end */ |
| 64 | unsigned char data; /* body of small packets */ |
| 65 | }; |
| 66 | |
| 67 | /* |
| 68 | * Determine the size of our message, the cacheline aligned size, |
| 69 | * and then the number of message will request from XPC. |
| 70 | * |
| 71 | * XPC expects each message to exist in an individual cacheline. |
| 72 | */ |
| 73 | #define XPNET_MSG_SIZE (L1_CACHE_BYTES - XPC_MSG_PAYLOAD_OFFSET) |
| 74 | #define XPNET_MSG_DATA_MAX \ |
| 75 | (XPNET_MSG_SIZE - (u64)(&((struct xpnet_message *)0)->data)) |
| 76 | #define XPNET_MSG_ALIGNED_SIZE (L1_CACHE_ALIGN(XPNET_MSG_SIZE)) |
| 77 | #define XPNET_MSG_NENTRIES (PAGE_SIZE / XPNET_MSG_ALIGNED_SIZE) |
| 78 | |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 79 | #define XPNET_MAX_KTHREADS (XPNET_MSG_NENTRIES + 1) |
| 80 | #define XPNET_MAX_IDLE_KTHREADS (XPNET_MSG_NENTRIES + 1) |
| 81 | |
| 82 | /* |
| 83 | * Version number of XPNET implementation. XPNET can always talk to versions |
| 84 | * with same major #, and never talk to versions with a different version. |
| 85 | */ |
| 86 | #define _XPNET_VERSION(_major, _minor) (((_major) << 4) | (_minor)) |
| 87 | #define XPNET_VERSION_MAJOR(_v) ((_v) >> 4) |
| 88 | #define XPNET_VERSION_MINOR(_v) ((_v) & 0xf) |
| 89 | |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame^] | 90 | #define XPNET_VERSION _XPNET_VERSION(1,0) /* version 1.0 */ |
| 91 | #define XPNET_VERSION_EMBED _XPNET_VERSION(1,1) /* version 1.1 */ |
| 92 | #define XPNET_MAGIC 0x88786984 /* "XNET" */ |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 93 | |
| 94 | #define XPNET_VALID_MSG(_m) \ |
| 95 | ((XPNET_VERSION_MAJOR(_m->version) == XPNET_VERSION_MAJOR(XPNET_VERSION)) \ |
| 96 | && (msg->magic == XPNET_MAGIC)) |
| 97 | |
| 98 | #define XPNET_DEVICE_NAME "xp0" |
| 99 | |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 100 | /* |
| 101 | * When messages are queued with xpc_send_notify, a kmalloc'd buffer |
| 102 | * of the following type is passed as a notification cookie. When the |
| 103 | * notification function is called, we use the cookie to decide |
| 104 | * whether all outstanding message sends have completed. The skb can |
| 105 | * then be released. |
| 106 | */ |
| 107 | struct xpnet_pending_msg { |
| 108 | struct list_head free_list; |
| 109 | struct sk_buff *skb; |
| 110 | atomic_t use_count; |
| 111 | }; |
| 112 | |
| 113 | /* driver specific structure pointed to by the device structure */ |
| 114 | struct xpnet_dev_private { |
| 115 | struct net_device_stats stats; |
| 116 | }; |
| 117 | |
| 118 | struct net_device *xpnet_device; |
| 119 | |
| 120 | /* |
| 121 | * When we are notified of other partitions activating, we add them to |
| 122 | * our bitmask of partitions to which we broadcast. |
| 123 | */ |
| 124 | static u64 xpnet_broadcast_partitions; |
| 125 | /* protect above */ |
Ingo Molnar | a9f6a0d | 2005-09-09 13:10:41 -0700 | [diff] [blame] | 126 | static DEFINE_SPINLOCK(xpnet_broadcast_lock); |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 127 | |
| 128 | /* |
| 129 | * Since the Block Transfer Engine (BTE) is being used for the transfer |
| 130 | * and it relies upon cache-line size transfers, we need to reserve at |
| 131 | * least one cache-line for head and tail alignment. The BTE is |
| 132 | * limited to 8MB transfers. |
| 133 | * |
| 134 | * Testing has shown that changing MTU to greater than 64KB has no effect |
| 135 | * on TCP as the two sides negotiate a Max Segment Size that is limited |
| 136 | * to 64K. Other protocols May use packets greater than this, but for |
| 137 | * now, the default is 64KB. |
| 138 | */ |
| 139 | #define XPNET_MAX_MTU (0x800000UL - L1_CACHE_BYTES) |
| 140 | /* 32KB has been determined to be the ideal */ |
| 141 | #define XPNET_DEF_MTU (0x8000UL) |
| 142 | |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 143 | /* |
| 144 | * The partition id is encapsulated in the MAC address. The following |
| 145 | * define locates the octet the partid is in. |
| 146 | */ |
| 147 | #define XPNET_PARTID_OCTET 1 |
| 148 | #define XPNET_LICENSE_OCTET 2 |
| 149 | |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 150 | /* |
| 151 | * Define the XPNET debug device structure that is to be used with dev_dbg(), |
| 152 | * dev_err(), dev_warn(), and dev_info(). |
| 153 | */ |
| 154 | struct device_driver xpnet_dbg_name = { |
| 155 | .name = "xpnet" |
| 156 | }; |
| 157 | |
| 158 | struct device xpnet_dbg_subname = { |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame^] | 159 | .bus_id = {0}, /* set to "" */ |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 160 | .driver = &xpnet_dbg_name |
| 161 | }; |
| 162 | |
| 163 | struct device *xpnet = &xpnet_dbg_subname; |
| 164 | |
| 165 | /* |
| 166 | * Packet was recevied by XPC and forwarded to us. |
| 167 | */ |
| 168 | static void |
| 169 | xpnet_receive(partid_t partid, int channel, struct xpnet_message *msg) |
| 170 | { |
| 171 | struct sk_buff *skb; |
| 172 | bte_result_t bret; |
| 173 | struct xpnet_dev_private *priv = |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame^] | 174 | (struct xpnet_dev_private *)xpnet_device->priv; |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 175 | |
| 176 | if (!XPNET_VALID_MSG(msg)) { |
| 177 | /* |
| 178 | * Packet with a different XPC version. Ignore. |
| 179 | */ |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame^] | 180 | xpc_received(partid, channel, (void *)msg); |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 181 | |
| 182 | priv->stats.rx_errors++; |
| 183 | |
| 184 | return; |
| 185 | } |
| 186 | dev_dbg(xpnet, "received 0x%lx, %d, %d, %d\n", msg->buf_pa, msg->size, |
| 187 | msg->leadin_ignore, msg->tailout_ignore); |
| 188 | |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 189 | /* reserve an extra cache line */ |
| 190 | skb = dev_alloc_skb(msg->size + L1_CACHE_BYTES); |
| 191 | if (!skb) { |
| 192 | dev_err(xpnet, "failed on dev_alloc_skb(%d)\n", |
| 193 | msg->size + L1_CACHE_BYTES); |
| 194 | |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame^] | 195 | xpc_received(partid, channel, (void *)msg); |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 196 | |
| 197 | priv->stats.rx_errors++; |
| 198 | |
| 199 | return; |
| 200 | } |
| 201 | |
| 202 | /* |
| 203 | * The allocated skb has some reserved space. |
| 204 | * In order to use bte_copy, we need to get the |
| 205 | * skb->data pointer moved forward. |
| 206 | */ |
| 207 | skb_reserve(skb, (L1_CACHE_BYTES - ((u64)skb->data & |
| 208 | (L1_CACHE_BYTES - 1)) + |
| 209 | msg->leadin_ignore)); |
| 210 | |
| 211 | /* |
| 212 | * Update the tail pointer to indicate data actually |
| 213 | * transferred. |
| 214 | */ |
| 215 | skb_put(skb, (msg->size - msg->leadin_ignore - msg->tailout_ignore)); |
| 216 | |
| 217 | /* |
Matt LaPlante | 4b3f686 | 2006-10-03 22:21:02 +0200 | [diff] [blame] | 218 | * Move the data over from the other side. |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 219 | */ |
| 220 | if ((XPNET_VERSION_MINOR(msg->version) == 1) && |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame^] | 221 | (msg->embedded_bytes != 0)) { |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 222 | dev_dbg(xpnet, "copying embedded message. memcpy(0x%p, 0x%p, " |
| 223 | "%lu)\n", skb->data, &msg->data, |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame^] | 224 | (size_t)msg->embedded_bytes); |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 225 | |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame^] | 226 | skb_copy_to_linear_data(skb, &msg->data, |
| 227 | (size_t)msg->embedded_bytes); |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 228 | } else { |
| 229 | dev_dbg(xpnet, "transferring buffer to the skb->data area;\n\t" |
| 230 | "bte_copy(0x%p, 0x%p, %hu)\n", (void *)msg->buf_pa, |
| 231 | (void *)__pa((u64)skb->data & ~(L1_CACHE_BYTES - 1)), |
| 232 | msg->size); |
| 233 | |
| 234 | bret = bte_copy(msg->buf_pa, |
| 235 | __pa((u64)skb->data & ~(L1_CACHE_BYTES - 1)), |
| 236 | msg->size, (BTE_NOTIFY | BTE_WACQUIRE), NULL); |
| 237 | |
| 238 | if (bret != BTE_SUCCESS) { |
| 239 | // >>> Need better way of cleaning skb. Currently skb |
| 240 | // >>> appears in_use and we can't just call |
| 241 | // >>> dev_kfree_skb. |
| 242 | dev_err(xpnet, "bte_copy(0x%p, 0x%p, 0x%hx) returned " |
| 243 | "error=0x%x\n", (void *)msg->buf_pa, |
| 244 | (void *)__pa((u64)skb->data & |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame^] | 245 | ~(L1_CACHE_BYTES - 1)), |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 246 | msg->size, bret); |
| 247 | |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame^] | 248 | xpc_received(partid, channel, (void *)msg); |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 249 | |
| 250 | priv->stats.rx_errors++; |
| 251 | |
| 252 | return; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | dev_dbg(xpnet, "<skb->head=0x%p skb->data=0x%p skb->tail=0x%p " |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame^] | 257 | "skb->end=0x%p skb->len=%d\n", (void *)skb->head, |
Arnaldo Carvalho de Melo | 4305b54 | 2007-04-19 20:43:29 -0700 | [diff] [blame] | 258 | (void *)skb->data, skb_tail_pointer(skb), skb_end_pointer(skb), |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 259 | skb->len); |
| 260 | |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 261 | skb->protocol = eth_type_trans(skb, xpnet_device); |
| 262 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
| 263 | |
Joe Perches | 898eb71 | 2007-10-18 03:06:30 -0700 | [diff] [blame] | 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", |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 267 | (void *)skb->head, (void *)skb->data, skb_tail_pointer(skb), |
Arnaldo Carvalho de Melo | 4305b54 | 2007-04-19 20:43:29 -0700 | [diff] [blame] | 268 | skb_end_pointer(skb), skb->len); |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 269 | |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 270 | xpnet_device->last_rx = jiffies; |
| 271 | priv->stats.rx_packets++; |
| 272 | priv->stats.rx_bytes += skb->len + ETH_HLEN; |
| 273 | |
| 274 | netif_rx_ni(skb); |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame^] | 275 | xpc_received(partid, channel, (void *)msg); |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 276 | } |
| 277 | |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 278 | /* |
| 279 | * This is the handler which XPC calls during any sort of change in |
| 280 | * state or message reception on a connection. |
| 281 | */ |
| 282 | static void |
| 283 | xpnet_connection_activity(enum xpc_retval reason, partid_t partid, int channel, |
| 284 | void *data, void *key) |
| 285 | { |
| 286 | long bp; |
| 287 | |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 288 | DBUG_ON(partid <= 0 || partid >= XP_MAX_PARTITIONS); |
| 289 | DBUG_ON(channel != XPC_NET_CHANNEL); |
| 290 | |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame^] | 291 | switch (reason) { |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 292 | case xpcMsgReceived: /* message received */ |
| 293 | DBUG_ON(data == NULL); |
| 294 | |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame^] | 295 | xpnet_receive(partid, channel, (struct xpnet_message *)data); |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 296 | break; |
| 297 | |
| 298 | case xpcConnected: /* connection completed to a partition */ |
| 299 | spin_lock_bh(&xpnet_broadcast_lock); |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame^] | 300 | xpnet_broadcast_partitions |= 1UL << (partid - 1); |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 301 | bp = xpnet_broadcast_partitions; |
| 302 | spin_unlock_bh(&xpnet_broadcast_lock); |
| 303 | |
| 304 | netif_carrier_on(xpnet_device); |
| 305 | |
| 306 | dev_dbg(xpnet, "%s connection created to partition %d; " |
| 307 | "xpnet_broadcast_partitions=0x%lx\n", |
| 308 | xpnet_device->name, partid, bp); |
| 309 | break; |
| 310 | |
| 311 | default: |
| 312 | spin_lock_bh(&xpnet_broadcast_lock); |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame^] | 313 | xpnet_broadcast_partitions &= ~(1UL << (partid - 1)); |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 314 | bp = xpnet_broadcast_partitions; |
| 315 | spin_unlock_bh(&xpnet_broadcast_lock); |
| 316 | |
| 317 | if (bp == 0) { |
| 318 | netif_carrier_off(xpnet_device); |
| 319 | } |
| 320 | |
| 321 | dev_dbg(xpnet, "%s disconnected from partition %d; " |
| 322 | "xpnet_broadcast_partitions=0x%lx\n", |
| 323 | xpnet_device->name, partid, bp); |
| 324 | break; |
| 325 | |
| 326 | } |
| 327 | } |
| 328 | |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 329 | static int |
| 330 | xpnet_dev_open(struct net_device *dev) |
| 331 | { |
| 332 | enum xpc_retval ret; |
| 333 | |
Tony Luck | b9ae3bd | 2007-05-10 11:47:38 -0700 | [diff] [blame] | 334 | dev_dbg(xpnet, "calling xpc_connect(%d, 0x%p, NULL, %ld, %ld, %ld, " |
| 335 | "%ld)\n", XPC_NET_CHANNEL, xpnet_connection_activity, |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 336 | XPNET_MSG_SIZE, XPNET_MSG_NENTRIES, XPNET_MAX_KTHREADS, |
| 337 | XPNET_MAX_IDLE_KTHREADS); |
| 338 | |
| 339 | ret = xpc_connect(XPC_NET_CHANNEL, xpnet_connection_activity, NULL, |
| 340 | XPNET_MSG_SIZE, XPNET_MSG_NENTRIES, |
| 341 | XPNET_MAX_KTHREADS, XPNET_MAX_IDLE_KTHREADS); |
| 342 | if (ret != xpcSuccess) { |
| 343 | dev_err(xpnet, "ifconfig up of %s failed on XPC connect, " |
| 344 | "ret=%d\n", dev->name, ret); |
| 345 | |
| 346 | return -ENOMEM; |
| 347 | } |
| 348 | |
| 349 | dev_dbg(xpnet, "ifconfig up of %s; XPC connected\n", dev->name); |
| 350 | |
| 351 | return 0; |
| 352 | } |
| 353 | |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 354 | static int |
| 355 | xpnet_dev_stop(struct net_device *dev) |
| 356 | { |
| 357 | xpc_disconnect(XPC_NET_CHANNEL); |
| 358 | |
| 359 | dev_dbg(xpnet, "ifconfig down of %s; XPC disconnected\n", dev->name); |
| 360 | |
| 361 | return 0; |
| 362 | } |
| 363 | |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 364 | static int |
| 365 | xpnet_dev_change_mtu(struct net_device *dev, int new_mtu) |
| 366 | { |
| 367 | /* 68 comes from min TCP+IP+MAC header */ |
| 368 | if ((new_mtu < 68) || (new_mtu > XPNET_MAX_MTU)) { |
| 369 | dev_err(xpnet, "ifconfig %s mtu %d failed; value must be " |
| 370 | "between 68 and %ld\n", dev->name, new_mtu, |
| 371 | XPNET_MAX_MTU); |
| 372 | return -EINVAL; |
| 373 | } |
| 374 | |
| 375 | dev->mtu = new_mtu; |
| 376 | dev_dbg(xpnet, "ifconfig %s mtu set to %d\n", dev->name, new_mtu); |
| 377 | return 0; |
| 378 | } |
| 379 | |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 380 | /* |
| 381 | * Required for the net_device structure. |
| 382 | */ |
| 383 | static int |
| 384 | xpnet_dev_set_config(struct net_device *dev, struct ifmap *new_map) |
| 385 | { |
| 386 | return 0; |
| 387 | } |
| 388 | |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 389 | /* |
| 390 | * Return statistics to the caller. |
| 391 | */ |
| 392 | static struct net_device_stats * |
| 393 | xpnet_dev_get_stats(struct net_device *dev) |
| 394 | { |
| 395 | struct xpnet_dev_private *priv; |
| 396 | |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame^] | 397 | priv = (struct xpnet_dev_private *)dev->priv; |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 398 | |
| 399 | return &priv->stats; |
| 400 | } |
| 401 | |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 402 | /* |
| 403 | * Notification that the other end has received the message and |
| 404 | * DMA'd the skb information. At this point, they are done with |
| 405 | * our side. When all recipients are done processing, we |
| 406 | * release the skb and then release our pending message structure. |
| 407 | */ |
| 408 | static void |
| 409 | xpnet_send_completed(enum xpc_retval reason, partid_t partid, int channel, |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame^] | 410 | void *__qm) |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 411 | { |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame^] | 412 | struct xpnet_pending_msg *queued_msg = (struct xpnet_pending_msg *)__qm; |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 413 | |
| 414 | DBUG_ON(queued_msg == NULL); |
| 415 | |
| 416 | dev_dbg(xpnet, "message to %d notified with reason %d\n", |
| 417 | partid, reason); |
| 418 | |
| 419 | if (atomic_dec_return(&queued_msg->use_count) == 0) { |
| 420 | dev_dbg(xpnet, "all acks for skb->head=-x%p\n", |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame^] | 421 | (void *)queued_msg->skb->head); |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 422 | |
| 423 | dev_kfree_skb_any(queued_msg->skb); |
| 424 | kfree(queued_msg); |
| 425 | } |
| 426 | } |
| 427 | |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 428 | /* |
| 429 | * Network layer has formatted a packet (skb) and is ready to place it |
| 430 | * "on the wire". Prepare and send an xpnet_message to all partitions |
| 431 | * which have connected with us and are targets of this packet. |
| 432 | * |
| 433 | * MAC-NOTE: For the XPNET driver, the MAC address contains the |
| 434 | * destination partition_id. If the destination partition id word |
| 435 | * is 0xff, this packet is to broadcast to all partitions. |
| 436 | */ |
| 437 | static int |
| 438 | xpnet_dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev) |
| 439 | { |
| 440 | struct xpnet_pending_msg *queued_msg; |
| 441 | enum xpc_retval ret; |
| 442 | struct xpnet_message *msg; |
| 443 | u64 start_addr, end_addr; |
| 444 | long dp; |
| 445 | u8 second_mac_octet; |
| 446 | partid_t dest_partid; |
| 447 | struct xpnet_dev_private *priv; |
| 448 | u16 embedded_bytes; |
| 449 | |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame^] | 450 | priv = (struct xpnet_dev_private *)dev->priv; |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 451 | |
| 452 | dev_dbg(xpnet, ">skb->head=0x%p skb->data=0x%p skb->tail=0x%p " |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame^] | 453 | "skb->end=0x%p skb->len=%d\n", (void *)skb->head, |
Arnaldo Carvalho de Melo | 4305b54 | 2007-04-19 20:43:29 -0700 | [diff] [blame] | 454 | (void *)skb->data, skb_tail_pointer(skb), skb_end_pointer(skb), |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 455 | skb->len); |
| 456 | |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 457 | /* |
| 458 | * The xpnet_pending_msg tracks how many outstanding |
| 459 | * xpc_send_notifies are relying on this skb. When none |
| 460 | * remain, release the skb. |
| 461 | */ |
| 462 | queued_msg = kmalloc(sizeof(struct xpnet_pending_msg), GFP_ATOMIC); |
| 463 | if (queued_msg == NULL) { |
| 464 | dev_warn(xpnet, "failed to kmalloc %ld bytes; dropping " |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame^] | 465 | "packet\n", sizeof(struct xpnet_pending_msg)); |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 466 | |
| 467 | priv->stats.tx_errors++; |
| 468 | |
| 469 | return -ENOMEM; |
| 470 | } |
| 471 | |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 472 | /* get the beginning of the first cacheline and end of last */ |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame^] | 473 | start_addr = ((u64)skb->data & ~(L1_CACHE_BYTES - 1)); |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 474 | end_addr = L1_CACHE_ALIGN((u64)skb_tail_pointer(skb)); |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 475 | |
| 476 | /* calculate how many bytes to embed in the XPC message */ |
| 477 | embedded_bytes = 0; |
| 478 | if (unlikely(skb->len <= XPNET_MSG_DATA_MAX)) { |
| 479 | /* skb->data does fit so embed */ |
| 480 | embedded_bytes = skb->len; |
| 481 | } |
| 482 | |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 483 | /* |
| 484 | * Since the send occurs asynchronously, we set the count to one |
| 485 | * and begin sending. Any sends that happen to complete before |
| 486 | * we are done sending will not free the skb. We will be left |
| 487 | * with that task during exit. This also handles the case of |
| 488 | * a packet destined for a partition which is no longer up. |
| 489 | */ |
| 490 | atomic_set(&queued_msg->use_count, 1); |
| 491 | queued_msg->skb = skb; |
| 492 | |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 493 | second_mac_octet = skb->data[XPNET_PARTID_OCTET]; |
| 494 | if (second_mac_octet == 0xff) { |
| 495 | /* we are being asked to broadcast to all partitions */ |
| 496 | dp = xpnet_broadcast_partitions; |
| 497 | } else if (second_mac_octet != 0) { |
| 498 | dp = xpnet_broadcast_partitions & |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame^] | 499 | (1UL << (second_mac_octet - 1)); |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 500 | } else { |
| 501 | /* 0 is an invalid partid. Ignore */ |
| 502 | dp = 0; |
| 503 | } |
| 504 | dev_dbg(xpnet, "destination Partitions mask (dp) = 0x%lx\n", dp); |
| 505 | |
| 506 | /* |
Simon Arlott | 72fdbdc | 2007-05-11 14:55:43 -0700 | [diff] [blame] | 507 | * If we wanted to allow promiscuous mode to work like an |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 508 | * unswitched network, this would be a good point to OR in a |
| 509 | * mask of partitions which should be receiving all packets. |
| 510 | */ |
| 511 | |
| 512 | /* |
| 513 | * Main send loop. |
| 514 | */ |
| 515 | for (dest_partid = 1; dp && dest_partid < XP_MAX_PARTITIONS; |
| 516 | dest_partid++) { |
| 517 | |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 518 | if (!(dp & (1UL << (dest_partid - 1)))) { |
| 519 | /* not destined for this partition */ |
| 520 | continue; |
| 521 | } |
| 522 | |
| 523 | /* remove this partition from the destinations mask */ |
| 524 | dp &= ~(1UL << (dest_partid - 1)); |
| 525 | |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 526 | /* found a partition to send to */ |
| 527 | |
| 528 | ret = xpc_allocate(dest_partid, XPC_NET_CHANNEL, |
| 529 | XPC_NOWAIT, (void **)&msg); |
| 530 | if (unlikely(ret != xpcSuccess)) { |
| 531 | continue; |
| 532 | } |
| 533 | |
| 534 | msg->embedded_bytes = embedded_bytes; |
| 535 | if (unlikely(embedded_bytes != 0)) { |
| 536 | msg->version = XPNET_VERSION_EMBED; |
| 537 | dev_dbg(xpnet, "calling memcpy(0x%p, 0x%p, 0x%lx)\n", |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame^] | 538 | &msg->data, skb->data, (size_t)embedded_bytes); |
Arnaldo Carvalho de Melo | d626f62 | 2007-03-27 18:55:52 -0300 | [diff] [blame] | 539 | skb_copy_from_linear_data(skb, &msg->data, |
| 540 | (size_t)embedded_bytes); |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 541 | } else { |
| 542 | msg->version = XPNET_VERSION; |
| 543 | } |
| 544 | msg->magic = XPNET_MAGIC; |
| 545 | msg->size = end_addr - start_addr; |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame^] | 546 | msg->leadin_ignore = (u64)skb->data - start_addr; |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 547 | msg->tailout_ignore = end_addr - (u64)skb_tail_pointer(skb); |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 548 | msg->buf_pa = __pa(start_addr); |
| 549 | |
Joe Perches | 898eb71 | 2007-10-18 03:06:30 -0700 | [diff] [blame] | 550 | dev_dbg(xpnet, "sending XPC message to %d:%d\n" |
| 551 | KERN_DEBUG "msg->buf_pa=0x%lx, msg->size=%u, " |
| 552 | "msg->leadin_ignore=%u, msg->tailout_ignore=%u\n", |
| 553 | dest_partid, XPC_NET_CHANNEL, msg->buf_pa, msg->size, |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 554 | msg->leadin_ignore, msg->tailout_ignore); |
| 555 | |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 556 | atomic_inc(&queued_msg->use_count); |
| 557 | |
| 558 | ret = xpc_send_notify(dest_partid, XPC_NET_CHANNEL, msg, |
| 559 | xpnet_send_completed, queued_msg); |
| 560 | if (unlikely(ret != xpcSuccess)) { |
| 561 | atomic_dec(&queued_msg->use_count); |
| 562 | continue; |
| 563 | } |
| 564 | |
| 565 | } |
| 566 | |
| 567 | if (atomic_dec_return(&queued_msg->use_count) == 0) { |
| 568 | dev_dbg(xpnet, "no partitions to receive packet destined for " |
| 569 | "%d\n", dest_partid); |
| 570 | |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 571 | dev_kfree_skb(skb); |
| 572 | kfree(queued_msg); |
| 573 | } |
| 574 | |
| 575 | priv->stats.tx_packets++; |
| 576 | priv->stats.tx_bytes += skb->len; |
| 577 | |
| 578 | return 0; |
| 579 | } |
| 580 | |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 581 | /* |
| 582 | * Deal with transmit timeouts coming from the network layer. |
| 583 | */ |
| 584 | static void |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame^] | 585 | xpnet_dev_tx_timeout(struct net_device *dev) |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 586 | { |
| 587 | struct xpnet_dev_private *priv; |
| 588 | |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame^] | 589 | priv = (struct xpnet_dev_private *)dev->priv; |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 590 | |
| 591 | priv->stats.tx_errors++; |
| 592 | return; |
| 593 | } |
| 594 | |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 595 | static int __init |
| 596 | xpnet_init(void) |
| 597 | { |
| 598 | int i; |
| 599 | u32 license_num; |
| 600 | int result = -ENOMEM; |
| 601 | |
Dean Nelson | 408865c | 2005-09-08 10:46:58 -0500 | [diff] [blame] | 602 | if (!ia64_platform_is("sn2")) { |
| 603 | return -ENODEV; |
| 604 | } |
| 605 | |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 606 | dev_info(xpnet, "registering network device %s\n", XPNET_DEVICE_NAME); |
| 607 | |
| 608 | /* |
| 609 | * use ether_setup() to init the majority of our device |
| 610 | * structure and then override the necessary pieces. |
| 611 | */ |
| 612 | xpnet_device = alloc_netdev(sizeof(struct xpnet_dev_private), |
| 613 | XPNET_DEVICE_NAME, ether_setup); |
| 614 | if (xpnet_device == NULL) { |
| 615 | return -ENOMEM; |
| 616 | } |
| 617 | |
| 618 | netif_carrier_off(xpnet_device); |
| 619 | |
| 620 | xpnet_device->mtu = XPNET_DEF_MTU; |
| 621 | xpnet_device->change_mtu = xpnet_dev_change_mtu; |
| 622 | xpnet_device->open = xpnet_dev_open; |
| 623 | xpnet_device->get_stats = xpnet_dev_get_stats; |
| 624 | xpnet_device->stop = xpnet_dev_stop; |
| 625 | xpnet_device->hard_start_xmit = xpnet_dev_hard_start_xmit; |
| 626 | xpnet_device->tx_timeout = xpnet_dev_tx_timeout; |
| 627 | xpnet_device->set_config = xpnet_dev_set_config; |
| 628 | |
| 629 | /* |
| 630 | * Multicast assumes the LSB of the first octet is set for multicast |
| 631 | * MAC addresses. We chose the first octet of the MAC to be unlikely |
| 632 | * to collide with any vendor's officially issued MAC. |
| 633 | */ |
| 634 | xpnet_device->dev_addr[0] = 0xfe; |
| 635 | xpnet_device->dev_addr[XPNET_PARTID_OCTET] = sn_partition_id; |
| 636 | license_num = sn_partition_serial_number_val(); |
| 637 | for (i = 3; i >= 0; i--) { |
| 638 | xpnet_device->dev_addr[XPNET_LICENSE_OCTET + i] = |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame^] | 639 | license_num & 0xff; |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 640 | license_num = license_num >> 8; |
| 641 | } |
| 642 | |
| 643 | /* |
| 644 | * ether_setup() sets this to a multicast device. We are |
| 645 | * really not supporting multicast at this time. |
| 646 | */ |
| 647 | xpnet_device->flags &= ~IFF_MULTICAST; |
| 648 | |
| 649 | /* |
| 650 | * No need to checksum as it is a DMA transfer. The BTE will |
| 651 | * report an error if the data is not retrievable and the |
| 652 | * packet will be dropped. |
| 653 | */ |
| 654 | xpnet_device->features = NETIF_F_NO_CSUM; |
| 655 | |
| 656 | result = register_netdev(xpnet_device); |
| 657 | if (result != 0) { |
| 658 | free_netdev(xpnet_device); |
| 659 | } |
| 660 | |
| 661 | return result; |
| 662 | } |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 663 | |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame^] | 664 | module_init(xpnet_init); |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 665 | |
| 666 | static void __exit |
| 667 | xpnet_exit(void) |
| 668 | { |
| 669 | dev_info(xpnet, "unregistering network device %s\n", |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame^] | 670 | xpnet_device[0].name); |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 671 | |
| 672 | unregister_netdev(xpnet_device); |
| 673 | |
| 674 | free_netdev(xpnet_device); |
| 675 | } |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 676 | |
Dean Nelson | 3519050 | 2008-04-22 14:48:55 -0500 | [diff] [blame^] | 677 | module_exit(xpnet_exit); |
Dean Nelson | a2d974d | 2005-03-23 20:50:00 -0700 | [diff] [blame] | 678 | |
| 679 | MODULE_AUTHOR("Silicon Graphics, Inc."); |
| 680 | MODULE_DESCRIPTION("Cross Partition Network adapter (XPNET)"); |
| 681 | MODULE_LICENSE("GPL"); |