ps3_gelic_net.c 39.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
/*
 *  PS3 gelic network driver.
 *
 * Copyright (C) 2007 Sony Computer Entertainment Inc.
 * Copyright 2006, 2007 Sony Corporation
 *
 * This file is based on: spider_net.c
 *
 * (C) Copyright IBM Corp. 2005
 *
 * Authors : Utz Bacher <utz.bacher@de.ibm.com>
 *           Jens Osterkamp <Jens.Osterkamp@de.ibm.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#undef DEBUG

#include <linux/kernel.h>
#include <linux/module.h>

#include <linux/etherdevice.h>
#include <linux/ethtool.h>
#include <linux/if_vlan.h>

#include <linux/in.h>
#include <linux/ip.h>
#include <linux/tcp.h>

#include <linux/dma-mapping.h>
#include <net/checksum.h>
#include <asm/firmware.h>
#include <asm/ps3.h>
#include <asm/lv1call.h>

#include "ps3_gelic_net.h"

#define DRV_NAME "Gelic Network Driver"
#define DRV_VERSION "1.0"

MODULE_AUTHOR("SCE Inc.");
MODULE_DESCRIPTION("Gelic Network driver");
MODULE_LICENSE("GPL");

M
Masakazu Mokuno 已提交
57
static inline struct device *ctodev(struct gelic_card *card)
58 59 60
{
	return &card->dev->core;
}
M
Masakazu Mokuno 已提交
61
static inline u64 bus_id(struct gelic_card *card)
62 63 64
{
	return card->dev->bus_id;
}
M
Masakazu Mokuno 已提交
65
static inline u64 dev_id(struct gelic_card *card)
66 67 68 69 70
{
	return card->dev->dev_id;
}

/* set irq_mask */
M
Masakazu Mokuno 已提交
71
static int gelic_card_set_irq_mask(struct gelic_card *card, u64 mask)
72 73 74 75 76 77 78 79 80 81
{
	int status;

	status = lv1_net_set_interrupt_mask(bus_id(card), dev_id(card),
					    mask, 0);
	if (status)
		dev_info(ctodev(card),
			 "lv1_net_set_interrupt_mask failed %d\n", status);
	return status;
}
M
Masakazu Mokuno 已提交
82
static inline void gelic_card_rx_irq_on(struct gelic_card *card)
83
{
M
Masakazu Mokuno 已提交
84
	gelic_card_set_irq_mask(card, card->ghiintmask | GELIC_CARD_RXINT);
85
}
M
Masakazu Mokuno 已提交
86
static inline void gelic_card_rx_irq_off(struct gelic_card *card)
87
{
M
Masakazu Mokuno 已提交
88
	gelic_card_set_irq_mask(card, card->ghiintmask & ~GELIC_CARD_RXINT);
89 90
}
/**
M
Masakazu Mokuno 已提交
91
 * gelic_descr_get_status -- returns the status of a descriptor
92 93 94 95
 * @descr: descriptor to look at
 *
 * returns the status as in the dmac_cmd_status field of the descriptor
 */
M
Masakazu Mokuno 已提交
96 97
static enum gelic_descr_dma_status
gelic_descr_get_status(struct gelic_descr *descr)
98
{
M
Masakazu Mokuno 已提交
99
	return be32_to_cpu(descr->dmac_cmd_status) & GELIC_DESCR_DMA_STAT_MASK;
100 101 102
}

/**
M
Masakazu Mokuno 已提交
103
 * gelic_descr_set_status -- sets the status of a descriptor
104 105 106 107 108 109
 * @descr: descriptor to change
 * @status: status to set in the descriptor
 *
 * changes the status to the specified value. Doesn't change other bits
 * in the status
 */
M
Masakazu Mokuno 已提交
110 111
static void gelic_descr_set_status(struct gelic_descr *descr,
				   enum gelic_descr_dma_status status)
112
{
M
Masakazu Mokuno 已提交
113 114 115
	descr->dmac_cmd_status = cpu_to_be32(status |
		(be32_to_cpu(descr->dmac_cmd_status) &
		 ~GELIC_DESCR_DMA_STAT_MASK));
116 117 118 119 120 121 122 123 124 125
	/*
	 * dma_cmd_status field is used to indicate whether the descriptor
	 * is valid or not.
	 * Usually caller of this function wants to inform that to the
	 * hardware, so we assure here the hardware sees the change.
	 */
	wmb();
}

/**
M
Masakazu Mokuno 已提交
126
 * gelic_card_free_chain - free descriptor chain
127 128 129
 * @card: card structure
 * @descr_in: address of desc
 */
M
Masakazu Mokuno 已提交
130 131
static void gelic_card_free_chain(struct gelic_card *card,
				  struct gelic_descr *descr_in)
132
{
M
Masakazu Mokuno 已提交
133
	struct gelic_descr *descr;
134 135 136

	for (descr = descr_in; descr && descr->bus_addr; descr = descr->next) {
		dma_unmap_single(ctodev(card), descr->bus_addr,
M
Masakazu Mokuno 已提交
137
				 GELIC_DESCR_SIZE, DMA_BIDIRECTIONAL);
138 139 140 141 142
		descr->bus_addr = 0;
	}
}

/**
M
Masakazu Mokuno 已提交
143
 * gelic_card_init_chain - links descriptor chain
144 145 146 147 148 149 150 151 152 153
 * @card: card structure
 * @chain: address of chain
 * @start_descr: address of descriptor array
 * @no: number of descriptors
 *
 * we manage a circular list that mirrors the hardware structure,
 * except that the hardware uses bus addresses.
 *
 * returns 0 on success, <0 on failure
 */
M
Masakazu Mokuno 已提交
154 155 156
static int gelic_card_init_chain(struct gelic_card *card,
				 struct gelic_descr_chain *chain,
				 struct gelic_descr *start_descr, int no)
157 158
{
	int i;
M
Masakazu Mokuno 已提交
159
	struct gelic_descr *descr;
160 161 162 163 164 165

	descr = start_descr;
	memset(descr, 0, sizeof(*descr) * no);

	/* set up the hardware pointers in each descriptor */
	for (i = 0; i < no; i++, descr++) {
M
Masakazu Mokuno 已提交
166
		gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
167 168
		descr->bus_addr =
			dma_map_single(ctodev(card), descr,
M
Masakazu Mokuno 已提交
169
				       GELIC_DESCR_SIZE,
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
				       DMA_BIDIRECTIONAL);

		if (!descr->bus_addr)
			goto iommu_error;

		descr->next = descr + 1;
		descr->prev = descr - 1;
	}
	/* make them as ring */
	(descr - 1)->next = start_descr;
	start_descr->prev = (descr - 1);

	/* chain bus addr of hw descriptor */
	descr = start_descr;
	for (i = 0; i < no; i++, descr++) {
185
		descr->next_descr_addr = cpu_to_be32(descr->next->bus_addr);
186 187 188 189 190 191 192 193 194 195 196 197 198 199
	}

	chain->head = start_descr;
	chain->tail = start_descr;

	/* do not chain last hw descriptor */
	(descr - 1)->next_descr_addr = 0;

	return 0;

iommu_error:
	for (i--, descr--; 0 <= i; i--, descr--)
		if (descr->bus_addr)
			dma_unmap_single(ctodev(card), descr->bus_addr,
M
Masakazu Mokuno 已提交
200
					 GELIC_DESCR_SIZE,
201 202 203 204 205
					 DMA_BIDIRECTIONAL);
	return -ENOMEM;
}

/**
M
Masakazu Mokuno 已提交
206
 * gelic_descr_prepare_rx - reinitializes a rx descriptor
207 208 209 210 211 212 213 214
 * @card: card structure
 * @descr: descriptor to re-init
 *
 * return 0 on succes, <0 on failure
 *
 * allocates a new rx skb, iommu-maps it and attaches it to the descriptor.
 * Activate the descriptor state-wise
 */
M
Masakazu Mokuno 已提交
215 216
static int gelic_descr_prepare_rx(struct gelic_card *card,
				      struct gelic_descr *descr)
217 218 219 220
{
	int offset;
	unsigned int bufsize;

M
Masakazu Mokuno 已提交
221
	if (gelic_descr_get_status(descr) !=  GELIC_DESCR_DMA_NOT_IN_USE)
222
		dev_info(ctodev(card), "%s: ERROR status \n", __func__);
M
Masakazu Mokuno 已提交
223

224 225 226 227 228 229 230 231 232 233 234 235 236
	/* we need to round up the buffer size to a multiple of 128 */
	bufsize = ALIGN(GELIC_NET_MAX_MTU, GELIC_NET_RXBUF_ALIGN);

	/* and we need to have it 128 byte aligned, therefore we allocate a
	 * bit more */
	descr->skb = netdev_alloc_skb(card->netdev,
		bufsize + GELIC_NET_RXBUF_ALIGN - 1);
	if (!descr->skb) {
		descr->buf_addr = 0; /* tell DMAC don't touch memory */
		dev_info(ctodev(card),
			 "%s:allocate skb failed !!\n", __func__);
		return -ENOMEM;
	}
237
	descr->buf_size = cpu_to_be32(bufsize);
238 239 240 241 242 243 244 245 246 247
	descr->dmac_cmd_status = 0;
	descr->result_size = 0;
	descr->valid_size = 0;
	descr->data_error = 0;

	offset = ((unsigned long)descr->skb->data) &
		(GELIC_NET_RXBUF_ALIGN - 1);
	if (offset)
		skb_reserve(descr->skb, GELIC_NET_RXBUF_ALIGN - offset);
	/* io-mmu-map the skb */
248 249 250 251
	descr->buf_addr = cpu_to_be32(dma_map_single(ctodev(card),
						     descr->skb->data,
						     GELIC_NET_MAX_MTU,
						     DMA_FROM_DEVICE));
252 253 254 255 256
	if (!descr->buf_addr) {
		dev_kfree_skb_any(descr->skb);
		descr->skb = NULL;
		dev_info(ctodev(card),
			 "%s:Could not iommu-map rx buffer\n", __func__);
M
Masakazu Mokuno 已提交
257
		gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
258 259
		return -ENOMEM;
	} else {
M
Masakazu Mokuno 已提交
260
		gelic_descr_set_status(descr, GELIC_DESCR_DMA_CARDOWNED);
261 262 263 264 265
		return 0;
	}
}

/**
M
Masakazu Mokuno 已提交
266
 * gelic_card_release_rx_chain - free all skb of rx descr
267 268 269
 * @card: card structure
 *
 */
M
Masakazu Mokuno 已提交
270
static void gelic_card_release_rx_chain(struct gelic_card *card)
271
{
M
Masakazu Mokuno 已提交
272
	struct gelic_descr *descr = card->rx_chain.head;
273 274 275 276

	do {
		if (descr->skb) {
			dma_unmap_single(ctodev(card),
277
					 be32_to_cpu(descr->buf_addr),
278 279 280 281 282
					 descr->skb->len,
					 DMA_FROM_DEVICE);
			descr->buf_addr = 0;
			dev_kfree_skb_any(descr->skb);
			descr->skb = NULL;
M
Masakazu Mokuno 已提交
283 284
			gelic_descr_set_status(descr,
					       GELIC_DESCR_DMA_NOT_IN_USE);
285 286 287 288 289 290
		}
		descr = descr->next;
	} while (descr != card->rx_chain.head);
}

/**
M
Masakazu Mokuno 已提交
291
 * gelic_card_fill_rx_chain - fills descriptors/skbs in the rx chains
292 293 294 295
 * @card: card structure
 *
 * fills all descriptors in the rx chain: allocates skbs
 * and iommu-maps them.
M
Masakazu Mokuno 已提交
296
 * returns 0 on success, < 0 on failure
297
 */
M
Masakazu Mokuno 已提交
298
static int gelic_card_fill_rx_chain(struct gelic_card *card)
299
{
M
Masakazu Mokuno 已提交
300
	struct gelic_descr *descr = card->rx_chain.head;
301 302 303 304
	int ret;

	do {
		if (!descr->skb) {
M
Masakazu Mokuno 已提交
305
			ret = gelic_descr_prepare_rx(card, descr);
306 307 308 309 310 311 312 313
			if (ret)
				goto rewind;
		}
		descr = descr->next;
	} while (descr != card->rx_chain.head);

	return 0;
rewind:
M
Masakazu Mokuno 已提交
314
	gelic_card_release_rx_chain(card);
315 316 317 318
	return ret;
}

/**
M
Masakazu Mokuno 已提交
319
 * gelic_card_alloc_rx_skbs - allocates rx skbs in rx descriptor chains
320 321
 * @card: card structure
 *
M
Masakazu Mokuno 已提交
322
 * returns 0 on success, < 0 on failure
323
 */
M
Masakazu Mokuno 已提交
324
static int gelic_card_alloc_rx_skbs(struct gelic_card *card)
325
{
M
Masakazu Mokuno 已提交
326
	struct gelic_descr_chain *chain;
327 328
	int ret;
	chain = &card->rx_chain;
M
Masakazu Mokuno 已提交
329
	ret = gelic_card_fill_rx_chain(card);
330 331 332 333 334
	chain->head = card->rx_top->prev; /* point to the last */
	return ret;
}

/**
M
Masakazu Mokuno 已提交
335
 * gelic_descr_release_tx - processes a used tx descriptor
336 337 338 339 340
 * @card: card structure
 * @descr: descriptor to release
 *
 * releases a used tx descriptor (unmapping, freeing of skb)
 */
M
Masakazu Mokuno 已提交
341 342
static void gelic_descr_release_tx(struct gelic_card *card,
			    struct gelic_descr *descr)
343
{
344
	struct sk_buff *skb = descr->skb;
345

M
Masakazu Mokuno 已提交
346
#ifdef DEBUG
347
	BUG_ON(!(be32_to_cpu(descr->data_status) &
M
Masakazu Mokuno 已提交
348 349
		 (1 << GELIC_DESCR_TX_DMA_FRAME_TAIL)));
#endif
350 351
	dma_unmap_single(ctodev(card),
			 be32_to_cpu(descr->buf_addr), skb->len, DMA_TO_DEVICE);
352
	dev_kfree_skb_any(skb);
353 354 355 356 357 358 359 360 361 362 363

	descr->buf_addr = 0;
	descr->buf_size = 0;
	descr->next_descr_addr = 0;
	descr->result_size = 0;
	descr->valid_size = 0;
	descr->data_status = 0;
	descr->data_error = 0;
	descr->skb = NULL;

	/* set descr status */
M
Masakazu Mokuno 已提交
364
	gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
365 366 367
}

/**
M
Masakazu Mokuno 已提交
368
 * gelic_card_release_tx_chain - processes sent tx descriptors
369 370 371 372 373
 * @card: adapter structure
 * @stop: net_stop sequence
 *
 * releases the tx descriptors that gelic has finished with
 */
M
Masakazu Mokuno 已提交
374
static void gelic_card_release_tx_chain(struct gelic_card *card, int stop)
375
{
M
Masakazu Mokuno 已提交
376 377
	struct gelic_descr_chain *tx_chain;
	enum gelic_descr_dma_status status;
378 379 380 381 382
	int release = 0;

	for (tx_chain = &card->tx_chain;
	     tx_chain->head != tx_chain->tail && tx_chain->tail;
	     tx_chain->tail = tx_chain->tail->next) {
M
Masakazu Mokuno 已提交
383
		status = gelic_descr_get_status(tx_chain->tail);
384
		switch (status) {
M
Masakazu Mokuno 已提交
385 386 387
		case GELIC_DESCR_DMA_RESPONSE_ERROR:
		case GELIC_DESCR_DMA_PROTECTION_ERROR:
		case GELIC_DESCR_DMA_FORCE_END:
388 389 390 391 392
			if (printk_ratelimit())
				dev_info(ctodev(card),
					 "%s: forcing end of tx descriptor " \
					 "with status %x\n",
					 __func__, status);
393
			card->netdev->stats.tx_dropped++;
394 395
			break;

M
Masakazu Mokuno 已提交
396
		case GELIC_DESCR_DMA_COMPLETE:
397
			if (tx_chain->tail->skb) {
398 399
				card->netdev->stats.tx_packets++;
				card->netdev->stats.tx_bytes +=
400 401
					tx_chain->tail->skb->len;
			}
402 403
			break;

M
Masakazu Mokuno 已提交
404
		case GELIC_DESCR_DMA_CARDOWNED:
405 406
			/* pending tx request */
		default:
M
Masakazu Mokuno 已提交
407
			/* any other value (== GELIC_DESCR_DMA_NOT_IN_USE) */
408 409
			if (!stop)
				goto out;
410
		}
M
Masakazu Mokuno 已提交
411
		gelic_descr_release_tx(card, tx_chain->tail);
412
		release ++;
413 414
	}
out:
415
	if (!stop && release)
416 417 418 419 420 421 422 423 424 425 426 427 428
		netif_wake_queue(card->netdev);
}

/**
 * gelic_net_set_multi - sets multicast addresses and promisc flags
 * @netdev: interface device structure
 *
 * gelic_net_set_multi configures multicast addresses as needed for the
 * netdev interface. It also sets up multicast, allmulti and promisc
 * flags appropriately
 */
static void gelic_net_set_multi(struct net_device *netdev)
{
M
Masakazu Mokuno 已提交
429
	struct gelic_card *card = netdev_priv(netdev);
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
	struct dev_mc_list *mc;
	unsigned int i;
	uint8_t *p;
	u64 addr;
	int status;

	/* clear all multicast address */
	status = lv1_net_remove_multicast_address(bus_id(card), dev_id(card),
						  0, 1);
	if (status)
		dev_err(ctodev(card),
			"lv1_net_remove_multicast_address failed %d\n",
			status);
	/* set broadcast address */
	status = lv1_net_add_multicast_address(bus_id(card), dev_id(card),
					       GELIC_NET_BROADCAST_ADDR, 0);
	if (status)
		dev_err(ctodev(card),
			"lv1_net_add_multicast_address failed, %d\n",
			status);

	if (netdev->flags & IFF_ALLMULTI
		|| netdev->mc_count > GELIC_NET_MC_COUNT_MAX) { /* list max */
		status = lv1_net_add_multicast_address(bus_id(card),
						       dev_id(card),
						       0, 1);
		if (status)
			dev_err(ctodev(card),
				"lv1_net_add_multicast_address failed, %d\n",
				status);
		return;
	}

	/* set multicast address */
	for (mc = netdev->mc_list; mc; mc = mc->next) {
		addr = 0;
		p = mc->dmi_addr;
		for (i = 0; i < ETH_ALEN; i++) {
			addr <<= 8;
			addr |= *p++;
		}
		status = lv1_net_add_multicast_address(bus_id(card),
						       dev_id(card),
						       addr, 0);
		if (status)
			dev_err(ctodev(card),
				"lv1_net_add_multicast_address failed, %d\n",
				status);
	}
}

/**
M
Masakazu Mokuno 已提交
482
 * gelic_card_enable_rxdmac - enables the receive DMA controller
483 484
 * @card: card structure
 *
M
Masakazu Mokuno 已提交
485
 * gelic_card_enable_rxdmac enables the DMA controller by setting RX_DMA_EN
486 487
 * in the GDADMACCNTR register
 */
M
Masakazu Mokuno 已提交
488
static inline void gelic_card_enable_rxdmac(struct gelic_card *card)
489 490 491 492 493 494 495 496 497 498 499
{
	int status;

	status = lv1_net_start_rx_dma(bus_id(card), dev_id(card),
				card->rx_chain.tail->bus_addr, 0);
	if (status)
		dev_info(ctodev(card),
			 "lv1_net_start_rx_dma failed, status=%d\n", status);
}

/**
M
Masakazu Mokuno 已提交
500
 * gelic_card_disable_rxdmac - disables the receive DMA controller
501 502
 * @card: card structure
 *
M
Masakazu Mokuno 已提交
503
 * gelic_card_disable_rxdmac terminates processing on the DMA controller by
504 505
 * turing off DMA and issueing a force end
 */
M
Masakazu Mokuno 已提交
506
static inline void gelic_card_disable_rxdmac(struct gelic_card *card)
507 508 509 510 511 512 513 514 515 516 517
{
	int status;

	/* this hvc blocks until the DMA in progress really stopped */
	status = lv1_net_stop_rx_dma(bus_id(card), dev_id(card), 0);
	if (status)
		dev_err(ctodev(card),
			"lv1_net_stop_rx_dma faild, %d\n", status);
}

/**
M
Masakazu Mokuno 已提交
518
 * gelic_card_disable_txdmac - disables the transmit DMA controller
519 520
 * @card: card structure
 *
M
Masakazu Mokuno 已提交
521
 * gelic_card_disable_txdmac terminates processing on the DMA controller by
522 523
 * turing off DMA and issueing a force end
 */
M
Masakazu Mokuno 已提交
524
static inline void gelic_card_disable_txdmac(struct gelic_card *card)
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
{
	int status;

	/* this hvc blocks until the DMA in progress really stopped */
	status = lv1_net_stop_tx_dma(bus_id(card), dev_id(card), 0);
	if (status)
		dev_err(ctodev(card),
			"lv1_net_stop_tx_dma faild, status=%d\n", status);
}

/**
 * gelic_net_stop - called upon ifconfig down
 * @netdev: interface device structure
 *
 * always returns 0
 */
static int gelic_net_stop(struct net_device *netdev)
{
M
Masakazu Mokuno 已提交
543
	struct gelic_card *card = netdev_priv(netdev);
544

545
	napi_disable(&card->napi);
546 547 548
	netif_stop_queue(netdev);

	/* turn off DMA, force end */
M
Masakazu Mokuno 已提交
549 550
	gelic_card_disable_rxdmac(card);
	gelic_card_disable_txdmac(card);
551

M
Masakazu Mokuno 已提交
552
	gelic_card_set_irq_mask(card, 0);
553 554 555 556 557 558 559 560 561

	/* disconnect event port */
	free_irq(card->netdev->irq, card->netdev);
	ps3_sb_event_receive_port_destroy(card->dev, card->netdev->irq);
	card->netdev->irq = NO_IRQ;

	netif_carrier_off(netdev);

	/* release chains */
M
Masakazu Mokuno 已提交
562 563
	gelic_card_release_tx_chain(card, 1);
	gelic_card_release_rx_chain(card);
564

M
Masakazu Mokuno 已提交
565 566
	gelic_card_free_chain(card, card->tx_top);
	gelic_card_free_chain(card, card->rx_top);
567 568 569 570 571

	return 0;
}

/**
M
Masakazu Mokuno 已提交
572
 * gelic_card_get_next_tx_descr - returns the next available tx descriptor
573 574 575 576
 * @card: device structure to get descriptor from
 *
 * returns the address of the next descriptor, or NULL if not available.
 */
M
Masakazu Mokuno 已提交
577 578
static struct gelic_descr *
gelic_card_get_next_tx_descr(struct gelic_card *card)
579 580 581
{
	if (!card->tx_chain.head)
		return NULL;
582
	/*  see if the next descriptor is free */
583
	if (card->tx_chain.tail != card->tx_chain.head->next &&
M
Masakazu Mokuno 已提交
584 585
	    gelic_descr_get_status(card->tx_chain.head) ==
	    GELIC_DESCR_DMA_NOT_IN_USE)
586 587 588 589 590 591 592
		return card->tx_chain.head;
	else
		return NULL;

}

/**
M
Masakazu Mokuno 已提交
593
 * gelic_descr_set_tx_cmdstat - sets the tx descriptor command field
594 595 596 597 598 599 600
 * @descr: descriptor structure to fill out
 * @skb: packet to consider
 *
 * fills out the command and status field of the descriptor structure,
 * depending on hardware checksum settings. This function assumes a wmb()
 * has executed before.
 */
M
Masakazu Mokuno 已提交
601 602
static void gelic_descr_set_tx_cmdstat(struct gelic_descr *descr,
				       struct sk_buff *skb)
603 604
{
	if (skb->ip_summed != CHECKSUM_PARTIAL)
605
		descr->dmac_cmd_status =
M
Masakazu Mokuno 已提交
606 607
			cpu_to_be32(GELIC_DESCR_DMA_CMD_NO_CHKSUM |
				    GELIC_DESCR_TX_DMA_FRAME_TAIL);
608 609 610 611 612 613
	else {
		/* is packet ip?
		 * if yes: tcp? udp? */
		if (skb->protocol == htons(ETH_P_IP)) {
			if (ip_hdr(skb)->protocol == IPPROTO_TCP)
				descr->dmac_cmd_status =
M
Masakazu Mokuno 已提交
614 615
				cpu_to_be32(GELIC_DESCR_DMA_CMD_TCP_CHKSUM |
					    GELIC_DESCR_TX_DMA_FRAME_TAIL);
616

617 618
			else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
				descr->dmac_cmd_status =
M
Masakazu Mokuno 已提交
619 620
				cpu_to_be32(GELIC_DESCR_DMA_CMD_UDP_CHKSUM |
					    GELIC_DESCR_TX_DMA_FRAME_TAIL);
621 622 623 624 625
			else	/*
				 * the stack should checksum non-tcp and non-udp
				 * packets on his own: NETIF_F_IP_CSUM
				 */
				descr->dmac_cmd_status =
M
Masakazu Mokuno 已提交
626 627
				cpu_to_be32(GELIC_DESCR_DMA_CMD_NO_CHKSUM |
					    GELIC_DESCR_TX_DMA_FRAME_TAIL);
628 629 630 631
		}
	}
}

632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
static inline struct sk_buff *gelic_put_vlan_tag(struct sk_buff *skb,
						 unsigned short tag)
{
	struct vlan_ethhdr *veth;
	static unsigned int c;

	if (skb_headroom(skb) < VLAN_HLEN) {
		struct sk_buff *sk_tmp = skb;
		pr_debug("%s: hd=%d c=%ud\n", __func__, skb_headroom(skb), c);
		skb = skb_realloc_headroom(sk_tmp, VLAN_HLEN);
		if (!skb)
			return NULL;
		dev_kfree_skb_any(sk_tmp);
	}
	veth = (struct vlan_ethhdr *)skb_push(skb, VLAN_HLEN);

	/* Move the mac addresses to the top of buffer */
	memmove(skb->data, skb->data + VLAN_HLEN, 2 * ETH_ALEN);

	veth->h_vlan_proto = __constant_htons(ETH_P_8021Q);
	veth->h_vlan_TCI = htons(tag);

	return skb;
}

657
/**
M
Masakazu Mokuno 已提交
658
 * gelic_descr_prepare_tx - get dma address of skb_data
659 660 661 662 663 664 665
 * @card: card structure
 * @descr: descriptor structure
 * @skb: packet to use
 *
 * returns 0 on success, <0 on failure.
 *
 */
M
Masakazu Mokuno 已提交
666 667 668
static int gelic_descr_prepare_tx(struct gelic_card *card,
				  struct gelic_descr *descr,
				  struct sk_buff *skb)
669
{
670
	dma_addr_t buf;
671 672

	if (card->vlan_index != -1) {
673 674 675 676 677 678
		struct sk_buff *skb_tmp;
		skb_tmp = gelic_put_vlan_tag(skb,
					     card->vlan_id[card->vlan_index]);
		if (!skb_tmp)
			return -ENOMEM;
		skb = skb_tmp;
679 680
	}

681
	buf = dma_map_single(ctodev(card), skb->data, skb->len, DMA_TO_DEVICE);
682

683
	if (!buf) {
684 685
		dev_err(ctodev(card),
			"dma map 2 failed (%p, %i). Dropping packet\n",
686
			skb->data, skb->len);
687 688 689
		return -ENOMEM;
	}

690 691
	descr->buf_addr = cpu_to_be32(buf);
	descr->buf_size = cpu_to_be32(skb->len);
692
	descr->skb = skb;
693
	descr->data_status = 0;
694
	descr->next_descr_addr = 0; /* terminate hw descr */
M
Masakazu Mokuno 已提交
695
	gelic_descr_set_tx_cmdstat(descr, skb);
696 697

	/* bump free descriptor pointer */
698
	card->tx_chain.head = descr->next;
699 700 701 702
	return 0;
}

/**
M
Masakazu Mokuno 已提交
703
 * gelic_card_kick_txdma - enables TX DMA processing
704 705 706 707
 * @card: card structure
 * @descr: descriptor address to enable TX processing at
 *
 */
M
Masakazu Mokuno 已提交
708 709
static int gelic_card_kick_txdma(struct gelic_card *card,
				 struct gelic_descr *descr)
710
{
711
	int status = 0;
712 713 714 715

	if (card->tx_dma_progress)
		return 0;

M
Masakazu Mokuno 已提交
716
	if (gelic_descr_get_status(descr) == GELIC_DESCR_DMA_CARDOWNED) {
717
		card->tx_dma_progress = 1;
718 719 720
		status = lv1_net_start_tx_dma(bus_id(card), dev_id(card),
					      descr->bus_addr, 0);
		if (status)
721
			dev_info(ctodev(card), "lv1_net_start_txdma failed," \
722
				 "status=%d\n", status);
723 724 725 726 727 728 729 730 731 732 733 734 735
	}
	return status;
}

/**
 * gelic_net_xmit - transmits a frame over the device
 * @skb: packet to send out
 * @netdev: interface device structure
 *
 * returns 0 on success, <0 on failure
 */
static int gelic_net_xmit(struct sk_buff *skb, struct net_device *netdev)
{
M
Masakazu Mokuno 已提交
736 737
	struct gelic_card *card = netdev_priv(netdev);
	struct gelic_descr *descr;
738 739 740 741 742
	int result;
	unsigned long flags;

	spin_lock_irqsave(&card->tx_dma_lock, flags);

M
Masakazu Mokuno 已提交
743
	gelic_card_release_tx_chain(card, 0);
744

M
Masakazu Mokuno 已提交
745
	descr = gelic_card_get_next_tx_descr(card);
746
	if (!descr) {
747 748 749
		/*
		 * no more descriptors free
		 */
750 751 752 753 754
		netif_stop_queue(netdev);
		spin_unlock_irqrestore(&card->tx_dma_lock, flags);
		return NETDEV_TX_BUSY;
	}

M
Masakazu Mokuno 已提交
755
	result = gelic_descr_prepare_tx(card, descr, skb);
756 757 758 759 760
	if (result) {
		/*
		 * DMA map failed.  As chanses are that failure
		 * would continue, just release skb and return
		 */
761
		card->netdev->stats.tx_dropped++;
762 763 764 765 766 767 768 769
		dev_kfree_skb_any(skb);
		spin_unlock_irqrestore(&card->tx_dma_lock, flags);
		return NETDEV_TX_OK;
	}
	/*
	 * link this prepared descriptor to previous one
	 * to achieve high performance
	 */
770
	descr->prev->next_descr_addr = cpu_to_be32(descr->bus_addr);
771 772 773 774 775
	/*
	 * as hardware descriptor is modified in the above lines,
	 * ensure that the hardware sees it
	 */
	wmb();
M
Masakazu Mokuno 已提交
776
	if (gelic_card_kick_txdma(card, descr)) {
777 778 779 780
		/*
		 * kick failed.
		 * release descriptors which were just prepared
		 */
781
		card->netdev->stats.tx_dropped++;
M
Masakazu Mokuno 已提交
782 783
		gelic_descr_release_tx(card, descr);
		gelic_descr_release_tx(card, descr->next);
784 785 786 787 788 789
		card->tx_chain.tail = descr->next->next;
		dev_info(ctodev(card), "%s: kick failure\n", __func__);
	} else {
		/* OK, DMA started/reserved */
		netdev->trans_start = jiffies;
	}
790 791 792 793 794 795 796 797 798 799 800 801 802

	spin_unlock_irqrestore(&card->tx_dma_lock, flags);
	return NETDEV_TX_OK;
}

/**
 * gelic_net_pass_skb_up - takes an skb from a descriptor and passes it on
 * @descr: descriptor to process
 * @card: card structure
 *
 * iommu-unmaps the skb, fills out skb structure and passes the data to the
 * stack. The descriptor state is not changed.
 */
M
Masakazu Mokuno 已提交
803 804
static void gelic_net_pass_skb_up(struct gelic_descr *descr,
				 struct gelic_card *card)
805 806 807 808 809
{
	struct sk_buff *skb;
	struct net_device *netdev;
	u32 data_status, data_error;

810 811
	data_status = be32_to_cpu(descr->data_status);
	data_error = be32_to_cpu(descr->data_error);
812 813 814
	netdev = card->netdev;
	/* unmap skb buffer */
	skb = descr->skb;
815 816
	dma_unmap_single(ctodev(card),
			 be32_to_cpu(descr->buf_addr), GELIC_NET_MAX_MTU,
817 818
			 DMA_FROM_DEVICE);

819 820 821
	skb_put(skb, descr->valid_size ?
		be32_to_cpu(descr->valid_size) :
		be32_to_cpu(descr->result_size));
822 823
	if (!descr->valid_size)
		dev_info(ctodev(card), "buffer full %x %x %x\n",
824 825 826
			 be32_to_cpu(descr->result_size),
			 be32_to_cpu(descr->buf_size),
			 be32_to_cpu(descr->dmac_cmd_status));
827 828 829 830 831 832 833 834 835 836 837

	descr->skb = NULL;
	/*
	 * the card put 2 bytes vlan tag in front
	 * of the ethernet frame
	 */
	skb_pull(skb, 2);
	skb->protocol = eth_type_trans(skb, netdev);

	/* checksum offload */
	if (card->rx_csum) {
M
Masakazu Mokuno 已提交
838 839
		if ((data_status & GELIC_DESCR_DATA_STATUS_CHK_MASK) &&
		    (!(data_error & GELIC_DESCR_DATA_ERROR_CHK_MASK)))
840 841 842 843 844 845 846
			skb->ip_summed = CHECKSUM_UNNECESSARY;
		else
			skb->ip_summed = CHECKSUM_NONE;
	} else
		skb->ip_summed = CHECKSUM_NONE;

	/* update netdevice statistics */
847 848
	card->netdev->stats.rx_packets++;
	card->netdev->stats.rx_bytes += skb->len;
849 850 851 852 853 854

	/* pass skb up to stack */
	netif_receive_skb(skb);
}

/**
M
Masakazu Mokuno 已提交
855
 * gelic_card_decode_one_descr - processes an rx descriptor
856 857 858 859 860 861 862
 * @card: card structure
 *
 * returns 1 if a packet has been sent to the stack, otherwise 0
 *
 * processes an rx descriptor by iommu-unmapping the data buffer and passing
 * the packet up to the stack
 */
M
Masakazu Mokuno 已提交
863
static int gelic_card_decode_one_descr(struct gelic_card *card)
864
{
M
Masakazu Mokuno 已提交
865 866 867
	enum gelic_descr_dma_status status;
	struct gelic_descr_chain *chain = &card->rx_chain;
	struct gelic_descr *descr = chain->tail;
868 869
	int dmac_chain_ended;

M
Masakazu Mokuno 已提交
870
	status = gelic_descr_get_status(descr);
871 872
	/* is this descriptor terminated with next_descr == NULL? */
	dmac_chain_ended =
873
		be32_to_cpu(descr->dmac_cmd_status) &
M
Masakazu Mokuno 已提交
874
		GELIC_DESCR_RX_DMA_CHAIN_END;
875

M
Masakazu Mokuno 已提交
876
	if (status == GELIC_DESCR_DMA_CARDOWNED)
877 878
		return 0;

M
Masakazu Mokuno 已提交
879
	if (status == GELIC_DESCR_DMA_NOT_IN_USE) {
880 881 882 883
		dev_dbg(ctodev(card), "dormant descr? %p\n", descr);
		return 0;
	}

M
Masakazu Mokuno 已提交
884 885 886
	if ((status == GELIC_DESCR_DMA_RESPONSE_ERROR) ||
	    (status == GELIC_DESCR_DMA_PROTECTION_ERROR) ||
	    (status == GELIC_DESCR_DMA_FORCE_END)) {
887 888
		dev_info(ctodev(card), "dropping RX descriptor with state %x\n",
			 status);
889
		card->netdev->stats.rx_dropped++;
890 891 892
		goto refill;
	}

M
Masakazu Mokuno 已提交
893
	if (status == GELIC_DESCR_DMA_BUFFER_FULL) {
894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909
		/*
		 * Buffer full would occur if and only if
		 * the frame length was longer than the size of this
		 * descriptor's buffer.  If the frame length was equal
		 * to or shorter than buffer'size, FRAME_END condition
		 * would occur.
		 * Anyway this frame was longer than the MTU,
		 * just drop it.
		 */
		dev_info(ctodev(card), "overlength frame\n");
		goto refill;
	}
	/*
	 * descriptoers any other than FRAME_END here should
	 * be treated as error.
	 */
M
Masakazu Mokuno 已提交
910
	if (status != GELIC_DESCR_DMA_FRAME_END) {
911 912 913 914 915 916
		dev_dbg(ctodev(card), "RX descriptor with state %x\n",
			status);
		goto refill;
	}

	/* ok, we've got a packet in descr */
917
	gelic_net_pass_skb_up(descr, card);
918
refill:
919 920 921 922 923 924
	/*
	 * So that always DMAC can see the end
	 * of the descriptor chain to avoid
	 * from unwanted DMAC overrun.
	 */
	descr->next_descr_addr = 0;
925 926

	/* change the descriptor state: */
M
Masakazu Mokuno 已提交
927
	gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
928

929 930 931
	/*
	 * this call can fail, but for now, just leave this
	 * decriptor without skb
932
	 */
M
Masakazu Mokuno 已提交
933
	gelic_descr_prepare_rx(card, descr);
934

935 936
	chain->head = descr;
	chain->tail = descr->next;
937 938 939 940

	/*
	 * Set this descriptor the end of the chain.
	 */
941
	descr->prev->next_descr_addr = cpu_to_be32(descr->bus_addr);
942

943 944 945 946
	/*
	 * If dmac chain was met, DMAC stopped.
	 * thus re-enable it
	 */
947
	if (dmac_chain_ended) {
948 949
		card->rx_dma_restart_required = 1;
		dev_dbg(ctodev(card), "reenable rx dma scheduled\n");
950 951 952 953 954 955 956 957 958 959 960 961 962 963
	}

	return 1;
}

/**
 * gelic_net_poll - NAPI poll function called by the stack to return packets
 * @netdev: interface device structure
 * @budget: number of packets we can pass to the stack at most
 *
 * returns 0 if no more packets available to the driver/stack. Returns 1,
 * if the quota is exceeded, but the driver has still packets.
 *
 */
964
static int gelic_net_poll(struct napi_struct *napi, int budget)
965
{
M
Masakazu Mokuno 已提交
966
	struct gelic_card *card = container_of(napi, struct gelic_card, napi);
967 968
	struct net_device *netdev = card->netdev;
	int packets_done = 0;
969

970
	while (packets_done < budget) {
M
Masakazu Mokuno 已提交
971
		if (!gelic_card_decode_one_descr(card))
972
			break;
973 974

		packets_done++;
975
	}
976 977 978

	if (packets_done < budget) {
		netif_rx_complete(netdev, napi);
M
Masakazu Mokuno 已提交
979
		gelic_card_rx_irq_on(card);
980 981
	}
	return packets_done;
982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002
}
/**
 * gelic_net_change_mtu - changes the MTU of an interface
 * @netdev: interface device structure
 * @new_mtu: new MTU value
 *
 * returns 0 on success, <0 on failure
 */
static int gelic_net_change_mtu(struct net_device *netdev, int new_mtu)
{
	/* no need to re-alloc skbs or so -- the max mtu is about 2.3k
	 * and mtu is outbound only anyway */
	if ((new_mtu < GELIC_NET_MIN_MTU) ||
	    (new_mtu > GELIC_NET_MAX_MTU)) {
		return -EINVAL;
	}
	netdev->mtu = new_mtu;
	return 0;
}

/**
M
Masakazu Mokuno 已提交
1003
 * gelic_card_interrupt - event handler for gelic_net
1004
 */
M
Masakazu Mokuno 已提交
1005
static irqreturn_t gelic_card_interrupt(int irq, void *ptr)
1006 1007 1008
{
	unsigned long flags;
	struct net_device *netdev = ptr;
M
Masakazu Mokuno 已提交
1009
	struct gelic_card *card = netdev_priv(netdev);
1010 1011 1012 1013 1014 1015 1016
	u64 status;

	status = card->irq_status;

	if (!status)
		return IRQ_NONE;

1017 1018
	if (card->rx_dma_restart_required) {
		card->rx_dma_restart_required = 0;
M
Masakazu Mokuno 已提交
1019
		gelic_card_enable_rxdmac(card);
1020 1021
	}

M
Masakazu Mokuno 已提交
1022 1023
	if (status & GELIC_CARD_RXINT) {
		gelic_card_rx_irq_off(card);
1024
		netif_rx_schedule(netdev, &card->napi);
1025 1026
	}

M
Masakazu Mokuno 已提交
1027
	if (status & GELIC_CARD_TXINT) {
1028 1029
		spin_lock_irqsave(&card->tx_dma_lock, flags);
		card->tx_dma_progress = 0;
M
Masakazu Mokuno 已提交
1030
		gelic_card_release_tx_chain(card, 0);
1031
		/* kick outstanding tx descriptor if any */
M
Masakazu Mokuno 已提交
1032
		gelic_card_kick_txdma(card, card->tx_chain.tail);
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046
		spin_unlock_irqrestore(&card->tx_dma_lock, flags);
	}
	return IRQ_HANDLED;
}

#ifdef CONFIG_NET_POLL_CONTROLLER
/**
 * gelic_net_poll_controller - artificial interrupt for netconsole etc.
 * @netdev: interface device structure
 *
 * see Documentation/networking/netconsole.txt
 */
static void gelic_net_poll_controller(struct net_device *netdev)
{
M
Masakazu Mokuno 已提交
1047
	struct gelic_card *card = netdev_priv(netdev);
1048

M
Masakazu Mokuno 已提交
1049 1050 1051
	gelic_card_set_irq_mask(card, 0);
	gelic_card_interrupt(netdev->irq, netdev);
	gelic_card_set_irq_mask(card, card->ghiintmask);
1052 1053 1054 1055
}
#endif /* CONFIG_NET_POLL_CONTROLLER */

/**
M
Masakazu Mokuno 已提交
1056
 * gelic_card_open - open device and map dma region
1057 1058
 * @card: card structure
 */
M
Masakazu Mokuno 已提交
1059
static int gelic_card_open(struct gelic_card *card)
1060 1061 1062 1063 1064 1065 1066 1067
{
	int result;

	result = ps3_sb_event_receive_port_setup(card->dev, PS3_BINDING_CPU_ANY,
		&card->netdev->irq);

	if (result) {
		dev_info(ctodev(card),
M
Masakazu Mokuno 已提交
1068
			 "%s:%d: recieve_port_setup failed (%d)\n",
1069 1070 1071 1072 1073
			 __func__, __LINE__, result);
		result = -EPERM;
		goto fail_alloc_irq;
	}

M
Masakazu Mokuno 已提交
1074
	result = request_irq(card->netdev->irq, gelic_card_interrupt,
1075
			     IRQF_DISABLED, card->netdev->name, card->netdev);
1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103

	if (result) {
		dev_info(ctodev(card), "%s:%d: request_irq failed (%d)\n",
			__func__, __LINE__, result);
		goto fail_request_irq;
	}

	return 0;

fail_request_irq:
	ps3_sb_event_receive_port_destroy(card->dev, card->netdev->irq);
	card->netdev->irq = NO_IRQ;
fail_alloc_irq:
	return result;
}


/**
 * gelic_net_open - called upon ifonfig up
 * @netdev: interface device structure
 *
 * returns 0 on success, <0 on failure
 *
 * gelic_net_open allocates all the descriptors and memory needed for
 * operation, sets up multicast list and enables interrupts
 */
static int gelic_net_open(struct net_device *netdev)
{
M
Masakazu Mokuno 已提交
1104
	struct gelic_card *card = netdev_priv(netdev);
1105 1106 1107

	dev_dbg(ctodev(card), " -> %s:%d\n", __func__, __LINE__);

M
Masakazu Mokuno 已提交
1108
	gelic_card_open(card);
1109

M
Masakazu Mokuno 已提交
1110 1111
	if (gelic_card_init_chain(card, &card->tx_chain,
				  card->descr, GELIC_NET_TX_DESCRIPTORS))
1112
		goto alloc_tx_failed;
M
Masakazu Mokuno 已提交
1113 1114 1115
	if (gelic_card_init_chain(card, &card->rx_chain,
				  card->descr + GELIC_NET_TX_DESCRIPTORS,
				  GELIC_NET_RX_DESCRIPTORS))
1116 1117 1118 1119 1120 1121
		goto alloc_rx_failed;

	/* head of chain */
	card->tx_top = card->tx_chain.head;
	card->rx_top = card->rx_chain.head;
	dev_dbg(ctodev(card), "descr rx %p, tx %p, size %#lx, num %#x\n",
M
Masakazu Mokuno 已提交
1122
		card->rx_top, card->tx_top, sizeof(struct gelic_descr),
1123 1124
		GELIC_NET_RX_DESCRIPTORS);
	/* allocate rx skbs */
M
Masakazu Mokuno 已提交
1125
	if (gelic_card_alloc_rx_skbs(card))
1126 1127
		goto alloc_skbs_failed;

1128 1129
	napi_enable(&card->napi);

1130
	card->tx_dma_progress = 0;
M
Masakazu Mokuno 已提交
1131
	card->ghiintmask = GELIC_CARD_RXINT | GELIC_CARD_TXINT;
1132

M
Masakazu Mokuno 已提交
1133 1134
	gelic_card_set_irq_mask(card, card->ghiintmask);
	gelic_card_enable_rxdmac(card);
1135 1136 1137 1138 1139 1140 1141

	netif_start_queue(netdev);
	netif_carrier_on(netdev);

	return 0;

alloc_skbs_failed:
M
Masakazu Mokuno 已提交
1142
	gelic_card_free_chain(card, card->rx_top);
1143
alloc_rx_failed:
M
Masakazu Mokuno 已提交
1144
	gelic_card_free_chain(card, card->tx_top);
1145 1146 1147 1148
alloc_tx_failed:
	return -ENOMEM;
}

M
Masakazu Mokuno 已提交
1149 1150
static void gelic_net_get_drvinfo(struct net_device *netdev,
				  struct ethtool_drvinfo *info)
1151 1152 1153 1154 1155
{
	strncpy(info->driver, DRV_NAME, sizeof(info->driver) - 1);
	strncpy(info->version, DRV_VERSION, sizeof(info->version) - 1);
}

M
Masakazu Mokuno 已提交
1156 1157
static int gelic_ether_get_settings(struct net_device *netdev,
				    struct ethtool_cmd *cmd)
1158
{
M
Masakazu Mokuno 已提交
1159
	struct gelic_card *card = netdev_priv(netdev);
1160 1161 1162 1163 1164 1165
	int status;
	u64 v1, v2;
	int speed, duplex;

	speed = duplex = -1;
	status = lv1_net_control(bus_id(card), dev_id(card),
M
Masakazu Mokuno 已提交
1166 1167 1168
				 GELIC_LV1_GET_ETH_PORT_STATUS,
				 GELIC_LV1_VLAN_TX_ETHERNET, 0, 0,
				 &v1, &v2);
1169 1170 1171
	if (status) {
		/* link down */
	} else {
M
Masakazu Mokuno 已提交
1172
		if (v1 & GELIC_LV1_ETHER_FULL_DUPLEX) {
1173 1174 1175 1176 1177
			duplex = DUPLEX_FULL;
		} else {
			duplex = DUPLEX_HALF;
		}

M
Masakazu Mokuno 已提交
1178
		if (v1 & GELIC_LV1_ETHER_SPEED_10) {
1179
			speed = SPEED_10;
M
Masakazu Mokuno 已提交
1180
		} else if (v1 & GELIC_LV1_ETHER_SPEED_100) {
1181
			speed = SPEED_100;
M
Masakazu Mokuno 已提交
1182
		} else if (v1 & GELIC_LV1_ETHER_SPEED_1000) {
1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198
			speed = SPEED_1000;
		}
	}
	cmd->supported = SUPPORTED_TP | SUPPORTED_Autoneg |
			SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full |
			SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full |
			SUPPORTED_1000baseT_Half | SUPPORTED_1000baseT_Full;
	cmd->advertising = cmd->supported;
	cmd->speed = speed;
	cmd->duplex = duplex;
	cmd->autoneg = AUTONEG_ENABLE; /* always enabled */
	cmd->port = PORT_TP;

	return 0;
}

M
Masakazu Mokuno 已提交
1199
static u32 gelic_ether_get_link(struct net_device *netdev)
1200
{
M
Masakazu Mokuno 已提交
1201
	struct gelic_card *card = netdev_priv(netdev);
1202 1203 1204 1205 1206
	int status;
	u64 v1, v2;
	int link;

	status = lv1_net_control(bus_id(card), dev_id(card),
M
Masakazu Mokuno 已提交
1207 1208 1209
				 GELIC_LV1_GET_ETH_PORT_STATUS,
				 GELIC_LV1_VLAN_TX_ETHERNET, 0, 0,
				 &v1, &v2);
1210 1211 1212
	if (status)
		return 0; /* link down */

M
Masakazu Mokuno 已提交
1213
	if (v1 & GELIC_LV1_ETHER_LINK_UP)
1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246
		link = 1;
	else
		link = 0;

	return link;
}

static int gelic_net_nway_reset(struct net_device *netdev)
{
	if (netif_running(netdev)) {
		gelic_net_stop(netdev);
		gelic_net_open(netdev);
	}
	return 0;
}

static u32 gelic_net_get_tx_csum(struct net_device *netdev)
{
	return (netdev->features & NETIF_F_IP_CSUM) != 0;
}

static int gelic_net_set_tx_csum(struct net_device *netdev, u32 data)
{
	if (data)
		netdev->features |= NETIF_F_IP_CSUM;
	else
		netdev->features &= ~NETIF_F_IP_CSUM;

	return 0;
}

static u32 gelic_net_get_rx_csum(struct net_device *netdev)
{
M
Masakazu Mokuno 已提交
1247
	struct gelic_card *card = netdev_priv(netdev);
1248 1249 1250 1251 1252 1253

	return card->rx_csum;
}

static int gelic_net_set_rx_csum(struct net_device *netdev, u32 data)
{
M
Masakazu Mokuno 已提交
1254
	struct gelic_card *card = netdev_priv(netdev);
1255 1256 1257 1258 1259 1260 1261

	card->rx_csum = data;
	return 0;
}

static struct ethtool_ops gelic_net_ethtool_ops = {
	.get_drvinfo	= gelic_net_get_drvinfo,
M
Masakazu Mokuno 已提交
1262 1263
	.get_settings	= gelic_ether_get_settings,
	.get_link	= gelic_ether_get_link,
1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279
	.nway_reset	= gelic_net_nway_reset,
	.get_tx_csum	= gelic_net_get_tx_csum,
	.set_tx_csum	= gelic_net_set_tx_csum,
	.get_rx_csum	= gelic_net_get_rx_csum,
	.set_rx_csum	= gelic_net_set_rx_csum,
};

/**
 * gelic_net_tx_timeout_task - task scheduled by the watchdog timeout
 * function (to be called not under interrupt status)
 * @work: work is context of tx timout task
 *
 * called as task when tx hangs, resets interface (if interface is up)
 */
static void gelic_net_tx_timeout_task(struct work_struct *work)
{
M
Masakazu Mokuno 已提交
1280 1281
	struct gelic_card *card =
		container_of(work, struct gelic_card, tx_timeout_task);
1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306
	struct net_device *netdev = card->netdev;

	dev_info(ctodev(card), "%s:Timed out. Restarting... \n", __func__);

	if (!(netdev->flags & IFF_UP))
		goto out;

	netif_device_detach(netdev);
	gelic_net_stop(netdev);

	gelic_net_open(netdev);
	netif_device_attach(netdev);

out:
	atomic_dec(&card->tx_timeout_task_counter);
}

/**
 * gelic_net_tx_timeout - called when the tx timeout watchdog kicks in.
 * @netdev: interface device structure
 *
 * called, if tx hangs. Schedules a task that resets the interface
 */
static void gelic_net_tx_timeout(struct net_device *netdev)
{
M
Masakazu Mokuno 已提交
1307
	struct gelic_card *card;
1308 1309 1310 1311 1312 1313 1314 1315 1316 1317

	card = netdev_priv(netdev);
	atomic_inc(&card->tx_timeout_task_counter);
	if (netdev->flags & IFF_UP)
		schedule_work(&card->tx_timeout_task);
	else
		atomic_dec(&card->tx_timeout_task_counter);
}

/**
M
Masakazu Mokuno 已提交
1318
 * gelic_ether_setup_netdev_ops - initialization of net_device operations
1319 1320 1321 1322
 * @netdev: net_device structure
 *
 * fills out function pointers in the net_device structure
 */
M
Masakazu Mokuno 已提交
1323
static void gelic_ether_setup_netdev_ops(struct net_device *netdev)
1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343
{
	netdev->open = &gelic_net_open;
	netdev->stop = &gelic_net_stop;
	netdev->hard_start_xmit = &gelic_net_xmit;
	netdev->set_multicast_list = &gelic_net_set_multi;
	netdev->change_mtu = &gelic_net_change_mtu;
	/* tx watchdog */
	netdev->tx_timeout = &gelic_net_tx_timeout;
	netdev->watchdog_timeo = GELIC_NET_WATCHDOG_TIMEOUT;
	netdev->ethtool_ops = &gelic_net_ethtool_ops;
}

/**
 * gelic_net_setup_netdev - initialization of net_device
 * @card: card structure
 *
 * Returns 0 on success or <0 on failure
 *
 * gelic_net_setup_netdev initializes the net_device structure
 **/
M
Masakazu Mokuno 已提交
1344
static int gelic_net_setup_netdev(struct gelic_card *card)
1345 1346 1347 1348 1349 1350
{
	struct net_device *netdev = card->netdev;
	struct sockaddr addr;
	unsigned int i;
	int status;
	u64 v1, v2;
1351
	DECLARE_MAC_BUF(mac);
1352 1353 1354 1355 1356 1357

	SET_NETDEV_DEV(netdev, &card->dev->core);
	spin_lock_init(&card->tx_dma_lock);

	card->rx_csum = GELIC_NET_RX_CSUM_DEFAULT;

M
Masakazu Mokuno 已提交
1358
	gelic_ether_setup_netdev_ops(netdev);
1359

1360 1361 1362
	netif_napi_add(netdev, &card->napi,
		       gelic_net_poll, GELIC_NET_NAPI_WEIGHT);

1363 1364 1365
	netdev->features = NETIF_F_IP_CSUM;

	status = lv1_net_control(bus_id(card), dev_id(card),
M
Masakazu Mokuno 已提交
1366
				 GELIC_LV1_GET_MAC_ADDRESS,
1367 1368 1369 1370 1371 1372 1373 1374 1375 1376
				 0, 0, 0, &v1, &v2);
	if (status || !is_valid_ether_addr((u8 *)&v1)) {
		dev_info(ctodev(card),
			 "%s:lv1_net_control GET_MAC_ADDR failed %d\n",
			 __func__, status);
		return -EINVAL;
	}
	v1 <<= 16;
	memcpy(addr.sa_data, &v1, ETH_ALEN);
	memcpy(netdev->dev_addr, addr.sa_data, ETH_ALEN);
1377 1378
	dev_info(ctodev(card), "MAC addr %s\n",
		 print_mac(mac, netdev->dev_addr));
1379 1380 1381 1382

	card->vlan_index = -1;	/* no vlan */
	for (i = 0; i < GELIC_NET_VLAN_MAX; i++) {
		status = lv1_net_control(bus_id(card), dev_id(card),
M
Masakazu Mokuno 已提交
1383
					GELIC_LV1_GET_VLAN_ID,
1384 1385
					i + 1, /* index; one based */
					0, 0, &v1, &v2);
M
Masakazu Mokuno 已提交
1386
		if (status == LV1_NO_ENTRY) {
1387 1388 1389 1390 1391 1392
			dev_dbg(ctodev(card),
				"GELIC_VLAN_ID no entry:%d, VLAN disabled\n",
				status);
			card->vlan_id[i] = 0;
		} else if (status) {
			dev_dbg(ctodev(card),
M
Masakazu Mokuno 已提交
1393
				"%s:get vlan id faild, status=%d\n",
1394 1395 1396 1397 1398 1399 1400
				__func__, status);
			card->vlan_id[i] = 0;
		} else {
			card->vlan_id[i] = (u32)v1;
			dev_dbg(ctodev(card), "vlan_id:%d, %lx\n", i, v1);
		}
	}
1401

M
Masakazu Mokuno 已提交
1402 1403
	if (card->vlan_id[GELIC_LV1_VLAN_TX_ETHERNET - 1]) {
		card->vlan_index = GELIC_LV1_VLAN_TX_ETHERNET - 1;
1404 1405
		netdev->hard_header_len += VLAN_HLEN;
	}
1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417

	status = register_netdev(netdev);
	if (status) {
		dev_err(ctodev(card), "%s:Couldn't register net_device: %d\n",
			__func__, status);
		return status;
	}

	return 0;
}

/**
M
Masakazu Mokuno 已提交
1418
 * gelic_alloc_card_net - allocates net_device and card structure
1419 1420 1421 1422 1423
 *
 * returns the card structure or NULL in case of errors
 *
 * the card and net_device structures are linked to each other
 */
M
Masakazu Mokuno 已提交
1424
static struct gelic_card *gelic_alloc_card_net(void)
1425 1426
{
	struct net_device *netdev;
M
Masakazu Mokuno 已提交
1427
	struct gelic_card *card;
1428 1429
	size_t alloc_size;

M
Masakazu Mokuno 已提交
1430 1431 1432
	alloc_size = sizeof(*card) +
		sizeof(struct gelic_descr) * GELIC_NET_RX_DESCRIPTORS +
		sizeof(struct gelic_descr) * GELIC_NET_TX_DESCRIPTORS;
1433 1434
	/*
	 * we assume private data is allocated 32 bytes (or more) aligned
M
Masakazu Mokuno 已提交
1435
	 * so that gelic_descr should be 32 bytes aligned.
1436 1437 1438 1439 1440
	 * Current alloc_etherdev() does do it because NETDEV_ALIGN
	 * is 32.
	 * check this assumption here.
	 */
	BUILD_BUG_ON(NETDEV_ALIGN < 32);
M
Masakazu Mokuno 已提交
1441 1442
	BUILD_BUG_ON(offsetof(struct gelic_card, irq_status) % 8);
	BUILD_BUG_ON(offsetof(struct gelic_card, descr) % 32);
1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459

	netdev = alloc_etherdev(alloc_size);
	if (!netdev)
		return NULL;

	card = netdev_priv(netdev);
	card->netdev = netdev;
	INIT_WORK(&card->tx_timeout_task, gelic_net_tx_timeout_task);
	init_waitqueue_head(&card->waitq);
	atomic_set(&card->tx_timeout_task_counter, 0);

	return card;
}

/**
 * ps3_gelic_driver_probe - add a device to the control of this driver
 */
M
Masakazu Mokuno 已提交
1460
static int ps3_gelic_driver_probe(struct ps3_system_bus_device *dev)
1461
{
M
Masakazu Mokuno 已提交
1462
	struct gelic_card *card = gelic_alloc_card_net();
1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514
	int result;

	if (!card) {
		dev_info(&dev->core, "gelic_net_alloc_card failed\n");
		result = -ENOMEM;
		goto fail_alloc_card;
	}

	ps3_system_bus_set_driver_data(dev, card);
	card->dev = dev;

	result = ps3_open_hv_device(dev);

	if (result) {
		dev_dbg(&dev->core, "ps3_open_hv_device failed\n");
		goto fail_open;
	}

	result = ps3_dma_region_create(dev->d_region);

	if (result) {
		dev_dbg(&dev->core, "ps3_dma_region_create failed(%d)\n",
			result);
		BUG_ON("check region type");
		goto fail_dma_region;
	}

	result = lv1_net_set_interrupt_status_indicator(bus_id(card),
							dev_id(card),
		ps3_mm_phys_to_lpar(__pa(&card->irq_status)),
		0);

	if (result) {
		dev_dbg(&dev->core,
			"lv1_net_set_interrupt_status_indicator failed: %s\n",
			ps3_result(result));
		result = -EIO;
		goto fail_status_indicator;
	}

	result = gelic_net_setup_netdev(card);

	if (result) {
		dev_dbg(&dev->core, "%s:%d: ps3_dma_region_create failed: "
			"(%d)\n", __func__, __LINE__, result);
		goto fail_setup_netdev;
	}

	return 0;

fail_setup_netdev:
	lv1_net_set_interrupt_status_indicator(bus_id(card),
1515
					       dev_id(card),
1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531
					       0 , 0);
fail_status_indicator:
	ps3_dma_region_free(dev->d_region);
fail_dma_region:
	ps3_close_hv_device(dev);
fail_open:
	ps3_system_bus_set_driver_data(dev, NULL);
	free_netdev(card->netdev);
fail_alloc_card:
	return result;
}

/**
 * ps3_gelic_driver_remove - remove a device from the control of this driver
 */

M
Masakazu Mokuno 已提交
1532
static int ps3_gelic_driver_remove(struct ps3_system_bus_device *dev)
1533
{
M
Masakazu Mokuno 已提交
1534
	struct gelic_card *card = ps3_system_bus_get_driver_data(dev);
1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574

	wait_event(card->waitq,
		   atomic_read(&card->tx_timeout_task_counter) == 0);

	lv1_net_set_interrupt_status_indicator(bus_id(card), dev_id(card),
					       0 , 0);

	unregister_netdev(card->netdev);
	free_netdev(card->netdev);

	ps3_system_bus_set_driver_data(dev, NULL);

	ps3_dma_region_free(dev->d_region);

	ps3_close_hv_device(dev);

	return 0;
}

static struct ps3_system_bus_driver ps3_gelic_driver = {
	.match_id = PS3_MATCH_ID_GELIC,
	.probe = ps3_gelic_driver_probe,
	.remove = ps3_gelic_driver_remove,
	.shutdown = ps3_gelic_driver_remove,
	.core.name = "ps3_gelic_driver",
	.core.owner = THIS_MODULE,
};

static int __init ps3_gelic_driver_init (void)
{
	return firmware_has_feature(FW_FEATURE_PS3_LV1)
		? ps3_system_bus_driver_register(&ps3_gelic_driver)
		: -ENODEV;
}

static void __exit ps3_gelic_driver_exit (void)
{
	ps3_system_bus_driver_unregister(&ps3_gelic_driver);
}

M
Masakazu Mokuno 已提交
1575 1576
module_init(ps3_gelic_driver_init);
module_exit(ps3_gelic_driver_exit);
1577 1578 1579

MODULE_ALIAS(PS3_MODULE_ALIAS_GELIC);