Linux Audio

Check our new training course

Loading...
v6.8
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 *  PS3 gelic network driver.
   4 *
   5 * Copyright (C) 2007 Sony Computer Entertainment Inc.
   6 * Copyright 2006, 2007 Sony Corporation
   7 *
   8 * This file is based on: spider_net.c
   9 *
  10 * (C) Copyright IBM Corp. 2005
  11 *
  12 * Authors : Utz Bacher <utz.bacher@de.ibm.com>
  13 *           Jens Osterkamp <Jens.Osterkamp@de.ibm.com>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  14 */
  15
  16#undef DEBUG
  17
  18#include <linux/interrupt.h>
  19#include <linux/kernel.h>
  20#include <linux/module.h>
  21#include <linux/slab.h>
  22
  23#include <linux/etherdevice.h>
  24#include <linux/ethtool.h>
  25#include <linux/if_vlan.h>
  26
  27#include <linux/in.h>
  28#include <linux/ip.h>
  29#include <linux/tcp.h>
  30
  31#include <linux/dma-mapping.h>
  32#include <net/checksum.h>
  33#include <asm/firmware.h>
  34#include <asm/ps3.h>
  35#include <asm/lv1call.h>
  36
  37#include "ps3_gelic_net.h"
  38#include "ps3_gelic_wireless.h"
  39
  40#define DRV_NAME "Gelic Network Driver"
  41#define DRV_VERSION "2.0"
  42
  43MODULE_AUTHOR("SCE Inc.");
  44MODULE_DESCRIPTION("Gelic Network driver");
  45MODULE_LICENSE("GPL");
  46
  47
  48/* set irq_mask */
  49int gelic_card_set_irq_mask(struct gelic_card *card, u64 mask)
  50{
  51	int status;
  52
  53	status = lv1_net_set_interrupt_mask(bus_id(card), dev_id(card),
  54					    mask, 0);
  55	if (status)
  56		dev_info(ctodev(card),
  57			 "%s failed %d\n", __func__, status);
  58	return status;
  59}
  60
  61static void gelic_card_rx_irq_on(struct gelic_card *card)
  62{
  63	card->irq_mask |= GELIC_CARD_RXINT;
  64	gelic_card_set_irq_mask(card, card->irq_mask);
  65}
  66static void gelic_card_rx_irq_off(struct gelic_card *card)
  67{
  68	card->irq_mask &= ~GELIC_CARD_RXINT;
  69	gelic_card_set_irq_mask(card, card->irq_mask);
  70}
  71
  72static void gelic_card_get_ether_port_status(struct gelic_card *card,
  73					     int inform)
  74{
  75	u64 v2;
  76	struct net_device *ether_netdev;
  77
  78	lv1_net_control(bus_id(card), dev_id(card),
  79			GELIC_LV1_GET_ETH_PORT_STATUS,
  80			GELIC_LV1_VLAN_TX_ETHERNET_0, 0, 0,
  81			&card->ether_port_status, &v2);
  82
  83	if (inform) {
  84		ether_netdev = card->netdev[GELIC_PORT_ETHERNET_0];
  85		if (card->ether_port_status & GELIC_LV1_ETHER_LINK_UP)
  86			netif_carrier_on(ether_netdev);
  87		else
  88			netif_carrier_off(ether_netdev);
  89	}
  90}
  91
  92/**
  93 * gelic_descr_get_status -- returns the status of a descriptor
  94 * @descr: descriptor to look at
  95 *
  96 * returns the status as in the hw_regs.dmac_cmd_status field of the descriptor
  97 */
  98static enum gelic_descr_dma_status
  99gelic_descr_get_status(struct gelic_descr *descr)
 100{
 101	return be32_to_cpu(descr->hw_regs.dmac_cmd_status) &
 102		GELIC_DESCR_DMA_STAT_MASK;
 103}
 104
 105static int gelic_card_set_link_mode(struct gelic_card *card, int mode)
 106{
 107	int status;
 108	u64 v1, v2;
 109
 110	status = lv1_net_control(bus_id(card), dev_id(card),
 111				 GELIC_LV1_SET_NEGOTIATION_MODE,
 112				 GELIC_LV1_PHY_ETHERNET_0, mode, 0, &v1, &v2);
 113	if (status) {
 114		pr_info("%s: failed setting negotiation mode %d\n", __func__,
 115			status);
 116		return -EBUSY;
 117	}
 118
 119	card->link_mode = mode;
 120	return 0;
 121}
 122
 123/**
 124 * gelic_card_disable_txdmac - disables the transmit DMA controller
 125 * @card: card structure
 126 *
 127 * gelic_card_disable_txdmac terminates processing on the DMA controller by
 128 * turing off DMA and issuing a force end
 129 */
 130static void gelic_card_disable_txdmac(struct gelic_card *card)
 131{
 132	int status;
 133
 134	/* this hvc blocks until the DMA in progress really stopped */
 135	status = lv1_net_stop_tx_dma(bus_id(card), dev_id(card));
 136	if (status)
 137		dev_err(ctodev(card),
 138			"lv1_net_stop_tx_dma failed, status=%d\n", status);
 139}
 140
 141/**
 142 * gelic_card_enable_rxdmac - enables the receive DMA controller
 143 * @card: card structure
 144 *
 145 * gelic_card_enable_rxdmac enables the DMA controller by setting RX_DMA_EN
 146 * in the GDADMACCNTR register
 147 */
 148static void gelic_card_enable_rxdmac(struct gelic_card *card)
 149{
 150	int status;
 151
 152#ifdef DEBUG
 153	if (gelic_descr_get_status(card->rx_chain.head) !=
 154	    GELIC_DESCR_DMA_CARDOWNED) {
 155		printk(KERN_ERR "%s: status=%x\n", __func__,
 156		       be32_to_cpu(card->rx_chain.head->hw_regs.dmac_cmd_status));
 157		printk(KERN_ERR "%s: nextphy=%x\n", __func__,
 158		       be32_to_cpu(card->rx_chain.head->hw_regs.next_descr_addr));
 159		printk(KERN_ERR "%s: head=%p\n", __func__,
 160		       card->rx_chain.head);
 161	}
 162#endif
 163	status = lv1_net_start_rx_dma(bus_id(card), dev_id(card),
 164				card->rx_chain.head->link.cpu_addr, 0);
 165	if (status)
 166		dev_info(ctodev(card),
 167			 "lv1_net_start_rx_dma failed, status=%d\n", status);
 168}
 169
 170/**
 171 * gelic_card_disable_rxdmac - disables the receive DMA controller
 172 * @card: card structure
 173 *
 174 * gelic_card_disable_rxdmac terminates processing on the DMA controller by
 175 * turing off DMA and issuing a force end
 176 */
 177static void gelic_card_disable_rxdmac(struct gelic_card *card)
 178{
 179	int status;
 180
 181	/* this hvc blocks until the DMA in progress really stopped */
 182	status = lv1_net_stop_rx_dma(bus_id(card), dev_id(card));
 183	if (status)
 184		dev_err(ctodev(card),
 185			"lv1_net_stop_rx_dma failed, %d\n", status);
 186}
 187
 188/**
 189 * gelic_descr_set_status -- sets the status of a descriptor
 190 * @descr: descriptor to change
 191 * @status: status to set in the descriptor
 192 *
 193 * changes the status to the specified value. Doesn't change other bits
 194 * in the status
 195 */
 196static void gelic_descr_set_status(struct gelic_descr *descr,
 197				   enum gelic_descr_dma_status status)
 198{
 199	descr->hw_regs.dmac_cmd_status = cpu_to_be32(status |
 200			(be32_to_cpu(descr->hw_regs.dmac_cmd_status) &
 201			 ~GELIC_DESCR_DMA_STAT_MASK));
 202	/*
 203	 * dma_cmd_status field is used to indicate whether the descriptor
 204	 * is valid or not.
 205	 * Usually caller of this function wants to inform that to the
 206	 * hardware, so we assure here the hardware sees the change.
 207	 */
 208	wmb();
 209}
 210
 211/**
 212 * gelic_card_reset_chain - reset status of a descriptor chain
 213 * @card: card structure
 214 * @chain: address of chain
 215 * @start_descr: address of descriptor array
 216 *
 217 * Reset the status of dma descriptors to ready state
 218 * and re-initialize the hardware chain for later use
 219 */
 220static void gelic_card_reset_chain(struct gelic_card *card,
 221				   struct gelic_descr_chain *chain,
 222				   struct gelic_descr *start_descr)
 223{
 224	struct gelic_descr *descr;
 225
 226	for (descr = start_descr; start_descr != descr->next; descr++) {
 227		gelic_descr_set_status(descr, GELIC_DESCR_DMA_CARDOWNED);
 228		descr->hw_regs.next_descr_addr
 229			= cpu_to_be32(descr->next->link.cpu_addr);
 230	}
 231
 232	chain->head = start_descr;
 233	chain->tail = (descr - 1);
 234
 235	(descr - 1)->hw_regs.next_descr_addr = 0;
 236}
 237
 238void gelic_card_up(struct gelic_card *card)
 239{
 240	pr_debug("%s: called\n", __func__);
 241	mutex_lock(&card->updown_lock);
 242	if (atomic_inc_return(&card->users) == 1) {
 243		pr_debug("%s: real do\n", __func__);
 244		/* enable irq */
 245		gelic_card_set_irq_mask(card, card->irq_mask);
 246		/* start rx */
 247		gelic_card_enable_rxdmac(card);
 248
 249		napi_enable(&card->napi);
 250	}
 251	mutex_unlock(&card->updown_lock);
 252	pr_debug("%s: done\n", __func__);
 253}
 254
 255void gelic_card_down(struct gelic_card *card)
 256{
 257	u64 mask;
 258	pr_debug("%s: called\n", __func__);
 259	mutex_lock(&card->updown_lock);
 260	if (atomic_dec_if_positive(&card->users) == 0) {
 261		pr_debug("%s: real do\n", __func__);
 262		napi_disable(&card->napi);
 263		/*
 264		 * Disable irq. Wireless interrupts will
 265		 * be disabled later if any
 266		 */
 267		mask = card->irq_mask & (GELIC_CARD_WLAN_EVENT_RECEIVED |
 268					 GELIC_CARD_WLAN_COMMAND_COMPLETED);
 269		gelic_card_set_irq_mask(card, mask);
 270		/* stop rx */
 271		gelic_card_disable_rxdmac(card);
 272		gelic_card_reset_chain(card, &card->rx_chain,
 273				       card->descr + GELIC_NET_TX_DESCRIPTORS);
 274		/* stop tx */
 275		gelic_card_disable_txdmac(card);
 276	}
 277	mutex_unlock(&card->updown_lock);
 278	pr_debug("%s: done\n", __func__);
 279}
 280
 281/**
 282 * gelic_card_free_chain - free descriptor chain
 283 * @card: card structure
 284 * @descr_in: address of desc
 285 */
 286static void gelic_card_free_chain(struct gelic_card *card,
 287				  struct gelic_descr *descr_in)
 288{
 289	struct gelic_descr *descr;
 290
 291	for (descr = descr_in; descr && descr->link.cpu_addr;
 292		descr = descr->next) {
 293		dma_unmap_single(ctodev(card), descr->link.cpu_addr,
 294				 descr->link.size, DMA_BIDIRECTIONAL);
 295		descr->link.cpu_addr = 0;
 296		descr->link.size = 0;
 297	}
 298}
 299
 300/**
 301 * gelic_card_init_chain - links descriptor chain
 302 * @card: card structure
 303 * @chain: address of chain
 304 * @start_descr: address of descriptor array
 305 * @no: number of descriptors
 306 *
 307 * we manage a circular list that mirrors the hardware structure,
 308 * except that the hardware uses bus addresses.
 309 *
 310 * returns 0 on success, <0 on failure
 311 */
 312static int gelic_card_init_chain(struct gelic_card *card,
 313				 struct gelic_descr_chain *chain,
 314				 struct gelic_descr *start_descr, int no)
 315{
 316	int i;
 317	struct gelic_descr *descr;
 318
 319	descr = start_descr;
 320	memset(descr, 0, sizeof(*descr) * no);
 321
 322	/* set up the hardware pointers in each descriptor */
 323	for (i = 0; i < no; i++, descr++) {
 324		gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
 
 
 
 
 325
 326		descr->link.size = sizeof(struct gelic_hw_regs);
 327		descr->link.cpu_addr = dma_map_single(ctodev(card), descr,
 328					  descr->link.size, DMA_BIDIRECTIONAL);
 329
 330		if (dma_mapping_error(ctodev(card), descr->link.cpu_addr)) {
 331			for (i--, descr--; 0 <= i; i--, descr--) {
 332				dma_unmap_single(ctodev(card),
 333					descr->link.cpu_addr, descr->link.size,
 334					DMA_BIDIRECTIONAL);
 335			}
 336			return -ENOMEM;
 337		}
 338
 339		descr->next = descr + 1;
 340		descr->prev = descr - 1;
 341	}
 342	/* make them as ring */
 343	(descr - 1)->next = start_descr;
 344	start_descr->prev = (descr - 1);
 345
 346	/* chain bus addr of hw descriptor */
 347	descr = start_descr;
 348	for (i = 0; i < no; i++, descr++) {
 349		descr->hw_regs.next_descr_addr =
 350			cpu_to_be32(descr->next->link.cpu_addr);
 351	}
 352
 353	chain->head = start_descr;
 354	chain->tail = start_descr;
 355
 356	/* do not chain last hw descriptor */
 357	(descr - 1)->hw_regs.next_descr_addr = 0;
 358
 359	return 0;
 
 
 
 
 
 
 
 
 360}
 361
 362/**
 363 * gelic_descr_prepare_rx - reinitializes a rx descriptor
 364 * @card: card structure
 365 * @descr: descriptor to re-init
 366 *
 367 * return 0 on success, <0 on failure
 368 *
 369 * allocates a new rx skb, iommu-maps it and attaches it to the descriptor.
 370 * Activate the descriptor state-wise
 371 *
 372 * Gelic RX sk_buffs must be aligned to GELIC_NET_RXBUF_ALIGN and the length
 373 * must be a multiple of GELIC_NET_RXBUF_ALIGN.
 374 */
 375static int gelic_descr_prepare_rx(struct gelic_card *card,
 376				  struct gelic_descr *descr)
 377{
 378	static const unsigned int rx_skb_size =
 379		ALIGN(GELIC_NET_MAX_FRAME, GELIC_NET_RXBUF_ALIGN) +
 380		GELIC_NET_RXBUF_ALIGN - 1;
 381	dma_addr_t cpu_addr;
 382	int offset;
 
 383
 384	if (gelic_descr_get_status(descr) !=  GELIC_DESCR_DMA_NOT_IN_USE)
 385		dev_info(ctodev(card), "%s: ERROR status\n", __func__);
 
 
 386
 387	descr->hw_regs.dmac_cmd_status = 0;
 388	descr->hw_regs.result_size = 0;
 389	descr->hw_regs.valid_size = 0;
 390	descr->hw_regs.data_error = 0;
 391	descr->hw_regs.payload.dev_addr = 0;
 392	descr->hw_regs.payload.size = 0;
 393
 394	descr->skb = netdev_alloc_skb(*card->netdev, rx_skb_size);
 395	if (!descr->skb) {
 396		descr->hw_regs.payload.dev_addr = 0; /* tell DMAC don't touch memory */
 
 
 397		return -ENOMEM;
 398	}
 
 
 
 
 
 399
 400	offset = ((unsigned long)descr->skb->data) &
 401		(GELIC_NET_RXBUF_ALIGN - 1);
 402	if (offset)
 403		skb_reserve(descr->skb, GELIC_NET_RXBUF_ALIGN - offset);
 404	/* io-mmu-map the skb */
 405	cpu_addr = dma_map_single(ctodev(card), descr->skb->data,
 406				  GELIC_NET_MAX_FRAME, DMA_FROM_DEVICE);
 407	descr->hw_regs.payload.dev_addr = cpu_to_be32(cpu_addr);
 408	if (dma_mapping_error(ctodev(card), cpu_addr)) {
 
 409		dev_kfree_skb_any(descr->skb);
 410		descr->skb = NULL;
 411		dev_info(ctodev(card),
 412			 "%s:Could not iommu-map rx buffer\n", __func__);
 413		gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
 414		return -ENOMEM;
 
 
 
 415	}
 416
 417	descr->hw_regs.payload.size = cpu_to_be32(GELIC_NET_MAX_FRAME);
 418	descr->hw_regs.payload.dev_addr = cpu_to_be32(cpu_addr);
 419
 420	gelic_descr_set_status(descr, GELIC_DESCR_DMA_CARDOWNED);
 421
 422	return 0;
 423}
 424
 425/**
 426 * gelic_card_release_rx_chain - free all skb of rx descr
 427 * @card: card structure
 428 *
 429 */
 430static void gelic_card_release_rx_chain(struct gelic_card *card)
 431{
 432	struct gelic_descr *descr = card->rx_chain.head;
 433
 434	do {
 435		if (descr->skb) {
 436			dma_unmap_single(ctodev(card),
 437				be32_to_cpu(descr->hw_regs.payload.dev_addr),
 438				descr->skb->len,
 439				DMA_FROM_DEVICE);
 440			descr->hw_regs.payload.dev_addr = 0;
 441			descr->hw_regs.payload.size = 0;
 442			dev_kfree_skb_any(descr->skb);
 443			descr->skb = NULL;
 444			gelic_descr_set_status(descr,
 445				GELIC_DESCR_DMA_NOT_IN_USE);
 446		}
 447		descr = descr->next;
 448	} while (descr != card->rx_chain.head);
 449}
 450
 451/**
 452 * gelic_card_fill_rx_chain - fills descriptors/skbs in the rx chains
 453 * @card: card structure
 454 *
 455 * fills all descriptors in the rx chain: allocates skbs
 456 * and iommu-maps them.
 457 * returns 0 on success, < 0 on failure
 458 */
 459static int gelic_card_fill_rx_chain(struct gelic_card *card)
 460{
 461	struct gelic_descr *descr = card->rx_chain.head;
 462	int ret;
 463
 464	do {
 465		if (!descr->skb) {
 466			ret = gelic_descr_prepare_rx(card, descr);
 467			if (ret)
 468				goto rewind;
 469		}
 470		descr = descr->next;
 471	} while (descr != card->rx_chain.head);
 472
 473	return 0;
 474rewind:
 475	gelic_card_release_rx_chain(card);
 476	return ret;
 477}
 478
 479/**
 480 * gelic_card_alloc_rx_skbs - allocates rx skbs in rx descriptor chains
 481 * @card: card structure
 482 *
 483 * returns 0 on success, < 0 on failure
 484 */
 485static int gelic_card_alloc_rx_skbs(struct gelic_card *card)
 486{
 487	struct gelic_descr_chain *chain;
 488	int ret;
 489	chain = &card->rx_chain;
 490	ret = gelic_card_fill_rx_chain(card);
 491	chain->tail = card->rx_top->prev; /* point to the last */
 492	return ret;
 493}
 494
 495/**
 496 * gelic_descr_release_tx - processes a used tx descriptor
 497 * @card: card structure
 498 * @descr: descriptor to release
 499 *
 500 * releases a used tx descriptor (unmapping, freeing of skb)
 501 */
 502static void gelic_descr_release_tx(struct gelic_card *card,
 503				       struct gelic_descr *descr)
 504{
 505	struct sk_buff *skb = descr->skb;
 506
 507	BUG_ON(!(be32_to_cpu(descr->hw_regs.data_status) & GELIC_DESCR_TX_TAIL));
 508
 509	dma_unmap_single(ctodev(card),
 510		be32_to_cpu(descr->hw_regs.payload.dev_addr), skb->len,
 511		DMA_TO_DEVICE);
 512	dev_kfree_skb_any(skb);
 513
 514	descr->hw_regs.payload.dev_addr = 0;
 515	descr->hw_regs.payload.size = 0;
 516	descr->hw_regs.next_descr_addr = 0;
 517	descr->hw_regs.result_size = 0;
 518	descr->hw_regs.valid_size = 0;
 519	descr->hw_regs.data_status = 0;
 520	descr->hw_regs.data_error = 0;
 521	descr->skb = NULL;
 522
 523	/* set descr status */
 524	gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
 525}
 526
 527static void gelic_card_stop_queues(struct gelic_card *card)
 528{
 529	netif_stop_queue(card->netdev[GELIC_PORT_ETHERNET_0]);
 530
 531	if (card->netdev[GELIC_PORT_WIRELESS])
 532		netif_stop_queue(card->netdev[GELIC_PORT_WIRELESS]);
 533}
 534static void gelic_card_wake_queues(struct gelic_card *card)
 535{
 536	netif_wake_queue(card->netdev[GELIC_PORT_ETHERNET_0]);
 537
 538	if (card->netdev[GELIC_PORT_WIRELESS])
 539		netif_wake_queue(card->netdev[GELIC_PORT_WIRELESS]);
 540}
 541/**
 542 * gelic_card_release_tx_chain - processes sent tx descriptors
 543 * @card: adapter structure
 544 * @stop: net_stop sequence
 545 *
 546 * releases the tx descriptors that gelic has finished with
 547 */
 548static void gelic_card_release_tx_chain(struct gelic_card *card, int stop)
 549{
 550	struct gelic_descr_chain *tx_chain;
 551	enum gelic_descr_dma_status status;
 552	struct net_device *netdev;
 553	int release = 0;
 554
 555	for (tx_chain = &card->tx_chain;
 556	     tx_chain->head != tx_chain->tail && tx_chain->tail;
 557	     tx_chain->tail = tx_chain->tail->next) {
 558		status = gelic_descr_get_status(tx_chain->tail);
 559		netdev = tx_chain->tail->skb->dev;
 560		switch (status) {
 561		case GELIC_DESCR_DMA_RESPONSE_ERROR:
 562		case GELIC_DESCR_DMA_PROTECTION_ERROR:
 563		case GELIC_DESCR_DMA_FORCE_END:
 564			if (printk_ratelimit())
 565				dev_info(ctodev(card),
 566					 "%s: forcing end of tx descriptor " \
 567					 "with status %x\n",
 568					 __func__, status);
 569			netdev->stats.tx_dropped++;
 570			break;
 571
 572		case GELIC_DESCR_DMA_COMPLETE:
 573			if (tx_chain->tail->skb) {
 574				netdev->stats.tx_packets++;
 575				netdev->stats.tx_bytes +=
 576					tx_chain->tail->skb->len;
 577			}
 578			break;
 579
 580		case GELIC_DESCR_DMA_CARDOWNED:
 581			/* pending tx request */
 582		default:
 583			/* any other value (== GELIC_DESCR_DMA_NOT_IN_USE) */
 584			if (!stop)
 585				goto out;
 586		}
 587		gelic_descr_release_tx(card, tx_chain->tail);
 588		release ++;
 589	}
 590out:
 591	if (!stop && release)
 592		gelic_card_wake_queues(card);
 593}
 594
 595/**
 596 * gelic_net_set_multi - sets multicast addresses and promisc flags
 597 * @netdev: interface device structure
 598 *
 599 * gelic_net_set_multi configures multicast addresses as needed for the
 600 * netdev interface. It also sets up multicast, allmulti and promisc
 601 * flags appropriately
 602 */
 603void gelic_net_set_multi(struct net_device *netdev)
 604{
 605	struct gelic_card *card = netdev_card(netdev);
 606	struct netdev_hw_addr *ha;
 607	unsigned int i;
 608	uint8_t *p;
 609	u64 addr;
 610	int status;
 611
 612	/* clear all multicast address */
 613	status = lv1_net_remove_multicast_address(bus_id(card), dev_id(card),
 614						  0, 1);
 615	if (status)
 616		dev_err(ctodev(card),
 617			"lv1_net_remove_multicast_address failed %d\n",
 618			status);
 619	/* set broadcast address */
 620	status = lv1_net_add_multicast_address(bus_id(card), dev_id(card),
 621					       GELIC_NET_BROADCAST_ADDR, 0);
 622	if (status)
 623		dev_err(ctodev(card),
 624			"lv1_net_add_multicast_address failed, %d\n",
 625			status);
 626
 627	if ((netdev->flags & IFF_ALLMULTI) ||
 628	    (netdev_mc_count(netdev) > GELIC_NET_MC_COUNT_MAX)) {
 629		status = lv1_net_add_multicast_address(bus_id(card),
 630						       dev_id(card),
 631						       0, 1);
 632		if (status)
 633			dev_err(ctodev(card),
 634				"lv1_net_add_multicast_address failed, %d\n",
 635				status);
 636		return;
 637	}
 638
 639	/* set multicast addresses */
 640	netdev_for_each_mc_addr(ha, netdev) {
 641		addr = 0;
 642		p = ha->addr;
 643		for (i = 0; i < ETH_ALEN; i++) {
 644			addr <<= 8;
 645			addr |= *p++;
 646		}
 647		status = lv1_net_add_multicast_address(bus_id(card),
 648						       dev_id(card),
 649						       addr, 0);
 650		if (status)
 651			dev_err(ctodev(card),
 652				"lv1_net_add_multicast_address failed, %d\n",
 653				status);
 654	}
 655}
 656
 657/**
 658 * gelic_net_stop - called upon ifconfig down
 659 * @netdev: interface device structure
 660 *
 661 * always returns 0
 662 */
 663int gelic_net_stop(struct net_device *netdev)
 664{
 665	struct gelic_card *card;
 666
 667	pr_debug("%s: start\n", __func__);
 668
 669	netif_stop_queue(netdev);
 670	netif_carrier_off(netdev);
 671
 672	card = netdev_card(netdev);
 673	gelic_card_down(card);
 674
 675	pr_debug("%s: done\n", __func__);
 676	return 0;
 677}
 678
 679/**
 680 * gelic_card_get_next_tx_descr - returns the next available tx descriptor
 681 * @card: device structure to get descriptor from
 682 *
 683 * returns the address of the next descriptor, or NULL if not available.
 684 */
 685static struct gelic_descr *
 686gelic_card_get_next_tx_descr(struct gelic_card *card)
 687{
 688	if (!card->tx_chain.head)
 689		return NULL;
 690	/*  see if the next descriptor is free */
 691	if (card->tx_chain.tail != card->tx_chain.head->next &&
 692	    gelic_descr_get_status(card->tx_chain.head) ==
 693	    GELIC_DESCR_DMA_NOT_IN_USE)
 694		return card->tx_chain.head;
 695	else
 696		return NULL;
 697
 698}
 699
 700/**
 701 * gelic_net_set_txdescr_cmdstat - sets the tx descriptor command field
 702 * @descr: descriptor structure to fill out
 703 * @skb: packet to consider
 704 *
 705 * fills out the command and status field of the descriptor structure,
 706 * depending on hardware checksum settings. This function assumes a wmb()
 707 * has executed before.
 708 */
 709static void gelic_descr_set_tx_cmdstat(struct gelic_descr *descr,
 710				       struct sk_buff *skb)
 711{
 712	if (skb->ip_summed != CHECKSUM_PARTIAL)
 713		descr->hw_regs.dmac_cmd_status =
 714			cpu_to_be32(GELIC_DESCR_DMA_CMD_NO_CHKSUM |
 715				    GELIC_DESCR_TX_DMA_FRAME_TAIL);
 716	else {
 717		/* is packet ip?
 718		 * if yes: tcp? udp? */
 719		if (skb->protocol == htons(ETH_P_IP)) {
 720			if (ip_hdr(skb)->protocol == IPPROTO_TCP)
 721				descr->hw_regs.dmac_cmd_status =
 722				cpu_to_be32(GELIC_DESCR_DMA_CMD_TCP_CHKSUM |
 723					    GELIC_DESCR_TX_DMA_FRAME_TAIL);
 724
 725			else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
 726				descr->hw_regs.dmac_cmd_status =
 727				cpu_to_be32(GELIC_DESCR_DMA_CMD_UDP_CHKSUM |
 728					    GELIC_DESCR_TX_DMA_FRAME_TAIL);
 729			else	/*
 730				 * the stack should checksum non-tcp and non-udp
 731				 * packets on his own: NETIF_F_IP_CSUM
 732				 */
 733				descr->hw_regs.dmac_cmd_status =
 734				cpu_to_be32(GELIC_DESCR_DMA_CMD_NO_CHKSUM |
 735					    GELIC_DESCR_TX_DMA_FRAME_TAIL);
 736		}
 737	}
 738}
 739
 740static struct sk_buff *gelic_put_vlan_tag(struct sk_buff *skb,
 741						 unsigned short tag)
 742{
 743	struct vlan_ethhdr *veth;
 744	static unsigned int c;
 745
 746	if (skb_headroom(skb) < VLAN_HLEN) {
 747		struct sk_buff *sk_tmp = skb;
 748		pr_debug("%s: hd=%d c=%ud\n", __func__, skb_headroom(skb), c);
 749		skb = skb_realloc_headroom(sk_tmp, VLAN_HLEN);
 750		if (!skb)
 751			return NULL;
 752		dev_kfree_skb_any(sk_tmp);
 753	}
 754	veth = skb_push(skb, VLAN_HLEN);
 755
 756	/* Move the mac addresses to the top of buffer */
 757	memmove(skb->data, skb->data + VLAN_HLEN, 2 * ETH_ALEN);
 758
 759	veth->h_vlan_proto = cpu_to_be16(ETH_P_8021Q);
 760	veth->h_vlan_TCI = htons(tag);
 761
 762	return skb;
 763}
 764
 765/**
 766 * gelic_descr_prepare_tx - setup a descriptor for sending packets
 767 * @card: card structure
 768 * @descr: descriptor structure
 769 * @skb: packet to use
 770 *
 771 * returns 0 on success, <0 on failure.
 772 *
 773 */
 774static int gelic_descr_prepare_tx(struct gelic_card *card,
 775				  struct gelic_descr *descr,
 776				  struct sk_buff *skb)
 777{
 778	dma_addr_t buf;
 779
 780	if (card->vlan_required) {
 781		struct sk_buff *skb_tmp;
 782		enum gelic_port_type type;
 783
 784		type = netdev_port(skb->dev)->type;
 785		skb_tmp = gelic_put_vlan_tag(skb,
 786					     card->vlan[type].tx);
 787		if (!skb_tmp)
 788			return -ENOMEM;
 789		skb = skb_tmp;
 790	}
 791
 792	buf = dma_map_single(ctodev(card), skb->data, skb->len, DMA_TO_DEVICE);
 793
 794	if (dma_mapping_error(ctodev(card), buf)) {
 795		dev_err(ctodev(card),
 796			"dma map 2 failed (%p, %i). Dropping packet\n",
 797			skb->data, skb->len);
 798		return -ENOMEM;
 799	}
 800
 801	descr->hw_regs.payload.dev_addr = cpu_to_be32(buf);
 802	descr->hw_regs.payload.size = cpu_to_be32(skb->len);
 803	descr->skb = skb;
 804	descr->hw_regs.data_status = 0;
 805	descr->hw_regs.next_descr_addr = 0; /* terminate hw descr */
 806	gelic_descr_set_tx_cmdstat(descr, skb);
 807
 808	/* bump free descriptor pointer */
 809	card->tx_chain.head = descr->next;
 810	return 0;
 811}
 812
 813/**
 814 * gelic_card_kick_txdma - enables TX DMA processing
 815 * @card: card structure
 816 * @descr: descriptor address to enable TX processing at
 817 *
 818 */
 819static int gelic_card_kick_txdma(struct gelic_card *card,
 820				 struct gelic_descr *descr)
 821{
 822	int status = 0;
 823
 824	if (card->tx_dma_progress)
 825		return 0;
 826
 827	if (gelic_descr_get_status(descr) == GELIC_DESCR_DMA_CARDOWNED) {
 828		card->tx_dma_progress = 1;
 829		status = lv1_net_start_tx_dma(bus_id(card), dev_id(card),
 830			descr->link.cpu_addr, 0);
 831		if (status) {
 832			card->tx_dma_progress = 0;
 833			dev_info(ctodev(card), "lv1_net_start_txdma failed," \
 834				 "status=%d\n", status);
 835		}
 836	}
 837	return status;
 838}
 839
 840/**
 841 * gelic_net_xmit - transmits a frame over the device
 842 * @skb: packet to send out
 843 * @netdev: interface device structure
 844 *
 845 * returns NETDEV_TX_OK on success, NETDEV_TX_BUSY on failure
 846 */
 847netdev_tx_t gelic_net_xmit(struct sk_buff *skb, struct net_device *netdev)
 848{
 849	struct gelic_card *card = netdev_card(netdev);
 850	struct gelic_descr *descr;
 851	int result;
 852	unsigned long flags;
 853
 854	spin_lock_irqsave(&card->tx_lock, flags);
 855
 856	gelic_card_release_tx_chain(card, 0);
 857
 858	descr = gelic_card_get_next_tx_descr(card);
 859	if (!descr) {
 860		/*
 861		 * no more descriptors free
 862		 */
 863		gelic_card_stop_queues(card);
 864		spin_unlock_irqrestore(&card->tx_lock, flags);
 865		return NETDEV_TX_BUSY;
 866	}
 867
 868	result = gelic_descr_prepare_tx(card, descr, skb);
 869	if (result) {
 870		/*
 871		 * DMA map failed.  As chances are that failure
 872		 * would continue, just release skb and return
 873		 */
 874		netdev->stats.tx_dropped++;
 875		dev_kfree_skb_any(skb);
 876		spin_unlock_irqrestore(&card->tx_lock, flags);
 877		return NETDEV_TX_OK;
 878	}
 879	/*
 880	 * link this prepared descriptor to previous one
 881	 * to achieve high performance
 882	 */
 883	descr->prev->hw_regs.next_descr_addr =
 884		cpu_to_be32(descr->link.cpu_addr);
 885	/*
 886	 * as hardware descriptor is modified in the above lines,
 887	 * ensure that the hardware sees it
 888	 */
 889	wmb();
 890	if (gelic_card_kick_txdma(card, descr)) {
 891		/*
 892		 * kick failed.
 893		 * release descriptor which was just prepared
 894		 */
 895		netdev->stats.tx_dropped++;
 896		/* don't trigger BUG_ON() in gelic_descr_release_tx */
 897		descr->hw_regs.data_status = cpu_to_be32(GELIC_DESCR_TX_TAIL);
 898		gelic_descr_release_tx(card, descr);
 899		/* reset head */
 900		card->tx_chain.head = descr;
 901		/* reset hw termination */
 902		descr->prev->hw_regs.next_descr_addr = 0;
 903		dev_info(ctodev(card), "%s: kick failure\n", __func__);
 904	}
 905
 906	spin_unlock_irqrestore(&card->tx_lock, flags);
 907	return NETDEV_TX_OK;
 908}
 909
 910/**
 911 * gelic_net_pass_skb_up - takes an skb from a descriptor and passes it on
 912 * @descr: descriptor to process
 913 * @card: card structure
 914 * @netdev: net_device structure to be passed packet
 915 *
 916 * iommu-unmaps the skb, fills out skb structure and passes the data to the
 917 * stack. The descriptor state is not changed.
 918 */
 919static void gelic_net_pass_skb_up(struct gelic_descr *descr,
 920				  struct gelic_card *card,
 921				  struct net_device *netdev)
 922
 923{
 924	struct sk_buff *skb = descr->skb;
 925	u32 data_status, data_error;
 926
 927	data_status = be32_to_cpu(descr->hw_regs.data_status);
 928	data_error = be32_to_cpu(descr->hw_regs.data_error);
 929	/* unmap skb buffer */
 930	dma_unmap_single(ctodev(card),
 931		be32_to_cpu(descr->hw_regs.payload.dev_addr),
 932		be32_to_cpu(descr->hw_regs.payload.size), DMA_FROM_DEVICE);
 933
 934	skb_put(skb, be32_to_cpu(descr->hw_regs.valid_size)?
 935		be32_to_cpu(descr->hw_regs.valid_size) :
 936		be32_to_cpu(descr->hw_regs.result_size));
 937	if (!descr->hw_regs.valid_size)
 938		dev_info(ctodev(card), "buffer full %x %x %x\n",
 939			 be32_to_cpu(descr->hw_regs.result_size),
 940			 be32_to_cpu(descr->hw_regs.payload.size),
 941			 be32_to_cpu(descr->hw_regs.dmac_cmd_status));
 942
 943	descr->skb = NULL;
 944	/*
 945	 * the card put 2 bytes vlan tag in front
 946	 * of the ethernet frame
 947	 */
 948	skb_pull(skb, 2);
 949	skb->protocol = eth_type_trans(skb, netdev);
 950
 951	/* checksum offload */
 952	if (netdev->features & NETIF_F_RXCSUM) {
 953		if ((data_status & GELIC_DESCR_DATA_STATUS_CHK_MASK) &&
 954		    (!(data_error & GELIC_DESCR_DATA_ERROR_CHK_MASK)))
 955			skb->ip_summed = CHECKSUM_UNNECESSARY;
 956		else
 957			skb_checksum_none_assert(skb);
 958	} else
 959		skb_checksum_none_assert(skb);
 960
 961	/* update netdevice statistics */
 962	netdev->stats.rx_packets++;
 963	netdev->stats.rx_bytes += skb->len;
 964
 965	/* pass skb up to stack */
 966	netif_receive_skb(skb);
 967}
 968
 969/**
 970 * gelic_card_decode_one_descr - processes an rx descriptor
 971 * @card: card structure
 972 *
 973 * returns 1 if a packet has been sent to the stack, otherwise 0
 974 *
 975 * processes an rx descriptor by iommu-unmapping the data buffer and passing
 976 * the packet up to the stack
 977 */
 978static int gelic_card_decode_one_descr(struct gelic_card *card)
 979{
 980	enum gelic_descr_dma_status status;
 981	struct gelic_descr_chain *chain = &card->rx_chain;
 982	struct gelic_descr *descr = chain->head;
 983	struct net_device *netdev = NULL;
 984	int dmac_chain_ended;
 985
 986	status = gelic_descr_get_status(descr);
 987
 988	if (status == GELIC_DESCR_DMA_CARDOWNED)
 989		return 0;
 990
 991	if (status == GELIC_DESCR_DMA_NOT_IN_USE) {
 992		dev_dbg(ctodev(card), "dormant descr? %p\n", descr);
 993		return 0;
 994	}
 995
 996	/* netdevice select */
 997	if (card->vlan_required) {
 998		unsigned int i;
 999		u16 vid;
1000		vid = *(u16 *)(descr->skb->data) & VLAN_VID_MASK;
1001		for (i = 0; i < GELIC_PORT_MAX; i++) {
1002			if (card->vlan[i].rx == vid) {
1003				netdev = card->netdev[i];
1004				break;
1005			}
1006		}
1007		if (GELIC_PORT_MAX <= i) {
1008			pr_info("%s: unknown packet vid=%x\n", __func__, vid);
1009			goto refill;
1010		}
1011	} else
1012		netdev = card->netdev[GELIC_PORT_ETHERNET_0];
1013
1014	if ((status == GELIC_DESCR_DMA_RESPONSE_ERROR) ||
1015	    (status == GELIC_DESCR_DMA_PROTECTION_ERROR) ||
1016	    (status == GELIC_DESCR_DMA_FORCE_END)) {
1017		dev_info(ctodev(card), "dropping RX descriptor with state %x\n",
1018			 status);
1019		netdev->stats.rx_dropped++;
1020		goto refill;
1021	}
1022
1023	if (status == GELIC_DESCR_DMA_BUFFER_FULL) {
1024		/*
1025		 * Buffer full would occur if and only if
1026		 * the frame length was longer than the size of this
1027		 * descriptor's buffer.  If the frame length was equal
1028		 * to or shorter than buffer'size, FRAME_END condition
1029		 * would occur.
1030		 * Anyway this frame was longer than the MTU,
1031		 * just drop it.
1032		 */
1033		dev_info(ctodev(card), "overlength frame\n");
1034		goto refill;
1035	}
1036	/*
1037	 * descriptors any other than FRAME_END here should
1038	 * be treated as error.
1039	 */
1040	if (status != GELIC_DESCR_DMA_FRAME_END) {
1041		dev_dbg(ctodev(card), "RX descriptor with state %x\n",
1042			status);
1043		goto refill;
1044	}
1045
1046	/* ok, we've got a packet in descr */
1047	gelic_net_pass_skb_up(descr, card, netdev);
1048refill:
1049
1050	/* is the current descriptor terminated with next_descr == NULL? */
1051	dmac_chain_ended =
1052		be32_to_cpu(descr->hw_regs.dmac_cmd_status) &
1053		GELIC_DESCR_RX_DMA_CHAIN_END;
1054	/*
1055	 * So that always DMAC can see the end
1056	 * of the descriptor chain to avoid
1057	 * from unwanted DMAC overrun.
1058	 */
1059	descr->hw_regs.next_descr_addr = 0;
1060
1061	/* change the descriptor state: */
1062	gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
1063
1064	/*
1065	 * this call can fail, but for now, just leave this
1066	 * descriptor without skb
1067	 */
1068	gelic_descr_prepare_rx(card, descr);
1069
1070	chain->tail = descr;
1071	chain->head = descr->next;
1072
1073	/*
1074	 * Set this descriptor the end of the chain.
1075	 */
1076	descr->prev->hw_regs.next_descr_addr =
1077		cpu_to_be32(descr->link.cpu_addr);
1078
1079	/*
1080	 * If dmac chain was met, DMAC stopped.
1081	 * thus re-enable it
1082	 */
1083
1084	if (dmac_chain_ended)
1085		gelic_card_enable_rxdmac(card);
1086
1087	return 1;
1088}
1089
1090/**
1091 * gelic_net_poll - NAPI poll function called by the stack to return packets
1092 * @napi: napi structure
1093 * @budget: number of packets we can pass to the stack at most
1094 *
1095 * returns the number of the processed packets
1096 *
1097 */
1098static int gelic_net_poll(struct napi_struct *napi, int budget)
1099{
1100	struct gelic_card *card = container_of(napi, struct gelic_card, napi);
1101	int packets_done = 0;
1102
1103	while (packets_done < budget) {
1104		if (!gelic_card_decode_one_descr(card))
1105			break;
1106
1107		packets_done++;
1108	}
1109
1110	if (packets_done < budget) {
1111		napi_complete_done(napi, packets_done);
1112		gelic_card_rx_irq_on(card);
1113	}
1114	return packets_done;
1115}
1116
1117/*
1118 * gelic_card_interrupt - event handler for gelic_net
1119 */
1120static irqreturn_t gelic_card_interrupt(int irq, void *ptr)
1121{
1122	unsigned long flags;
1123	struct gelic_card *card = ptr;
1124	u64 status;
1125
1126	status = card->irq_status;
1127
1128	if (!status)
1129		return IRQ_NONE;
1130
1131	status &= card->irq_mask;
1132
1133	if (status & GELIC_CARD_RXINT) {
1134		gelic_card_rx_irq_off(card);
1135		napi_schedule(&card->napi);
1136	}
1137
1138	if (status & GELIC_CARD_TXINT) {
1139		spin_lock_irqsave(&card->tx_lock, flags);
1140		card->tx_dma_progress = 0;
1141		gelic_card_release_tx_chain(card, 0);
1142		/* kick outstanding tx descriptor if any */
1143		gelic_card_kick_txdma(card, card->tx_chain.tail);
1144		spin_unlock_irqrestore(&card->tx_lock, flags);
1145	}
1146
1147	/* ether port status changed */
1148	if (status & GELIC_CARD_PORT_STATUS_CHANGED)
1149		gelic_card_get_ether_port_status(card, 1);
1150
1151#ifdef CONFIG_GELIC_WIRELESS
1152	if (status & (GELIC_CARD_WLAN_EVENT_RECEIVED |
1153		      GELIC_CARD_WLAN_COMMAND_COMPLETED))
1154		gelic_wl_interrupt(card->netdev[GELIC_PORT_WIRELESS], status);
1155#endif
1156
1157	return IRQ_HANDLED;
1158}
1159
1160#ifdef CONFIG_NET_POLL_CONTROLLER
1161/**
1162 * gelic_net_poll_controller - artificial interrupt for netconsole etc.
1163 * @netdev: interface device structure
1164 *
1165 * see Documentation/networking/netconsole.rst
1166 */
1167void gelic_net_poll_controller(struct net_device *netdev)
1168{
1169	struct gelic_card *card = netdev_card(netdev);
1170
1171	gelic_card_set_irq_mask(card, 0);
1172	gelic_card_interrupt(netdev->irq, netdev);
1173	gelic_card_set_irq_mask(card, card->irq_mask);
1174}
1175#endif /* CONFIG_NET_POLL_CONTROLLER */
1176
1177/**
1178 * gelic_net_open - called upon ifconfig up
1179 * @netdev: interface device structure
1180 *
1181 * returns 0 on success, <0 on failure
1182 *
1183 * gelic_net_open allocates all the descriptors and memory needed for
1184 * operation, sets up multicast list and enables interrupts
1185 */
1186int gelic_net_open(struct net_device *netdev)
1187{
1188	struct gelic_card *card = netdev_card(netdev);
1189
1190	dev_dbg(ctodev(card), " -> %s %p\n", __func__, netdev);
1191
1192	gelic_card_up(card);
1193
1194	netif_start_queue(netdev);
1195	gelic_card_get_ether_port_status(card, 1);
1196
1197	dev_dbg(ctodev(card), " <- %s\n", __func__);
1198	return 0;
1199}
1200
1201void gelic_net_get_drvinfo(struct net_device *netdev,
1202			   struct ethtool_drvinfo *info)
1203{
1204	strscpy(info->driver, DRV_NAME, sizeof(info->driver));
1205	strscpy(info->version, DRV_VERSION, sizeof(info->version));
1206}
1207
1208static int gelic_ether_get_link_ksettings(struct net_device *netdev,
1209					  struct ethtool_link_ksettings *cmd)
1210{
1211	struct gelic_card *card = netdev_card(netdev);
1212	u32 supported, advertising;
1213
1214	gelic_card_get_ether_port_status(card, 0);
1215
1216	if (card->ether_port_status & GELIC_LV1_ETHER_FULL_DUPLEX)
1217		cmd->base.duplex = DUPLEX_FULL;
1218	else
1219		cmd->base.duplex = DUPLEX_HALF;
1220
1221	switch (card->ether_port_status & GELIC_LV1_ETHER_SPEED_MASK) {
1222	case GELIC_LV1_ETHER_SPEED_10:
1223		cmd->base.speed = SPEED_10;
1224		break;
1225	case GELIC_LV1_ETHER_SPEED_100:
1226		cmd->base.speed = SPEED_100;
1227		break;
1228	case GELIC_LV1_ETHER_SPEED_1000:
1229		cmd->base.speed = SPEED_1000;
1230		break;
1231	default:
1232		pr_info("%s: speed unknown\n", __func__);
1233		cmd->base.speed = SPEED_10;
1234		break;
1235	}
1236
1237	supported = SUPPORTED_TP | SUPPORTED_Autoneg |
1238			SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full |
1239			SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full |
1240			SUPPORTED_1000baseT_Full;
1241	advertising = supported;
1242	if (card->link_mode & GELIC_LV1_ETHER_AUTO_NEG) {
1243		cmd->base.autoneg = AUTONEG_ENABLE;
1244	} else {
1245		cmd->base.autoneg = AUTONEG_DISABLE;
1246		advertising &= ~ADVERTISED_Autoneg;
1247	}
1248	cmd->base.port = PORT_TP;
1249
1250	ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
1251						supported);
1252	ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
1253						advertising);
1254
1255	return 0;
1256}
1257
1258static int
1259gelic_ether_set_link_ksettings(struct net_device *netdev,
1260			       const struct ethtool_link_ksettings *cmd)
1261{
1262	struct gelic_card *card = netdev_card(netdev);
1263	u64 mode;
1264	int ret;
1265
1266	if (cmd->base.autoneg == AUTONEG_ENABLE) {
1267		mode = GELIC_LV1_ETHER_AUTO_NEG;
1268	} else {
1269		switch (cmd->base.speed) {
1270		case SPEED_10:
1271			mode = GELIC_LV1_ETHER_SPEED_10;
1272			break;
1273		case SPEED_100:
1274			mode = GELIC_LV1_ETHER_SPEED_100;
1275			break;
1276		case SPEED_1000:
1277			mode = GELIC_LV1_ETHER_SPEED_1000;
1278			break;
1279		default:
1280			return -EINVAL;
1281		}
1282		if (cmd->base.duplex == DUPLEX_FULL) {
1283			mode |= GELIC_LV1_ETHER_FULL_DUPLEX;
1284		} else if (cmd->base.speed == SPEED_1000) {
1285			pr_info("1000 half duplex is not supported.\n");
1286			return -EINVAL;
1287		}
1288	}
1289
1290	ret = gelic_card_set_link_mode(card, mode);
1291
1292	if (ret)
1293		return ret;
1294
1295	return 0;
1296}
1297
1298static void gelic_net_get_wol(struct net_device *netdev,
1299			      struct ethtool_wolinfo *wol)
1300{
1301	if (0 <= ps3_compare_firmware_version(2, 2, 0))
1302		wol->supported = WAKE_MAGIC;
1303	else
1304		wol->supported = 0;
1305
1306	wol->wolopts = ps3_sys_manager_get_wol() ? wol->supported : 0;
1307	memset(&wol->sopass, 0, sizeof(wol->sopass));
1308}
1309static int gelic_net_set_wol(struct net_device *netdev,
1310			     struct ethtool_wolinfo *wol)
1311{
1312	int status;
1313	struct gelic_card *card;
1314	u64 v1, v2;
1315
1316	if (ps3_compare_firmware_version(2, 2, 0) < 0 ||
1317	    !capable(CAP_NET_ADMIN))
1318		return -EPERM;
1319
1320	if (wol->wolopts & ~WAKE_MAGIC)
1321		return -EINVAL;
1322
1323	card = netdev_card(netdev);
1324	if (wol->wolopts & WAKE_MAGIC) {
1325		status = lv1_net_control(bus_id(card), dev_id(card),
1326					 GELIC_LV1_SET_WOL,
1327					 GELIC_LV1_WOL_MAGIC_PACKET,
1328					 0, GELIC_LV1_WOL_MP_ENABLE,
1329					 &v1, &v2);
1330		if (status) {
1331			pr_info("%s: enabling WOL failed %d\n", __func__,
1332				status);
1333			status = -EIO;
1334			goto done;
1335		}
1336		status = lv1_net_control(bus_id(card), dev_id(card),
1337					 GELIC_LV1_SET_WOL,
1338					 GELIC_LV1_WOL_ADD_MATCH_ADDR,
1339					 0, GELIC_LV1_WOL_MATCH_ALL,
1340					 &v1, &v2);
1341		if (!status)
1342			ps3_sys_manager_set_wol(1);
1343		else {
1344			pr_info("%s: enabling WOL filter failed %d\n",
1345				__func__, status);
1346			status = -EIO;
1347		}
1348	} else {
1349		status = lv1_net_control(bus_id(card), dev_id(card),
1350					 GELIC_LV1_SET_WOL,
1351					 GELIC_LV1_WOL_MAGIC_PACKET,
1352					 0, GELIC_LV1_WOL_MP_DISABLE,
1353					 &v1, &v2);
1354		if (status) {
1355			pr_info("%s: disabling WOL failed %d\n", __func__,
1356				status);
1357			status = -EIO;
1358			goto done;
1359		}
1360		status = lv1_net_control(bus_id(card), dev_id(card),
1361					 GELIC_LV1_SET_WOL,
1362					 GELIC_LV1_WOL_DELETE_MATCH_ADDR,
1363					 0, GELIC_LV1_WOL_MATCH_ALL,
1364					 &v1, &v2);
1365		if (!status)
1366			ps3_sys_manager_set_wol(0);
1367		else {
1368			pr_info("%s: removing WOL filter failed %d\n",
1369				__func__, status);
1370			status = -EIO;
1371		}
1372	}
1373done:
1374	return status;
1375}
1376
1377static const struct ethtool_ops gelic_ether_ethtool_ops = {
1378	.get_drvinfo	= gelic_net_get_drvinfo,
 
 
1379	.get_link	= ethtool_op_get_link,
1380	.get_wol	= gelic_net_get_wol,
1381	.set_wol	= gelic_net_set_wol,
1382	.get_link_ksettings = gelic_ether_get_link_ksettings,
1383	.set_link_ksettings = gelic_ether_set_link_ksettings,
1384};
1385
1386/**
1387 * gelic_net_tx_timeout_task - task scheduled by the watchdog timeout
1388 * function (to be called not under interrupt status)
1389 * @work: work is context of tx timout task
1390 *
1391 * called as task when tx hangs, resets interface (if interface is up)
1392 */
1393static void gelic_net_tx_timeout_task(struct work_struct *work)
1394{
1395	struct gelic_card *card =
1396		container_of(work, struct gelic_card, tx_timeout_task);
1397	struct net_device *netdev = card->netdev[GELIC_PORT_ETHERNET_0];
1398
1399	dev_info(ctodev(card), "%s:Timed out. Restarting...\n", __func__);
1400
1401	if (!(netdev->flags & IFF_UP))
1402		goto out;
1403
1404	netif_device_detach(netdev);
1405	gelic_net_stop(netdev);
1406
1407	gelic_net_open(netdev);
1408	netif_device_attach(netdev);
1409
1410out:
1411	atomic_dec(&card->tx_timeout_task_counter);
1412}
1413
1414/**
1415 * gelic_net_tx_timeout - called when the tx timeout watchdog kicks in.
1416 * @netdev: interface device structure
1417 * @txqueue: unused
1418 *
1419 * called, if tx hangs. Schedules a task that resets the interface
1420 */
1421void gelic_net_tx_timeout(struct net_device *netdev, unsigned int txqueue)
1422{
1423	struct gelic_card *card;
1424
1425	card = netdev_card(netdev);
1426	atomic_inc(&card->tx_timeout_task_counter);
1427	if (netdev->flags & IFF_UP)
1428		schedule_work(&card->tx_timeout_task);
1429	else
1430		atomic_dec(&card->tx_timeout_task_counter);
1431}
1432
1433static const struct net_device_ops gelic_netdevice_ops = {
1434	.ndo_open = gelic_net_open,
1435	.ndo_stop = gelic_net_stop,
1436	.ndo_start_xmit = gelic_net_xmit,
1437	.ndo_set_rx_mode = gelic_net_set_multi,
1438	.ndo_tx_timeout = gelic_net_tx_timeout,
1439	.ndo_set_mac_address = eth_mac_addr,
1440	.ndo_validate_addr = eth_validate_addr,
1441#ifdef CONFIG_NET_POLL_CONTROLLER
1442	.ndo_poll_controller = gelic_net_poll_controller,
1443#endif
1444};
1445
1446/**
1447 * gelic_ether_setup_netdev_ops - initialization of net_device operations
1448 * @netdev: net_device structure
1449 * @napi: napi structure
1450 *
1451 * fills out function pointers in the net_device structure
1452 */
1453static void gelic_ether_setup_netdev_ops(struct net_device *netdev,
1454					 struct napi_struct *napi)
1455{
1456	netdev->watchdog_timeo = GELIC_NET_WATCHDOG_TIMEOUT;
1457	/* NAPI */
1458	netif_napi_add(netdev, napi, gelic_net_poll);
1459	netdev->ethtool_ops = &gelic_ether_ethtool_ops;
1460	netdev->netdev_ops = &gelic_netdevice_ops;
1461}
1462
1463/**
1464 * gelic_ether_setup_netdev - initialization of net_device
1465 * @netdev: net_device structure
1466 * @card: card structure
1467 *
1468 * Returns 0 on success or <0 on failure
1469 *
1470 * gelic_ether_setup_netdev initializes the net_device structure
1471 * and register it.
1472 **/
1473int gelic_net_setup_netdev(struct net_device *netdev, struct gelic_card *card)
1474{
1475	int status;
1476	u64 v1, v2;
1477
1478	netdev->hw_features = NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
1479
1480	netdev->features = NETIF_F_IP_CSUM;
1481	if (GELIC_CARD_RX_CSUM_DEFAULT)
1482		netdev->features |= NETIF_F_RXCSUM;
1483
1484	status = lv1_net_control(bus_id(card), dev_id(card),
1485				 GELIC_LV1_GET_MAC_ADDRESS,
1486				 0, 0, 0, &v1, &v2);
1487	v1 <<= 16;
1488	if (status || !is_valid_ether_addr((u8 *)&v1)) {
1489		dev_info(ctodev(card),
1490			 "%s:lv1_net_control GET_MAC_ADDR failed %d\n",
1491			 __func__, status);
1492		return -EINVAL;
1493	}
1494	eth_hw_addr_set(netdev, (u8 *)&v1);
1495
1496	if (card->vlan_required) {
1497		netdev->hard_header_len += VLAN_HLEN;
1498		/*
1499		 * As vlan is internally used,
1500		 * we can not receive vlan packets
1501		 */
1502		netdev->features |= NETIF_F_VLAN_CHALLENGED;
1503	}
1504
1505	/* MTU range: 64 - 1518 */
1506	netdev->min_mtu = GELIC_NET_MIN_MTU;
1507	netdev->max_mtu = GELIC_NET_MAX_MTU;
1508
1509	status = register_netdev(netdev);
1510	if (status) {
1511		dev_err(ctodev(card), "%s:Couldn't register %s %d\n",
1512			__func__, netdev->name, status);
1513		return status;
1514	}
1515	dev_info(ctodev(card), "%s: MAC addr %pM\n",
1516		 netdev->name, netdev->dev_addr);
1517
1518	return 0;
1519}
1520
1521/**
1522 * gelic_alloc_card_net - allocates net_device and card structure
1523 *
1524 * returns the card structure or NULL in case of errors
1525 *
1526 * the card and net_device structures are linked to each other
1527 */
1528#define GELIC_ALIGN (32)
1529static struct gelic_card *gelic_alloc_card_net(struct net_device **netdev)
1530{
1531	struct gelic_card *card;
1532	struct gelic_port *port;
1533	void *p;
1534	size_t alloc_size;
1535	/*
1536	 * gelic requires dma descriptor is 32 bytes aligned and
1537	 * the hypervisor requires irq_status is 8 bytes aligned.
1538	 */
1539	BUILD_BUG_ON(offsetof(struct gelic_card, irq_status) % 8);
1540	BUILD_BUG_ON(offsetof(struct gelic_card, descr) % 32);
1541	alloc_size =
1542		sizeof(struct gelic_card) +
1543		sizeof(struct gelic_descr) * GELIC_NET_RX_DESCRIPTORS +
1544		sizeof(struct gelic_descr) * GELIC_NET_TX_DESCRIPTORS +
1545		GELIC_ALIGN - 1;
1546
1547	p  = kzalloc(alloc_size, GFP_KERNEL);
1548	if (!p)
1549		return NULL;
1550	card = PTR_ALIGN(p, GELIC_ALIGN);
1551	card->unalign = p;
1552
1553	/*
1554	 * alloc netdev
1555	 */
1556	*netdev = alloc_etherdev(sizeof(struct gelic_port));
1557	if (!*netdev) {
1558		kfree(card->unalign);
1559		return NULL;
1560	}
1561	port = netdev_priv(*netdev);
1562
1563	/* gelic_port */
1564	port->netdev = *netdev;
1565	port->card = card;
1566	port->type = GELIC_PORT_ETHERNET_0;
1567
1568	/* gelic_card */
1569	card->netdev[GELIC_PORT_ETHERNET_0] = *netdev;
1570
1571	INIT_WORK(&card->tx_timeout_task, gelic_net_tx_timeout_task);
1572	init_waitqueue_head(&card->waitq);
1573	atomic_set(&card->tx_timeout_task_counter, 0);
1574	mutex_init(&card->updown_lock);
1575	atomic_set(&card->users, 0);
1576
1577	return card;
1578}
1579
1580static void gelic_card_get_vlan_info(struct gelic_card *card)
1581{
1582	u64 v1, v2;
1583	int status;
1584	unsigned int i;
1585	struct {
1586		int tx;
1587		int rx;
1588	} vlan_id_ix[2] = {
1589		[GELIC_PORT_ETHERNET_0] = {
1590			.tx = GELIC_LV1_VLAN_TX_ETHERNET_0,
1591			.rx = GELIC_LV1_VLAN_RX_ETHERNET_0
1592		},
1593		[GELIC_PORT_WIRELESS] = {
1594			.tx = GELIC_LV1_VLAN_TX_WIRELESS,
1595			.rx = GELIC_LV1_VLAN_RX_WIRELESS
1596		}
1597	};
1598
1599	for (i = 0; i < ARRAY_SIZE(vlan_id_ix); i++) {
1600		/* tx tag */
1601		status = lv1_net_control(bus_id(card), dev_id(card),
1602					 GELIC_LV1_GET_VLAN_ID,
1603					 vlan_id_ix[i].tx,
1604					 0, 0, &v1, &v2);
1605		if (status || !v1) {
1606			if (status != LV1_NO_ENTRY)
1607				dev_dbg(ctodev(card),
1608					"get vlan id for tx(%d) failed(%d)\n",
1609					vlan_id_ix[i].tx, status);
1610			card->vlan[i].tx = 0;
1611			card->vlan[i].rx = 0;
1612			continue;
1613		}
1614		card->vlan[i].tx = (u16)v1;
1615
1616		/* rx tag */
1617		status = lv1_net_control(bus_id(card), dev_id(card),
1618					 GELIC_LV1_GET_VLAN_ID,
1619					 vlan_id_ix[i].rx,
1620					 0, 0, &v1, &v2);
1621		if (status || !v1) {
1622			if (status != LV1_NO_ENTRY)
1623				dev_info(ctodev(card),
1624					 "get vlan id for rx(%d) failed(%d)\n",
1625					 vlan_id_ix[i].rx, status);
1626			card->vlan[i].tx = 0;
1627			card->vlan[i].rx = 0;
1628			continue;
1629		}
1630		card->vlan[i].rx = (u16)v1;
1631
1632		dev_dbg(ctodev(card), "vlan_id[%d] tx=%02x rx=%02x\n",
1633			i, card->vlan[i].tx, card->vlan[i].rx);
1634	}
1635
1636	if (card->vlan[GELIC_PORT_ETHERNET_0].tx) {
1637		BUG_ON(!card->vlan[GELIC_PORT_WIRELESS].tx);
1638		card->vlan_required = 1;
1639	} else
1640		card->vlan_required = 0;
1641
1642	/* check wirelss capable firmware */
1643	if (ps3_compare_firmware_version(1, 6, 0) < 0) {
1644		card->vlan[GELIC_PORT_WIRELESS].tx = 0;
1645		card->vlan[GELIC_PORT_WIRELESS].rx = 0;
1646	}
1647
1648	dev_info(ctodev(card), "internal vlan %s\n",
1649		 card->vlan_required? "enabled" : "disabled");
1650}
1651/*
1652 * ps3_gelic_driver_probe - add a device to the control of this driver
1653 */
1654static int ps3_gelic_driver_probe(struct ps3_system_bus_device *dev)
1655{
1656	struct gelic_card *card;
1657	struct net_device *netdev;
1658	int result;
1659
1660	pr_debug("%s: called\n", __func__);
1661
1662	udbg_shutdown_ps3gelic();
1663
1664	result = ps3_open_hv_device(dev);
1665
1666	if (result) {
1667		dev_dbg(&dev->core, "%s:ps3_open_hv_device failed\n",
1668			__func__);
1669		goto fail_open;
1670	}
1671
1672	result = ps3_dma_region_create(dev->d_region);
1673
1674	if (result) {
1675		dev_dbg(&dev->core, "%s:ps3_dma_region_create failed(%d)\n",
1676			__func__, result);
1677		BUG_ON("check region type");
1678		goto fail_dma_region;
1679	}
1680
1681	/* alloc card/netdevice */
1682	card = gelic_alloc_card_net(&netdev);
1683	if (!card) {
1684		dev_info(&dev->core, "%s:gelic_net_alloc_card failed\n",
1685			 __func__);
1686		result = -ENOMEM;
1687		goto fail_alloc_card;
1688	}
1689	ps3_system_bus_set_drvdata(dev, card);
1690	card->dev = dev;
1691
1692	/* get internal vlan info */
1693	gelic_card_get_vlan_info(card);
1694
1695	card->link_mode = GELIC_LV1_ETHER_AUTO_NEG;
1696
1697	/* setup interrupt */
1698	result = lv1_net_set_interrupt_status_indicator(bus_id(card),
1699							dev_id(card),
1700		ps3_mm_phys_to_lpar(__pa(&card->irq_status)),
1701		0);
1702
1703	if (result) {
1704		dev_dbg(&dev->core,
1705			"%s:set_interrupt_status_indicator failed: %s\n",
1706			__func__, ps3_result(result));
1707		result = -EIO;
1708		goto fail_status_indicator;
1709	}
1710
1711	result = ps3_sb_event_receive_port_setup(dev, PS3_BINDING_CPU_ANY,
1712		&card->irq);
1713
1714	if (result) {
1715		dev_info(ctodev(card),
1716			 "%s:gelic_net_open_device failed (%d)\n",
1717			 __func__, result);
1718		result = -EPERM;
1719		goto fail_alloc_irq;
1720	}
1721	result = request_irq(card->irq, gelic_card_interrupt,
1722			     0, netdev->name, card);
1723
1724	if (result) {
1725		dev_info(ctodev(card), "%s:request_irq failed (%d)\n",
1726			__func__, result);
1727		goto fail_request_irq;
1728	}
1729
1730	/* setup card structure */
1731	card->irq_mask = GELIC_CARD_RXINT | GELIC_CARD_TXINT |
1732		GELIC_CARD_PORT_STATUS_CHANGED;
1733
1734
1735	result = gelic_card_init_chain(card, &card->tx_chain,
1736				       card->descr, GELIC_NET_TX_DESCRIPTORS);
1737	if (result)
1738		goto fail_alloc_tx;
1739	result = gelic_card_init_chain(card, &card->rx_chain,
1740				       card->descr + GELIC_NET_TX_DESCRIPTORS,
1741				       GELIC_NET_RX_DESCRIPTORS);
1742	if (result)
1743		goto fail_alloc_rx;
1744
1745	/* head of chain */
1746	card->tx_top = card->tx_chain.head;
1747	card->rx_top = card->rx_chain.head;
1748	dev_dbg(ctodev(card), "descr rx %p, tx %p, size %#lx, num %#x\n",
1749		card->rx_top, card->tx_top, sizeof(struct gelic_descr),
1750		GELIC_NET_RX_DESCRIPTORS);
1751	/* allocate rx skbs */
1752	result = gelic_card_alloc_rx_skbs(card);
1753	if (result)
1754		goto fail_alloc_skbs;
1755
1756	spin_lock_init(&card->tx_lock);
1757	card->tx_dma_progress = 0;
1758
1759	/* setup net_device structure */
1760	netdev->irq = card->irq;
1761	SET_NETDEV_DEV(netdev, &card->dev->core);
1762	gelic_ether_setup_netdev_ops(netdev, &card->napi);
1763	result = gelic_net_setup_netdev(netdev, card);
1764	if (result) {
1765		dev_dbg(&dev->core, "%s: setup_netdev failed %d\n",
1766			__func__, result);
1767		goto fail_setup_netdev;
1768	}
1769
1770#ifdef CONFIG_GELIC_WIRELESS
1771	result = gelic_wl_driver_probe(card);
1772	if (result) {
1773		dev_dbg(&dev->core, "%s: WL init failed\n", __func__);
1774		goto fail_setup_netdev;
1775	}
1776#endif
1777	pr_debug("%s: done\n", __func__);
1778	return 0;
1779
1780fail_setup_netdev:
1781fail_alloc_skbs:
1782	gelic_card_free_chain(card, card->rx_chain.head);
1783fail_alloc_rx:
1784	gelic_card_free_chain(card, card->tx_chain.head);
1785fail_alloc_tx:
1786	free_irq(card->irq, card);
1787	netdev->irq = 0;
1788fail_request_irq:
1789	ps3_sb_event_receive_port_destroy(dev, card->irq);
1790fail_alloc_irq:
1791	lv1_net_set_interrupt_status_indicator(bus_id(card),
1792					       bus_id(card),
1793					       0, 0);
1794fail_status_indicator:
1795	ps3_system_bus_set_drvdata(dev, NULL);
1796	kfree(netdev_card(netdev)->unalign);
1797	free_netdev(netdev);
1798fail_alloc_card:
1799	ps3_dma_region_free(dev->d_region);
1800fail_dma_region:
1801	ps3_close_hv_device(dev);
1802fail_open:
1803	return result;
1804}
1805
1806/*
1807 * ps3_gelic_driver_remove - remove a device from the control of this driver
1808 */
1809
1810static void ps3_gelic_driver_remove(struct ps3_system_bus_device *dev)
1811{
1812	struct gelic_card *card = ps3_system_bus_get_drvdata(dev);
1813	struct net_device *netdev0;
1814	pr_debug("%s: called\n", __func__);
1815
1816	/* set auto-negotiation */
1817	gelic_card_set_link_mode(card, GELIC_LV1_ETHER_AUTO_NEG);
1818
1819#ifdef CONFIG_GELIC_WIRELESS
1820	gelic_wl_driver_remove(card);
1821#endif
1822	/* stop interrupt */
1823	gelic_card_set_irq_mask(card, 0);
1824
1825	/* turn off DMA, force end */
1826	gelic_card_disable_rxdmac(card);
1827	gelic_card_disable_txdmac(card);
1828
1829	/* release chains */
1830	gelic_card_release_tx_chain(card, 1);
1831	gelic_card_release_rx_chain(card);
1832
1833	gelic_card_free_chain(card, card->tx_top);
1834	gelic_card_free_chain(card, card->rx_top);
1835
1836	netdev0 = card->netdev[GELIC_PORT_ETHERNET_0];
1837	/* disconnect event port */
1838	free_irq(card->irq, card);
1839	netdev0->irq = 0;
1840	ps3_sb_event_receive_port_destroy(card->dev, card->irq);
1841
1842	wait_event(card->waitq,
1843		   atomic_read(&card->tx_timeout_task_counter) == 0);
1844
1845	lv1_net_set_interrupt_status_indicator(bus_id(card), dev_id(card),
1846					       0 , 0);
1847
1848	unregister_netdev(netdev0);
1849	kfree(netdev_card(netdev0)->unalign);
1850	free_netdev(netdev0);
1851
1852	ps3_system_bus_set_drvdata(dev, NULL);
1853
1854	ps3_dma_region_free(dev->d_region);
1855
1856	ps3_close_hv_device(dev);
1857
1858	pr_debug("%s: done\n", __func__);
 
1859}
1860
1861static struct ps3_system_bus_driver ps3_gelic_driver = {
1862	.match_id = PS3_MATCH_ID_GELIC,
1863	.probe = ps3_gelic_driver_probe,
1864	.remove = ps3_gelic_driver_remove,
1865	.shutdown = ps3_gelic_driver_remove,
1866	.core.name = "ps3_gelic_driver",
1867	.core.owner = THIS_MODULE,
1868};
1869
1870static int __init ps3_gelic_driver_init (void)
1871{
1872	return firmware_has_feature(FW_FEATURE_PS3_LV1)
1873		? ps3_system_bus_driver_register(&ps3_gelic_driver)
1874		: -ENODEV;
1875}
1876
1877static void __exit ps3_gelic_driver_exit (void)
1878{
1879	ps3_system_bus_driver_unregister(&ps3_gelic_driver);
1880}
1881
1882module_init(ps3_gelic_driver_init);
1883module_exit(ps3_gelic_driver_exit);
1884
1885MODULE_ALIAS(PS3_MODULE_ALIAS_GELIC);
1886
v4.10.11
 
   1/*
   2 *  PS3 gelic network driver.
   3 *
   4 * Copyright (C) 2007 Sony Computer Entertainment Inc.
   5 * Copyright 2006, 2007 Sony Corporation
   6 *
   7 * This file is based on: spider_net.c
   8 *
   9 * (C) Copyright IBM Corp. 2005
  10 *
  11 * Authors : Utz Bacher <utz.bacher@de.ibm.com>
  12 *           Jens Osterkamp <Jens.Osterkamp@de.ibm.com>
  13 *
  14 * This program is free software; you can redistribute it and/or modify
  15 * it under the terms of the GNU General Public License as published by
  16 * the Free Software Foundation; either version 2, or (at your option)
  17 * any later version.
  18 *
  19 * This program is distributed in the hope that it will be useful,
  20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  22 * GNU General Public License for more details.
  23 *
  24 * You should have received a copy of the GNU General Public License
  25 * along with this program; if not, write to the Free Software
  26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  27 */
  28
  29#undef DEBUG
  30
  31#include <linux/interrupt.h>
  32#include <linux/kernel.h>
  33#include <linux/module.h>
  34#include <linux/slab.h>
  35
  36#include <linux/etherdevice.h>
  37#include <linux/ethtool.h>
  38#include <linux/if_vlan.h>
  39
  40#include <linux/in.h>
  41#include <linux/ip.h>
  42#include <linux/tcp.h>
  43
  44#include <linux/dma-mapping.h>
  45#include <net/checksum.h>
  46#include <asm/firmware.h>
  47#include <asm/ps3.h>
  48#include <asm/lv1call.h>
  49
  50#include "ps3_gelic_net.h"
  51#include "ps3_gelic_wireless.h"
  52
  53#define DRV_NAME "Gelic Network Driver"
  54#define DRV_VERSION "2.0"
  55
  56MODULE_AUTHOR("SCE Inc.");
  57MODULE_DESCRIPTION("Gelic Network driver");
  58MODULE_LICENSE("GPL");
  59
  60
  61/* set irq_mask */
  62int gelic_card_set_irq_mask(struct gelic_card *card, u64 mask)
  63{
  64	int status;
  65
  66	status = lv1_net_set_interrupt_mask(bus_id(card), dev_id(card),
  67					    mask, 0);
  68	if (status)
  69		dev_info(ctodev(card),
  70			 "%s failed %d\n", __func__, status);
  71	return status;
  72}
  73
  74static void gelic_card_rx_irq_on(struct gelic_card *card)
  75{
  76	card->irq_mask |= GELIC_CARD_RXINT;
  77	gelic_card_set_irq_mask(card, card->irq_mask);
  78}
  79static void gelic_card_rx_irq_off(struct gelic_card *card)
  80{
  81	card->irq_mask &= ~GELIC_CARD_RXINT;
  82	gelic_card_set_irq_mask(card, card->irq_mask);
  83}
  84
  85static void gelic_card_get_ether_port_status(struct gelic_card *card,
  86					     int inform)
  87{
  88	u64 v2;
  89	struct net_device *ether_netdev;
  90
  91	lv1_net_control(bus_id(card), dev_id(card),
  92			GELIC_LV1_GET_ETH_PORT_STATUS,
  93			GELIC_LV1_VLAN_TX_ETHERNET_0, 0, 0,
  94			&card->ether_port_status, &v2);
  95
  96	if (inform) {
  97		ether_netdev = card->netdev[GELIC_PORT_ETHERNET_0];
  98		if (card->ether_port_status & GELIC_LV1_ETHER_LINK_UP)
  99			netif_carrier_on(ether_netdev);
 100		else
 101			netif_carrier_off(ether_netdev);
 102	}
 103}
 104
 105/**
 106 * gelic_descr_get_status -- returns the status of a descriptor
 107 * @descr: descriptor to look at
 108 *
 109 * returns the status as in the dmac_cmd_status field of the descriptor
 110 */
 111static enum gelic_descr_dma_status
 112gelic_descr_get_status(struct gelic_descr *descr)
 113{
 114	return be32_to_cpu(descr->dmac_cmd_status) & GELIC_DESCR_DMA_STAT_MASK;
 
 115}
 116
 117static int gelic_card_set_link_mode(struct gelic_card *card, int mode)
 118{
 119	int status;
 120	u64 v1, v2;
 121
 122	status = lv1_net_control(bus_id(card), dev_id(card),
 123				 GELIC_LV1_SET_NEGOTIATION_MODE,
 124				 GELIC_LV1_PHY_ETHERNET_0, mode, 0, &v1, &v2);
 125	if (status) {
 126		pr_info("%s: failed setting negotiation mode %d\n", __func__,
 127			status);
 128		return -EBUSY;
 129	}
 130
 131	card->link_mode = mode;
 132	return 0;
 133}
 134
 135/**
 136 * gelic_card_disable_txdmac - disables the transmit DMA controller
 137 * @card: card structure
 138 *
 139 * gelic_card_disable_txdmac terminates processing on the DMA controller by
 140 * turing off DMA and issuing a force end
 141 */
 142static void gelic_card_disable_txdmac(struct gelic_card *card)
 143{
 144	int status;
 145
 146	/* this hvc blocks until the DMA in progress really stopped */
 147	status = lv1_net_stop_tx_dma(bus_id(card), dev_id(card));
 148	if (status)
 149		dev_err(ctodev(card),
 150			"lv1_net_stop_tx_dma failed, status=%d\n", status);
 151}
 152
 153/**
 154 * gelic_card_enable_rxdmac - enables the receive DMA controller
 155 * @card: card structure
 156 *
 157 * gelic_card_enable_rxdmac enables the DMA controller by setting RX_DMA_EN
 158 * in the GDADMACCNTR register
 159 */
 160static void gelic_card_enable_rxdmac(struct gelic_card *card)
 161{
 162	int status;
 163
 164#ifdef DEBUG
 165	if (gelic_descr_get_status(card->rx_chain.head) !=
 166	    GELIC_DESCR_DMA_CARDOWNED) {
 167		printk(KERN_ERR "%s: status=%x\n", __func__,
 168		       be32_to_cpu(card->rx_chain.head->dmac_cmd_status));
 169		printk(KERN_ERR "%s: nextphy=%x\n", __func__,
 170		       be32_to_cpu(card->rx_chain.head->next_descr_addr));
 171		printk(KERN_ERR "%s: head=%p\n", __func__,
 172		       card->rx_chain.head);
 173	}
 174#endif
 175	status = lv1_net_start_rx_dma(bus_id(card), dev_id(card),
 176				card->rx_chain.head->bus_addr, 0);
 177	if (status)
 178		dev_info(ctodev(card),
 179			 "lv1_net_start_rx_dma failed, status=%d\n", status);
 180}
 181
 182/**
 183 * gelic_card_disable_rxdmac - disables the receive DMA controller
 184 * @card: card structure
 185 *
 186 * gelic_card_disable_rxdmac terminates processing on the DMA controller by
 187 * turing off DMA and issuing a force end
 188 */
 189static void gelic_card_disable_rxdmac(struct gelic_card *card)
 190{
 191	int status;
 192
 193	/* this hvc blocks until the DMA in progress really stopped */
 194	status = lv1_net_stop_rx_dma(bus_id(card), dev_id(card));
 195	if (status)
 196		dev_err(ctodev(card),
 197			"lv1_net_stop_rx_dma failed, %d\n", status);
 198}
 199
 200/**
 201 * gelic_descr_set_status -- sets the status of a descriptor
 202 * @descr: descriptor to change
 203 * @status: status to set in the descriptor
 204 *
 205 * changes the status to the specified value. Doesn't change other bits
 206 * in the status
 207 */
 208static void gelic_descr_set_status(struct gelic_descr *descr,
 209				   enum gelic_descr_dma_status status)
 210{
 211	descr->dmac_cmd_status = cpu_to_be32(status |
 212			(be32_to_cpu(descr->dmac_cmd_status) &
 213			 ~GELIC_DESCR_DMA_STAT_MASK));
 214	/*
 215	 * dma_cmd_status field is used to indicate whether the descriptor
 216	 * is valid or not.
 217	 * Usually caller of this function wants to inform that to the
 218	 * hardware, so we assure here the hardware sees the change.
 219	 */
 220	wmb();
 221}
 222
 223/**
 224 * gelic_card_reset_chain - reset status of a descriptor chain
 225 * @card: card structure
 226 * @chain: address of chain
 227 * @start_descr: address of descriptor array
 228 *
 229 * Reset the status of dma descriptors to ready state
 230 * and re-initialize the hardware chain for later use
 231 */
 232static void gelic_card_reset_chain(struct gelic_card *card,
 233				   struct gelic_descr_chain *chain,
 234				   struct gelic_descr *start_descr)
 235{
 236	struct gelic_descr *descr;
 237
 238	for (descr = start_descr; start_descr != descr->next; descr++) {
 239		gelic_descr_set_status(descr, GELIC_DESCR_DMA_CARDOWNED);
 240		descr->next_descr_addr = cpu_to_be32(descr->next->bus_addr);
 
 241	}
 242
 243	chain->head = start_descr;
 244	chain->tail = (descr - 1);
 245
 246	(descr - 1)->next_descr_addr = 0;
 247}
 248
 249void gelic_card_up(struct gelic_card *card)
 250{
 251	pr_debug("%s: called\n", __func__);
 252	mutex_lock(&card->updown_lock);
 253	if (atomic_inc_return(&card->users) == 1) {
 254		pr_debug("%s: real do\n", __func__);
 255		/* enable irq */
 256		gelic_card_set_irq_mask(card, card->irq_mask);
 257		/* start rx */
 258		gelic_card_enable_rxdmac(card);
 259
 260		napi_enable(&card->napi);
 261	}
 262	mutex_unlock(&card->updown_lock);
 263	pr_debug("%s: done\n", __func__);
 264}
 265
 266void gelic_card_down(struct gelic_card *card)
 267{
 268	u64 mask;
 269	pr_debug("%s: called\n", __func__);
 270	mutex_lock(&card->updown_lock);
 271	if (atomic_dec_if_positive(&card->users) == 0) {
 272		pr_debug("%s: real do\n", __func__);
 273		napi_disable(&card->napi);
 274		/*
 275		 * Disable irq. Wireless interrupts will
 276		 * be disabled later if any
 277		 */
 278		mask = card->irq_mask & (GELIC_CARD_WLAN_EVENT_RECEIVED |
 279					 GELIC_CARD_WLAN_COMMAND_COMPLETED);
 280		gelic_card_set_irq_mask(card, mask);
 281		/* stop rx */
 282		gelic_card_disable_rxdmac(card);
 283		gelic_card_reset_chain(card, &card->rx_chain,
 284				       card->descr + GELIC_NET_TX_DESCRIPTORS);
 285		/* stop tx */
 286		gelic_card_disable_txdmac(card);
 287	}
 288	mutex_unlock(&card->updown_lock);
 289	pr_debug("%s: done\n", __func__);
 290}
 291
 292/**
 293 * gelic_card_free_chain - free descriptor chain
 294 * @card: card structure
 295 * @descr_in: address of desc
 296 */
 297static void gelic_card_free_chain(struct gelic_card *card,
 298				  struct gelic_descr *descr_in)
 299{
 300	struct gelic_descr *descr;
 301
 302	for (descr = descr_in; descr && descr->bus_addr; descr = descr->next) {
 303		dma_unmap_single(ctodev(card), descr->bus_addr,
 304				 GELIC_DESCR_SIZE, DMA_BIDIRECTIONAL);
 305		descr->bus_addr = 0;
 
 
 306	}
 307}
 308
 309/**
 310 * gelic_card_init_chain - links descriptor chain
 311 * @card: card structure
 312 * @chain: address of chain
 313 * @start_descr: address of descriptor array
 314 * @no: number of descriptors
 315 *
 316 * we manage a circular list that mirrors the hardware structure,
 317 * except that the hardware uses bus addresses.
 318 *
 319 * returns 0 on success, <0 on failure
 320 */
 321static int gelic_card_init_chain(struct gelic_card *card,
 322				 struct gelic_descr_chain *chain,
 323				 struct gelic_descr *start_descr, int no)
 324{
 325	int i;
 326	struct gelic_descr *descr;
 327
 328	descr = start_descr;
 329	memset(descr, 0, sizeof(*descr) * no);
 330
 331	/* set up the hardware pointers in each descriptor */
 332	for (i = 0; i < no; i++, descr++) {
 333		gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
 334		descr->bus_addr =
 335			dma_map_single(ctodev(card), descr,
 336				       GELIC_DESCR_SIZE,
 337				       DMA_BIDIRECTIONAL);
 338
 339		if (!descr->bus_addr)
 340			goto iommu_error;
 
 
 
 
 
 
 
 
 
 
 341
 342		descr->next = descr + 1;
 343		descr->prev = descr - 1;
 344	}
 345	/* make them as ring */
 346	(descr - 1)->next = start_descr;
 347	start_descr->prev = (descr - 1);
 348
 349	/* chain bus addr of hw descriptor */
 350	descr = start_descr;
 351	for (i = 0; i < no; i++, descr++) {
 352		descr->next_descr_addr = cpu_to_be32(descr->next->bus_addr);
 
 353	}
 354
 355	chain->head = start_descr;
 356	chain->tail = start_descr;
 357
 358	/* do not chain last hw descriptor */
 359	(descr - 1)->next_descr_addr = 0;
 360
 361	return 0;
 362
 363iommu_error:
 364	for (i--, descr--; 0 <= i; i--, descr--)
 365		if (descr->bus_addr)
 366			dma_unmap_single(ctodev(card), descr->bus_addr,
 367					 GELIC_DESCR_SIZE,
 368					 DMA_BIDIRECTIONAL);
 369	return -ENOMEM;
 370}
 371
 372/**
 373 * gelic_descr_prepare_rx - reinitializes a rx descriptor
 374 * @card: card structure
 375 * @descr: descriptor to re-init
 376 *
 377 * return 0 on success, <0 on failure
 378 *
 379 * allocates a new rx skb, iommu-maps it and attaches it to the descriptor.
 380 * Activate the descriptor state-wise
 
 
 
 381 */
 382static int gelic_descr_prepare_rx(struct gelic_card *card,
 383				  struct gelic_descr *descr)
 384{
 
 
 
 
 385	int offset;
 386	unsigned int bufsize;
 387
 388	if (gelic_descr_get_status(descr) !=  GELIC_DESCR_DMA_NOT_IN_USE)
 389		dev_info(ctodev(card), "%s: ERROR status\n", __func__);
 390	/* we need to round up the buffer size to a multiple of 128 */
 391	bufsize = ALIGN(GELIC_NET_MAX_MTU, GELIC_NET_RXBUF_ALIGN);
 392
 393	/* and we need to have it 128 byte aligned, therefore we allocate a
 394	 * bit more */
 395	descr->skb = dev_alloc_skb(bufsize + GELIC_NET_RXBUF_ALIGN - 1);
 
 
 
 
 
 396	if (!descr->skb) {
 397		descr->buf_addr = 0; /* tell DMAC don't touch memory */
 398		dev_info(ctodev(card),
 399			 "%s:allocate skb failed !!\n", __func__);
 400		return -ENOMEM;
 401	}
 402	descr->buf_size = cpu_to_be32(bufsize);
 403	descr->dmac_cmd_status = 0;
 404	descr->result_size = 0;
 405	descr->valid_size = 0;
 406	descr->data_error = 0;
 407
 408	offset = ((unsigned long)descr->skb->data) &
 409		(GELIC_NET_RXBUF_ALIGN - 1);
 410	if (offset)
 411		skb_reserve(descr->skb, GELIC_NET_RXBUF_ALIGN - offset);
 412	/* io-mmu-map the skb */
 413	descr->buf_addr = cpu_to_be32(dma_map_single(ctodev(card),
 414						     descr->skb->data,
 415						     GELIC_NET_MAX_MTU,
 416						     DMA_FROM_DEVICE));
 417	if (!descr->buf_addr) {
 418		dev_kfree_skb_any(descr->skb);
 419		descr->skb = NULL;
 420		dev_info(ctodev(card),
 421			 "%s:Could not iommu-map rx buffer\n", __func__);
 422		gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
 423		return -ENOMEM;
 424	} else {
 425		gelic_descr_set_status(descr, GELIC_DESCR_DMA_CARDOWNED);
 426		return 0;
 427	}
 
 
 
 
 
 
 
 428}
 429
 430/**
 431 * gelic_card_release_rx_chain - free all skb of rx descr
 432 * @card: card structure
 433 *
 434 */
 435static void gelic_card_release_rx_chain(struct gelic_card *card)
 436{
 437	struct gelic_descr *descr = card->rx_chain.head;
 438
 439	do {
 440		if (descr->skb) {
 441			dma_unmap_single(ctodev(card),
 442					 be32_to_cpu(descr->buf_addr),
 443					 descr->skb->len,
 444					 DMA_FROM_DEVICE);
 445			descr->buf_addr = 0;
 
 446			dev_kfree_skb_any(descr->skb);
 447			descr->skb = NULL;
 448			gelic_descr_set_status(descr,
 449					       GELIC_DESCR_DMA_NOT_IN_USE);
 450		}
 451		descr = descr->next;
 452	} while (descr != card->rx_chain.head);
 453}
 454
 455/**
 456 * gelic_card_fill_rx_chain - fills descriptors/skbs in the rx chains
 457 * @card: card structure
 458 *
 459 * fills all descriptors in the rx chain: allocates skbs
 460 * and iommu-maps them.
 461 * returns 0 on success, < 0 on failure
 462 */
 463static int gelic_card_fill_rx_chain(struct gelic_card *card)
 464{
 465	struct gelic_descr *descr = card->rx_chain.head;
 466	int ret;
 467
 468	do {
 469		if (!descr->skb) {
 470			ret = gelic_descr_prepare_rx(card, descr);
 471			if (ret)
 472				goto rewind;
 473		}
 474		descr = descr->next;
 475	} while (descr != card->rx_chain.head);
 476
 477	return 0;
 478rewind:
 479	gelic_card_release_rx_chain(card);
 480	return ret;
 481}
 482
 483/**
 484 * gelic_card_alloc_rx_skbs - allocates rx skbs in rx descriptor chains
 485 * @card: card structure
 486 *
 487 * returns 0 on success, < 0 on failure
 488 */
 489static int gelic_card_alloc_rx_skbs(struct gelic_card *card)
 490{
 491	struct gelic_descr_chain *chain;
 492	int ret;
 493	chain = &card->rx_chain;
 494	ret = gelic_card_fill_rx_chain(card);
 495	chain->tail = card->rx_top->prev; /* point to the last */
 496	return ret;
 497}
 498
 499/**
 500 * gelic_descr_release_tx - processes a used tx descriptor
 501 * @card: card structure
 502 * @descr: descriptor to release
 503 *
 504 * releases a used tx descriptor (unmapping, freeing of skb)
 505 */
 506static void gelic_descr_release_tx(struct gelic_card *card,
 507				       struct gelic_descr *descr)
 508{
 509	struct sk_buff *skb = descr->skb;
 510
 511	BUG_ON(!(be32_to_cpu(descr->data_status) & GELIC_DESCR_TX_TAIL));
 512
 513	dma_unmap_single(ctodev(card), be32_to_cpu(descr->buf_addr), skb->len,
 514			 DMA_TO_DEVICE);
 
 515	dev_kfree_skb_any(skb);
 516
 517	descr->buf_addr = 0;
 518	descr->buf_size = 0;
 519	descr->next_descr_addr = 0;
 520	descr->result_size = 0;
 521	descr->valid_size = 0;
 522	descr->data_status = 0;
 523	descr->data_error = 0;
 524	descr->skb = NULL;
 525
 526	/* set descr status */
 527	gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
 528}
 529
 530static void gelic_card_stop_queues(struct gelic_card *card)
 531{
 532	netif_stop_queue(card->netdev[GELIC_PORT_ETHERNET_0]);
 533
 534	if (card->netdev[GELIC_PORT_WIRELESS])
 535		netif_stop_queue(card->netdev[GELIC_PORT_WIRELESS]);
 536}
 537static void gelic_card_wake_queues(struct gelic_card *card)
 538{
 539	netif_wake_queue(card->netdev[GELIC_PORT_ETHERNET_0]);
 540
 541	if (card->netdev[GELIC_PORT_WIRELESS])
 542		netif_wake_queue(card->netdev[GELIC_PORT_WIRELESS]);
 543}
 544/**
 545 * gelic_card_release_tx_chain - processes sent tx descriptors
 546 * @card: adapter structure
 547 * @stop: net_stop sequence
 548 *
 549 * releases the tx descriptors that gelic has finished with
 550 */
 551static void gelic_card_release_tx_chain(struct gelic_card *card, int stop)
 552{
 553	struct gelic_descr_chain *tx_chain;
 554	enum gelic_descr_dma_status status;
 555	struct net_device *netdev;
 556	int release = 0;
 557
 558	for (tx_chain = &card->tx_chain;
 559	     tx_chain->head != tx_chain->tail && tx_chain->tail;
 560	     tx_chain->tail = tx_chain->tail->next) {
 561		status = gelic_descr_get_status(tx_chain->tail);
 562		netdev = tx_chain->tail->skb->dev;
 563		switch (status) {
 564		case GELIC_DESCR_DMA_RESPONSE_ERROR:
 565		case GELIC_DESCR_DMA_PROTECTION_ERROR:
 566		case GELIC_DESCR_DMA_FORCE_END:
 567			if (printk_ratelimit())
 568				dev_info(ctodev(card),
 569					 "%s: forcing end of tx descriptor " \
 570					 "with status %x\n",
 571					 __func__, status);
 572			netdev->stats.tx_dropped++;
 573			break;
 574
 575		case GELIC_DESCR_DMA_COMPLETE:
 576			if (tx_chain->tail->skb) {
 577				netdev->stats.tx_packets++;
 578				netdev->stats.tx_bytes +=
 579					tx_chain->tail->skb->len;
 580			}
 581			break;
 582
 583		case GELIC_DESCR_DMA_CARDOWNED:
 584			/* pending tx request */
 585		default:
 586			/* any other value (== GELIC_DESCR_DMA_NOT_IN_USE) */
 587			if (!stop)
 588				goto out;
 589		}
 590		gelic_descr_release_tx(card, tx_chain->tail);
 591		release ++;
 592	}
 593out:
 594	if (!stop && release)
 595		gelic_card_wake_queues(card);
 596}
 597
 598/**
 599 * gelic_net_set_multi - sets multicast addresses and promisc flags
 600 * @netdev: interface device structure
 601 *
 602 * gelic_net_set_multi configures multicast addresses as needed for the
 603 * netdev interface. It also sets up multicast, allmulti and promisc
 604 * flags appropriately
 605 */
 606void gelic_net_set_multi(struct net_device *netdev)
 607{
 608	struct gelic_card *card = netdev_card(netdev);
 609	struct netdev_hw_addr *ha;
 610	unsigned int i;
 611	uint8_t *p;
 612	u64 addr;
 613	int status;
 614
 615	/* clear all multicast address */
 616	status = lv1_net_remove_multicast_address(bus_id(card), dev_id(card),
 617						  0, 1);
 618	if (status)
 619		dev_err(ctodev(card),
 620			"lv1_net_remove_multicast_address failed %d\n",
 621			status);
 622	/* set broadcast address */
 623	status = lv1_net_add_multicast_address(bus_id(card), dev_id(card),
 624					       GELIC_NET_BROADCAST_ADDR, 0);
 625	if (status)
 626		dev_err(ctodev(card),
 627			"lv1_net_add_multicast_address failed, %d\n",
 628			status);
 629
 630	if ((netdev->flags & IFF_ALLMULTI) ||
 631	    (netdev_mc_count(netdev) > GELIC_NET_MC_COUNT_MAX)) {
 632		status = lv1_net_add_multicast_address(bus_id(card),
 633						       dev_id(card),
 634						       0, 1);
 635		if (status)
 636			dev_err(ctodev(card),
 637				"lv1_net_add_multicast_address failed, %d\n",
 638				status);
 639		return;
 640	}
 641
 642	/* set multicast addresses */
 643	netdev_for_each_mc_addr(ha, netdev) {
 644		addr = 0;
 645		p = ha->addr;
 646		for (i = 0; i < ETH_ALEN; i++) {
 647			addr <<= 8;
 648			addr |= *p++;
 649		}
 650		status = lv1_net_add_multicast_address(bus_id(card),
 651						       dev_id(card),
 652						       addr, 0);
 653		if (status)
 654			dev_err(ctodev(card),
 655				"lv1_net_add_multicast_address failed, %d\n",
 656				status);
 657	}
 658}
 659
 660/**
 661 * gelic_net_stop - called upon ifconfig down
 662 * @netdev: interface device structure
 663 *
 664 * always returns 0
 665 */
 666int gelic_net_stop(struct net_device *netdev)
 667{
 668	struct gelic_card *card;
 669
 670	pr_debug("%s: start\n", __func__);
 671
 672	netif_stop_queue(netdev);
 673	netif_carrier_off(netdev);
 674
 675	card = netdev_card(netdev);
 676	gelic_card_down(card);
 677
 678	pr_debug("%s: done\n", __func__);
 679	return 0;
 680}
 681
 682/**
 683 * gelic_card_get_next_tx_descr - returns the next available tx descriptor
 684 * @card: device structure to get descriptor from
 685 *
 686 * returns the address of the next descriptor, or NULL if not available.
 687 */
 688static struct gelic_descr *
 689gelic_card_get_next_tx_descr(struct gelic_card *card)
 690{
 691	if (!card->tx_chain.head)
 692		return NULL;
 693	/*  see if the next descriptor is free */
 694	if (card->tx_chain.tail != card->tx_chain.head->next &&
 695	    gelic_descr_get_status(card->tx_chain.head) ==
 696	    GELIC_DESCR_DMA_NOT_IN_USE)
 697		return card->tx_chain.head;
 698	else
 699		return NULL;
 700
 701}
 702
 703/**
 704 * gelic_net_set_txdescr_cmdstat - sets the tx descriptor command field
 705 * @descr: descriptor structure to fill out
 706 * @skb: packet to consider
 707 *
 708 * fills out the command and status field of the descriptor structure,
 709 * depending on hardware checksum settings. This function assumes a wmb()
 710 * has executed before.
 711 */
 712static void gelic_descr_set_tx_cmdstat(struct gelic_descr *descr,
 713				       struct sk_buff *skb)
 714{
 715	if (skb->ip_summed != CHECKSUM_PARTIAL)
 716		descr->dmac_cmd_status =
 717			cpu_to_be32(GELIC_DESCR_DMA_CMD_NO_CHKSUM |
 718				    GELIC_DESCR_TX_DMA_FRAME_TAIL);
 719	else {
 720		/* is packet ip?
 721		 * if yes: tcp? udp? */
 722		if (skb->protocol == htons(ETH_P_IP)) {
 723			if (ip_hdr(skb)->protocol == IPPROTO_TCP)
 724				descr->dmac_cmd_status =
 725				cpu_to_be32(GELIC_DESCR_DMA_CMD_TCP_CHKSUM |
 726					    GELIC_DESCR_TX_DMA_FRAME_TAIL);
 727
 728			else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
 729				descr->dmac_cmd_status =
 730				cpu_to_be32(GELIC_DESCR_DMA_CMD_UDP_CHKSUM |
 731					    GELIC_DESCR_TX_DMA_FRAME_TAIL);
 732			else	/*
 733				 * the stack should checksum non-tcp and non-udp
 734				 * packets on his own: NETIF_F_IP_CSUM
 735				 */
 736				descr->dmac_cmd_status =
 737				cpu_to_be32(GELIC_DESCR_DMA_CMD_NO_CHKSUM |
 738					    GELIC_DESCR_TX_DMA_FRAME_TAIL);
 739		}
 740	}
 741}
 742
 743static struct sk_buff *gelic_put_vlan_tag(struct sk_buff *skb,
 744						 unsigned short tag)
 745{
 746	struct vlan_ethhdr *veth;
 747	static unsigned int c;
 748
 749	if (skb_headroom(skb) < VLAN_HLEN) {
 750		struct sk_buff *sk_tmp = skb;
 751		pr_debug("%s: hd=%d c=%ud\n", __func__, skb_headroom(skb), c);
 752		skb = skb_realloc_headroom(sk_tmp, VLAN_HLEN);
 753		if (!skb)
 754			return NULL;
 755		dev_kfree_skb_any(sk_tmp);
 756	}
 757	veth = (struct vlan_ethhdr *)skb_push(skb, VLAN_HLEN);
 758
 759	/* Move the mac addresses to the top of buffer */
 760	memmove(skb->data, skb->data + VLAN_HLEN, 2 * ETH_ALEN);
 761
 762	veth->h_vlan_proto = cpu_to_be16(ETH_P_8021Q);
 763	veth->h_vlan_TCI = htons(tag);
 764
 765	return skb;
 766}
 767
 768/**
 769 * gelic_descr_prepare_tx - setup a descriptor for sending packets
 770 * @card: card structure
 771 * @descr: descriptor structure
 772 * @skb: packet to use
 773 *
 774 * returns 0 on success, <0 on failure.
 775 *
 776 */
 777static int gelic_descr_prepare_tx(struct gelic_card *card,
 778				  struct gelic_descr *descr,
 779				  struct sk_buff *skb)
 780{
 781	dma_addr_t buf;
 782
 783	if (card->vlan_required) {
 784		struct sk_buff *skb_tmp;
 785		enum gelic_port_type type;
 786
 787		type = netdev_port(skb->dev)->type;
 788		skb_tmp = gelic_put_vlan_tag(skb,
 789					     card->vlan[type].tx);
 790		if (!skb_tmp)
 791			return -ENOMEM;
 792		skb = skb_tmp;
 793	}
 794
 795	buf = dma_map_single(ctodev(card), skb->data, skb->len, DMA_TO_DEVICE);
 796
 797	if (!buf) {
 798		dev_err(ctodev(card),
 799			"dma map 2 failed (%p, %i). Dropping packet\n",
 800			skb->data, skb->len);
 801		return -ENOMEM;
 802	}
 803
 804	descr->buf_addr = cpu_to_be32(buf);
 805	descr->buf_size = cpu_to_be32(skb->len);
 806	descr->skb = skb;
 807	descr->data_status = 0;
 808	descr->next_descr_addr = 0; /* terminate hw descr */
 809	gelic_descr_set_tx_cmdstat(descr, skb);
 810
 811	/* bump free descriptor pointer */
 812	card->tx_chain.head = descr->next;
 813	return 0;
 814}
 815
 816/**
 817 * gelic_card_kick_txdma - enables TX DMA processing
 818 * @card: card structure
 819 * @descr: descriptor address to enable TX processing at
 820 *
 821 */
 822static int gelic_card_kick_txdma(struct gelic_card *card,
 823				 struct gelic_descr *descr)
 824{
 825	int status = 0;
 826
 827	if (card->tx_dma_progress)
 828		return 0;
 829
 830	if (gelic_descr_get_status(descr) == GELIC_DESCR_DMA_CARDOWNED) {
 831		card->tx_dma_progress = 1;
 832		status = lv1_net_start_tx_dma(bus_id(card), dev_id(card),
 833					      descr->bus_addr, 0);
 834		if (status) {
 835			card->tx_dma_progress = 0;
 836			dev_info(ctodev(card), "lv1_net_start_txdma failed," \
 837				 "status=%d\n", status);
 838		}
 839	}
 840	return status;
 841}
 842
 843/**
 844 * gelic_net_xmit - transmits a frame over the device
 845 * @skb: packet to send out
 846 * @netdev: interface device structure
 847 *
 848 * returns 0 on success, <0 on failure
 849 */
 850int gelic_net_xmit(struct sk_buff *skb, struct net_device *netdev)
 851{
 852	struct gelic_card *card = netdev_card(netdev);
 853	struct gelic_descr *descr;
 854	int result;
 855	unsigned long flags;
 856
 857	spin_lock_irqsave(&card->tx_lock, flags);
 858
 859	gelic_card_release_tx_chain(card, 0);
 860
 861	descr = gelic_card_get_next_tx_descr(card);
 862	if (!descr) {
 863		/*
 864		 * no more descriptors free
 865		 */
 866		gelic_card_stop_queues(card);
 867		spin_unlock_irqrestore(&card->tx_lock, flags);
 868		return NETDEV_TX_BUSY;
 869	}
 870
 871	result = gelic_descr_prepare_tx(card, descr, skb);
 872	if (result) {
 873		/*
 874		 * DMA map failed.  As chances are that failure
 875		 * would continue, just release skb and return
 876		 */
 877		netdev->stats.tx_dropped++;
 878		dev_kfree_skb_any(skb);
 879		spin_unlock_irqrestore(&card->tx_lock, flags);
 880		return NETDEV_TX_OK;
 881	}
 882	/*
 883	 * link this prepared descriptor to previous one
 884	 * to achieve high performance
 885	 */
 886	descr->prev->next_descr_addr = cpu_to_be32(descr->bus_addr);
 
 887	/*
 888	 * as hardware descriptor is modified in the above lines,
 889	 * ensure that the hardware sees it
 890	 */
 891	wmb();
 892	if (gelic_card_kick_txdma(card, descr)) {
 893		/*
 894		 * kick failed.
 895		 * release descriptor which was just prepared
 896		 */
 897		netdev->stats.tx_dropped++;
 898		/* don't trigger BUG_ON() in gelic_descr_release_tx */
 899		descr->data_status = cpu_to_be32(GELIC_DESCR_TX_TAIL);
 900		gelic_descr_release_tx(card, descr);
 901		/* reset head */
 902		card->tx_chain.head = descr;
 903		/* reset hw termination */
 904		descr->prev->next_descr_addr = 0;
 905		dev_info(ctodev(card), "%s: kick failure\n", __func__);
 906	}
 907
 908	spin_unlock_irqrestore(&card->tx_lock, flags);
 909	return NETDEV_TX_OK;
 910}
 911
 912/**
 913 * gelic_net_pass_skb_up - takes an skb from a descriptor and passes it on
 914 * @descr: descriptor to process
 915 * @card: card structure
 916 * @netdev: net_device structure to be passed packet
 917 *
 918 * iommu-unmaps the skb, fills out skb structure and passes the data to the
 919 * stack. The descriptor state is not changed.
 920 */
 921static void gelic_net_pass_skb_up(struct gelic_descr *descr,
 922				  struct gelic_card *card,
 923				  struct net_device *netdev)
 924
 925{
 926	struct sk_buff *skb = descr->skb;
 927	u32 data_status, data_error;
 928
 929	data_status = be32_to_cpu(descr->data_status);
 930	data_error = be32_to_cpu(descr->data_error);
 931	/* unmap skb buffer */
 932	dma_unmap_single(ctodev(card), be32_to_cpu(descr->buf_addr),
 933			 GELIC_NET_MAX_MTU,
 934			 DMA_FROM_DEVICE);
 935
 936	skb_put(skb, be32_to_cpu(descr->valid_size)?
 937		be32_to_cpu(descr->valid_size) :
 938		be32_to_cpu(descr->result_size));
 939	if (!descr->valid_size)
 940		dev_info(ctodev(card), "buffer full %x %x %x\n",
 941			 be32_to_cpu(descr->result_size),
 942			 be32_to_cpu(descr->buf_size),
 943			 be32_to_cpu(descr->dmac_cmd_status));
 944
 945	descr->skb = NULL;
 946	/*
 947	 * the card put 2 bytes vlan tag in front
 948	 * of the ethernet frame
 949	 */
 950	skb_pull(skb, 2);
 951	skb->protocol = eth_type_trans(skb, netdev);
 952
 953	/* checksum offload */
 954	if (netdev->features & NETIF_F_RXCSUM) {
 955		if ((data_status & GELIC_DESCR_DATA_STATUS_CHK_MASK) &&
 956		    (!(data_error & GELIC_DESCR_DATA_ERROR_CHK_MASK)))
 957			skb->ip_summed = CHECKSUM_UNNECESSARY;
 958		else
 959			skb_checksum_none_assert(skb);
 960	} else
 961		skb_checksum_none_assert(skb);
 962
 963	/* update netdevice statistics */
 964	netdev->stats.rx_packets++;
 965	netdev->stats.rx_bytes += skb->len;
 966
 967	/* pass skb up to stack */
 968	netif_receive_skb(skb);
 969}
 970
 971/**
 972 * gelic_card_decode_one_descr - processes an rx descriptor
 973 * @card: card structure
 974 *
 975 * returns 1 if a packet has been sent to the stack, otherwise 0
 976 *
 977 * processes an rx descriptor by iommu-unmapping the data buffer and passing
 978 * the packet up to the stack
 979 */
 980static int gelic_card_decode_one_descr(struct gelic_card *card)
 981{
 982	enum gelic_descr_dma_status status;
 983	struct gelic_descr_chain *chain = &card->rx_chain;
 984	struct gelic_descr *descr = chain->head;
 985	struct net_device *netdev = NULL;
 986	int dmac_chain_ended;
 987
 988	status = gelic_descr_get_status(descr);
 989
 990	if (status == GELIC_DESCR_DMA_CARDOWNED)
 991		return 0;
 992
 993	if (status == GELIC_DESCR_DMA_NOT_IN_USE) {
 994		dev_dbg(ctodev(card), "dormant descr? %p\n", descr);
 995		return 0;
 996	}
 997
 998	/* netdevice select */
 999	if (card->vlan_required) {
1000		unsigned int i;
1001		u16 vid;
1002		vid = *(u16 *)(descr->skb->data) & VLAN_VID_MASK;
1003		for (i = 0; i < GELIC_PORT_MAX; i++) {
1004			if (card->vlan[i].rx == vid) {
1005				netdev = card->netdev[i];
1006				break;
1007			}
1008		}
1009		if (GELIC_PORT_MAX <= i) {
1010			pr_info("%s: unknown packet vid=%x\n", __func__, vid);
1011			goto refill;
1012		}
1013	} else
1014		netdev = card->netdev[GELIC_PORT_ETHERNET_0];
1015
1016	if ((status == GELIC_DESCR_DMA_RESPONSE_ERROR) ||
1017	    (status == GELIC_DESCR_DMA_PROTECTION_ERROR) ||
1018	    (status == GELIC_DESCR_DMA_FORCE_END)) {
1019		dev_info(ctodev(card), "dropping RX descriptor with state %x\n",
1020			 status);
1021		netdev->stats.rx_dropped++;
1022		goto refill;
1023	}
1024
1025	if (status == GELIC_DESCR_DMA_BUFFER_FULL) {
1026		/*
1027		 * Buffer full would occur if and only if
1028		 * the frame length was longer than the size of this
1029		 * descriptor's buffer.  If the frame length was equal
1030		 * to or shorter than buffer'size, FRAME_END condition
1031		 * would occur.
1032		 * Anyway this frame was longer than the MTU,
1033		 * just drop it.
1034		 */
1035		dev_info(ctodev(card), "overlength frame\n");
1036		goto refill;
1037	}
1038	/*
1039	 * descriptors any other than FRAME_END here should
1040	 * be treated as error.
1041	 */
1042	if (status != GELIC_DESCR_DMA_FRAME_END) {
1043		dev_dbg(ctodev(card), "RX descriptor with state %x\n",
1044			status);
1045		goto refill;
1046	}
1047
1048	/* ok, we've got a packet in descr */
1049	gelic_net_pass_skb_up(descr, card, netdev);
1050refill:
1051
1052	/* is the current descriptor terminated with next_descr == NULL? */
1053	dmac_chain_ended =
1054		be32_to_cpu(descr->dmac_cmd_status) &
1055		GELIC_DESCR_RX_DMA_CHAIN_END;
1056	/*
1057	 * So that always DMAC can see the end
1058	 * of the descriptor chain to avoid
1059	 * from unwanted DMAC overrun.
1060	 */
1061	descr->next_descr_addr = 0;
1062
1063	/* change the descriptor state: */
1064	gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
1065
1066	/*
1067	 * this call can fail, but for now, just leave this
1068	 * descriptor without skb
1069	 */
1070	gelic_descr_prepare_rx(card, descr);
1071
1072	chain->tail = descr;
1073	chain->head = descr->next;
1074
1075	/*
1076	 * Set this descriptor the end of the chain.
1077	 */
1078	descr->prev->next_descr_addr = cpu_to_be32(descr->bus_addr);
 
1079
1080	/*
1081	 * If dmac chain was met, DMAC stopped.
1082	 * thus re-enable it
1083	 */
1084
1085	if (dmac_chain_ended)
1086		gelic_card_enable_rxdmac(card);
1087
1088	return 1;
1089}
1090
1091/**
1092 * gelic_net_poll - NAPI poll function called by the stack to return packets
1093 * @napi: napi structure
1094 * @budget: number of packets we can pass to the stack at most
1095 *
1096 * returns the number of the processed packets
1097 *
1098 */
1099static int gelic_net_poll(struct napi_struct *napi, int budget)
1100{
1101	struct gelic_card *card = container_of(napi, struct gelic_card, napi);
1102	int packets_done = 0;
1103
1104	while (packets_done < budget) {
1105		if (!gelic_card_decode_one_descr(card))
1106			break;
1107
1108		packets_done++;
1109	}
1110
1111	if (packets_done < budget) {
1112		napi_complete(napi);
1113		gelic_card_rx_irq_on(card);
1114	}
1115	return packets_done;
1116}
1117
1118/**
1119 * gelic_card_interrupt - event handler for gelic_net
1120 */
1121static irqreturn_t gelic_card_interrupt(int irq, void *ptr)
1122{
1123	unsigned long flags;
1124	struct gelic_card *card = ptr;
1125	u64 status;
1126
1127	status = card->irq_status;
1128
1129	if (!status)
1130		return IRQ_NONE;
1131
1132	status &= card->irq_mask;
1133
1134	if (status & GELIC_CARD_RXINT) {
1135		gelic_card_rx_irq_off(card);
1136		napi_schedule(&card->napi);
1137	}
1138
1139	if (status & GELIC_CARD_TXINT) {
1140		spin_lock_irqsave(&card->tx_lock, flags);
1141		card->tx_dma_progress = 0;
1142		gelic_card_release_tx_chain(card, 0);
1143		/* kick outstanding tx descriptor if any */
1144		gelic_card_kick_txdma(card, card->tx_chain.tail);
1145		spin_unlock_irqrestore(&card->tx_lock, flags);
1146	}
1147
1148	/* ether port status changed */
1149	if (status & GELIC_CARD_PORT_STATUS_CHANGED)
1150		gelic_card_get_ether_port_status(card, 1);
1151
1152#ifdef CONFIG_GELIC_WIRELESS
1153	if (status & (GELIC_CARD_WLAN_EVENT_RECEIVED |
1154		      GELIC_CARD_WLAN_COMMAND_COMPLETED))
1155		gelic_wl_interrupt(card->netdev[GELIC_PORT_WIRELESS], status);
1156#endif
1157
1158	return IRQ_HANDLED;
1159}
1160
1161#ifdef CONFIG_NET_POLL_CONTROLLER
1162/**
1163 * gelic_net_poll_controller - artificial interrupt for netconsole etc.
1164 * @netdev: interface device structure
1165 *
1166 * see Documentation/networking/netconsole.txt
1167 */
1168void gelic_net_poll_controller(struct net_device *netdev)
1169{
1170	struct gelic_card *card = netdev_card(netdev);
1171
1172	gelic_card_set_irq_mask(card, 0);
1173	gelic_card_interrupt(netdev->irq, netdev);
1174	gelic_card_set_irq_mask(card, card->irq_mask);
1175}
1176#endif /* CONFIG_NET_POLL_CONTROLLER */
1177
1178/**
1179 * gelic_net_open - called upon ifconfig up
1180 * @netdev: interface device structure
1181 *
1182 * returns 0 on success, <0 on failure
1183 *
1184 * gelic_net_open allocates all the descriptors and memory needed for
1185 * operation, sets up multicast list and enables interrupts
1186 */
1187int gelic_net_open(struct net_device *netdev)
1188{
1189	struct gelic_card *card = netdev_card(netdev);
1190
1191	dev_dbg(ctodev(card), " -> %s %p\n", __func__, netdev);
1192
1193	gelic_card_up(card);
1194
1195	netif_start_queue(netdev);
1196	gelic_card_get_ether_port_status(card, 1);
1197
1198	dev_dbg(ctodev(card), " <- %s\n", __func__);
1199	return 0;
1200}
1201
1202void gelic_net_get_drvinfo(struct net_device *netdev,
1203			   struct ethtool_drvinfo *info)
1204{
1205	strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
1206	strlcpy(info->version, DRV_VERSION, sizeof(info->version));
1207}
1208
1209static int gelic_ether_get_settings(struct net_device *netdev,
1210				    struct ethtool_cmd *cmd)
1211{
1212	struct gelic_card *card = netdev_card(netdev);
 
1213
1214	gelic_card_get_ether_port_status(card, 0);
1215
1216	if (card->ether_port_status & GELIC_LV1_ETHER_FULL_DUPLEX)
1217		cmd->duplex = DUPLEX_FULL;
1218	else
1219		cmd->duplex = DUPLEX_HALF;
1220
1221	switch (card->ether_port_status & GELIC_LV1_ETHER_SPEED_MASK) {
1222	case GELIC_LV1_ETHER_SPEED_10:
1223		ethtool_cmd_speed_set(cmd, SPEED_10);
1224		break;
1225	case GELIC_LV1_ETHER_SPEED_100:
1226		ethtool_cmd_speed_set(cmd, SPEED_100);
1227		break;
1228	case GELIC_LV1_ETHER_SPEED_1000:
1229		ethtool_cmd_speed_set(cmd, SPEED_1000);
1230		break;
1231	default:
1232		pr_info("%s: speed unknown\n", __func__);
1233		ethtool_cmd_speed_set(cmd, SPEED_10);
1234		break;
1235	}
1236
1237	cmd->supported = SUPPORTED_TP | SUPPORTED_Autoneg |
1238			SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full |
1239			SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full |
1240			SUPPORTED_1000baseT_Full;
1241	cmd->advertising = cmd->supported;
1242	if (card->link_mode & GELIC_LV1_ETHER_AUTO_NEG) {
1243		cmd->autoneg = AUTONEG_ENABLE;
1244	} else {
1245		cmd->autoneg = AUTONEG_DISABLE;
1246		cmd->advertising &= ~ADVERTISED_Autoneg;
1247	}
1248	cmd->port = PORT_TP;
 
 
 
 
 
1249
1250	return 0;
1251}
1252
1253static int gelic_ether_set_settings(struct net_device *netdev,
1254				    struct ethtool_cmd *cmd)
 
1255{
1256	struct gelic_card *card = netdev_card(netdev);
1257	u64 mode;
1258	int ret;
1259
1260	if (cmd->autoneg == AUTONEG_ENABLE) {
1261		mode = GELIC_LV1_ETHER_AUTO_NEG;
1262	} else {
1263		switch (cmd->speed) {
1264		case SPEED_10:
1265			mode = GELIC_LV1_ETHER_SPEED_10;
1266			break;
1267		case SPEED_100:
1268			mode = GELIC_LV1_ETHER_SPEED_100;
1269			break;
1270		case SPEED_1000:
1271			mode = GELIC_LV1_ETHER_SPEED_1000;
1272			break;
1273		default:
1274			return -EINVAL;
1275		}
1276		if (cmd->duplex == DUPLEX_FULL)
1277			mode |= GELIC_LV1_ETHER_FULL_DUPLEX;
1278		else if (cmd->speed == SPEED_1000) {
1279			pr_info("1000 half duplex is not supported.\n");
1280			return -EINVAL;
1281		}
1282	}
1283
1284	ret = gelic_card_set_link_mode(card, mode);
1285
1286	if (ret)
1287		return ret;
1288
1289	return 0;
1290}
1291
1292static void gelic_net_get_wol(struct net_device *netdev,
1293			      struct ethtool_wolinfo *wol)
1294{
1295	if (0 <= ps3_compare_firmware_version(2, 2, 0))
1296		wol->supported = WAKE_MAGIC;
1297	else
1298		wol->supported = 0;
1299
1300	wol->wolopts = ps3_sys_manager_get_wol() ? wol->supported : 0;
1301	memset(&wol->sopass, 0, sizeof(wol->sopass));
1302}
1303static int gelic_net_set_wol(struct net_device *netdev,
1304			     struct ethtool_wolinfo *wol)
1305{
1306	int status;
1307	struct gelic_card *card;
1308	u64 v1, v2;
1309
1310	if (ps3_compare_firmware_version(2, 2, 0) < 0 ||
1311	    !capable(CAP_NET_ADMIN))
1312		return -EPERM;
1313
1314	if (wol->wolopts & ~WAKE_MAGIC)
1315		return -EINVAL;
1316
1317	card = netdev_card(netdev);
1318	if (wol->wolopts & WAKE_MAGIC) {
1319		status = lv1_net_control(bus_id(card), dev_id(card),
1320					 GELIC_LV1_SET_WOL,
1321					 GELIC_LV1_WOL_MAGIC_PACKET,
1322					 0, GELIC_LV1_WOL_MP_ENABLE,
1323					 &v1, &v2);
1324		if (status) {
1325			pr_info("%s: enabling WOL failed %d\n", __func__,
1326				status);
1327			status = -EIO;
1328			goto done;
1329		}
1330		status = lv1_net_control(bus_id(card), dev_id(card),
1331					 GELIC_LV1_SET_WOL,
1332					 GELIC_LV1_WOL_ADD_MATCH_ADDR,
1333					 0, GELIC_LV1_WOL_MATCH_ALL,
1334					 &v1, &v2);
1335		if (!status)
1336			ps3_sys_manager_set_wol(1);
1337		else {
1338			pr_info("%s: enabling WOL filter failed %d\n",
1339				__func__, status);
1340			status = -EIO;
1341		}
1342	} else {
1343		status = lv1_net_control(bus_id(card), dev_id(card),
1344					 GELIC_LV1_SET_WOL,
1345					 GELIC_LV1_WOL_MAGIC_PACKET,
1346					 0, GELIC_LV1_WOL_MP_DISABLE,
1347					 &v1, &v2);
1348		if (status) {
1349			pr_info("%s: disabling WOL failed %d\n", __func__,
1350				status);
1351			status = -EIO;
1352			goto done;
1353		}
1354		status = lv1_net_control(bus_id(card), dev_id(card),
1355					 GELIC_LV1_SET_WOL,
1356					 GELIC_LV1_WOL_DELETE_MATCH_ADDR,
1357					 0, GELIC_LV1_WOL_MATCH_ALL,
1358					 &v1, &v2);
1359		if (!status)
1360			ps3_sys_manager_set_wol(0);
1361		else {
1362			pr_info("%s: removing WOL filter failed %d\n",
1363				__func__, status);
1364			status = -EIO;
1365		}
1366	}
1367done:
1368	return status;
1369}
1370
1371static const struct ethtool_ops gelic_ether_ethtool_ops = {
1372	.get_drvinfo	= gelic_net_get_drvinfo,
1373	.get_settings	= gelic_ether_get_settings,
1374	.set_settings	= gelic_ether_set_settings,
1375	.get_link	= ethtool_op_get_link,
1376	.get_wol	= gelic_net_get_wol,
1377	.set_wol	= gelic_net_set_wol,
 
 
1378};
1379
1380/**
1381 * gelic_net_tx_timeout_task - task scheduled by the watchdog timeout
1382 * function (to be called not under interrupt status)
1383 * @work: work is context of tx timout task
1384 *
1385 * called as task when tx hangs, resets interface (if interface is up)
1386 */
1387static void gelic_net_tx_timeout_task(struct work_struct *work)
1388{
1389	struct gelic_card *card =
1390		container_of(work, struct gelic_card, tx_timeout_task);
1391	struct net_device *netdev = card->netdev[GELIC_PORT_ETHERNET_0];
1392
1393	dev_info(ctodev(card), "%s:Timed out. Restarting...\n", __func__);
1394
1395	if (!(netdev->flags & IFF_UP))
1396		goto out;
1397
1398	netif_device_detach(netdev);
1399	gelic_net_stop(netdev);
1400
1401	gelic_net_open(netdev);
1402	netif_device_attach(netdev);
1403
1404out:
1405	atomic_dec(&card->tx_timeout_task_counter);
1406}
1407
1408/**
1409 * gelic_net_tx_timeout - called when the tx timeout watchdog kicks in.
1410 * @netdev: interface device structure
 
1411 *
1412 * called, if tx hangs. Schedules a task that resets the interface
1413 */
1414void gelic_net_tx_timeout(struct net_device *netdev)
1415{
1416	struct gelic_card *card;
1417
1418	card = netdev_card(netdev);
1419	atomic_inc(&card->tx_timeout_task_counter);
1420	if (netdev->flags & IFF_UP)
1421		schedule_work(&card->tx_timeout_task);
1422	else
1423		atomic_dec(&card->tx_timeout_task_counter);
1424}
1425
1426static const struct net_device_ops gelic_netdevice_ops = {
1427	.ndo_open = gelic_net_open,
1428	.ndo_stop = gelic_net_stop,
1429	.ndo_start_xmit = gelic_net_xmit,
1430	.ndo_set_rx_mode = gelic_net_set_multi,
1431	.ndo_tx_timeout = gelic_net_tx_timeout,
1432	.ndo_set_mac_address = eth_mac_addr,
1433	.ndo_validate_addr = eth_validate_addr,
1434#ifdef CONFIG_NET_POLL_CONTROLLER
1435	.ndo_poll_controller = gelic_net_poll_controller,
1436#endif
1437};
1438
1439/**
1440 * gelic_ether_setup_netdev_ops - initialization of net_device operations
1441 * @netdev: net_device structure
 
1442 *
1443 * fills out function pointers in the net_device structure
1444 */
1445static void gelic_ether_setup_netdev_ops(struct net_device *netdev,
1446					 struct napi_struct *napi)
1447{
1448	netdev->watchdog_timeo = GELIC_NET_WATCHDOG_TIMEOUT;
1449	/* NAPI */
1450	netif_napi_add(netdev, napi, gelic_net_poll, NAPI_POLL_WEIGHT);
1451	netdev->ethtool_ops = &gelic_ether_ethtool_ops;
1452	netdev->netdev_ops = &gelic_netdevice_ops;
1453}
1454
1455/**
1456 * gelic_ether_setup_netdev - initialization of net_device
1457 * @netdev: net_device structure
1458 * @card: card structure
1459 *
1460 * Returns 0 on success or <0 on failure
1461 *
1462 * gelic_ether_setup_netdev initializes the net_device structure
1463 * and register it.
1464 **/
1465int gelic_net_setup_netdev(struct net_device *netdev, struct gelic_card *card)
1466{
1467	int status;
1468	u64 v1, v2;
1469
1470	netdev->hw_features = NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
1471
1472	netdev->features = NETIF_F_IP_CSUM;
1473	if (GELIC_CARD_RX_CSUM_DEFAULT)
1474		netdev->features |= NETIF_F_RXCSUM;
1475
1476	status = lv1_net_control(bus_id(card), dev_id(card),
1477				 GELIC_LV1_GET_MAC_ADDRESS,
1478				 0, 0, 0, &v1, &v2);
1479	v1 <<= 16;
1480	if (status || !is_valid_ether_addr((u8 *)&v1)) {
1481		dev_info(ctodev(card),
1482			 "%s:lv1_net_control GET_MAC_ADDR failed %d\n",
1483			 __func__, status);
1484		return -EINVAL;
1485	}
1486	memcpy(netdev->dev_addr, &v1, ETH_ALEN);
1487
1488	if (card->vlan_required) {
1489		netdev->hard_header_len += VLAN_HLEN;
1490		/*
1491		 * As vlan is internally used,
1492		 * we can not receive vlan packets
1493		 */
1494		netdev->features |= NETIF_F_VLAN_CHALLENGED;
1495	}
1496
1497	/* MTU range: 64 - 1518 */
1498	netdev->min_mtu = GELIC_NET_MIN_MTU;
1499	netdev->max_mtu = GELIC_NET_MAX_MTU;
1500
1501	status = register_netdev(netdev);
1502	if (status) {
1503		dev_err(ctodev(card), "%s:Couldn't register %s %d\n",
1504			__func__, netdev->name, status);
1505		return status;
1506	}
1507	dev_info(ctodev(card), "%s: MAC addr %pM\n",
1508		 netdev->name, netdev->dev_addr);
1509
1510	return 0;
1511}
1512
1513/**
1514 * gelic_alloc_card_net - allocates net_device and card structure
1515 *
1516 * returns the card structure or NULL in case of errors
1517 *
1518 * the card and net_device structures are linked to each other
1519 */
1520#define GELIC_ALIGN (32)
1521static struct gelic_card *gelic_alloc_card_net(struct net_device **netdev)
1522{
1523	struct gelic_card *card;
1524	struct gelic_port *port;
1525	void *p;
1526	size_t alloc_size;
1527	/*
1528	 * gelic requires dma descriptor is 32 bytes aligned and
1529	 * the hypervisor requires irq_status is 8 bytes aligned.
1530	 */
1531	BUILD_BUG_ON(offsetof(struct gelic_card, irq_status) % 8);
1532	BUILD_BUG_ON(offsetof(struct gelic_card, descr) % 32);
1533	alloc_size =
1534		sizeof(struct gelic_card) +
1535		sizeof(struct gelic_descr) * GELIC_NET_RX_DESCRIPTORS +
1536		sizeof(struct gelic_descr) * GELIC_NET_TX_DESCRIPTORS +
1537		GELIC_ALIGN - 1;
1538
1539	p  = kzalloc(alloc_size, GFP_KERNEL);
1540	if (!p)
1541		return NULL;
1542	card = PTR_ALIGN(p, GELIC_ALIGN);
1543	card->unalign = p;
1544
1545	/*
1546	 * alloc netdev
1547	 */
1548	*netdev = alloc_etherdev(sizeof(struct gelic_port));
1549	if (!*netdev) {
1550		kfree(card->unalign);
1551		return NULL;
1552	}
1553	port = netdev_priv(*netdev);
1554
1555	/* gelic_port */
1556	port->netdev = *netdev;
1557	port->card = card;
1558	port->type = GELIC_PORT_ETHERNET_0;
1559
1560	/* gelic_card */
1561	card->netdev[GELIC_PORT_ETHERNET_0] = *netdev;
1562
1563	INIT_WORK(&card->tx_timeout_task, gelic_net_tx_timeout_task);
1564	init_waitqueue_head(&card->waitq);
1565	atomic_set(&card->tx_timeout_task_counter, 0);
1566	mutex_init(&card->updown_lock);
1567	atomic_set(&card->users, 0);
1568
1569	return card;
1570}
1571
1572static void gelic_card_get_vlan_info(struct gelic_card *card)
1573{
1574	u64 v1, v2;
1575	int status;
1576	unsigned int i;
1577	struct {
1578		int tx;
1579		int rx;
1580	} vlan_id_ix[2] = {
1581		[GELIC_PORT_ETHERNET_0] = {
1582			.tx = GELIC_LV1_VLAN_TX_ETHERNET_0,
1583			.rx = GELIC_LV1_VLAN_RX_ETHERNET_0
1584		},
1585		[GELIC_PORT_WIRELESS] = {
1586			.tx = GELIC_LV1_VLAN_TX_WIRELESS,
1587			.rx = GELIC_LV1_VLAN_RX_WIRELESS
1588		}
1589	};
1590
1591	for (i = 0; i < ARRAY_SIZE(vlan_id_ix); i++) {
1592		/* tx tag */
1593		status = lv1_net_control(bus_id(card), dev_id(card),
1594					 GELIC_LV1_GET_VLAN_ID,
1595					 vlan_id_ix[i].tx,
1596					 0, 0, &v1, &v2);
1597		if (status || !v1) {
1598			if (status != LV1_NO_ENTRY)
1599				dev_dbg(ctodev(card),
1600					"get vlan id for tx(%d) failed(%d)\n",
1601					vlan_id_ix[i].tx, status);
1602			card->vlan[i].tx = 0;
1603			card->vlan[i].rx = 0;
1604			continue;
1605		}
1606		card->vlan[i].tx = (u16)v1;
1607
1608		/* rx tag */
1609		status = lv1_net_control(bus_id(card), dev_id(card),
1610					 GELIC_LV1_GET_VLAN_ID,
1611					 vlan_id_ix[i].rx,
1612					 0, 0, &v1, &v2);
1613		if (status || !v1) {
1614			if (status != LV1_NO_ENTRY)
1615				dev_info(ctodev(card),
1616					 "get vlan id for rx(%d) failed(%d)\n",
1617					 vlan_id_ix[i].rx, status);
1618			card->vlan[i].tx = 0;
1619			card->vlan[i].rx = 0;
1620			continue;
1621		}
1622		card->vlan[i].rx = (u16)v1;
1623
1624		dev_dbg(ctodev(card), "vlan_id[%d] tx=%02x rx=%02x\n",
1625			i, card->vlan[i].tx, card->vlan[i].rx);
1626	}
1627
1628	if (card->vlan[GELIC_PORT_ETHERNET_0].tx) {
1629		BUG_ON(!card->vlan[GELIC_PORT_WIRELESS].tx);
1630		card->vlan_required = 1;
1631	} else
1632		card->vlan_required = 0;
1633
1634	/* check wirelss capable firmware */
1635	if (ps3_compare_firmware_version(1, 6, 0) < 0) {
1636		card->vlan[GELIC_PORT_WIRELESS].tx = 0;
1637		card->vlan[GELIC_PORT_WIRELESS].rx = 0;
1638	}
1639
1640	dev_info(ctodev(card), "internal vlan %s\n",
1641		 card->vlan_required? "enabled" : "disabled");
1642}
1643/**
1644 * ps3_gelic_driver_probe - add a device to the control of this driver
1645 */
1646static int ps3_gelic_driver_probe(struct ps3_system_bus_device *dev)
1647{
1648	struct gelic_card *card;
1649	struct net_device *netdev;
1650	int result;
1651
1652	pr_debug("%s: called\n", __func__);
1653
1654	udbg_shutdown_ps3gelic();
1655
1656	result = ps3_open_hv_device(dev);
1657
1658	if (result) {
1659		dev_dbg(&dev->core, "%s:ps3_open_hv_device failed\n",
1660			__func__);
1661		goto fail_open;
1662	}
1663
1664	result = ps3_dma_region_create(dev->d_region);
1665
1666	if (result) {
1667		dev_dbg(&dev->core, "%s:ps3_dma_region_create failed(%d)\n",
1668			__func__, result);
1669		BUG_ON("check region type");
1670		goto fail_dma_region;
1671	}
1672
1673	/* alloc card/netdevice */
1674	card = gelic_alloc_card_net(&netdev);
1675	if (!card) {
1676		dev_info(&dev->core, "%s:gelic_net_alloc_card failed\n",
1677			 __func__);
1678		result = -ENOMEM;
1679		goto fail_alloc_card;
1680	}
1681	ps3_system_bus_set_drvdata(dev, card);
1682	card->dev = dev;
1683
1684	/* get internal vlan info */
1685	gelic_card_get_vlan_info(card);
1686
1687	card->link_mode = GELIC_LV1_ETHER_AUTO_NEG;
1688
1689	/* setup interrupt */
1690	result = lv1_net_set_interrupt_status_indicator(bus_id(card),
1691							dev_id(card),
1692		ps3_mm_phys_to_lpar(__pa(&card->irq_status)),
1693		0);
1694
1695	if (result) {
1696		dev_dbg(&dev->core,
1697			"%s:set_interrupt_status_indicator failed: %s\n",
1698			__func__, ps3_result(result));
1699		result = -EIO;
1700		goto fail_status_indicator;
1701	}
1702
1703	result = ps3_sb_event_receive_port_setup(dev, PS3_BINDING_CPU_ANY,
1704		&card->irq);
1705
1706	if (result) {
1707		dev_info(ctodev(card),
1708			 "%s:gelic_net_open_device failed (%d)\n",
1709			 __func__, result);
1710		result = -EPERM;
1711		goto fail_alloc_irq;
1712	}
1713	result = request_irq(card->irq, gelic_card_interrupt,
1714			     0, netdev->name, card);
1715
1716	if (result) {
1717		dev_info(ctodev(card), "%s:request_irq failed (%d)\n",
1718			__func__, result);
1719		goto fail_request_irq;
1720	}
1721
1722	/* setup card structure */
1723	card->irq_mask = GELIC_CARD_RXINT | GELIC_CARD_TXINT |
1724		GELIC_CARD_PORT_STATUS_CHANGED;
1725
1726
1727	result = gelic_card_init_chain(card, &card->tx_chain,
1728				       card->descr, GELIC_NET_TX_DESCRIPTORS);
1729	if (result)
1730		goto fail_alloc_tx;
1731	result = gelic_card_init_chain(card, &card->rx_chain,
1732				       card->descr + GELIC_NET_TX_DESCRIPTORS,
1733				       GELIC_NET_RX_DESCRIPTORS);
1734	if (result)
1735		goto fail_alloc_rx;
1736
1737	/* head of chain */
1738	card->tx_top = card->tx_chain.head;
1739	card->rx_top = card->rx_chain.head;
1740	dev_dbg(ctodev(card), "descr rx %p, tx %p, size %#lx, num %#x\n",
1741		card->rx_top, card->tx_top, sizeof(struct gelic_descr),
1742		GELIC_NET_RX_DESCRIPTORS);
1743	/* allocate rx skbs */
1744	result = gelic_card_alloc_rx_skbs(card);
1745	if (result)
1746		goto fail_alloc_skbs;
1747
1748	spin_lock_init(&card->tx_lock);
1749	card->tx_dma_progress = 0;
1750
1751	/* setup net_device structure */
1752	netdev->irq = card->irq;
1753	SET_NETDEV_DEV(netdev, &card->dev->core);
1754	gelic_ether_setup_netdev_ops(netdev, &card->napi);
1755	result = gelic_net_setup_netdev(netdev, card);
1756	if (result) {
1757		dev_dbg(&dev->core, "%s: setup_netdev failed %d\n",
1758			__func__, result);
1759		goto fail_setup_netdev;
1760	}
1761
1762#ifdef CONFIG_GELIC_WIRELESS
1763	result = gelic_wl_driver_probe(card);
1764	if (result) {
1765		dev_dbg(&dev->core, "%s: WL init failed\n", __func__);
1766		goto fail_setup_netdev;
1767	}
1768#endif
1769	pr_debug("%s: done\n", __func__);
1770	return 0;
1771
1772fail_setup_netdev:
1773fail_alloc_skbs:
1774	gelic_card_free_chain(card, card->rx_chain.head);
1775fail_alloc_rx:
1776	gelic_card_free_chain(card, card->tx_chain.head);
1777fail_alloc_tx:
1778	free_irq(card->irq, card);
1779	netdev->irq = 0;
1780fail_request_irq:
1781	ps3_sb_event_receive_port_destroy(dev, card->irq);
1782fail_alloc_irq:
1783	lv1_net_set_interrupt_status_indicator(bus_id(card),
1784					       bus_id(card),
1785					       0, 0);
1786fail_status_indicator:
1787	ps3_system_bus_set_drvdata(dev, NULL);
1788	kfree(netdev_card(netdev)->unalign);
1789	free_netdev(netdev);
1790fail_alloc_card:
1791	ps3_dma_region_free(dev->d_region);
1792fail_dma_region:
1793	ps3_close_hv_device(dev);
1794fail_open:
1795	return result;
1796}
1797
1798/**
1799 * ps3_gelic_driver_remove - remove a device from the control of this driver
1800 */
1801
1802static int ps3_gelic_driver_remove(struct ps3_system_bus_device *dev)
1803{
1804	struct gelic_card *card = ps3_system_bus_get_drvdata(dev);
1805	struct net_device *netdev0;
1806	pr_debug("%s: called\n", __func__);
1807
1808	/* set auto-negotiation */
1809	gelic_card_set_link_mode(card, GELIC_LV1_ETHER_AUTO_NEG);
1810
1811#ifdef CONFIG_GELIC_WIRELESS
1812	gelic_wl_driver_remove(card);
1813#endif
1814	/* stop interrupt */
1815	gelic_card_set_irq_mask(card, 0);
1816
1817	/* turn off DMA, force end */
1818	gelic_card_disable_rxdmac(card);
1819	gelic_card_disable_txdmac(card);
1820
1821	/* release chains */
1822	gelic_card_release_tx_chain(card, 1);
1823	gelic_card_release_rx_chain(card);
1824
1825	gelic_card_free_chain(card, card->tx_top);
1826	gelic_card_free_chain(card, card->rx_top);
1827
1828	netdev0 = card->netdev[GELIC_PORT_ETHERNET_0];
1829	/* disconnect event port */
1830	free_irq(card->irq, card);
1831	netdev0->irq = 0;
1832	ps3_sb_event_receive_port_destroy(card->dev, card->irq);
1833
1834	wait_event(card->waitq,
1835		   atomic_read(&card->tx_timeout_task_counter) == 0);
1836
1837	lv1_net_set_interrupt_status_indicator(bus_id(card), dev_id(card),
1838					       0 , 0);
1839
1840	unregister_netdev(netdev0);
1841	kfree(netdev_card(netdev0)->unalign);
1842	free_netdev(netdev0);
1843
1844	ps3_system_bus_set_drvdata(dev, NULL);
1845
1846	ps3_dma_region_free(dev->d_region);
1847
1848	ps3_close_hv_device(dev);
1849
1850	pr_debug("%s: done\n", __func__);
1851	return 0;
1852}
1853
1854static struct ps3_system_bus_driver ps3_gelic_driver = {
1855	.match_id = PS3_MATCH_ID_GELIC,
1856	.probe = ps3_gelic_driver_probe,
1857	.remove = ps3_gelic_driver_remove,
1858	.shutdown = ps3_gelic_driver_remove,
1859	.core.name = "ps3_gelic_driver",
1860	.core.owner = THIS_MODULE,
1861};
1862
1863static int __init ps3_gelic_driver_init (void)
1864{
1865	return firmware_has_feature(FW_FEATURE_PS3_LV1)
1866		? ps3_system_bus_driver_register(&ps3_gelic_driver)
1867		: -ENODEV;
1868}
1869
1870static void __exit ps3_gelic_driver_exit (void)
1871{
1872	ps3_system_bus_driver_unregister(&ps3_gelic_driver);
1873}
1874
1875module_init(ps3_gelic_driver_init);
1876module_exit(ps3_gelic_driver_exit);
1877
1878MODULE_ALIAS(PS3_MODULE_ALIAS_GELIC);
1879