1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * PS3 gelic network driver.
5 * Copyright (C) 2007 Sony Computer Entertainment Inc.
6 * Copyright 2006, 2007 Sony Corporation
8 * This file is based on: spider_net.c
10 * (C) Copyright IBM Corp. 2005
12 * Authors : Utz Bacher <utz.bacher@de.ibm.com>
13 * Jens Osterkamp <Jens.Osterkamp@de.ibm.com>
18 #include <linux/interrupt.h>
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/slab.h>
23 #include <linux/etherdevice.h>
24 #include <linux/ethtool.h>
25 #include <linux/if_vlan.h>
29 #include <linux/tcp.h>
31 #include <linux/dma-mapping.h>
32 #include <net/checksum.h>
33 #include <asm/firmware.h>
35 #include <asm/lv1call.h>
37 #include "ps3_gelic_net.h"
38 #include "ps3_gelic_wireless.h"
40 #define DRV_NAME "Gelic Network Driver"
41 #define DRV_VERSION "2.0"
43 MODULE_AUTHOR("SCE Inc.");
44 MODULE_DESCRIPTION("Gelic Network driver");
45 MODULE_LICENSE("GPL");
49 int gelic_card_set_irq_mask(struct gelic_card *card, u64 mask)
53 status = lv1_net_set_interrupt_mask(bus_id(card), dev_id(card),
56 dev_info(ctodev(card),
57 "%s failed %d\n", __func__, status);
61 static void gelic_card_rx_irq_on(struct gelic_card *card)
63 card->irq_mask |= GELIC_CARD_RXINT;
64 gelic_card_set_irq_mask(card, card->irq_mask);
66 static void gelic_card_rx_irq_off(struct gelic_card *card)
68 card->irq_mask &= ~GELIC_CARD_RXINT;
69 gelic_card_set_irq_mask(card, card->irq_mask);
72 static void gelic_card_get_ether_port_status(struct gelic_card *card,
76 struct net_device *ether_netdev;
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);
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);
88 netif_carrier_off(ether_netdev);
93 * gelic_descr_get_status -- returns the status of a descriptor
94 * @descr: descriptor to look at
96 * returns the status as in the dmac_cmd_status field of the descriptor
98 static enum gelic_descr_dma_status
99 gelic_descr_get_status(struct gelic_descr *descr)
101 return be32_to_cpu(descr->dmac_cmd_status) & GELIC_DESCR_DMA_STAT_MASK;
104 static int gelic_card_set_link_mode(struct gelic_card *card, int mode)
109 status = lv1_net_control(bus_id(card), dev_id(card),
110 GELIC_LV1_SET_NEGOTIATION_MODE,
111 GELIC_LV1_PHY_ETHERNET_0, mode, 0, &v1, &v2);
113 pr_info("%s: failed setting negotiation mode %d\n", __func__,
118 card->link_mode = mode;
123 * gelic_card_disable_txdmac - disables the transmit DMA controller
124 * @card: card structure
126 * gelic_card_disable_txdmac terminates processing on the DMA controller by
127 * turing off DMA and issuing a force end
129 static void gelic_card_disable_txdmac(struct gelic_card *card)
133 /* this hvc blocks until the DMA in progress really stopped */
134 status = lv1_net_stop_tx_dma(bus_id(card), dev_id(card));
136 dev_err(ctodev(card),
137 "lv1_net_stop_tx_dma failed, status=%d\n", status);
141 * gelic_card_enable_rxdmac - enables the receive DMA controller
142 * @card: card structure
144 * gelic_card_enable_rxdmac enables the DMA controller by setting RX_DMA_EN
145 * in the GDADMACCNTR register
147 static void gelic_card_enable_rxdmac(struct gelic_card *card)
152 if (gelic_descr_get_status(card->rx_chain.head) !=
153 GELIC_DESCR_DMA_CARDOWNED) {
154 printk(KERN_ERR "%s: status=%x\n", __func__,
155 be32_to_cpu(card->rx_chain.head->dmac_cmd_status));
156 printk(KERN_ERR "%s: nextphy=%x\n", __func__,
157 be32_to_cpu(card->rx_chain.head->next_descr_addr));
158 printk(KERN_ERR "%s: head=%p\n", __func__,
159 card->rx_chain.head);
162 status = lv1_net_start_rx_dma(bus_id(card), dev_id(card),
163 card->rx_chain.head->bus_addr, 0);
165 dev_info(ctodev(card),
166 "lv1_net_start_rx_dma failed, status=%d\n", status);
170 * gelic_card_disable_rxdmac - disables the receive DMA controller
171 * @card: card structure
173 * gelic_card_disable_rxdmac terminates processing on the DMA controller by
174 * turing off DMA and issuing a force end
176 static void gelic_card_disable_rxdmac(struct gelic_card *card)
180 /* this hvc blocks until the DMA in progress really stopped */
181 status = lv1_net_stop_rx_dma(bus_id(card), dev_id(card));
183 dev_err(ctodev(card),
184 "lv1_net_stop_rx_dma failed, %d\n", status);
188 * gelic_descr_set_status -- sets the status of a descriptor
189 * @descr: descriptor to change
190 * @status: status to set in the descriptor
192 * changes the status to the specified value. Doesn't change other bits
195 static void gelic_descr_set_status(struct gelic_descr *descr,
196 enum gelic_descr_dma_status status)
198 descr->dmac_cmd_status = cpu_to_be32(status |
199 (be32_to_cpu(descr->dmac_cmd_status) &
200 ~GELIC_DESCR_DMA_STAT_MASK));
202 * dma_cmd_status field is used to indicate whether the descriptor
204 * Usually caller of this function wants to inform that to the
205 * hardware, so we assure here the hardware sees the change.
211 * gelic_card_reset_chain - reset status of a descriptor chain
212 * @card: card structure
213 * @chain: address of chain
214 * @start_descr: address of descriptor array
216 * Reset the status of dma descriptors to ready state
217 * and re-initialize the hardware chain for later use
219 static void gelic_card_reset_chain(struct gelic_card *card,
220 struct gelic_descr_chain *chain,
221 struct gelic_descr *start_descr)
223 struct gelic_descr *descr;
225 for (descr = start_descr; start_descr != descr->next; descr++) {
226 gelic_descr_set_status(descr, GELIC_DESCR_DMA_CARDOWNED);
227 descr->next_descr_addr = cpu_to_be32(descr->next->bus_addr);
230 chain->head = start_descr;
231 chain->tail = (descr - 1);
233 (descr - 1)->next_descr_addr = 0;
236 void gelic_card_up(struct gelic_card *card)
238 pr_debug("%s: called\n", __func__);
239 mutex_lock(&card->updown_lock);
240 if (atomic_inc_return(&card->users) == 1) {
241 pr_debug("%s: real do\n", __func__);
243 gelic_card_set_irq_mask(card, card->irq_mask);
245 gelic_card_enable_rxdmac(card);
247 napi_enable(&card->napi);
249 mutex_unlock(&card->updown_lock);
250 pr_debug("%s: done\n", __func__);
253 void gelic_card_down(struct gelic_card *card)
256 pr_debug("%s: called\n", __func__);
257 mutex_lock(&card->updown_lock);
258 if (atomic_dec_if_positive(&card->users) == 0) {
259 pr_debug("%s: real do\n", __func__);
260 napi_disable(&card->napi);
262 * Disable irq. Wireless interrupts will
263 * be disabled later if any
265 mask = card->irq_mask & (GELIC_CARD_WLAN_EVENT_RECEIVED |
266 GELIC_CARD_WLAN_COMMAND_COMPLETED);
267 gelic_card_set_irq_mask(card, mask);
269 gelic_card_disable_rxdmac(card);
270 gelic_card_reset_chain(card, &card->rx_chain,
271 card->descr + GELIC_NET_TX_DESCRIPTORS);
273 gelic_card_disable_txdmac(card);
275 mutex_unlock(&card->updown_lock);
276 pr_debug("%s: done\n", __func__);
280 * gelic_card_free_chain - free descriptor chain
281 * @card: card structure
282 * @descr_in: address of desc
284 static void gelic_card_free_chain(struct gelic_card *card,
285 struct gelic_descr *descr_in)
287 struct gelic_descr *descr;
289 for (descr = descr_in; descr && descr->bus_addr; descr = descr->next) {
290 dma_unmap_single(ctodev(card), descr->bus_addr,
291 GELIC_DESCR_SIZE, DMA_BIDIRECTIONAL);
297 * gelic_card_init_chain - links descriptor chain
298 * @card: card structure
299 * @chain: address of chain
300 * @start_descr: address of descriptor array
301 * @no: number of descriptors
303 * we manage a circular list that mirrors the hardware structure,
304 * except that the hardware uses bus addresses.
306 * returns 0 on success, <0 on failure
308 static int gelic_card_init_chain(struct gelic_card *card,
309 struct gelic_descr_chain *chain,
310 struct gelic_descr *start_descr, int no)
313 struct gelic_descr *descr;
316 memset(descr, 0, sizeof(*descr) * no);
318 /* set up the hardware pointers in each descriptor */
319 for (i = 0; i < no; i++, descr++) {
320 gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
322 dma_map_single(ctodev(card), descr,
326 if (!descr->bus_addr)
329 descr->next = descr + 1;
330 descr->prev = descr - 1;
332 /* make them as ring */
333 (descr - 1)->next = start_descr;
334 start_descr->prev = (descr - 1);
336 /* chain bus addr of hw descriptor */
338 for (i = 0; i < no; i++, descr++) {
339 descr->next_descr_addr = cpu_to_be32(descr->next->bus_addr);
342 chain->head = start_descr;
343 chain->tail = start_descr;
345 /* do not chain last hw descriptor */
346 (descr - 1)->next_descr_addr = 0;
351 for (i--, descr--; 0 <= i; i--, descr--)
353 dma_unmap_single(ctodev(card), descr->bus_addr,
360 * gelic_descr_prepare_rx - reinitializes a rx descriptor
361 * @card: card structure
362 * @descr: descriptor to re-init
364 * return 0 on success, <0 on failure
366 * allocates a new rx skb, iommu-maps it and attaches it to the descriptor.
367 * Activate the descriptor state-wise
369 static int gelic_descr_prepare_rx(struct gelic_card *card,
370 struct gelic_descr *descr)
373 unsigned int bufsize;
375 if (gelic_descr_get_status(descr) != GELIC_DESCR_DMA_NOT_IN_USE)
376 dev_info(ctodev(card), "%s: ERROR status\n", __func__);
377 /* we need to round up the buffer size to a multiple of 128 */
378 bufsize = ALIGN(GELIC_NET_MAX_MTU, GELIC_NET_RXBUF_ALIGN);
380 /* and we need to have it 128 byte aligned, therefore we allocate a
382 descr->skb = dev_alloc_skb(bufsize + GELIC_NET_RXBUF_ALIGN - 1);
384 descr->buf_addr = 0; /* tell DMAC don't touch memory */
387 descr->buf_size = cpu_to_be32(bufsize);
388 descr->dmac_cmd_status = 0;
389 descr->result_size = 0;
390 descr->valid_size = 0;
391 descr->data_error = 0;
393 offset = ((unsigned long)descr->skb->data) &
394 (GELIC_NET_RXBUF_ALIGN - 1);
396 skb_reserve(descr->skb, GELIC_NET_RXBUF_ALIGN - offset);
397 /* io-mmu-map the skb */
398 descr->buf_addr = cpu_to_be32(dma_map_single(ctodev(card),
402 if (!descr->buf_addr) {
403 dev_kfree_skb_any(descr->skb);
405 dev_info(ctodev(card),
406 "%s:Could not iommu-map rx buffer\n", __func__);
407 gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
410 gelic_descr_set_status(descr, GELIC_DESCR_DMA_CARDOWNED);
416 * gelic_card_release_rx_chain - free all skb of rx descr
417 * @card: card structure
420 static void gelic_card_release_rx_chain(struct gelic_card *card)
422 struct gelic_descr *descr = card->rx_chain.head;
426 dma_unmap_single(ctodev(card),
427 be32_to_cpu(descr->buf_addr),
431 dev_kfree_skb_any(descr->skb);
433 gelic_descr_set_status(descr,
434 GELIC_DESCR_DMA_NOT_IN_USE);
437 } while (descr != card->rx_chain.head);
441 * gelic_card_fill_rx_chain - fills descriptors/skbs in the rx chains
442 * @card: card structure
444 * fills all descriptors in the rx chain: allocates skbs
445 * and iommu-maps them.
446 * returns 0 on success, < 0 on failure
448 static int gelic_card_fill_rx_chain(struct gelic_card *card)
450 struct gelic_descr *descr = card->rx_chain.head;
455 ret = gelic_descr_prepare_rx(card, descr);
460 } while (descr != card->rx_chain.head);
464 gelic_card_release_rx_chain(card);
469 * gelic_card_alloc_rx_skbs - allocates rx skbs in rx descriptor chains
470 * @card: card structure
472 * returns 0 on success, < 0 on failure
474 static int gelic_card_alloc_rx_skbs(struct gelic_card *card)
476 struct gelic_descr_chain *chain;
478 chain = &card->rx_chain;
479 ret = gelic_card_fill_rx_chain(card);
480 chain->tail = card->rx_top->prev; /* point to the last */
485 * gelic_descr_release_tx - processes a used tx descriptor
486 * @card: card structure
487 * @descr: descriptor to release
489 * releases a used tx descriptor (unmapping, freeing of skb)
491 static void gelic_descr_release_tx(struct gelic_card *card,
492 struct gelic_descr *descr)
494 struct sk_buff *skb = descr->skb;
496 BUG_ON(!(be32_to_cpu(descr->data_status) & GELIC_DESCR_TX_TAIL));
498 dma_unmap_single(ctodev(card), be32_to_cpu(descr->buf_addr), skb->len,
500 dev_kfree_skb_any(skb);
504 descr->next_descr_addr = 0;
505 descr->result_size = 0;
506 descr->valid_size = 0;
507 descr->data_status = 0;
508 descr->data_error = 0;
511 /* set descr status */
512 gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
515 static void gelic_card_stop_queues(struct gelic_card *card)
517 netif_stop_queue(card->netdev[GELIC_PORT_ETHERNET_0]);
519 if (card->netdev[GELIC_PORT_WIRELESS])
520 netif_stop_queue(card->netdev[GELIC_PORT_WIRELESS]);
522 static void gelic_card_wake_queues(struct gelic_card *card)
524 netif_wake_queue(card->netdev[GELIC_PORT_ETHERNET_0]);
526 if (card->netdev[GELIC_PORT_WIRELESS])
527 netif_wake_queue(card->netdev[GELIC_PORT_WIRELESS]);
530 * gelic_card_release_tx_chain - processes sent tx descriptors
531 * @card: adapter structure
532 * @stop: net_stop sequence
534 * releases the tx descriptors that gelic has finished with
536 static void gelic_card_release_tx_chain(struct gelic_card *card, int stop)
538 struct gelic_descr_chain *tx_chain;
539 enum gelic_descr_dma_status status;
540 struct net_device *netdev;
543 for (tx_chain = &card->tx_chain;
544 tx_chain->head != tx_chain->tail && tx_chain->tail;
545 tx_chain->tail = tx_chain->tail->next) {
546 status = gelic_descr_get_status(tx_chain->tail);
547 netdev = tx_chain->tail->skb->dev;
549 case GELIC_DESCR_DMA_RESPONSE_ERROR:
550 case GELIC_DESCR_DMA_PROTECTION_ERROR:
551 case GELIC_DESCR_DMA_FORCE_END:
552 if (printk_ratelimit())
553 dev_info(ctodev(card),
554 "%s: forcing end of tx descriptor " \
557 netdev->stats.tx_dropped++;
560 case GELIC_DESCR_DMA_COMPLETE:
561 if (tx_chain->tail->skb) {
562 netdev->stats.tx_packets++;
563 netdev->stats.tx_bytes +=
564 tx_chain->tail->skb->len;
568 case GELIC_DESCR_DMA_CARDOWNED:
569 /* pending tx request */
571 /* any other value (== GELIC_DESCR_DMA_NOT_IN_USE) */
575 gelic_descr_release_tx(card, tx_chain->tail);
579 if (!stop && release)
580 gelic_card_wake_queues(card);
584 * gelic_net_set_multi - sets multicast addresses and promisc flags
585 * @netdev: interface device structure
587 * gelic_net_set_multi configures multicast addresses as needed for the
588 * netdev interface. It also sets up multicast, allmulti and promisc
589 * flags appropriately
591 void gelic_net_set_multi(struct net_device *netdev)
593 struct gelic_card *card = netdev_card(netdev);
594 struct netdev_hw_addr *ha;
600 /* clear all multicast address */
601 status = lv1_net_remove_multicast_address(bus_id(card), dev_id(card),
604 dev_err(ctodev(card),
605 "lv1_net_remove_multicast_address failed %d\n",
607 /* set broadcast address */
608 status = lv1_net_add_multicast_address(bus_id(card), dev_id(card),
609 GELIC_NET_BROADCAST_ADDR, 0);
611 dev_err(ctodev(card),
612 "lv1_net_add_multicast_address failed, %d\n",
615 if ((netdev->flags & IFF_ALLMULTI) ||
616 (netdev_mc_count(netdev) > GELIC_NET_MC_COUNT_MAX)) {
617 status = lv1_net_add_multicast_address(bus_id(card),
621 dev_err(ctodev(card),
622 "lv1_net_add_multicast_address failed, %d\n",
627 /* set multicast addresses */
628 netdev_for_each_mc_addr(ha, netdev) {
631 for (i = 0; i < ETH_ALEN; i++) {
635 status = lv1_net_add_multicast_address(bus_id(card),
639 dev_err(ctodev(card),
640 "lv1_net_add_multicast_address failed, %d\n",
646 * gelic_net_stop - called upon ifconfig down
647 * @netdev: interface device structure
651 int gelic_net_stop(struct net_device *netdev)
653 struct gelic_card *card;
655 pr_debug("%s: start\n", __func__);
657 netif_stop_queue(netdev);
658 netif_carrier_off(netdev);
660 card = netdev_card(netdev);
661 gelic_card_down(card);
663 pr_debug("%s: done\n", __func__);
668 * gelic_card_get_next_tx_descr - returns the next available tx descriptor
669 * @card: device structure to get descriptor from
671 * returns the address of the next descriptor, or NULL if not available.
673 static struct gelic_descr *
674 gelic_card_get_next_tx_descr(struct gelic_card *card)
676 if (!card->tx_chain.head)
678 /* see if the next descriptor is free */
679 if (card->tx_chain.tail != card->tx_chain.head->next &&
680 gelic_descr_get_status(card->tx_chain.head) ==
681 GELIC_DESCR_DMA_NOT_IN_USE)
682 return card->tx_chain.head;
689 * gelic_net_set_txdescr_cmdstat - sets the tx descriptor command field
690 * @descr: descriptor structure to fill out
691 * @skb: packet to consider
693 * fills out the command and status field of the descriptor structure,
694 * depending on hardware checksum settings. This function assumes a wmb()
695 * has executed before.
697 static void gelic_descr_set_tx_cmdstat(struct gelic_descr *descr,
700 if (skb->ip_summed != CHECKSUM_PARTIAL)
701 descr->dmac_cmd_status =
702 cpu_to_be32(GELIC_DESCR_DMA_CMD_NO_CHKSUM |
703 GELIC_DESCR_TX_DMA_FRAME_TAIL);
706 * if yes: tcp? udp? */
707 if (skb->protocol == htons(ETH_P_IP)) {
708 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
709 descr->dmac_cmd_status =
710 cpu_to_be32(GELIC_DESCR_DMA_CMD_TCP_CHKSUM |
711 GELIC_DESCR_TX_DMA_FRAME_TAIL);
713 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
714 descr->dmac_cmd_status =
715 cpu_to_be32(GELIC_DESCR_DMA_CMD_UDP_CHKSUM |
716 GELIC_DESCR_TX_DMA_FRAME_TAIL);
718 * the stack should checksum non-tcp and non-udp
719 * packets on his own: NETIF_F_IP_CSUM
721 descr->dmac_cmd_status =
722 cpu_to_be32(GELIC_DESCR_DMA_CMD_NO_CHKSUM |
723 GELIC_DESCR_TX_DMA_FRAME_TAIL);
728 static struct sk_buff *gelic_put_vlan_tag(struct sk_buff *skb,
731 struct vlan_ethhdr *veth;
732 static unsigned int c;
734 if (skb_headroom(skb) < VLAN_HLEN) {
735 struct sk_buff *sk_tmp = skb;
736 pr_debug("%s: hd=%d c=%ud\n", __func__, skb_headroom(skb), c);
737 skb = skb_realloc_headroom(sk_tmp, VLAN_HLEN);
740 dev_kfree_skb_any(sk_tmp);
742 veth = skb_push(skb, VLAN_HLEN);
744 /* Move the mac addresses to the top of buffer */
745 memmove(skb->data, skb->data + VLAN_HLEN, 2 * ETH_ALEN);
747 veth->h_vlan_proto = cpu_to_be16(ETH_P_8021Q);
748 veth->h_vlan_TCI = htons(tag);
754 * gelic_descr_prepare_tx - setup a descriptor for sending packets
755 * @card: card structure
756 * @descr: descriptor structure
757 * @skb: packet to use
759 * returns 0 on success, <0 on failure.
762 static int gelic_descr_prepare_tx(struct gelic_card *card,
763 struct gelic_descr *descr,
768 if (card->vlan_required) {
769 struct sk_buff *skb_tmp;
770 enum gelic_port_type type;
772 type = netdev_port(skb->dev)->type;
773 skb_tmp = gelic_put_vlan_tag(skb,
774 card->vlan[type].tx);
780 buf = dma_map_single(ctodev(card), skb->data, skb->len, DMA_TO_DEVICE);
783 dev_err(ctodev(card),
784 "dma map 2 failed (%p, %i). Dropping packet\n",
785 skb->data, skb->len);
789 descr->buf_addr = cpu_to_be32(buf);
790 descr->buf_size = cpu_to_be32(skb->len);
792 descr->data_status = 0;
793 descr->next_descr_addr = 0; /* terminate hw descr */
794 gelic_descr_set_tx_cmdstat(descr, skb);
796 /* bump free descriptor pointer */
797 card->tx_chain.head = descr->next;
802 * gelic_card_kick_txdma - enables TX DMA processing
803 * @card: card structure
804 * @descr: descriptor address to enable TX processing at
807 static int gelic_card_kick_txdma(struct gelic_card *card,
808 struct gelic_descr *descr)
812 if (card->tx_dma_progress)
815 if (gelic_descr_get_status(descr) == GELIC_DESCR_DMA_CARDOWNED) {
816 card->tx_dma_progress = 1;
817 status = lv1_net_start_tx_dma(bus_id(card), dev_id(card),
820 card->tx_dma_progress = 0;
821 dev_info(ctodev(card), "lv1_net_start_txdma failed," \
822 "status=%d\n", status);
829 * gelic_net_xmit - transmits a frame over the device
830 * @skb: packet to send out
831 * @netdev: interface device structure
833 * returns NETDEV_TX_OK on success, NETDEV_TX_BUSY on failure
835 netdev_tx_t gelic_net_xmit(struct sk_buff *skb, struct net_device *netdev)
837 struct gelic_card *card = netdev_card(netdev);
838 struct gelic_descr *descr;
842 spin_lock_irqsave(&card->tx_lock, flags);
844 gelic_card_release_tx_chain(card, 0);
846 descr = gelic_card_get_next_tx_descr(card);
849 * no more descriptors free
851 gelic_card_stop_queues(card);
852 spin_unlock_irqrestore(&card->tx_lock, flags);
853 return NETDEV_TX_BUSY;
856 result = gelic_descr_prepare_tx(card, descr, skb);
859 * DMA map failed. As chances are that failure
860 * would continue, just release skb and return
862 netdev->stats.tx_dropped++;
863 dev_kfree_skb_any(skb);
864 spin_unlock_irqrestore(&card->tx_lock, flags);
868 * link this prepared descriptor to previous one
869 * to achieve high performance
871 descr->prev->next_descr_addr = cpu_to_be32(descr->bus_addr);
873 * as hardware descriptor is modified in the above lines,
874 * ensure that the hardware sees it
877 if (gelic_card_kick_txdma(card, descr)) {
880 * release descriptor which was just prepared
882 netdev->stats.tx_dropped++;
883 /* don't trigger BUG_ON() in gelic_descr_release_tx */
884 descr->data_status = cpu_to_be32(GELIC_DESCR_TX_TAIL);
885 gelic_descr_release_tx(card, descr);
887 card->tx_chain.head = descr;
888 /* reset hw termination */
889 descr->prev->next_descr_addr = 0;
890 dev_info(ctodev(card), "%s: kick failure\n", __func__);
893 spin_unlock_irqrestore(&card->tx_lock, flags);
898 * gelic_net_pass_skb_up - takes an skb from a descriptor and passes it on
899 * @descr: descriptor to process
900 * @card: card structure
901 * @netdev: net_device structure to be passed packet
903 * iommu-unmaps the skb, fills out skb structure and passes the data to the
904 * stack. The descriptor state is not changed.
906 static void gelic_net_pass_skb_up(struct gelic_descr *descr,
907 struct gelic_card *card,
908 struct net_device *netdev)
911 struct sk_buff *skb = descr->skb;
912 u32 data_status, data_error;
914 data_status = be32_to_cpu(descr->data_status);
915 data_error = be32_to_cpu(descr->data_error);
916 /* unmap skb buffer */
917 dma_unmap_single(ctodev(card), be32_to_cpu(descr->buf_addr),
921 skb_put(skb, be32_to_cpu(descr->valid_size)?
922 be32_to_cpu(descr->valid_size) :
923 be32_to_cpu(descr->result_size));
924 if (!descr->valid_size)
925 dev_info(ctodev(card), "buffer full %x %x %x\n",
926 be32_to_cpu(descr->result_size),
927 be32_to_cpu(descr->buf_size),
928 be32_to_cpu(descr->dmac_cmd_status));
932 * the card put 2 bytes vlan tag in front
933 * of the ethernet frame
936 skb->protocol = eth_type_trans(skb, netdev);
938 /* checksum offload */
939 if (netdev->features & NETIF_F_RXCSUM) {
940 if ((data_status & GELIC_DESCR_DATA_STATUS_CHK_MASK) &&
941 (!(data_error & GELIC_DESCR_DATA_ERROR_CHK_MASK)))
942 skb->ip_summed = CHECKSUM_UNNECESSARY;
944 skb_checksum_none_assert(skb);
946 skb_checksum_none_assert(skb);
948 /* update netdevice statistics */
949 netdev->stats.rx_packets++;
950 netdev->stats.rx_bytes += skb->len;
952 /* pass skb up to stack */
953 netif_receive_skb(skb);
957 * gelic_card_decode_one_descr - processes an rx descriptor
958 * @card: card structure
960 * returns 1 if a packet has been sent to the stack, otherwise 0
962 * processes an rx descriptor by iommu-unmapping the data buffer and passing
963 * the packet up to the stack
965 static int gelic_card_decode_one_descr(struct gelic_card *card)
967 enum gelic_descr_dma_status status;
968 struct gelic_descr_chain *chain = &card->rx_chain;
969 struct gelic_descr *descr = chain->head;
970 struct net_device *netdev = NULL;
971 int dmac_chain_ended;
973 status = gelic_descr_get_status(descr);
975 if (status == GELIC_DESCR_DMA_CARDOWNED)
978 if (status == GELIC_DESCR_DMA_NOT_IN_USE) {
979 dev_dbg(ctodev(card), "dormant descr? %p\n", descr);
983 /* netdevice select */
984 if (card->vlan_required) {
987 vid = *(u16 *)(descr->skb->data) & VLAN_VID_MASK;
988 for (i = 0; i < GELIC_PORT_MAX; i++) {
989 if (card->vlan[i].rx == vid) {
990 netdev = card->netdev[i];
994 if (GELIC_PORT_MAX <= i) {
995 pr_info("%s: unknown packet vid=%x\n", __func__, vid);
999 netdev = card->netdev[GELIC_PORT_ETHERNET_0];
1001 if ((status == GELIC_DESCR_DMA_RESPONSE_ERROR) ||
1002 (status == GELIC_DESCR_DMA_PROTECTION_ERROR) ||
1003 (status == GELIC_DESCR_DMA_FORCE_END)) {
1004 dev_info(ctodev(card), "dropping RX descriptor with state %x\n",
1006 netdev->stats.rx_dropped++;
1010 if (status == GELIC_DESCR_DMA_BUFFER_FULL) {
1012 * Buffer full would occur if and only if
1013 * the frame length was longer than the size of this
1014 * descriptor's buffer. If the frame length was equal
1015 * to or shorter than buffer'size, FRAME_END condition
1017 * Anyway this frame was longer than the MTU,
1020 dev_info(ctodev(card), "overlength frame\n");
1024 * descriptors any other than FRAME_END here should
1025 * be treated as error.
1027 if (status != GELIC_DESCR_DMA_FRAME_END) {
1028 dev_dbg(ctodev(card), "RX descriptor with state %x\n",
1033 /* ok, we've got a packet in descr */
1034 gelic_net_pass_skb_up(descr, card, netdev);
1037 /* is the current descriptor terminated with next_descr == NULL? */
1039 be32_to_cpu(descr->dmac_cmd_status) &
1040 GELIC_DESCR_RX_DMA_CHAIN_END;
1042 * So that always DMAC can see the end
1043 * of the descriptor chain to avoid
1044 * from unwanted DMAC overrun.
1046 descr->next_descr_addr = 0;
1048 /* change the descriptor state: */
1049 gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
1052 * this call can fail, but for now, just leave this
1053 * descriptor without skb
1055 gelic_descr_prepare_rx(card, descr);
1057 chain->tail = descr;
1058 chain->head = descr->next;
1061 * Set this descriptor the end of the chain.
1063 descr->prev->next_descr_addr = cpu_to_be32(descr->bus_addr);
1066 * If dmac chain was met, DMAC stopped.
1070 if (dmac_chain_ended)
1071 gelic_card_enable_rxdmac(card);
1077 * gelic_net_poll - NAPI poll function called by the stack to return packets
1078 * @napi: napi structure
1079 * @budget: number of packets we can pass to the stack at most
1081 * returns the number of the processed packets
1084 static int gelic_net_poll(struct napi_struct *napi, int budget)
1086 struct gelic_card *card = container_of(napi, struct gelic_card, napi);
1087 int packets_done = 0;
1089 while (packets_done < budget) {
1090 if (!gelic_card_decode_one_descr(card))
1096 if (packets_done < budget) {
1097 napi_complete_done(napi, packets_done);
1098 gelic_card_rx_irq_on(card);
1100 return packets_done;
1104 * gelic_card_interrupt - event handler for gelic_net
1106 static irqreturn_t gelic_card_interrupt(int irq, void *ptr)
1108 unsigned long flags;
1109 struct gelic_card *card = ptr;
1112 status = card->irq_status;
1117 status &= card->irq_mask;
1119 if (status & GELIC_CARD_RXINT) {
1120 gelic_card_rx_irq_off(card);
1121 napi_schedule(&card->napi);
1124 if (status & GELIC_CARD_TXINT) {
1125 spin_lock_irqsave(&card->tx_lock, flags);
1126 card->tx_dma_progress = 0;
1127 gelic_card_release_tx_chain(card, 0);
1128 /* kick outstanding tx descriptor if any */
1129 gelic_card_kick_txdma(card, card->tx_chain.tail);
1130 spin_unlock_irqrestore(&card->tx_lock, flags);
1133 /* ether port status changed */
1134 if (status & GELIC_CARD_PORT_STATUS_CHANGED)
1135 gelic_card_get_ether_port_status(card, 1);
1137 #ifdef CONFIG_GELIC_WIRELESS
1138 if (status & (GELIC_CARD_WLAN_EVENT_RECEIVED |
1139 GELIC_CARD_WLAN_COMMAND_COMPLETED))
1140 gelic_wl_interrupt(card->netdev[GELIC_PORT_WIRELESS], status);
1146 #ifdef CONFIG_NET_POLL_CONTROLLER
1148 * gelic_net_poll_controller - artificial interrupt for netconsole etc.
1149 * @netdev: interface device structure
1151 * see Documentation/networking/netconsole.rst
1153 void gelic_net_poll_controller(struct net_device *netdev)
1155 struct gelic_card *card = netdev_card(netdev);
1157 gelic_card_set_irq_mask(card, 0);
1158 gelic_card_interrupt(netdev->irq, netdev);
1159 gelic_card_set_irq_mask(card, card->irq_mask);
1161 #endif /* CONFIG_NET_POLL_CONTROLLER */
1164 * gelic_net_open - called upon ifconfig up
1165 * @netdev: interface device structure
1167 * returns 0 on success, <0 on failure
1169 * gelic_net_open allocates all the descriptors and memory needed for
1170 * operation, sets up multicast list and enables interrupts
1172 int gelic_net_open(struct net_device *netdev)
1174 struct gelic_card *card = netdev_card(netdev);
1176 dev_dbg(ctodev(card), " -> %s %p\n", __func__, netdev);
1178 gelic_card_up(card);
1180 netif_start_queue(netdev);
1181 gelic_card_get_ether_port_status(card, 1);
1183 dev_dbg(ctodev(card), " <- %s\n", __func__);
1187 void gelic_net_get_drvinfo(struct net_device *netdev,
1188 struct ethtool_drvinfo *info)
1190 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
1191 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
1194 static int gelic_ether_get_link_ksettings(struct net_device *netdev,
1195 struct ethtool_link_ksettings *cmd)
1197 struct gelic_card *card = netdev_card(netdev);
1198 u32 supported, advertising;
1200 gelic_card_get_ether_port_status(card, 0);
1202 if (card->ether_port_status & GELIC_LV1_ETHER_FULL_DUPLEX)
1203 cmd->base.duplex = DUPLEX_FULL;
1205 cmd->base.duplex = DUPLEX_HALF;
1207 switch (card->ether_port_status & GELIC_LV1_ETHER_SPEED_MASK) {
1208 case GELIC_LV1_ETHER_SPEED_10:
1209 cmd->base.speed = SPEED_10;
1211 case GELIC_LV1_ETHER_SPEED_100:
1212 cmd->base.speed = SPEED_100;
1214 case GELIC_LV1_ETHER_SPEED_1000:
1215 cmd->base.speed = SPEED_1000;
1218 pr_info("%s: speed unknown\n", __func__);
1219 cmd->base.speed = SPEED_10;
1223 supported = SUPPORTED_TP | SUPPORTED_Autoneg |
1224 SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full |
1225 SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full |
1226 SUPPORTED_1000baseT_Full;
1227 advertising = supported;
1228 if (card->link_mode & GELIC_LV1_ETHER_AUTO_NEG) {
1229 cmd->base.autoneg = AUTONEG_ENABLE;
1231 cmd->base.autoneg = AUTONEG_DISABLE;
1232 advertising &= ~ADVERTISED_Autoneg;
1234 cmd->base.port = PORT_TP;
1236 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
1238 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
1245 gelic_ether_set_link_ksettings(struct net_device *netdev,
1246 const struct ethtool_link_ksettings *cmd)
1248 struct gelic_card *card = netdev_card(netdev);
1252 if (cmd->base.autoneg == AUTONEG_ENABLE) {
1253 mode = GELIC_LV1_ETHER_AUTO_NEG;
1255 switch (cmd->base.speed) {
1257 mode = GELIC_LV1_ETHER_SPEED_10;
1260 mode = GELIC_LV1_ETHER_SPEED_100;
1263 mode = GELIC_LV1_ETHER_SPEED_1000;
1268 if (cmd->base.duplex == DUPLEX_FULL) {
1269 mode |= GELIC_LV1_ETHER_FULL_DUPLEX;
1270 } else if (cmd->base.speed == SPEED_1000) {
1271 pr_info("1000 half duplex is not supported.\n");
1276 ret = gelic_card_set_link_mode(card, mode);
1284 static void gelic_net_get_wol(struct net_device *netdev,
1285 struct ethtool_wolinfo *wol)
1287 if (0 <= ps3_compare_firmware_version(2, 2, 0))
1288 wol->supported = WAKE_MAGIC;
1292 wol->wolopts = ps3_sys_manager_get_wol() ? wol->supported : 0;
1293 memset(&wol->sopass, 0, sizeof(wol->sopass));
1295 static int gelic_net_set_wol(struct net_device *netdev,
1296 struct ethtool_wolinfo *wol)
1299 struct gelic_card *card;
1302 if (ps3_compare_firmware_version(2, 2, 0) < 0 ||
1303 !capable(CAP_NET_ADMIN))
1306 if (wol->wolopts & ~WAKE_MAGIC)
1309 card = netdev_card(netdev);
1310 if (wol->wolopts & WAKE_MAGIC) {
1311 status = lv1_net_control(bus_id(card), dev_id(card),
1313 GELIC_LV1_WOL_MAGIC_PACKET,
1314 0, GELIC_LV1_WOL_MP_ENABLE,
1317 pr_info("%s: enabling WOL failed %d\n", __func__,
1322 status = lv1_net_control(bus_id(card), dev_id(card),
1324 GELIC_LV1_WOL_ADD_MATCH_ADDR,
1325 0, GELIC_LV1_WOL_MATCH_ALL,
1328 ps3_sys_manager_set_wol(1);
1330 pr_info("%s: enabling WOL filter failed %d\n",
1335 status = lv1_net_control(bus_id(card), dev_id(card),
1337 GELIC_LV1_WOL_MAGIC_PACKET,
1338 0, GELIC_LV1_WOL_MP_DISABLE,
1341 pr_info("%s: disabling WOL failed %d\n", __func__,
1346 status = lv1_net_control(bus_id(card), dev_id(card),
1348 GELIC_LV1_WOL_DELETE_MATCH_ADDR,
1349 0, GELIC_LV1_WOL_MATCH_ALL,
1352 ps3_sys_manager_set_wol(0);
1354 pr_info("%s: removing WOL filter failed %d\n",
1363 static const struct ethtool_ops gelic_ether_ethtool_ops = {
1364 .get_drvinfo = gelic_net_get_drvinfo,
1365 .get_link = ethtool_op_get_link,
1366 .get_wol = gelic_net_get_wol,
1367 .set_wol = gelic_net_set_wol,
1368 .get_link_ksettings = gelic_ether_get_link_ksettings,
1369 .set_link_ksettings = gelic_ether_set_link_ksettings,
1373 * gelic_net_tx_timeout_task - task scheduled by the watchdog timeout
1374 * function (to be called not under interrupt status)
1375 * @work: work is context of tx timout task
1377 * called as task when tx hangs, resets interface (if interface is up)
1379 static void gelic_net_tx_timeout_task(struct work_struct *work)
1381 struct gelic_card *card =
1382 container_of(work, struct gelic_card, tx_timeout_task);
1383 struct net_device *netdev = card->netdev[GELIC_PORT_ETHERNET_0];
1385 dev_info(ctodev(card), "%s:Timed out. Restarting...\n", __func__);
1387 if (!(netdev->flags & IFF_UP))
1390 netif_device_detach(netdev);
1391 gelic_net_stop(netdev);
1393 gelic_net_open(netdev);
1394 netif_device_attach(netdev);
1397 atomic_dec(&card->tx_timeout_task_counter);
1401 * gelic_net_tx_timeout - called when the tx timeout watchdog kicks in.
1402 * @netdev: interface device structure
1404 * called, if tx hangs. Schedules a task that resets the interface
1406 void gelic_net_tx_timeout(struct net_device *netdev, unsigned int txqueue)
1408 struct gelic_card *card;
1410 card = netdev_card(netdev);
1411 atomic_inc(&card->tx_timeout_task_counter);
1412 if (netdev->flags & IFF_UP)
1413 schedule_work(&card->tx_timeout_task);
1415 atomic_dec(&card->tx_timeout_task_counter);
1418 static const struct net_device_ops gelic_netdevice_ops = {
1419 .ndo_open = gelic_net_open,
1420 .ndo_stop = gelic_net_stop,
1421 .ndo_start_xmit = gelic_net_xmit,
1422 .ndo_set_rx_mode = gelic_net_set_multi,
1423 .ndo_tx_timeout = gelic_net_tx_timeout,
1424 .ndo_set_mac_address = eth_mac_addr,
1425 .ndo_validate_addr = eth_validate_addr,
1426 #ifdef CONFIG_NET_POLL_CONTROLLER
1427 .ndo_poll_controller = gelic_net_poll_controller,
1432 * gelic_ether_setup_netdev_ops - initialization of net_device operations
1433 * @netdev: net_device structure
1435 * fills out function pointers in the net_device structure
1437 static void gelic_ether_setup_netdev_ops(struct net_device *netdev,
1438 struct napi_struct *napi)
1440 netdev->watchdog_timeo = GELIC_NET_WATCHDOG_TIMEOUT;
1442 netif_napi_add(netdev, napi, gelic_net_poll, NAPI_POLL_WEIGHT);
1443 netdev->ethtool_ops = &gelic_ether_ethtool_ops;
1444 netdev->netdev_ops = &gelic_netdevice_ops;
1448 * gelic_ether_setup_netdev - initialization of net_device
1449 * @netdev: net_device structure
1450 * @card: card structure
1452 * Returns 0 on success or <0 on failure
1454 * gelic_ether_setup_netdev initializes the net_device structure
1457 int gelic_net_setup_netdev(struct net_device *netdev, struct gelic_card *card)
1462 netdev->hw_features = NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
1464 netdev->features = NETIF_F_IP_CSUM;
1465 if (GELIC_CARD_RX_CSUM_DEFAULT)
1466 netdev->features |= NETIF_F_RXCSUM;
1468 status = lv1_net_control(bus_id(card), dev_id(card),
1469 GELIC_LV1_GET_MAC_ADDRESS,
1472 if (status || !is_valid_ether_addr((u8 *)&v1)) {
1473 dev_info(ctodev(card),
1474 "%s:lv1_net_control GET_MAC_ADDR failed %d\n",
1478 memcpy(netdev->dev_addr, &v1, ETH_ALEN);
1480 if (card->vlan_required) {
1481 netdev->hard_header_len += VLAN_HLEN;
1483 * As vlan is internally used,
1484 * we can not receive vlan packets
1486 netdev->features |= NETIF_F_VLAN_CHALLENGED;
1489 /* MTU range: 64 - 1518 */
1490 netdev->min_mtu = GELIC_NET_MIN_MTU;
1491 netdev->max_mtu = GELIC_NET_MAX_MTU;
1493 status = register_netdev(netdev);
1495 dev_err(ctodev(card), "%s:Couldn't register %s %d\n",
1496 __func__, netdev->name, status);
1499 dev_info(ctodev(card), "%s: MAC addr %pM\n",
1500 netdev->name, netdev->dev_addr);
1506 * gelic_alloc_card_net - allocates net_device and card structure
1508 * returns the card structure or NULL in case of errors
1510 * the card and net_device structures are linked to each other
1512 #define GELIC_ALIGN (32)
1513 static struct gelic_card *gelic_alloc_card_net(struct net_device **netdev)
1515 struct gelic_card *card;
1516 struct gelic_port *port;
1520 * gelic requires dma descriptor is 32 bytes aligned and
1521 * the hypervisor requires irq_status is 8 bytes aligned.
1523 BUILD_BUG_ON(offsetof(struct gelic_card, irq_status) % 8);
1524 BUILD_BUG_ON(offsetof(struct gelic_card, descr) % 32);
1526 sizeof(struct gelic_card) +
1527 sizeof(struct gelic_descr) * GELIC_NET_RX_DESCRIPTORS +
1528 sizeof(struct gelic_descr) * GELIC_NET_TX_DESCRIPTORS +
1531 p = kzalloc(alloc_size, GFP_KERNEL);
1534 card = PTR_ALIGN(p, GELIC_ALIGN);
1540 *netdev = alloc_etherdev(sizeof(struct gelic_port));
1542 kfree(card->unalign);
1545 port = netdev_priv(*netdev);
1548 port->netdev = *netdev;
1550 port->type = GELIC_PORT_ETHERNET_0;
1553 card->netdev[GELIC_PORT_ETHERNET_0] = *netdev;
1555 INIT_WORK(&card->tx_timeout_task, gelic_net_tx_timeout_task);
1556 init_waitqueue_head(&card->waitq);
1557 atomic_set(&card->tx_timeout_task_counter, 0);
1558 mutex_init(&card->updown_lock);
1559 atomic_set(&card->users, 0);
1564 static void gelic_card_get_vlan_info(struct gelic_card *card)
1573 [GELIC_PORT_ETHERNET_0] = {
1574 .tx = GELIC_LV1_VLAN_TX_ETHERNET_0,
1575 .rx = GELIC_LV1_VLAN_RX_ETHERNET_0
1577 [GELIC_PORT_WIRELESS] = {
1578 .tx = GELIC_LV1_VLAN_TX_WIRELESS,
1579 .rx = GELIC_LV1_VLAN_RX_WIRELESS
1583 for (i = 0; i < ARRAY_SIZE(vlan_id_ix); i++) {
1585 status = lv1_net_control(bus_id(card), dev_id(card),
1586 GELIC_LV1_GET_VLAN_ID,
1589 if (status || !v1) {
1590 if (status != LV1_NO_ENTRY)
1591 dev_dbg(ctodev(card),
1592 "get vlan id for tx(%d) failed(%d)\n",
1593 vlan_id_ix[i].tx, status);
1594 card->vlan[i].tx = 0;
1595 card->vlan[i].rx = 0;
1598 card->vlan[i].tx = (u16)v1;
1601 status = lv1_net_control(bus_id(card), dev_id(card),
1602 GELIC_LV1_GET_VLAN_ID,
1605 if (status || !v1) {
1606 if (status != LV1_NO_ENTRY)
1607 dev_info(ctodev(card),
1608 "get vlan id for rx(%d) failed(%d)\n",
1609 vlan_id_ix[i].rx, status);
1610 card->vlan[i].tx = 0;
1611 card->vlan[i].rx = 0;
1614 card->vlan[i].rx = (u16)v1;
1616 dev_dbg(ctodev(card), "vlan_id[%d] tx=%02x rx=%02x\n",
1617 i, card->vlan[i].tx, card->vlan[i].rx);
1620 if (card->vlan[GELIC_PORT_ETHERNET_0].tx) {
1621 BUG_ON(!card->vlan[GELIC_PORT_WIRELESS].tx);
1622 card->vlan_required = 1;
1624 card->vlan_required = 0;
1626 /* check wirelss capable firmware */
1627 if (ps3_compare_firmware_version(1, 6, 0) < 0) {
1628 card->vlan[GELIC_PORT_WIRELESS].tx = 0;
1629 card->vlan[GELIC_PORT_WIRELESS].rx = 0;
1632 dev_info(ctodev(card), "internal vlan %s\n",
1633 card->vlan_required? "enabled" : "disabled");
1636 * ps3_gelic_driver_probe - add a device to the control of this driver
1638 static int ps3_gelic_driver_probe(struct ps3_system_bus_device *dev)
1640 struct gelic_card *card;
1641 struct net_device *netdev;
1644 pr_debug("%s: called\n", __func__);
1646 udbg_shutdown_ps3gelic();
1648 result = ps3_open_hv_device(dev);
1651 dev_dbg(&dev->core, "%s:ps3_open_hv_device failed\n",
1656 result = ps3_dma_region_create(dev->d_region);
1659 dev_dbg(&dev->core, "%s:ps3_dma_region_create failed(%d)\n",
1661 BUG_ON("check region type");
1662 goto fail_dma_region;
1665 /* alloc card/netdevice */
1666 card = gelic_alloc_card_net(&netdev);
1668 dev_info(&dev->core, "%s:gelic_net_alloc_card failed\n",
1671 goto fail_alloc_card;
1673 ps3_system_bus_set_drvdata(dev, card);
1676 /* get internal vlan info */
1677 gelic_card_get_vlan_info(card);
1679 card->link_mode = GELIC_LV1_ETHER_AUTO_NEG;
1681 /* setup interrupt */
1682 result = lv1_net_set_interrupt_status_indicator(bus_id(card),
1684 ps3_mm_phys_to_lpar(__pa(&card->irq_status)),
1689 "%s:set_interrupt_status_indicator failed: %s\n",
1690 __func__, ps3_result(result));
1692 goto fail_status_indicator;
1695 result = ps3_sb_event_receive_port_setup(dev, PS3_BINDING_CPU_ANY,
1699 dev_info(ctodev(card),
1700 "%s:gelic_net_open_device failed (%d)\n",
1703 goto fail_alloc_irq;
1705 result = request_irq(card->irq, gelic_card_interrupt,
1706 0, netdev->name, card);
1709 dev_info(ctodev(card), "%s:request_irq failed (%d)\n",
1711 goto fail_request_irq;
1714 /* setup card structure */
1715 card->irq_mask = GELIC_CARD_RXINT | GELIC_CARD_TXINT |
1716 GELIC_CARD_PORT_STATUS_CHANGED;
1719 result = gelic_card_init_chain(card, &card->tx_chain,
1720 card->descr, GELIC_NET_TX_DESCRIPTORS);
1723 result = gelic_card_init_chain(card, &card->rx_chain,
1724 card->descr + GELIC_NET_TX_DESCRIPTORS,
1725 GELIC_NET_RX_DESCRIPTORS);
1730 card->tx_top = card->tx_chain.head;
1731 card->rx_top = card->rx_chain.head;
1732 dev_dbg(ctodev(card), "descr rx %p, tx %p, size %#lx, num %#x\n",
1733 card->rx_top, card->tx_top, sizeof(struct gelic_descr),
1734 GELIC_NET_RX_DESCRIPTORS);
1735 /* allocate rx skbs */
1736 result = gelic_card_alloc_rx_skbs(card);
1738 goto fail_alloc_skbs;
1740 spin_lock_init(&card->tx_lock);
1741 card->tx_dma_progress = 0;
1743 /* setup net_device structure */
1744 netdev->irq = card->irq;
1745 SET_NETDEV_DEV(netdev, &card->dev->core);
1746 gelic_ether_setup_netdev_ops(netdev, &card->napi);
1747 result = gelic_net_setup_netdev(netdev, card);
1749 dev_dbg(&dev->core, "%s: setup_netdev failed %d\n",
1751 goto fail_setup_netdev;
1754 #ifdef CONFIG_GELIC_WIRELESS
1755 result = gelic_wl_driver_probe(card);
1757 dev_dbg(&dev->core, "%s: WL init failed\n", __func__);
1758 goto fail_setup_netdev;
1761 pr_debug("%s: done\n", __func__);
1766 gelic_card_free_chain(card, card->rx_chain.head);
1768 gelic_card_free_chain(card, card->tx_chain.head);
1770 free_irq(card->irq, card);
1773 ps3_sb_event_receive_port_destroy(dev, card->irq);
1775 lv1_net_set_interrupt_status_indicator(bus_id(card),
1778 fail_status_indicator:
1779 ps3_system_bus_set_drvdata(dev, NULL);
1780 kfree(netdev_card(netdev)->unalign);
1781 free_netdev(netdev);
1783 ps3_dma_region_free(dev->d_region);
1785 ps3_close_hv_device(dev);
1791 * ps3_gelic_driver_remove - remove a device from the control of this driver
1794 static int ps3_gelic_driver_remove(struct ps3_system_bus_device *dev)
1796 struct gelic_card *card = ps3_system_bus_get_drvdata(dev);
1797 struct net_device *netdev0;
1798 pr_debug("%s: called\n", __func__);
1800 /* set auto-negotiation */
1801 gelic_card_set_link_mode(card, GELIC_LV1_ETHER_AUTO_NEG);
1803 #ifdef CONFIG_GELIC_WIRELESS
1804 gelic_wl_driver_remove(card);
1806 /* stop interrupt */
1807 gelic_card_set_irq_mask(card, 0);
1809 /* turn off DMA, force end */
1810 gelic_card_disable_rxdmac(card);
1811 gelic_card_disable_txdmac(card);
1813 /* release chains */
1814 gelic_card_release_tx_chain(card, 1);
1815 gelic_card_release_rx_chain(card);
1817 gelic_card_free_chain(card, card->tx_top);
1818 gelic_card_free_chain(card, card->rx_top);
1820 netdev0 = card->netdev[GELIC_PORT_ETHERNET_0];
1821 /* disconnect event port */
1822 free_irq(card->irq, card);
1824 ps3_sb_event_receive_port_destroy(card->dev, card->irq);
1826 wait_event(card->waitq,
1827 atomic_read(&card->tx_timeout_task_counter) == 0);
1829 lv1_net_set_interrupt_status_indicator(bus_id(card), dev_id(card),
1832 unregister_netdev(netdev0);
1833 kfree(netdev_card(netdev0)->unalign);
1834 free_netdev(netdev0);
1836 ps3_system_bus_set_drvdata(dev, NULL);
1838 ps3_dma_region_free(dev->d_region);
1840 ps3_close_hv_device(dev);
1842 pr_debug("%s: done\n", __func__);
1846 static struct ps3_system_bus_driver ps3_gelic_driver = {
1847 .match_id = PS3_MATCH_ID_GELIC,
1848 .probe = ps3_gelic_driver_probe,
1849 .remove = ps3_gelic_driver_remove,
1850 .shutdown = ps3_gelic_driver_remove,
1851 .core.name = "ps3_gelic_driver",
1852 .core.owner = THIS_MODULE,
1855 static int __init ps3_gelic_driver_init (void)
1857 return firmware_has_feature(FW_FEATURE_PS3_LV1)
1858 ? ps3_system_bus_driver_register(&ps3_gelic_driver)
1862 static void __exit ps3_gelic_driver_exit (void)
1864 ps3_system_bus_driver_unregister(&ps3_gelic_driver);
1867 module_init(ps3_gelic_driver_init);
1868 module_exit(ps3_gelic_driver_exit);
1870 MODULE_ALIAS(PS3_MODULE_ALIAS_GELIC);