2 * PS3 gelic network driver.
4 * Copyright (C) 2007 Sony Computer Entertainment Inc.
5 * Copyright 2006, 2007 Sony Corporation
7 * This file is based on: spider_net.c
9 * (C) Copyright IBM Corp. 2005
11 * Authors : Utz Bacher <utz.bacher@de.ibm.com>
12 * Jens Osterkamp <Jens.Osterkamp@de.ibm.com>
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)
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.
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.
31 #include <linux/interrupt.h>
32 #include <linux/kernel.h>
33 #include <linux/module.h>
34 #include <linux/slab.h>
36 #include <linux/etherdevice.h>
37 #include <linux/ethtool.h>
38 #include <linux/if_vlan.h>
42 #include <linux/tcp.h>
44 #include <linux/dma-mapping.h>
45 #include <net/checksum.h>
46 #include <asm/firmware.h>
48 #include <asm/lv1call.h>
50 #include "ps3_gelic_net.h"
51 #include "ps3_gelic_wireless.h"
53 #define DRV_NAME "Gelic Network Driver"
54 #define DRV_VERSION "2.0"
56 MODULE_AUTHOR("SCE Inc.");
57 MODULE_DESCRIPTION("Gelic Network driver");
58 MODULE_LICENSE("GPL");
62 int gelic_card_set_irq_mask(struct gelic_card *card, u64 mask)
66 status = lv1_net_set_interrupt_mask(bus_id(card), dev_id(card),
69 dev_info(ctodev(card),
70 "%s failed %d\n", __func__, status);
74 static void gelic_card_rx_irq_on(struct gelic_card *card)
76 card->irq_mask |= GELIC_CARD_RXINT;
77 gelic_card_set_irq_mask(card, card->irq_mask);
79 static void gelic_card_rx_irq_off(struct gelic_card *card)
81 card->irq_mask &= ~GELIC_CARD_RXINT;
82 gelic_card_set_irq_mask(card, card->irq_mask);
85 static void gelic_card_get_ether_port_status(struct gelic_card *card,
89 struct net_device *ether_netdev;
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);
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);
101 netif_carrier_off(ether_netdev);
106 * gelic_descr_get_status -- returns the status of a descriptor
107 * @descr: descriptor to look at
109 * returns the status as in the dmac_cmd_status field of the descriptor
111 static enum gelic_descr_dma_status
112 gelic_descr_get_status(struct gelic_descr *descr)
114 return be32_to_cpu(descr->dmac_cmd_status) & GELIC_DESCR_DMA_STAT_MASK;
117 static int gelic_card_set_link_mode(struct gelic_card *card, int mode)
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);
126 pr_info("%s: failed setting negotiation mode %d\n", __func__,
131 card->link_mode = mode;
136 * gelic_card_disable_txdmac - disables the transmit DMA controller
137 * @card: card structure
139 * gelic_card_disable_txdmac terminates processing on the DMA controller by
140 * turing off DMA and issuing a force end
142 static void gelic_card_disable_txdmac(struct gelic_card *card)
146 /* this hvc blocks until the DMA in progress really stopped */
147 status = lv1_net_stop_tx_dma(bus_id(card), dev_id(card));
149 dev_err(ctodev(card),
150 "lv1_net_stop_tx_dma failed, status=%d\n", status);
154 * gelic_card_enable_rxdmac - enables the receive DMA controller
155 * @card: card structure
157 * gelic_card_enable_rxdmac enables the DMA controller by setting RX_DMA_EN
158 * in the GDADMACCNTR register
160 static void gelic_card_enable_rxdmac(struct gelic_card *card)
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);
175 status = lv1_net_start_rx_dma(bus_id(card), dev_id(card),
176 card->rx_chain.head->bus_addr, 0);
178 dev_info(ctodev(card),
179 "lv1_net_start_rx_dma failed, status=%d\n", status);
183 * gelic_card_disable_rxdmac - disables the receive DMA controller
184 * @card: card structure
186 * gelic_card_disable_rxdmac terminates processing on the DMA controller by
187 * turing off DMA and issuing a force end
189 static void gelic_card_disable_rxdmac(struct gelic_card *card)
193 /* this hvc blocks until the DMA in progress really stopped */
194 status = lv1_net_stop_rx_dma(bus_id(card), dev_id(card));
196 dev_err(ctodev(card),
197 "lv1_net_stop_rx_dma failed, %d\n", status);
201 * gelic_descr_set_status -- sets the status of a descriptor
202 * @descr: descriptor to change
203 * @status: status to set in the descriptor
205 * changes the status to the specified value. Doesn't change other bits
208 static void gelic_descr_set_status(struct gelic_descr *descr,
209 enum gelic_descr_dma_status status)
211 descr->dmac_cmd_status = cpu_to_be32(status |
212 (be32_to_cpu(descr->dmac_cmd_status) &
213 ~GELIC_DESCR_DMA_STAT_MASK));
215 * dma_cmd_status field is used to indicate whether the descriptor
217 * Usually caller of this function wants to inform that to the
218 * hardware, so we assure here the hardware sees the change.
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
229 * Reset the status of dma descriptors to ready state
230 * and re-initialize the hardware chain for later use
232 static void gelic_card_reset_chain(struct gelic_card *card,
233 struct gelic_descr_chain *chain,
234 struct gelic_descr *start_descr)
236 struct gelic_descr *descr;
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);
243 chain->head = start_descr;
244 chain->tail = (descr - 1);
246 (descr - 1)->next_descr_addr = 0;
249 void gelic_card_up(struct gelic_card *card)
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__);
256 gelic_card_set_irq_mask(card, card->irq_mask);
258 gelic_card_enable_rxdmac(card);
260 napi_enable(&card->napi);
262 mutex_unlock(&card->updown_lock);
263 pr_debug("%s: done\n", __func__);
266 void gelic_card_down(struct gelic_card *card)
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);
275 * Disable irq. Wireless interrupts will
276 * be disabled later if any
278 mask = card->irq_mask & (GELIC_CARD_WLAN_EVENT_RECEIVED |
279 GELIC_CARD_WLAN_COMMAND_COMPLETED);
280 gelic_card_set_irq_mask(card, mask);
282 gelic_card_disable_rxdmac(card);
283 gelic_card_reset_chain(card, &card->rx_chain,
284 card->descr + GELIC_NET_TX_DESCRIPTORS);
286 gelic_card_disable_txdmac(card);
288 mutex_unlock(&card->updown_lock);
289 pr_debug("%s: done\n", __func__);
293 * gelic_card_free_chain - free descriptor chain
294 * @card: card structure
295 * @descr_in: address of desc
297 static void gelic_card_free_chain(struct gelic_card *card,
298 struct gelic_descr *descr_in)
300 struct gelic_descr *descr;
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);
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
316 * we manage a circular list that mirrors the hardware structure,
317 * except that the hardware uses bus addresses.
319 * returns 0 on success, <0 on failure
321 static int gelic_card_init_chain(struct gelic_card *card,
322 struct gelic_descr_chain *chain,
323 struct gelic_descr *start_descr, int no)
326 struct gelic_descr *descr;
329 memset(descr, 0, sizeof(*descr) * no);
331 /* set up the hardware pointers in each descriptor */
332 for (i = 0; i < no; i++, descr++) {
335 gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
337 cpu_addr = dma_map_single(ctodev(card), descr,
338 GELIC_DESCR_SIZE, DMA_BIDIRECTIONAL);
340 if (dma_mapping_error(ctodev(card), cpu_addr))
343 descr->bus_addr = cpu_to_be32(cpu_addr);
344 descr->next = descr + 1;
345 descr->prev = descr - 1;
347 /* make them as ring */
348 (descr - 1)->next = start_descr;
349 start_descr->prev = (descr - 1);
351 /* chain bus addr of hw descriptor */
353 for (i = 0; i < no; i++, descr++) {
354 descr->next_descr_addr = cpu_to_be32(descr->next->bus_addr);
357 chain->head = start_descr;
358 chain->tail = start_descr;
360 /* do not chain last hw descriptor */
361 (descr - 1)->next_descr_addr = 0;
366 for (i--, descr--; 0 <= i; i--, descr--)
368 dma_unmap_single(ctodev(card), descr->bus_addr,
375 * gelic_descr_prepare_rx - reinitializes a rx descriptor
376 * @card: card structure
377 * @descr: descriptor to re-init
379 * return 0 on success, <0 on failure
381 * allocates a new rx skb, iommu-maps it and attaches it to the descriptor.
382 * Activate the descriptor state-wise
384 * Gelic RX sk_buffs must be aligned to GELIC_NET_RXBUF_ALIGN and the length
385 * must be a multiple of GELIC_NET_RXBUF_ALIGN.
387 static int gelic_descr_prepare_rx(struct gelic_card *card,
388 struct gelic_descr *descr)
390 static const unsigned int rx_skb_size =
391 ALIGN(GELIC_NET_MAX_FRAME, GELIC_NET_RXBUF_ALIGN) +
392 GELIC_NET_RXBUF_ALIGN - 1;
396 if (gelic_descr_get_status(descr) != GELIC_DESCR_DMA_NOT_IN_USE)
397 dev_info(ctodev(card), "%s: ERROR status\n", __func__);
399 descr->skb = netdev_alloc_skb(*card->netdev, rx_skb_size);
401 descr->buf_addr = 0; /* tell DMAC don't touch memory */
402 dev_info(ctodev(card),
403 "%s:allocate skb failed !!\n", __func__);
406 descr->buf_size = cpu_to_be32(rx_skb_size);
407 descr->dmac_cmd_status = 0;
408 descr->result_size = 0;
409 descr->valid_size = 0;
410 descr->data_error = 0;
412 offset = ((unsigned long)descr->skb->data) &
413 (GELIC_NET_RXBUF_ALIGN - 1);
415 skb_reserve(descr->skb, GELIC_NET_RXBUF_ALIGN - offset);
416 /* io-mmu-map the skb */
417 cpu_addr = dma_map_single(ctodev(card), descr->skb->data,
418 GELIC_NET_MAX_FRAME, DMA_FROM_DEVICE);
419 descr->buf_addr = cpu_to_be32(cpu_addr);
420 if (dma_mapping_error(ctodev(card), cpu_addr)) {
421 dev_kfree_skb_any(descr->skb);
423 dev_info(ctodev(card),
424 "%s:Could not iommu-map rx buffer\n", __func__);
425 gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
428 gelic_descr_set_status(descr, GELIC_DESCR_DMA_CARDOWNED);
434 * gelic_card_release_rx_chain - free all skb of rx descr
435 * @card: card structure
438 static void gelic_card_release_rx_chain(struct gelic_card *card)
440 struct gelic_descr *descr = card->rx_chain.head;
444 dma_unmap_single(ctodev(card),
445 be32_to_cpu(descr->buf_addr),
449 dev_kfree_skb_any(descr->skb);
451 gelic_descr_set_status(descr,
452 GELIC_DESCR_DMA_NOT_IN_USE);
455 } while (descr != card->rx_chain.head);
459 * gelic_card_fill_rx_chain - fills descriptors/skbs in the rx chains
460 * @card: card structure
462 * fills all descriptors in the rx chain: allocates skbs
463 * and iommu-maps them.
464 * returns 0 on success, < 0 on failure
466 static int gelic_card_fill_rx_chain(struct gelic_card *card)
468 struct gelic_descr *descr = card->rx_chain.head;
473 ret = gelic_descr_prepare_rx(card, descr);
478 } while (descr != card->rx_chain.head);
482 gelic_card_release_rx_chain(card);
487 * gelic_card_alloc_rx_skbs - allocates rx skbs in rx descriptor chains
488 * @card: card structure
490 * returns 0 on success, < 0 on failure
492 static int gelic_card_alloc_rx_skbs(struct gelic_card *card)
494 struct gelic_descr_chain *chain;
496 chain = &card->rx_chain;
497 ret = gelic_card_fill_rx_chain(card);
498 chain->tail = card->rx_top->prev; /* point to the last */
503 * gelic_descr_release_tx - processes a used tx descriptor
504 * @card: card structure
505 * @descr: descriptor to release
507 * releases a used tx descriptor (unmapping, freeing of skb)
509 static void gelic_descr_release_tx(struct gelic_card *card,
510 struct gelic_descr *descr)
512 struct sk_buff *skb = descr->skb;
514 BUG_ON(!(be32_to_cpu(descr->data_status) & GELIC_DESCR_TX_TAIL));
516 dma_unmap_single(ctodev(card), be32_to_cpu(descr->buf_addr), skb->len,
518 dev_kfree_skb_any(skb);
522 descr->next_descr_addr = 0;
523 descr->result_size = 0;
524 descr->valid_size = 0;
525 descr->data_status = 0;
526 descr->data_error = 0;
529 /* set descr status */
530 gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
533 static void gelic_card_stop_queues(struct gelic_card *card)
535 netif_stop_queue(card->netdev[GELIC_PORT_ETHERNET_0]);
537 if (card->netdev[GELIC_PORT_WIRELESS])
538 netif_stop_queue(card->netdev[GELIC_PORT_WIRELESS]);
540 static void gelic_card_wake_queues(struct gelic_card *card)
542 netif_wake_queue(card->netdev[GELIC_PORT_ETHERNET_0]);
544 if (card->netdev[GELIC_PORT_WIRELESS])
545 netif_wake_queue(card->netdev[GELIC_PORT_WIRELESS]);
548 * gelic_card_release_tx_chain - processes sent tx descriptors
549 * @card: adapter structure
550 * @stop: net_stop sequence
552 * releases the tx descriptors that gelic has finished with
554 static void gelic_card_release_tx_chain(struct gelic_card *card, int stop)
556 struct gelic_descr_chain *tx_chain;
557 enum gelic_descr_dma_status status;
558 struct net_device *netdev;
561 for (tx_chain = &card->tx_chain;
562 tx_chain->head != tx_chain->tail && tx_chain->tail;
563 tx_chain->tail = tx_chain->tail->next) {
564 status = gelic_descr_get_status(tx_chain->tail);
565 netdev = tx_chain->tail->skb->dev;
567 case GELIC_DESCR_DMA_RESPONSE_ERROR:
568 case GELIC_DESCR_DMA_PROTECTION_ERROR:
569 case GELIC_DESCR_DMA_FORCE_END:
570 if (printk_ratelimit())
571 dev_info(ctodev(card),
572 "%s: forcing end of tx descriptor " \
575 netdev->stats.tx_dropped++;
578 case GELIC_DESCR_DMA_COMPLETE:
579 if (tx_chain->tail->skb) {
580 netdev->stats.tx_packets++;
581 netdev->stats.tx_bytes +=
582 tx_chain->tail->skb->len;
586 case GELIC_DESCR_DMA_CARDOWNED:
587 /* pending tx request */
589 /* any other value (== GELIC_DESCR_DMA_NOT_IN_USE) */
593 gelic_descr_release_tx(card, tx_chain->tail);
597 if (!stop && release)
598 gelic_card_wake_queues(card);
602 * gelic_net_set_multi - sets multicast addresses and promisc flags
603 * @netdev: interface device structure
605 * gelic_net_set_multi configures multicast addresses as needed for the
606 * netdev interface. It also sets up multicast, allmulti and promisc
607 * flags appropriately
609 void gelic_net_set_multi(struct net_device *netdev)
611 struct gelic_card *card = netdev_card(netdev);
612 struct netdev_hw_addr *ha;
618 /* clear all multicast address */
619 status = lv1_net_remove_multicast_address(bus_id(card), dev_id(card),
622 dev_err(ctodev(card),
623 "lv1_net_remove_multicast_address failed %d\n",
625 /* set broadcast address */
626 status = lv1_net_add_multicast_address(bus_id(card), dev_id(card),
627 GELIC_NET_BROADCAST_ADDR, 0);
629 dev_err(ctodev(card),
630 "lv1_net_add_multicast_address failed, %d\n",
633 if ((netdev->flags & IFF_ALLMULTI) ||
634 (netdev_mc_count(netdev) > GELIC_NET_MC_COUNT_MAX)) {
635 status = lv1_net_add_multicast_address(bus_id(card),
639 dev_err(ctodev(card),
640 "lv1_net_add_multicast_address failed, %d\n",
645 /* set multicast addresses */
646 netdev_for_each_mc_addr(ha, netdev) {
649 for (i = 0; i < ETH_ALEN; i++) {
653 status = lv1_net_add_multicast_address(bus_id(card),
657 dev_err(ctodev(card),
658 "lv1_net_add_multicast_address failed, %d\n",
664 * gelic_net_stop - called upon ifconfig down
665 * @netdev: interface device structure
669 int gelic_net_stop(struct net_device *netdev)
671 struct gelic_card *card;
673 pr_debug("%s: start\n", __func__);
675 netif_stop_queue(netdev);
676 netif_carrier_off(netdev);
678 card = netdev_card(netdev);
679 gelic_card_down(card);
681 pr_debug("%s: done\n", __func__);
686 * gelic_card_get_next_tx_descr - returns the next available tx descriptor
687 * @card: device structure to get descriptor from
689 * returns the address of the next descriptor, or NULL if not available.
691 static struct gelic_descr *
692 gelic_card_get_next_tx_descr(struct gelic_card *card)
694 if (!card->tx_chain.head)
696 /* see if the next descriptor is free */
697 if (card->tx_chain.tail != card->tx_chain.head->next &&
698 gelic_descr_get_status(card->tx_chain.head) ==
699 GELIC_DESCR_DMA_NOT_IN_USE)
700 return card->tx_chain.head;
707 * gelic_net_set_txdescr_cmdstat - sets the tx descriptor command field
708 * @descr: descriptor structure to fill out
709 * @skb: packet to consider
711 * fills out the command and status field of the descriptor structure,
712 * depending on hardware checksum settings. This function assumes a wmb()
713 * has executed before.
715 static void gelic_descr_set_tx_cmdstat(struct gelic_descr *descr,
718 if (skb->ip_summed != CHECKSUM_PARTIAL)
719 descr->dmac_cmd_status =
720 cpu_to_be32(GELIC_DESCR_DMA_CMD_NO_CHKSUM |
721 GELIC_DESCR_TX_DMA_FRAME_TAIL);
724 * if yes: tcp? udp? */
725 if (skb->protocol == htons(ETH_P_IP)) {
726 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
727 descr->dmac_cmd_status =
728 cpu_to_be32(GELIC_DESCR_DMA_CMD_TCP_CHKSUM |
729 GELIC_DESCR_TX_DMA_FRAME_TAIL);
731 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
732 descr->dmac_cmd_status =
733 cpu_to_be32(GELIC_DESCR_DMA_CMD_UDP_CHKSUM |
734 GELIC_DESCR_TX_DMA_FRAME_TAIL);
736 * the stack should checksum non-tcp and non-udp
737 * packets on his own: NETIF_F_IP_CSUM
739 descr->dmac_cmd_status =
740 cpu_to_be32(GELIC_DESCR_DMA_CMD_NO_CHKSUM |
741 GELIC_DESCR_TX_DMA_FRAME_TAIL);
746 static struct sk_buff *gelic_put_vlan_tag(struct sk_buff *skb,
749 struct vlan_ethhdr *veth;
750 static unsigned int c;
752 if (skb_headroom(skb) < VLAN_HLEN) {
753 struct sk_buff *sk_tmp = skb;
754 pr_debug("%s: hd=%d c=%ud\n", __func__, skb_headroom(skb), c);
755 skb = skb_realloc_headroom(sk_tmp, VLAN_HLEN);
758 dev_kfree_skb_any(sk_tmp);
760 veth = skb_push(skb, VLAN_HLEN);
762 /* Move the mac addresses to the top of buffer */
763 memmove(skb->data, skb->data + VLAN_HLEN, 2 * ETH_ALEN);
765 veth->h_vlan_proto = cpu_to_be16(ETH_P_8021Q);
766 veth->h_vlan_TCI = htons(tag);
772 * gelic_descr_prepare_tx - setup a descriptor for sending packets
773 * @card: card structure
774 * @descr: descriptor structure
775 * @skb: packet to use
777 * returns 0 on success, <0 on failure.
780 static int gelic_descr_prepare_tx(struct gelic_card *card,
781 struct gelic_descr *descr,
786 if (card->vlan_required) {
787 struct sk_buff *skb_tmp;
788 enum gelic_port_type type;
790 type = netdev_port(skb->dev)->type;
791 skb_tmp = gelic_put_vlan_tag(skb,
792 card->vlan[type].tx);
798 buf = dma_map_single(ctodev(card), skb->data, skb->len, DMA_TO_DEVICE);
800 if (dma_mapping_error(ctodev(card), buf)) {
801 dev_err(ctodev(card),
802 "dma map 2 failed (%p, %i). Dropping packet\n",
803 skb->data, skb->len);
807 descr->buf_addr = cpu_to_be32(buf);
808 descr->buf_size = cpu_to_be32(skb->len);
810 descr->data_status = 0;
811 descr->next_descr_addr = 0; /* terminate hw descr */
812 gelic_descr_set_tx_cmdstat(descr, skb);
814 /* bump free descriptor pointer */
815 card->tx_chain.head = descr->next;
820 * gelic_card_kick_txdma - enables TX DMA processing
821 * @card: card structure
822 * @descr: descriptor address to enable TX processing at
825 static int gelic_card_kick_txdma(struct gelic_card *card,
826 struct gelic_descr *descr)
830 if (card->tx_dma_progress)
833 if (gelic_descr_get_status(descr) == GELIC_DESCR_DMA_CARDOWNED) {
834 card->tx_dma_progress = 1;
835 status = lv1_net_start_tx_dma(bus_id(card), dev_id(card),
838 card->tx_dma_progress = 0;
839 dev_info(ctodev(card), "lv1_net_start_txdma failed," \
840 "status=%d\n", status);
847 * gelic_net_xmit - transmits a frame over the device
848 * @skb: packet to send out
849 * @netdev: interface device structure
851 * returns NETDEV_TX_OK on success, NETDEV_TX_BUSY on failure
853 netdev_tx_t gelic_net_xmit(struct sk_buff *skb, struct net_device *netdev)
855 struct gelic_card *card = netdev_card(netdev);
856 struct gelic_descr *descr;
860 spin_lock_irqsave(&card->tx_lock, flags);
862 gelic_card_release_tx_chain(card, 0);
864 descr = gelic_card_get_next_tx_descr(card);
867 * no more descriptors free
869 gelic_card_stop_queues(card);
870 spin_unlock_irqrestore(&card->tx_lock, flags);
871 return NETDEV_TX_BUSY;
874 result = gelic_descr_prepare_tx(card, descr, skb);
877 * DMA map failed. As chances are that failure
878 * would continue, just release skb and return
880 netdev->stats.tx_dropped++;
881 dev_kfree_skb_any(skb);
882 spin_unlock_irqrestore(&card->tx_lock, flags);
886 * link this prepared descriptor to previous one
887 * to achieve high performance
889 descr->prev->next_descr_addr = cpu_to_be32(descr->bus_addr);
891 * as hardware descriptor is modified in the above lines,
892 * ensure that the hardware sees it
895 if (gelic_card_kick_txdma(card, descr)) {
898 * release descriptor which was just prepared
900 netdev->stats.tx_dropped++;
901 /* don't trigger BUG_ON() in gelic_descr_release_tx */
902 descr->data_status = cpu_to_be32(GELIC_DESCR_TX_TAIL);
903 gelic_descr_release_tx(card, descr);
905 card->tx_chain.head = descr;
906 /* reset hw termination */
907 descr->prev->next_descr_addr = 0;
908 dev_info(ctodev(card), "%s: kick failure\n", __func__);
911 spin_unlock_irqrestore(&card->tx_lock, flags);
916 * gelic_net_pass_skb_up - takes an skb from a descriptor and passes it on
917 * @descr: descriptor to process
918 * @card: card structure
919 * @netdev: net_device structure to be passed packet
921 * iommu-unmaps the skb, fills out skb structure and passes the data to the
922 * stack. The descriptor state is not changed.
924 static void gelic_net_pass_skb_up(struct gelic_descr *descr,
925 struct gelic_card *card,
926 struct net_device *netdev)
929 struct sk_buff *skb = descr->skb;
930 u32 data_status, data_error;
932 data_status = be32_to_cpu(descr->data_status);
933 data_error = be32_to_cpu(descr->data_error);
934 /* unmap skb buffer */
935 dma_unmap_single(ctodev(card), be32_to_cpu(descr->buf_addr),
939 skb_put(skb, be32_to_cpu(descr->valid_size)?
940 be32_to_cpu(descr->valid_size) :
941 be32_to_cpu(descr->result_size));
942 if (!descr->valid_size)
943 dev_info(ctodev(card), "buffer full %x %x %x\n",
944 be32_to_cpu(descr->result_size),
945 be32_to_cpu(descr->buf_size),
946 be32_to_cpu(descr->dmac_cmd_status));
950 * the card put 2 bytes vlan tag in front
951 * of the ethernet frame
954 skb->protocol = eth_type_trans(skb, netdev);
956 /* checksum offload */
957 if (netdev->features & NETIF_F_RXCSUM) {
958 if ((data_status & GELIC_DESCR_DATA_STATUS_CHK_MASK) &&
959 (!(data_error & GELIC_DESCR_DATA_ERROR_CHK_MASK)))
960 skb->ip_summed = CHECKSUM_UNNECESSARY;
962 skb_checksum_none_assert(skb);
964 skb_checksum_none_assert(skb);
966 /* update netdevice statistics */
967 netdev->stats.rx_packets++;
968 netdev->stats.rx_bytes += skb->len;
970 /* pass skb up to stack */
971 netif_receive_skb(skb);
975 * gelic_card_decode_one_descr - processes an rx descriptor
976 * @card: card structure
978 * returns 1 if a packet has been sent to the stack, otherwise 0
980 * processes an rx descriptor by iommu-unmapping the data buffer and passing
981 * the packet up to the stack
983 static int gelic_card_decode_one_descr(struct gelic_card *card)
985 enum gelic_descr_dma_status status;
986 struct gelic_descr_chain *chain = &card->rx_chain;
987 struct gelic_descr *descr = chain->head;
988 struct net_device *netdev = NULL;
989 int dmac_chain_ended;
991 status = gelic_descr_get_status(descr);
993 if (status == GELIC_DESCR_DMA_CARDOWNED)
996 if (status == GELIC_DESCR_DMA_NOT_IN_USE) {
997 dev_dbg(ctodev(card), "dormant descr? %p\n", descr);
1001 /* netdevice select */
1002 if (card->vlan_required) {
1005 vid = *(u16 *)(descr->skb->data) & VLAN_VID_MASK;
1006 for (i = 0; i < GELIC_PORT_MAX; i++) {
1007 if (card->vlan[i].rx == vid) {
1008 netdev = card->netdev[i];
1012 if (GELIC_PORT_MAX <= i) {
1013 pr_info("%s: unknown packet vid=%x\n", __func__, vid);
1017 netdev = card->netdev[GELIC_PORT_ETHERNET_0];
1019 if ((status == GELIC_DESCR_DMA_RESPONSE_ERROR) ||
1020 (status == GELIC_DESCR_DMA_PROTECTION_ERROR) ||
1021 (status == GELIC_DESCR_DMA_FORCE_END)) {
1022 dev_info(ctodev(card), "dropping RX descriptor with state %x\n",
1024 netdev->stats.rx_dropped++;
1028 if (status == GELIC_DESCR_DMA_BUFFER_FULL) {
1030 * Buffer full would occur if and only if
1031 * the frame length was longer than the size of this
1032 * descriptor's buffer. If the frame length was equal
1033 * to or shorter than buffer'size, FRAME_END condition
1035 * Anyway this frame was longer than the MTU,
1038 dev_info(ctodev(card), "overlength frame\n");
1042 * descriptors any other than FRAME_END here should
1043 * be treated as error.
1045 if (status != GELIC_DESCR_DMA_FRAME_END) {
1046 dev_dbg(ctodev(card), "RX descriptor with state %x\n",
1051 /* ok, we've got a packet in descr */
1052 gelic_net_pass_skb_up(descr, card, netdev);
1055 /* is the current descriptor terminated with next_descr == NULL? */
1057 be32_to_cpu(descr->dmac_cmd_status) &
1058 GELIC_DESCR_RX_DMA_CHAIN_END;
1060 * So that always DMAC can see the end
1061 * of the descriptor chain to avoid
1062 * from unwanted DMAC overrun.
1064 descr->next_descr_addr = 0;
1066 /* change the descriptor state: */
1067 gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
1070 * this call can fail, but for now, just leave this
1071 * descriptor without skb
1073 gelic_descr_prepare_rx(card, descr);
1075 chain->tail = descr;
1076 chain->head = descr->next;
1079 * Set this descriptor the end of the chain.
1081 descr->prev->next_descr_addr = cpu_to_be32(descr->bus_addr);
1084 * If dmac chain was met, DMAC stopped.
1088 if (dmac_chain_ended)
1089 gelic_card_enable_rxdmac(card);
1095 * gelic_net_poll - NAPI poll function called by the stack to return packets
1096 * @napi: napi structure
1097 * @budget: number of packets we can pass to the stack at most
1099 * returns the number of the processed packets
1102 static int gelic_net_poll(struct napi_struct *napi, int budget)
1104 struct gelic_card *card = container_of(napi, struct gelic_card, napi);
1105 int packets_done = 0;
1107 while (packets_done < budget) {
1108 if (!gelic_card_decode_one_descr(card))
1114 if (packets_done < budget) {
1115 napi_complete_done(napi, packets_done);
1116 gelic_card_rx_irq_on(card);
1118 return packets_done;
1122 * gelic_card_interrupt - event handler for gelic_net
1124 static irqreturn_t gelic_card_interrupt(int irq, void *ptr)
1126 unsigned long flags;
1127 struct gelic_card *card = ptr;
1130 status = card->irq_status;
1135 status &= card->irq_mask;
1137 if (status & GELIC_CARD_RXINT) {
1138 gelic_card_rx_irq_off(card);
1139 napi_schedule(&card->napi);
1142 if (status & GELIC_CARD_TXINT) {
1143 spin_lock_irqsave(&card->tx_lock, flags);
1144 card->tx_dma_progress = 0;
1145 gelic_card_release_tx_chain(card, 0);
1146 /* kick outstanding tx descriptor if any */
1147 gelic_card_kick_txdma(card, card->tx_chain.tail);
1148 spin_unlock_irqrestore(&card->tx_lock, flags);
1151 /* ether port status changed */
1152 if (status & GELIC_CARD_PORT_STATUS_CHANGED)
1153 gelic_card_get_ether_port_status(card, 1);
1155 #ifdef CONFIG_GELIC_WIRELESS
1156 if (status & (GELIC_CARD_WLAN_EVENT_RECEIVED |
1157 GELIC_CARD_WLAN_COMMAND_COMPLETED))
1158 gelic_wl_interrupt(card->netdev[GELIC_PORT_WIRELESS], status);
1164 #ifdef CONFIG_NET_POLL_CONTROLLER
1166 * gelic_net_poll_controller - artificial interrupt for netconsole etc.
1167 * @netdev: interface device structure
1169 * see Documentation/networking/netconsole.txt
1171 void gelic_net_poll_controller(struct net_device *netdev)
1173 struct gelic_card *card = netdev_card(netdev);
1175 gelic_card_set_irq_mask(card, 0);
1176 gelic_card_interrupt(netdev->irq, netdev);
1177 gelic_card_set_irq_mask(card, card->irq_mask);
1179 #endif /* CONFIG_NET_POLL_CONTROLLER */
1182 * gelic_net_open - called upon ifconfig up
1183 * @netdev: interface device structure
1185 * returns 0 on success, <0 on failure
1187 * gelic_net_open allocates all the descriptors and memory needed for
1188 * operation, sets up multicast list and enables interrupts
1190 int gelic_net_open(struct net_device *netdev)
1192 struct gelic_card *card = netdev_card(netdev);
1194 dev_dbg(ctodev(card), " -> %s %p\n", __func__, netdev);
1196 gelic_card_up(card);
1198 netif_start_queue(netdev);
1199 gelic_card_get_ether_port_status(card, 1);
1201 dev_dbg(ctodev(card), " <- %s\n", __func__);
1205 void gelic_net_get_drvinfo(struct net_device *netdev,
1206 struct ethtool_drvinfo *info)
1208 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
1209 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
1212 static int gelic_ether_get_link_ksettings(struct net_device *netdev,
1213 struct ethtool_link_ksettings *cmd)
1215 struct gelic_card *card = netdev_card(netdev);
1216 u32 supported, advertising;
1218 gelic_card_get_ether_port_status(card, 0);
1220 if (card->ether_port_status & GELIC_LV1_ETHER_FULL_DUPLEX)
1221 cmd->base.duplex = DUPLEX_FULL;
1223 cmd->base.duplex = DUPLEX_HALF;
1225 switch (card->ether_port_status & GELIC_LV1_ETHER_SPEED_MASK) {
1226 case GELIC_LV1_ETHER_SPEED_10:
1227 cmd->base.speed = SPEED_10;
1229 case GELIC_LV1_ETHER_SPEED_100:
1230 cmd->base.speed = SPEED_100;
1232 case GELIC_LV1_ETHER_SPEED_1000:
1233 cmd->base.speed = SPEED_1000;
1236 pr_info("%s: speed unknown\n", __func__);
1237 cmd->base.speed = SPEED_10;
1241 supported = SUPPORTED_TP | SUPPORTED_Autoneg |
1242 SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full |
1243 SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full |
1244 SUPPORTED_1000baseT_Full;
1245 advertising = supported;
1246 if (card->link_mode & GELIC_LV1_ETHER_AUTO_NEG) {
1247 cmd->base.autoneg = AUTONEG_ENABLE;
1249 cmd->base.autoneg = AUTONEG_DISABLE;
1250 advertising &= ~ADVERTISED_Autoneg;
1252 cmd->base.port = PORT_TP;
1254 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
1256 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
1263 gelic_ether_set_link_ksettings(struct net_device *netdev,
1264 const struct ethtool_link_ksettings *cmd)
1266 struct gelic_card *card = netdev_card(netdev);
1270 if (cmd->base.autoneg == AUTONEG_ENABLE) {
1271 mode = GELIC_LV1_ETHER_AUTO_NEG;
1273 switch (cmd->base.speed) {
1275 mode = GELIC_LV1_ETHER_SPEED_10;
1278 mode = GELIC_LV1_ETHER_SPEED_100;
1281 mode = GELIC_LV1_ETHER_SPEED_1000;
1286 if (cmd->base.duplex == DUPLEX_FULL) {
1287 mode |= GELIC_LV1_ETHER_FULL_DUPLEX;
1288 } else if (cmd->base.speed == SPEED_1000) {
1289 pr_info("1000 half duplex is not supported.\n");
1294 ret = gelic_card_set_link_mode(card, mode);
1302 static void gelic_net_get_wol(struct net_device *netdev,
1303 struct ethtool_wolinfo *wol)
1305 if (0 <= ps3_compare_firmware_version(2, 2, 0))
1306 wol->supported = WAKE_MAGIC;
1310 wol->wolopts = ps3_sys_manager_get_wol() ? wol->supported : 0;
1311 memset(&wol->sopass, 0, sizeof(wol->sopass));
1313 static int gelic_net_set_wol(struct net_device *netdev,
1314 struct ethtool_wolinfo *wol)
1317 struct gelic_card *card;
1320 if (ps3_compare_firmware_version(2, 2, 0) < 0 ||
1321 !capable(CAP_NET_ADMIN))
1324 if (wol->wolopts & ~WAKE_MAGIC)
1327 card = netdev_card(netdev);
1328 if (wol->wolopts & WAKE_MAGIC) {
1329 status = lv1_net_control(bus_id(card), dev_id(card),
1331 GELIC_LV1_WOL_MAGIC_PACKET,
1332 0, GELIC_LV1_WOL_MP_ENABLE,
1335 pr_info("%s: enabling WOL failed %d\n", __func__,
1340 status = lv1_net_control(bus_id(card), dev_id(card),
1342 GELIC_LV1_WOL_ADD_MATCH_ADDR,
1343 0, GELIC_LV1_WOL_MATCH_ALL,
1346 ps3_sys_manager_set_wol(1);
1348 pr_info("%s: enabling WOL filter failed %d\n",
1353 status = lv1_net_control(bus_id(card), dev_id(card),
1355 GELIC_LV1_WOL_MAGIC_PACKET,
1356 0, GELIC_LV1_WOL_MP_DISABLE,
1359 pr_info("%s: disabling WOL failed %d\n", __func__,
1364 status = lv1_net_control(bus_id(card), dev_id(card),
1366 GELIC_LV1_WOL_DELETE_MATCH_ADDR,
1367 0, GELIC_LV1_WOL_MATCH_ALL,
1370 ps3_sys_manager_set_wol(0);
1372 pr_info("%s: removing WOL filter failed %d\n",
1381 static const struct ethtool_ops gelic_ether_ethtool_ops = {
1382 .get_drvinfo = gelic_net_get_drvinfo,
1383 .get_link = ethtool_op_get_link,
1384 .get_wol = gelic_net_get_wol,
1385 .set_wol = gelic_net_set_wol,
1386 .get_link_ksettings = gelic_ether_get_link_ksettings,
1387 .set_link_ksettings = gelic_ether_set_link_ksettings,
1391 * gelic_net_tx_timeout_task - task scheduled by the watchdog timeout
1392 * function (to be called not under interrupt status)
1393 * @work: work is context of tx timout task
1395 * called as task when tx hangs, resets interface (if interface is up)
1397 static void gelic_net_tx_timeout_task(struct work_struct *work)
1399 struct gelic_card *card =
1400 container_of(work, struct gelic_card, tx_timeout_task);
1401 struct net_device *netdev = card->netdev[GELIC_PORT_ETHERNET_0];
1403 dev_info(ctodev(card), "%s:Timed out. Restarting...\n", __func__);
1405 if (!(netdev->flags & IFF_UP))
1408 netif_device_detach(netdev);
1409 gelic_net_stop(netdev);
1411 gelic_net_open(netdev);
1412 netif_device_attach(netdev);
1415 atomic_dec(&card->tx_timeout_task_counter);
1419 * gelic_net_tx_timeout - called when the tx timeout watchdog kicks in.
1420 * @netdev: interface device structure
1422 * called, if tx hangs. Schedules a task that resets the interface
1424 void gelic_net_tx_timeout(struct net_device *netdev)
1426 struct gelic_card *card;
1428 card = netdev_card(netdev);
1429 atomic_inc(&card->tx_timeout_task_counter);
1430 if (netdev->flags & IFF_UP)
1431 schedule_work(&card->tx_timeout_task);
1433 atomic_dec(&card->tx_timeout_task_counter);
1436 static const struct net_device_ops gelic_netdevice_ops = {
1437 .ndo_open = gelic_net_open,
1438 .ndo_stop = gelic_net_stop,
1439 .ndo_start_xmit = gelic_net_xmit,
1440 .ndo_set_rx_mode = gelic_net_set_multi,
1441 .ndo_tx_timeout = gelic_net_tx_timeout,
1442 .ndo_set_mac_address = eth_mac_addr,
1443 .ndo_validate_addr = eth_validate_addr,
1444 #ifdef CONFIG_NET_POLL_CONTROLLER
1445 .ndo_poll_controller = gelic_net_poll_controller,
1450 * gelic_ether_setup_netdev_ops - initialization of net_device operations
1451 * @netdev: net_device structure
1453 * fills out function pointers in the net_device structure
1455 static void gelic_ether_setup_netdev_ops(struct net_device *netdev,
1456 struct napi_struct *napi)
1458 netdev->watchdog_timeo = GELIC_NET_WATCHDOG_TIMEOUT;
1460 netif_napi_add(netdev, napi, gelic_net_poll, NAPI_POLL_WEIGHT);
1461 netdev->ethtool_ops = &gelic_ether_ethtool_ops;
1462 netdev->netdev_ops = &gelic_netdevice_ops;
1466 * gelic_ether_setup_netdev - initialization of net_device
1467 * @netdev: net_device structure
1468 * @card: card structure
1470 * Returns 0 on success or <0 on failure
1472 * gelic_ether_setup_netdev initializes the net_device structure
1475 int gelic_net_setup_netdev(struct net_device *netdev, struct gelic_card *card)
1480 netdev->hw_features = NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
1482 netdev->features = NETIF_F_IP_CSUM;
1483 if (GELIC_CARD_RX_CSUM_DEFAULT)
1484 netdev->features |= NETIF_F_RXCSUM;
1486 status = lv1_net_control(bus_id(card), dev_id(card),
1487 GELIC_LV1_GET_MAC_ADDRESS,
1490 if (status || !is_valid_ether_addr((u8 *)&v1)) {
1491 dev_info(ctodev(card),
1492 "%s:lv1_net_control GET_MAC_ADDR failed %d\n",
1496 memcpy(netdev->dev_addr, &v1, ETH_ALEN);
1498 if (card->vlan_required) {
1499 netdev->hard_header_len += VLAN_HLEN;
1501 * As vlan is internally used,
1502 * we can not receive vlan packets
1504 netdev->features |= NETIF_F_VLAN_CHALLENGED;
1507 /* MTU range: 64 - 1518 */
1508 netdev->min_mtu = GELIC_NET_MIN_MTU;
1509 netdev->max_mtu = GELIC_NET_MAX_MTU;
1511 status = register_netdev(netdev);
1513 dev_err(ctodev(card), "%s:Couldn't register %s %d\n",
1514 __func__, netdev->name, status);
1517 dev_info(ctodev(card), "%s: MAC addr %pM\n",
1518 netdev->name, netdev->dev_addr);
1524 * gelic_alloc_card_net - allocates net_device and card structure
1526 * returns the card structure or NULL in case of errors
1528 * the card and net_device structures are linked to each other
1530 #define GELIC_ALIGN (32)
1531 static struct gelic_card *gelic_alloc_card_net(struct net_device **netdev)
1533 struct gelic_card *card;
1534 struct gelic_port *port;
1538 * gelic requires dma descriptor is 32 bytes aligned and
1539 * the hypervisor requires irq_status is 8 bytes aligned.
1541 BUILD_BUG_ON(offsetof(struct gelic_card, irq_status) % 8);
1542 BUILD_BUG_ON(offsetof(struct gelic_card, descr) % 32);
1544 sizeof(struct gelic_card) +
1545 sizeof(struct gelic_descr) * GELIC_NET_RX_DESCRIPTORS +
1546 sizeof(struct gelic_descr) * GELIC_NET_TX_DESCRIPTORS +
1549 p = kzalloc(alloc_size, GFP_KERNEL);
1552 card = PTR_ALIGN(p, GELIC_ALIGN);
1558 *netdev = alloc_etherdev(sizeof(struct gelic_port));
1560 kfree(card->unalign);
1563 port = netdev_priv(*netdev);
1566 port->netdev = *netdev;
1568 port->type = GELIC_PORT_ETHERNET_0;
1571 card->netdev[GELIC_PORT_ETHERNET_0] = *netdev;
1573 INIT_WORK(&card->tx_timeout_task, gelic_net_tx_timeout_task);
1574 init_waitqueue_head(&card->waitq);
1575 atomic_set(&card->tx_timeout_task_counter, 0);
1576 mutex_init(&card->updown_lock);
1577 atomic_set(&card->users, 0);
1582 static void gelic_card_get_vlan_info(struct gelic_card *card)
1591 [GELIC_PORT_ETHERNET_0] = {
1592 .tx = GELIC_LV1_VLAN_TX_ETHERNET_0,
1593 .rx = GELIC_LV1_VLAN_RX_ETHERNET_0
1595 [GELIC_PORT_WIRELESS] = {
1596 .tx = GELIC_LV1_VLAN_TX_WIRELESS,
1597 .rx = GELIC_LV1_VLAN_RX_WIRELESS
1601 for (i = 0; i < ARRAY_SIZE(vlan_id_ix); i++) {
1603 status = lv1_net_control(bus_id(card), dev_id(card),
1604 GELIC_LV1_GET_VLAN_ID,
1607 if (status || !v1) {
1608 if (status != LV1_NO_ENTRY)
1609 dev_dbg(ctodev(card),
1610 "get vlan id for tx(%d) failed(%d)\n",
1611 vlan_id_ix[i].tx, status);
1612 card->vlan[i].tx = 0;
1613 card->vlan[i].rx = 0;
1616 card->vlan[i].tx = (u16)v1;
1619 status = lv1_net_control(bus_id(card), dev_id(card),
1620 GELIC_LV1_GET_VLAN_ID,
1623 if (status || !v1) {
1624 if (status != LV1_NO_ENTRY)
1625 dev_info(ctodev(card),
1626 "get vlan id for rx(%d) failed(%d)\n",
1627 vlan_id_ix[i].rx, status);
1628 card->vlan[i].tx = 0;
1629 card->vlan[i].rx = 0;
1632 card->vlan[i].rx = (u16)v1;
1634 dev_dbg(ctodev(card), "vlan_id[%d] tx=%02x rx=%02x\n",
1635 i, card->vlan[i].tx, card->vlan[i].rx);
1638 if (card->vlan[GELIC_PORT_ETHERNET_0].tx) {
1639 BUG_ON(!card->vlan[GELIC_PORT_WIRELESS].tx);
1640 card->vlan_required = 1;
1642 card->vlan_required = 0;
1644 /* check wirelss capable firmware */
1645 if (ps3_compare_firmware_version(1, 6, 0) < 0) {
1646 card->vlan[GELIC_PORT_WIRELESS].tx = 0;
1647 card->vlan[GELIC_PORT_WIRELESS].rx = 0;
1650 dev_info(ctodev(card), "internal vlan %s\n",
1651 card->vlan_required? "enabled" : "disabled");
1654 * ps3_gelic_driver_probe - add a device to the control of this driver
1656 static int ps3_gelic_driver_probe(struct ps3_system_bus_device *dev)
1658 struct gelic_card *card;
1659 struct net_device *netdev;
1662 pr_debug("%s: called\n", __func__);
1664 udbg_shutdown_ps3gelic();
1666 result = ps3_open_hv_device(dev);
1669 dev_dbg(&dev->core, "%s:ps3_open_hv_device failed\n",
1674 result = ps3_dma_region_create(dev->d_region);
1677 dev_dbg(&dev->core, "%s:ps3_dma_region_create failed(%d)\n",
1679 BUG_ON("check region type");
1680 goto fail_dma_region;
1683 /* alloc card/netdevice */
1684 card = gelic_alloc_card_net(&netdev);
1686 dev_info(&dev->core, "%s:gelic_net_alloc_card failed\n",
1689 goto fail_alloc_card;
1691 ps3_system_bus_set_drvdata(dev, card);
1694 /* get internal vlan info */
1695 gelic_card_get_vlan_info(card);
1697 card->link_mode = GELIC_LV1_ETHER_AUTO_NEG;
1699 /* setup interrupt */
1700 result = lv1_net_set_interrupt_status_indicator(bus_id(card),
1702 ps3_mm_phys_to_lpar(__pa(&card->irq_status)),
1707 "%s:set_interrupt_status_indicator failed: %s\n",
1708 __func__, ps3_result(result));
1710 goto fail_status_indicator;
1713 result = ps3_sb_event_receive_port_setup(dev, PS3_BINDING_CPU_ANY,
1717 dev_info(ctodev(card),
1718 "%s:gelic_net_open_device failed (%d)\n",
1721 goto fail_alloc_irq;
1723 result = request_irq(card->irq, gelic_card_interrupt,
1724 0, netdev->name, card);
1727 dev_info(ctodev(card), "%s:request_irq failed (%d)\n",
1729 goto fail_request_irq;
1732 /* setup card structure */
1733 card->irq_mask = GELIC_CARD_RXINT | GELIC_CARD_TXINT |
1734 GELIC_CARD_PORT_STATUS_CHANGED;
1737 result = gelic_card_init_chain(card, &card->tx_chain,
1738 card->descr, GELIC_NET_TX_DESCRIPTORS);
1741 result = gelic_card_init_chain(card, &card->rx_chain,
1742 card->descr + GELIC_NET_TX_DESCRIPTORS,
1743 GELIC_NET_RX_DESCRIPTORS);
1748 card->tx_top = card->tx_chain.head;
1749 card->rx_top = card->rx_chain.head;
1750 dev_dbg(ctodev(card), "descr rx %p, tx %p, size %#lx, num %#x\n",
1751 card->rx_top, card->tx_top, sizeof(struct gelic_descr),
1752 GELIC_NET_RX_DESCRIPTORS);
1753 /* allocate rx skbs */
1754 result = gelic_card_alloc_rx_skbs(card);
1756 goto fail_alloc_skbs;
1758 spin_lock_init(&card->tx_lock);
1759 card->tx_dma_progress = 0;
1761 /* setup net_device structure */
1762 netdev->irq = card->irq;
1763 SET_NETDEV_DEV(netdev, &card->dev->core);
1764 gelic_ether_setup_netdev_ops(netdev, &card->napi);
1765 result = gelic_net_setup_netdev(netdev, card);
1767 dev_dbg(&dev->core, "%s: setup_netdev failed %d\n",
1769 goto fail_setup_netdev;
1772 #ifdef CONFIG_GELIC_WIRELESS
1773 result = gelic_wl_driver_probe(card);
1775 dev_dbg(&dev->core, "%s: WL init failed\n", __func__);
1776 goto fail_setup_netdev;
1779 pr_debug("%s: done\n", __func__);
1784 gelic_card_free_chain(card, card->rx_chain.head);
1786 gelic_card_free_chain(card, card->tx_chain.head);
1788 free_irq(card->irq, card);
1791 ps3_sb_event_receive_port_destroy(dev, card->irq);
1793 lv1_net_set_interrupt_status_indicator(bus_id(card),
1796 fail_status_indicator:
1797 ps3_system_bus_set_drvdata(dev, NULL);
1798 kfree(netdev_card(netdev)->unalign);
1799 free_netdev(netdev);
1801 ps3_dma_region_free(dev->d_region);
1803 ps3_close_hv_device(dev);
1809 * ps3_gelic_driver_remove - remove a device from the control of this driver
1812 static int ps3_gelic_driver_remove(struct ps3_system_bus_device *dev)
1814 struct gelic_card *card = ps3_system_bus_get_drvdata(dev);
1815 struct net_device *netdev0;
1816 pr_debug("%s: called\n", __func__);
1818 /* set auto-negotiation */
1819 gelic_card_set_link_mode(card, GELIC_LV1_ETHER_AUTO_NEG);
1821 #ifdef CONFIG_GELIC_WIRELESS
1822 gelic_wl_driver_remove(card);
1824 /* stop interrupt */
1825 gelic_card_set_irq_mask(card, 0);
1827 /* turn off DMA, force end */
1828 gelic_card_disable_rxdmac(card);
1829 gelic_card_disable_txdmac(card);
1831 /* release chains */
1832 gelic_card_release_tx_chain(card, 1);
1833 gelic_card_release_rx_chain(card);
1835 gelic_card_free_chain(card, card->tx_top);
1836 gelic_card_free_chain(card, card->rx_top);
1838 netdev0 = card->netdev[GELIC_PORT_ETHERNET_0];
1839 /* disconnect event port */
1840 free_irq(card->irq, card);
1842 ps3_sb_event_receive_port_destroy(card->dev, card->irq);
1844 wait_event(card->waitq,
1845 atomic_read(&card->tx_timeout_task_counter) == 0);
1847 lv1_net_set_interrupt_status_indicator(bus_id(card), dev_id(card),
1850 unregister_netdev(netdev0);
1851 kfree(netdev_card(netdev0)->unalign);
1852 free_netdev(netdev0);
1854 ps3_system_bus_set_drvdata(dev, NULL);
1856 ps3_dma_region_free(dev->d_region);
1858 ps3_close_hv_device(dev);
1860 pr_debug("%s: done\n", __func__);
1864 static struct ps3_system_bus_driver ps3_gelic_driver = {
1865 .match_id = PS3_MATCH_ID_GELIC,
1866 .probe = ps3_gelic_driver_probe,
1867 .remove = ps3_gelic_driver_remove,
1868 .shutdown = ps3_gelic_driver_remove,
1869 .core.name = "ps3_gelic_driver",
1870 .core.owner = THIS_MODULE,
1873 static int __init ps3_gelic_driver_init (void)
1875 return firmware_has_feature(FW_FEATURE_PS3_LV1)
1876 ? ps3_system_bus_driver_register(&ps3_gelic_driver)
1880 static void __exit ps3_gelic_driver_exit (void)
1882 ps3_system_bus_driver_unregister(&ps3_gelic_driver);
1885 module_init(ps3_gelic_driver_init);
1886 module_exit(ps3_gelic_driver_exit);
1888 MODULE_ALIAS(PS3_MODULE_ALIAS_GELIC);