blob: f9356ba7315571151fc17ee6bd4c244f85956601 [file] [log] [blame]
Dean Nelsona2d974d2005-03-23 20:50:00 -07001/*
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 Nelson45d9ca42008-04-22 14:46:56 -05006 * Copyright (C) 1999-2008 Silicon Graphics, Inc. All rights reserved.
Dean Nelsona2d974d2005-03-23 20:50:00 -07007 */
8
Dean Nelsona2d974d2005-03-23 20:50:00 -07009/*
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 Nelsona2d974d2005-03-23 20:50:00 -070023#include <linux/module.h>
Dean Nelson2c2b94f2008-04-22 14:50:17 -050024#include <linux/types.h>
Dean Nelsona2d974d2005-03-23 20:50:00 -070025#include <linux/kernel.h>
Dean Nelsona2d974d2005-03-23 20:50:00 -070026#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>
Dean Nelsona2d974d2005-03-23 20:50:00 -070035#include <asm/sn/io.h>
36#include <asm/sn/sn_sal.h>
Dean Nelsona2d974d2005-03-23 20:50:00 -070037#include <asm/atomic.h>
Dean Nelson45d9ca42008-04-22 14:46:56 -050038#include "xp.h"
Dean Nelsona2d974d2005-03-23 20:50:00 -070039
Dean Nelsona2d974d2005-03-23 20:50:00 -070040/*
41 * The message payload transferred by XPC.
42 *
43 * buf_pa is the physical address where the DMA should pull from.
44 *
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
50 * (tailout_ignore).
51 *
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
54 */
55struct 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 */
64};
65
66/*
67 * Determine the size of our message, the cacheline aligned size,
68 * and then the number of message will request from XPC.
69 *
70 * XPC expects each message to exist in an individual cacheline.
71 */
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)
77
Dean Nelsona2d974d2005-03-23 20:50:00 -070078#define XPNET_MAX_KTHREADS (XPNET_MSG_NENTRIES + 1)
79#define XPNET_MAX_IDLE_KTHREADS (XPNET_MSG_NENTRIES + 1)
80
81/*
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.
84 */
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)
88
Dean Nelson2c2b94f2008-04-22 14:50:17 -050089#define XPNET_VERSION _XPNET_VERSION(1, 0) /* version 1.0 */
90#define XPNET_VERSION_EMBED _XPNET_VERSION(1, 1) /* version 1.1 */
Dean Nelson35190502008-04-22 14:48:55 -050091#define XPNET_MAGIC 0x88786984 /* "XNET" */
Dean Nelsona2d974d2005-03-23 20:50:00 -070092
93#define XPNET_VALID_MSG(_m) \
94 ((XPNET_VERSION_MAJOR(_m->version) == XPNET_VERSION_MAJOR(XPNET_VERSION)) \
95 && (msg->magic == XPNET_MAGIC))
96
97#define XPNET_DEVICE_NAME "xp0"
98
Dean Nelsona2d974d2005-03-23 20:50:00 -070099/*
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
104 * then be released.
105 */
106struct xpnet_pending_msg {
107 struct list_head free_list;
108 struct sk_buff *skb;
109 atomic_t use_count;
110};
111
112/* driver specific structure pointed to by the device structure */
113struct xpnet_dev_private {
114 struct net_device_stats stats;
115};
116
117struct net_device *xpnet_device;
118
119/*
120 * When we are notified of other partitions activating, we add them to
121 * our bitmask of partitions to which we broadcast.
122 */
123static u64 xpnet_broadcast_partitions;
124/* protect above */
Ingo Molnara9f6a0d2005-09-09 13:10:41 -0700125static DEFINE_SPINLOCK(xpnet_broadcast_lock);
Dean Nelsona2d974d2005-03-23 20:50:00 -0700126
127/*
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.
132 *
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.
137 */
138#define XPNET_MAX_MTU (0x800000UL - L1_CACHE_BYTES)
139/* 32KB has been determined to be the ideal */
140#define XPNET_DEF_MTU (0x8000UL)
141
Dean Nelsona2d974d2005-03-23 20:50:00 -0700142/*
143 * The partition id is encapsulated in the MAC address. The following
144 * define locates the octet the partid is in.
145 */
146#define XPNET_PARTID_OCTET 1
147#define XPNET_LICENSE_OCTET 2
148
Dean Nelsona2d974d2005-03-23 20:50:00 -0700149/*
150 * Define the XPNET debug device structure that is to be used with dev_dbg(),
151 * dev_err(), dev_warn(), and dev_info().
152 */
153struct device_driver xpnet_dbg_name = {
154 .name = "xpnet"
155};
156
157struct device xpnet_dbg_subname = {
Dean Nelson35190502008-04-22 14:48:55 -0500158 .bus_id = {0}, /* set to "" */
Dean Nelsona2d974d2005-03-23 20:50:00 -0700159 .driver = &xpnet_dbg_name
160};
161
162struct device *xpnet = &xpnet_dbg_subname;
163
164/*
165 * Packet was recevied by XPC and forwarded to us.
166 */
167static void
Dean Nelson64d032b2008-05-12 14:02:03 -0700168xpnet_receive(short partid, int channel, struct xpnet_message *msg)
Dean Nelsona2d974d2005-03-23 20:50:00 -0700169{
170 struct sk_buff *skb;
Dean Nelson908787d2008-07-29 22:34:05 -0700171 enum xp_retval ret;
Dean Nelsona2d974d2005-03-23 20:50:00 -0700172 struct xpnet_dev_private *priv =
Dean Nelson35190502008-04-22 14:48:55 -0500173 (struct xpnet_dev_private *)xpnet_device->priv;
Dean Nelsona2d974d2005-03-23 20:50:00 -0700174
175 if (!XPNET_VALID_MSG(msg)) {
176 /*
177 * Packet with a different XPC version. Ignore.
178 */
Dean Nelson35190502008-04-22 14:48:55 -0500179 xpc_received(partid, channel, (void *)msg);
Dean Nelsona2d974d2005-03-23 20:50:00 -0700180
181 priv->stats.rx_errors++;
182
183 return;
184 }
185 dev_dbg(xpnet, "received 0x%lx, %d, %d, %d\n", msg->buf_pa, msg->size,
186 msg->leadin_ignore, msg->tailout_ignore);
187
Dean Nelsona2d974d2005-03-23 20:50:00 -0700188 /* reserve an extra cache line */
189 skb = dev_alloc_skb(msg->size + L1_CACHE_BYTES);
190 if (!skb) {
191 dev_err(xpnet, "failed on dev_alloc_skb(%d)\n",
192 msg->size + L1_CACHE_BYTES);
193
Dean Nelson35190502008-04-22 14:48:55 -0500194 xpc_received(partid, channel, (void *)msg);
Dean Nelsona2d974d2005-03-23 20:50:00 -0700195
196 priv->stats.rx_errors++;
197
198 return;
199 }
200
201 /*
202 * The allocated skb has some reserved space.
Dean Nelson908787d2008-07-29 22:34:05 -0700203 * In order to use xp_remote_memcpy(), we need to get the
Dean Nelsona2d974d2005-03-23 20:50:00 -0700204 * skb->data pointer moved forward.
205 */
206 skb_reserve(skb, (L1_CACHE_BYTES - ((u64)skb->data &
207 (L1_CACHE_BYTES - 1)) +
208 msg->leadin_ignore));
209
210 /*
211 * Update the tail pointer to indicate data actually
212 * transferred.
213 */
214 skb_put(skb, (msg->size - msg->leadin_ignore - msg->tailout_ignore));
215
216 /*
Matt LaPlante4b3f6862006-10-03 22:21:02 +0200217 * Move the data over from the other side.
Dean Nelsona2d974d2005-03-23 20:50:00 -0700218 */
219 if ((XPNET_VERSION_MINOR(msg->version) == 1) &&
Dean Nelson35190502008-04-22 14:48:55 -0500220 (msg->embedded_bytes != 0)) {
Dean Nelsona2d974d2005-03-23 20:50:00 -0700221 dev_dbg(xpnet, "copying embedded message. memcpy(0x%p, 0x%p, "
222 "%lu)\n", skb->data, &msg->data,
Dean Nelson35190502008-04-22 14:48:55 -0500223 (size_t)msg->embedded_bytes);
Dean Nelsona2d974d2005-03-23 20:50:00 -0700224
Dean Nelson35190502008-04-22 14:48:55 -0500225 skb_copy_to_linear_data(skb, &msg->data,
226 (size_t)msg->embedded_bytes);
Dean Nelsona2d974d2005-03-23 20:50:00 -0700227 } else {
228 dev_dbg(xpnet, "transferring buffer to the skb->data area;\n\t"
Dean Nelson908787d2008-07-29 22:34:05 -0700229 "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);
Dean Nelsona2d974d2005-03-23 20:50:00 -0700232
Dean Nelson908787d2008-07-29 22:34:05 -0700233 ret = xp_remote_memcpy((void *)((u64)skb->data &
234 ~(L1_CACHE_BYTES - 1)),
235 (void *)msg->buf_pa, msg->size);
Dean Nelsona2d974d2005-03-23 20:50:00 -0700236
Dean Nelson908787d2008-07-29 22:34:05 -0700237 if (ret != xpSuccess) {
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500238 /*
239 * >>> Need better way of cleaning skb. Currently skb
240 * >>> appears in_use and we can't just call
241 * >>> dev_kfree_skb.
242 */
Dean Nelson908787d2008-07-29 22:34:05 -0700243 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);
Dean Nelsona2d974d2005-03-23 20:50:00 -0700247
Dean Nelson35190502008-04-22 14:48:55 -0500248 xpc_received(partid, channel, (void *)msg);
Dean Nelsona2d974d2005-03-23 20:50:00 -0700249
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 Nelson35190502008-04-22 14:48:55 -0500257 "skb->end=0x%p skb->len=%d\n", (void *)skb->head,
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700258 (void *)skb->data, skb_tail_pointer(skb), skb_end_pointer(skb),
Dean Nelsona2d974d2005-03-23 20:50:00 -0700259 skb->len);
260
Dean Nelsona2d974d2005-03-23 20:50:00 -0700261 skb->protocol = eth_type_trans(skb, xpnet_device);
262 skb->ip_summed = CHECKSUM_UNNECESSARY;
263
Joe Perches898eb712007-10-18 03:06:30 -0700264 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 Melo27a884d2007-04-19 20:29:13 -0700267 (void *)skb->head, (void *)skb->data, skb_tail_pointer(skb),
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700268 skb_end_pointer(skb), skb->len);
Dean Nelsona2d974d2005-03-23 20:50:00 -0700269
Dean Nelsona2d974d2005-03-23 20:50:00 -0700270 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 Nelson35190502008-04-22 14:48:55 -0500275 xpc_received(partid, channel, (void *)msg);
Dean Nelsona2d974d2005-03-23 20:50:00 -0700276}
277
Dean Nelsona2d974d2005-03-23 20:50:00 -0700278/*
279 * This is the handler which XPC calls during any sort of change in
280 * state or message reception on a connection.
281 */
282static void
Dean Nelson64d032b2008-05-12 14:02:03 -0700283xpnet_connection_activity(enum xp_retval reason, short partid, int channel,
Dean Nelsona2d974d2005-03-23 20:50:00 -0700284 void *data, void *key)
285{
286 long bp;
287
Dean Nelsonbc63d382008-07-29 22:34:04 -0700288 DBUG_ON(partid < 0 || partid >= xp_max_npartitions);
Dean Nelsona2d974d2005-03-23 20:50:00 -0700289 DBUG_ON(channel != XPC_NET_CHANNEL);
290
Dean Nelson35190502008-04-22 14:48:55 -0500291 switch (reason) {
Dean Nelson65c17b82008-05-12 14:02:02 -0700292 case xpMsgReceived: /* message received */
Dean Nelsona2d974d2005-03-23 20:50:00 -0700293 DBUG_ON(data == NULL);
294
Dean Nelson35190502008-04-22 14:48:55 -0500295 xpnet_receive(partid, channel, (struct xpnet_message *)data);
Dean Nelsona2d974d2005-03-23 20:50:00 -0700296 break;
297
Dean Nelson65c17b82008-05-12 14:02:02 -0700298 case xpConnected: /* connection completed to a partition */
Dean Nelsona2d974d2005-03-23 20:50:00 -0700299 spin_lock_bh(&xpnet_broadcast_lock);
Dean Nelson35190502008-04-22 14:48:55 -0500300 xpnet_broadcast_partitions |= 1UL << (partid - 1);
Dean Nelsona2d974d2005-03-23 20:50:00 -0700301 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 Nelson35190502008-04-22 14:48:55 -0500313 xpnet_broadcast_partitions &= ~(1UL << (partid - 1));
Dean Nelsona2d974d2005-03-23 20:50:00 -0700314 bp = xpnet_broadcast_partitions;
315 spin_unlock_bh(&xpnet_broadcast_lock);
316
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500317 if (bp == 0)
Dean Nelsona2d974d2005-03-23 20:50:00 -0700318 netif_carrier_off(xpnet_device);
Dean Nelsona2d974d2005-03-23 20:50:00 -0700319
320 dev_dbg(xpnet, "%s disconnected from partition %d; "
321 "xpnet_broadcast_partitions=0x%lx\n",
322 xpnet_device->name, partid, bp);
323 break;
324
325 }
326}
327
Dean Nelsona2d974d2005-03-23 20:50:00 -0700328static int
329xpnet_dev_open(struct net_device *dev)
330{
Dean Nelson65c17b82008-05-12 14:02:02 -0700331 enum xp_retval ret;
Dean Nelsona2d974d2005-03-23 20:50:00 -0700332
Tony Luckb9ae3bd2007-05-10 11:47:38 -0700333 dev_dbg(xpnet, "calling xpc_connect(%d, 0x%p, NULL, %ld, %ld, %ld, "
334 "%ld)\n", XPC_NET_CHANNEL, xpnet_connection_activity,
Dean Nelsona2d974d2005-03-23 20:50:00 -0700335 XPNET_MSG_SIZE, XPNET_MSG_NENTRIES, XPNET_MAX_KTHREADS,
336 XPNET_MAX_IDLE_KTHREADS);
337
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);
Dean Nelson65c17b82008-05-12 14:02:02 -0700341 if (ret != xpSuccess) {
Dean Nelsona2d974d2005-03-23 20:50:00 -0700342 dev_err(xpnet, "ifconfig up of %s failed on XPC connect, "
343 "ret=%d\n", dev->name, ret);
344
345 return -ENOMEM;
346 }
347
348 dev_dbg(xpnet, "ifconfig up of %s; XPC connected\n", dev->name);
349
350 return 0;
351}
352
Dean Nelsona2d974d2005-03-23 20:50:00 -0700353static int
354xpnet_dev_stop(struct net_device *dev)
355{
356 xpc_disconnect(XPC_NET_CHANNEL);
357
358 dev_dbg(xpnet, "ifconfig down of %s; XPC disconnected\n", dev->name);
359
360 return 0;
361}
362
Dean Nelsona2d974d2005-03-23 20:50:00 -0700363static int
364xpnet_dev_change_mtu(struct net_device *dev, int new_mtu)
365{
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,
370 XPNET_MAX_MTU);
371 return -EINVAL;
372 }
373
374 dev->mtu = new_mtu;
375 dev_dbg(xpnet, "ifconfig %s mtu set to %d\n", dev->name, new_mtu);
376 return 0;
377}
378
Dean Nelsona2d974d2005-03-23 20:50:00 -0700379/*
380 * Required for the net_device structure.
381 */
382static int
383xpnet_dev_set_config(struct net_device *dev, struct ifmap *new_map)
384{
385 return 0;
386}
387
Dean Nelsona2d974d2005-03-23 20:50:00 -0700388/*
389 * Return statistics to the caller.
390 */
391static struct net_device_stats *
392xpnet_dev_get_stats(struct net_device *dev)
393{
394 struct xpnet_dev_private *priv;
395
Dean Nelson35190502008-04-22 14:48:55 -0500396 priv = (struct xpnet_dev_private *)dev->priv;
Dean Nelsona2d974d2005-03-23 20:50:00 -0700397
398 return &priv->stats;
399}
400
Dean Nelsona2d974d2005-03-23 20:50:00 -0700401/*
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.
406 */
407static void
Dean Nelson64d032b2008-05-12 14:02:03 -0700408xpnet_send_completed(enum xp_retval reason, short partid, int channel,
Dean Nelson35190502008-04-22 14:48:55 -0500409 void *__qm)
Dean Nelsona2d974d2005-03-23 20:50:00 -0700410{
Dean Nelson35190502008-04-22 14:48:55 -0500411 struct xpnet_pending_msg *queued_msg = (struct xpnet_pending_msg *)__qm;
Dean Nelsona2d974d2005-03-23 20:50:00 -0700412
413 DBUG_ON(queued_msg == NULL);
414
415 dev_dbg(xpnet, "message to %d notified with reason %d\n",
416 partid, reason);
417
418 if (atomic_dec_return(&queued_msg->use_count) == 0) {
419 dev_dbg(xpnet, "all acks for skb->head=-x%p\n",
Dean Nelson35190502008-04-22 14:48:55 -0500420 (void *)queued_msg->skb->head);
Dean Nelsona2d974d2005-03-23 20:50:00 -0700421
422 dev_kfree_skb_any(queued_msg->skb);
423 kfree(queued_msg);
424 }
425}
426
Dean Nelsona2d974d2005-03-23 20:50:00 -0700427/*
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.
431 *
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.
435 */
436static int
437xpnet_dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
438{
439 struct xpnet_pending_msg *queued_msg;
Dean Nelson65c17b82008-05-12 14:02:02 -0700440 enum xp_retval ret;
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700441 u8 msg_buffer[XPNET_MSG_SIZE];
442 struct xpnet_message *msg = (struct xpnet_message *)&msg_buffer[0];
Dean Nelsona2d974d2005-03-23 20:50:00 -0700443 u64 start_addr, end_addr;
444 long dp;
445 u8 second_mac_octet;
Dean Nelson64d032b2008-05-12 14:02:03 -0700446 short dest_partid;
Dean Nelsona2d974d2005-03-23 20:50:00 -0700447 struct xpnet_dev_private *priv;
448 u16 embedded_bytes;
449
Dean Nelson35190502008-04-22 14:48:55 -0500450 priv = (struct xpnet_dev_private *)dev->priv;
Dean Nelsona2d974d2005-03-23 20:50:00 -0700451
452 dev_dbg(xpnet, ">skb->head=0x%p skb->data=0x%p skb->tail=0x%p "
Dean Nelson35190502008-04-22 14:48:55 -0500453 "skb->end=0x%p skb->len=%d\n", (void *)skb->head,
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -0700454 (void *)skb->data, skb_tail_pointer(skb), skb_end_pointer(skb),
Dean Nelsona2d974d2005-03-23 20:50:00 -0700455 skb->len);
456
Dean Nelsona2d974d2005-03-23 20:50:00 -0700457 /*
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 Nelson35190502008-04-22 14:48:55 -0500465 "packet\n", sizeof(struct xpnet_pending_msg));
Dean Nelsona2d974d2005-03-23 20:50:00 -0700466
467 priv->stats.tx_errors++;
468
469 return -ENOMEM;
470 }
471
Dean Nelsona2d974d2005-03-23 20:50:00 -0700472 /* get the beginning of the first cacheline and end of last */
Dean Nelson35190502008-04-22 14:48:55 -0500473 start_addr = ((u64)skb->data & ~(L1_CACHE_BYTES - 1));
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700474 end_addr = L1_CACHE_ALIGN((u64)skb_tail_pointer(skb));
Dean Nelsona2d974d2005-03-23 20:50:00 -0700475
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 Nelsona2d974d2005-03-23 20:50:00 -0700483 /*
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 Nelsona2d974d2005-03-23 20:50:00 -0700493 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 Nelson35190502008-04-22 14:48:55 -0500499 (1UL << (second_mac_octet - 1));
Dean Nelsona2d974d2005-03-23 20:50:00 -0700500 } 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 Arlott72fdbdc2007-05-11 14:55:43 -0700507 * If we wanted to allow promiscuous mode to work like an
Dean Nelsona2d974d2005-03-23 20:50:00 -0700508 * 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 */
Dean Nelsonbc63d382008-07-29 22:34:04 -0700515 for (dest_partid = 0; dp && dest_partid < xp_max_npartitions;
Dean Nelsona2d974d2005-03-23 20:50:00 -0700516 dest_partid++) {
517
Dean Nelsona2d974d2005-03-23 20:50:00 -0700518 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 Nelsona2d974d2005-03-23 20:50:00 -0700526 /* found a partition to send to */
527
Dean Nelsona2d974d2005-03-23 20:50:00 -0700528 msg->embedded_bytes = embedded_bytes;
529 if (unlikely(embedded_bytes != 0)) {
530 msg->version = XPNET_VERSION_EMBED;
531 dev_dbg(xpnet, "calling memcpy(0x%p, 0x%p, 0x%lx)\n",
Dean Nelson35190502008-04-22 14:48:55 -0500532 &msg->data, skb->data, (size_t)embedded_bytes);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300533 skb_copy_from_linear_data(skb, &msg->data,
534 (size_t)embedded_bytes);
Dean Nelsona2d974d2005-03-23 20:50:00 -0700535 } else {
536 msg->version = XPNET_VERSION;
537 }
538 msg->magic = XPNET_MAGIC;
539 msg->size = end_addr - start_addr;
Dean Nelson35190502008-04-22 14:48:55 -0500540 msg->leadin_ignore = (u64)skb->data - start_addr;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700541 msg->tailout_ignore = end_addr - (u64)skb_tail_pointer(skb);
Dean Nelsona2d974d2005-03-23 20:50:00 -0700542 msg->buf_pa = __pa(start_addr);
543
Joe Perches898eb712007-10-18 03:06:30 -0700544 dev_dbg(xpnet, "sending XPC message to %d:%d\n"
545 KERN_DEBUG "msg->buf_pa=0x%lx, msg->size=%u, "
546 "msg->leadin_ignore=%u, msg->tailout_ignore=%u\n",
547 dest_partid, XPC_NET_CHANNEL, msg->buf_pa, msg->size,
Dean Nelsona2d974d2005-03-23 20:50:00 -0700548 msg->leadin_ignore, msg->tailout_ignore);
549
Dean Nelsona2d974d2005-03-23 20:50:00 -0700550 atomic_inc(&queued_msg->use_count);
551
Dean Nelson97bf1aa2008-07-29 22:34:08 -0700552 ret = xpc_send_notify(dest_partid, XPC_NET_CHANNEL, XPC_NOWAIT,
553 &msg, sizeof(msg) + embedded_bytes - 1,
Dean Nelsona2d974d2005-03-23 20:50:00 -0700554 xpnet_send_completed, queued_msg);
Dean Nelson65c17b82008-05-12 14:02:02 -0700555 if (unlikely(ret != xpSuccess)) {
Dean Nelsona2d974d2005-03-23 20:50:00 -0700556 atomic_dec(&queued_msg->use_count);
557 continue;
558 }
Dean Nelsona2d974d2005-03-23 20:50:00 -0700559 }
560
561 if (atomic_dec_return(&queued_msg->use_count) == 0) {
562 dev_dbg(xpnet, "no partitions to receive packet destined for "
563 "%d\n", dest_partid);
564
Dean Nelsona2d974d2005-03-23 20:50:00 -0700565 dev_kfree_skb(skb);
566 kfree(queued_msg);
567 }
568
569 priv->stats.tx_packets++;
570 priv->stats.tx_bytes += skb->len;
571
572 return 0;
573}
574
Dean Nelsona2d974d2005-03-23 20:50:00 -0700575/*
576 * Deal with transmit timeouts coming from the network layer.
577 */
578static void
Dean Nelson35190502008-04-22 14:48:55 -0500579xpnet_dev_tx_timeout(struct net_device *dev)
Dean Nelsona2d974d2005-03-23 20:50:00 -0700580{
581 struct xpnet_dev_private *priv;
582
Dean Nelson35190502008-04-22 14:48:55 -0500583 priv = (struct xpnet_dev_private *)dev->priv;
Dean Nelsona2d974d2005-03-23 20:50:00 -0700584
585 priv->stats.tx_errors++;
586 return;
587}
588
Dean Nelsona2d974d2005-03-23 20:50:00 -0700589static int __init
590xpnet_init(void)
591{
592 int i;
593 u32 license_num;
594 int result = -ENOMEM;
595
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500596 if (!ia64_platform_is("sn2"))
Dean Nelson408865c2005-09-08 10:46:58 -0500597 return -ENODEV;
Dean Nelson408865c2005-09-08 10:46:58 -0500598
Dean Nelsona2d974d2005-03-23 20:50:00 -0700599 dev_info(xpnet, "registering network device %s\n", XPNET_DEVICE_NAME);
600
601 /*
602 * use ether_setup() to init the majority of our device
603 * structure and then override the necessary pieces.
604 */
605 xpnet_device = alloc_netdev(sizeof(struct xpnet_dev_private),
606 XPNET_DEVICE_NAME, ether_setup);
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500607 if (xpnet_device == NULL)
Dean Nelsona2d974d2005-03-23 20:50:00 -0700608 return -ENOMEM;
Dean Nelsona2d974d2005-03-23 20:50:00 -0700609
610 netif_carrier_off(xpnet_device);
611
612 xpnet_device->mtu = XPNET_DEF_MTU;
613 xpnet_device->change_mtu = xpnet_dev_change_mtu;
614 xpnet_device->open = xpnet_dev_open;
615 xpnet_device->get_stats = xpnet_dev_get_stats;
616 xpnet_device->stop = xpnet_dev_stop;
617 xpnet_device->hard_start_xmit = xpnet_dev_hard_start_xmit;
618 xpnet_device->tx_timeout = xpnet_dev_tx_timeout;
619 xpnet_device->set_config = xpnet_dev_set_config;
620
621 /*
622 * Multicast assumes the LSB of the first octet is set for multicast
623 * MAC addresses. We chose the first octet of the MAC to be unlikely
624 * to collide with any vendor's officially issued MAC.
625 */
626 xpnet_device->dev_addr[0] = 0xfe;
627 xpnet_device->dev_addr[XPNET_PARTID_OCTET] = sn_partition_id;
628 license_num = sn_partition_serial_number_val();
629 for (i = 3; i >= 0; i--) {
630 xpnet_device->dev_addr[XPNET_LICENSE_OCTET + i] =
Dean Nelson35190502008-04-22 14:48:55 -0500631 license_num & 0xff;
Dean Nelsona2d974d2005-03-23 20:50:00 -0700632 license_num = license_num >> 8;
633 }
634
635 /*
636 * ether_setup() sets this to a multicast device. We are
637 * really not supporting multicast at this time.
638 */
639 xpnet_device->flags &= ~IFF_MULTICAST;
640
641 /*
642 * No need to checksum as it is a DMA transfer. The BTE will
643 * report an error if the data is not retrievable and the
644 * packet will be dropped.
645 */
646 xpnet_device->features = NETIF_F_NO_CSUM;
647
648 result = register_netdev(xpnet_device);
Dean Nelson2c2b94f2008-04-22 14:50:17 -0500649 if (result != 0)
Dean Nelsona2d974d2005-03-23 20:50:00 -0700650 free_netdev(xpnet_device);
Dean Nelsona2d974d2005-03-23 20:50:00 -0700651
652 return result;
653}
Dean Nelsona2d974d2005-03-23 20:50:00 -0700654
Dean Nelson35190502008-04-22 14:48:55 -0500655module_init(xpnet_init);
Dean Nelsona2d974d2005-03-23 20:50:00 -0700656
657static void __exit
658xpnet_exit(void)
659{
660 dev_info(xpnet, "unregistering network device %s\n",
Dean Nelson35190502008-04-22 14:48:55 -0500661 xpnet_device[0].name);
Dean Nelsona2d974d2005-03-23 20:50:00 -0700662
663 unregister_netdev(xpnet_device);
664
665 free_netdev(xpnet_device);
666}
Dean Nelsona2d974d2005-03-23 20:50:00 -0700667
Dean Nelson35190502008-04-22 14:48:55 -0500668module_exit(xpnet_exit);
Dean Nelsona2d974d2005-03-23 20:50:00 -0700669
670MODULE_AUTHOR("Silicon Graphics, Inc.");
671MODULE_DESCRIPTION("Cross Partition Network adapter (XPNET)");
672MODULE_LICENSE("GPL");