Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *  PS3 gelic network driver.
0004  *
0005  * Copyright (C) 2007 Sony Computer Entertainment Inc.
0006  * Copyright 2006, 2007 Sony Corporation
0007  *
0008  * This file is based on: spider_net.c
0009  *
0010  * (C) Copyright IBM Corp. 2005
0011  *
0012  * Authors : Utz Bacher <utz.bacher@de.ibm.com>
0013  *           Jens Osterkamp <Jens.Osterkamp@de.ibm.com>
0014  */
0015 
0016 #undef DEBUG
0017 
0018 #include <linux/interrupt.h>
0019 #include <linux/kernel.h>
0020 #include <linux/module.h>
0021 #include <linux/slab.h>
0022 
0023 #include <linux/etherdevice.h>
0024 #include <linux/ethtool.h>
0025 #include <linux/if_vlan.h>
0026 
0027 #include <linux/in.h>
0028 #include <linux/ip.h>
0029 #include <linux/tcp.h>
0030 
0031 #include <linux/dma-mapping.h>
0032 #include <net/checksum.h>
0033 #include <asm/firmware.h>
0034 #include <asm/ps3.h>
0035 #include <asm/lv1call.h>
0036 
0037 #include "ps3_gelic_net.h"
0038 #include "ps3_gelic_wireless.h"
0039 
0040 #define DRV_NAME "Gelic Network Driver"
0041 #define DRV_VERSION "2.0"
0042 
0043 MODULE_AUTHOR("SCE Inc.");
0044 MODULE_DESCRIPTION("Gelic Network driver");
0045 MODULE_LICENSE("GPL");
0046 
0047 
0048 /* set irq_mask */
0049 int gelic_card_set_irq_mask(struct gelic_card *card, u64 mask)
0050 {
0051     int status;
0052 
0053     status = lv1_net_set_interrupt_mask(bus_id(card), dev_id(card),
0054                         mask, 0);
0055     if (status)
0056         dev_info(ctodev(card),
0057              "%s failed %d\n", __func__, status);
0058     return status;
0059 }
0060 
0061 static void gelic_card_rx_irq_on(struct gelic_card *card)
0062 {
0063     card->irq_mask |= GELIC_CARD_RXINT;
0064     gelic_card_set_irq_mask(card, card->irq_mask);
0065 }
0066 static void gelic_card_rx_irq_off(struct gelic_card *card)
0067 {
0068     card->irq_mask &= ~GELIC_CARD_RXINT;
0069     gelic_card_set_irq_mask(card, card->irq_mask);
0070 }
0071 
0072 static void gelic_card_get_ether_port_status(struct gelic_card *card,
0073                          int inform)
0074 {
0075     u64 v2;
0076     struct net_device *ether_netdev;
0077 
0078     lv1_net_control(bus_id(card), dev_id(card),
0079             GELIC_LV1_GET_ETH_PORT_STATUS,
0080             GELIC_LV1_VLAN_TX_ETHERNET_0, 0, 0,
0081             &card->ether_port_status, &v2);
0082 
0083     if (inform) {
0084         ether_netdev = card->netdev[GELIC_PORT_ETHERNET_0];
0085         if (card->ether_port_status & GELIC_LV1_ETHER_LINK_UP)
0086             netif_carrier_on(ether_netdev);
0087         else
0088             netif_carrier_off(ether_netdev);
0089     }
0090 }
0091 
0092 /**
0093  * gelic_descr_get_status -- returns the status of a descriptor
0094  * @descr: descriptor to look at
0095  *
0096  * returns the status as in the dmac_cmd_status field of the descriptor
0097  */
0098 static enum gelic_descr_dma_status
0099 gelic_descr_get_status(struct gelic_descr *descr)
0100 {
0101     return be32_to_cpu(descr->dmac_cmd_status) & GELIC_DESCR_DMA_STAT_MASK;
0102 }
0103 
0104 static int gelic_card_set_link_mode(struct gelic_card *card, int mode)
0105 {
0106     int status;
0107     u64 v1, v2;
0108 
0109     status = lv1_net_control(bus_id(card), dev_id(card),
0110                  GELIC_LV1_SET_NEGOTIATION_MODE,
0111                  GELIC_LV1_PHY_ETHERNET_0, mode, 0, &v1, &v2);
0112     if (status) {
0113         pr_info("%s: failed setting negotiation mode %d\n", __func__,
0114             status);
0115         return -EBUSY;
0116     }
0117 
0118     card->link_mode = mode;
0119     return 0;
0120 }
0121 
0122 /**
0123  * gelic_card_disable_txdmac - disables the transmit DMA controller
0124  * @card: card structure
0125  *
0126  * gelic_card_disable_txdmac terminates processing on the DMA controller by
0127  * turing off DMA and issuing a force end
0128  */
0129 static void gelic_card_disable_txdmac(struct gelic_card *card)
0130 {
0131     int status;
0132 
0133     /* this hvc blocks until the DMA in progress really stopped */
0134     status = lv1_net_stop_tx_dma(bus_id(card), dev_id(card));
0135     if (status)
0136         dev_err(ctodev(card),
0137             "lv1_net_stop_tx_dma failed, status=%d\n", status);
0138 }
0139 
0140 /**
0141  * gelic_card_enable_rxdmac - enables the receive DMA controller
0142  * @card: card structure
0143  *
0144  * gelic_card_enable_rxdmac enables the DMA controller by setting RX_DMA_EN
0145  * in the GDADMACCNTR register
0146  */
0147 static void gelic_card_enable_rxdmac(struct gelic_card *card)
0148 {
0149     int status;
0150 
0151 #ifdef DEBUG
0152     if (gelic_descr_get_status(card->rx_chain.head) !=
0153         GELIC_DESCR_DMA_CARDOWNED) {
0154         printk(KERN_ERR "%s: status=%x\n", __func__,
0155                be32_to_cpu(card->rx_chain.head->dmac_cmd_status));
0156         printk(KERN_ERR "%s: nextphy=%x\n", __func__,
0157                be32_to_cpu(card->rx_chain.head->next_descr_addr));
0158         printk(KERN_ERR "%s: head=%p\n", __func__,
0159                card->rx_chain.head);
0160     }
0161 #endif
0162     status = lv1_net_start_rx_dma(bus_id(card), dev_id(card),
0163                 card->rx_chain.head->bus_addr, 0);
0164     if (status)
0165         dev_info(ctodev(card),
0166              "lv1_net_start_rx_dma failed, status=%d\n", status);
0167 }
0168 
0169 /**
0170  * gelic_card_disable_rxdmac - disables the receive DMA controller
0171  * @card: card structure
0172  *
0173  * gelic_card_disable_rxdmac terminates processing on the DMA controller by
0174  * turing off DMA and issuing a force end
0175  */
0176 static void gelic_card_disable_rxdmac(struct gelic_card *card)
0177 {
0178     int status;
0179 
0180     /* this hvc blocks until the DMA in progress really stopped */
0181     status = lv1_net_stop_rx_dma(bus_id(card), dev_id(card));
0182     if (status)
0183         dev_err(ctodev(card),
0184             "lv1_net_stop_rx_dma failed, %d\n", status);
0185 }
0186 
0187 /**
0188  * gelic_descr_set_status -- sets the status of a descriptor
0189  * @descr: descriptor to change
0190  * @status: status to set in the descriptor
0191  *
0192  * changes the status to the specified value. Doesn't change other bits
0193  * in the status
0194  */
0195 static void gelic_descr_set_status(struct gelic_descr *descr,
0196                    enum gelic_descr_dma_status status)
0197 {
0198     descr->dmac_cmd_status = cpu_to_be32(status |
0199             (be32_to_cpu(descr->dmac_cmd_status) &
0200              ~GELIC_DESCR_DMA_STAT_MASK));
0201     /*
0202      * dma_cmd_status field is used to indicate whether the descriptor
0203      * is valid or not.
0204      * Usually caller of this function wants to inform that to the
0205      * hardware, so we assure here the hardware sees the change.
0206      */
0207     wmb();
0208 }
0209 
0210 /**
0211  * gelic_card_reset_chain - reset status of a descriptor chain
0212  * @card: card structure
0213  * @chain: address of chain
0214  * @start_descr: address of descriptor array
0215  *
0216  * Reset the status of dma descriptors to ready state
0217  * and re-initialize the hardware chain for later use
0218  */
0219 static void gelic_card_reset_chain(struct gelic_card *card,
0220                    struct gelic_descr_chain *chain,
0221                    struct gelic_descr *start_descr)
0222 {
0223     struct gelic_descr *descr;
0224 
0225     for (descr = start_descr; start_descr != descr->next; descr++) {
0226         gelic_descr_set_status(descr, GELIC_DESCR_DMA_CARDOWNED);
0227         descr->next_descr_addr = cpu_to_be32(descr->next->bus_addr);
0228     }
0229 
0230     chain->head = start_descr;
0231     chain->tail = (descr - 1);
0232 
0233     (descr - 1)->next_descr_addr = 0;
0234 }
0235 
0236 void gelic_card_up(struct gelic_card *card)
0237 {
0238     pr_debug("%s: called\n", __func__);
0239     mutex_lock(&card->updown_lock);
0240     if (atomic_inc_return(&card->users) == 1) {
0241         pr_debug("%s: real do\n", __func__);
0242         /* enable irq */
0243         gelic_card_set_irq_mask(card, card->irq_mask);
0244         /* start rx */
0245         gelic_card_enable_rxdmac(card);
0246 
0247         napi_enable(&card->napi);
0248     }
0249     mutex_unlock(&card->updown_lock);
0250     pr_debug("%s: done\n", __func__);
0251 }
0252 
0253 void gelic_card_down(struct gelic_card *card)
0254 {
0255     u64 mask;
0256     pr_debug("%s: called\n", __func__);
0257     mutex_lock(&card->updown_lock);
0258     if (atomic_dec_if_positive(&card->users) == 0) {
0259         pr_debug("%s: real do\n", __func__);
0260         napi_disable(&card->napi);
0261         /*
0262          * Disable irq. Wireless interrupts will
0263          * be disabled later if any
0264          */
0265         mask = card->irq_mask & (GELIC_CARD_WLAN_EVENT_RECEIVED |
0266                      GELIC_CARD_WLAN_COMMAND_COMPLETED);
0267         gelic_card_set_irq_mask(card, mask);
0268         /* stop rx */
0269         gelic_card_disable_rxdmac(card);
0270         gelic_card_reset_chain(card, &card->rx_chain,
0271                        card->descr + GELIC_NET_TX_DESCRIPTORS);
0272         /* stop tx */
0273         gelic_card_disable_txdmac(card);
0274     }
0275     mutex_unlock(&card->updown_lock);
0276     pr_debug("%s: done\n", __func__);
0277 }
0278 
0279 /**
0280  * gelic_card_free_chain - free descriptor chain
0281  * @card: card structure
0282  * @descr_in: address of desc
0283  */
0284 static void gelic_card_free_chain(struct gelic_card *card,
0285                   struct gelic_descr *descr_in)
0286 {
0287     struct gelic_descr *descr;
0288 
0289     for (descr = descr_in; descr && descr->bus_addr; descr = descr->next) {
0290         dma_unmap_single(ctodev(card), descr->bus_addr,
0291                  GELIC_DESCR_SIZE, DMA_BIDIRECTIONAL);
0292         descr->bus_addr = 0;
0293     }
0294 }
0295 
0296 /**
0297  * gelic_card_init_chain - links descriptor chain
0298  * @card: card structure
0299  * @chain: address of chain
0300  * @start_descr: address of descriptor array
0301  * @no: number of descriptors
0302  *
0303  * we manage a circular list that mirrors the hardware structure,
0304  * except that the hardware uses bus addresses.
0305  *
0306  * returns 0 on success, <0 on failure
0307  */
0308 static int gelic_card_init_chain(struct gelic_card *card,
0309                  struct gelic_descr_chain *chain,
0310                  struct gelic_descr *start_descr, int no)
0311 {
0312     int i;
0313     struct gelic_descr *descr;
0314 
0315     descr = start_descr;
0316     memset(descr, 0, sizeof(*descr) * no);
0317 
0318     /* set up the hardware pointers in each descriptor */
0319     for (i = 0; i < no; i++, descr++) {
0320         gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
0321         descr->bus_addr =
0322             dma_map_single(ctodev(card), descr,
0323                        GELIC_DESCR_SIZE,
0324                        DMA_BIDIRECTIONAL);
0325 
0326         if (!descr->bus_addr)
0327             goto iommu_error;
0328 
0329         descr->next = descr + 1;
0330         descr->prev = descr - 1;
0331     }
0332     /* make them as ring */
0333     (descr - 1)->next = start_descr;
0334     start_descr->prev = (descr - 1);
0335 
0336     /* chain bus addr of hw descriptor */
0337     descr = start_descr;
0338     for (i = 0; i < no; i++, descr++) {
0339         descr->next_descr_addr = cpu_to_be32(descr->next->bus_addr);
0340     }
0341 
0342     chain->head = start_descr;
0343     chain->tail = start_descr;
0344 
0345     /* do not chain last hw descriptor */
0346     (descr - 1)->next_descr_addr = 0;
0347 
0348     return 0;
0349 
0350 iommu_error:
0351     for (i--, descr--; 0 <= i; i--, descr--)
0352         if (descr->bus_addr)
0353             dma_unmap_single(ctodev(card), descr->bus_addr,
0354                      GELIC_DESCR_SIZE,
0355                      DMA_BIDIRECTIONAL);
0356     return -ENOMEM;
0357 }
0358 
0359 /**
0360  * gelic_descr_prepare_rx - reinitializes a rx descriptor
0361  * @card: card structure
0362  * @descr: descriptor to re-init
0363  *
0364  * return 0 on success, <0 on failure
0365  *
0366  * allocates a new rx skb, iommu-maps it and attaches it to the descriptor.
0367  * Activate the descriptor state-wise
0368  */
0369 static int gelic_descr_prepare_rx(struct gelic_card *card,
0370                   struct gelic_descr *descr)
0371 {
0372     int offset;
0373     unsigned int bufsize;
0374 
0375     if (gelic_descr_get_status(descr) !=  GELIC_DESCR_DMA_NOT_IN_USE)
0376         dev_info(ctodev(card), "%s: ERROR status\n", __func__);
0377     /* we need to round up the buffer size to a multiple of 128 */
0378     bufsize = ALIGN(GELIC_NET_MAX_MTU, GELIC_NET_RXBUF_ALIGN);
0379 
0380     /* and we need to have it 128 byte aligned, therefore we allocate a
0381      * bit more */
0382     descr->skb = dev_alloc_skb(bufsize + GELIC_NET_RXBUF_ALIGN - 1);
0383     if (!descr->skb) {
0384         descr->buf_addr = 0; /* tell DMAC don't touch memory */
0385         return -ENOMEM;
0386     }
0387     descr->buf_size = cpu_to_be32(bufsize);
0388     descr->dmac_cmd_status = 0;
0389     descr->result_size = 0;
0390     descr->valid_size = 0;
0391     descr->data_error = 0;
0392 
0393     offset = ((unsigned long)descr->skb->data) &
0394         (GELIC_NET_RXBUF_ALIGN - 1);
0395     if (offset)
0396         skb_reserve(descr->skb, GELIC_NET_RXBUF_ALIGN - offset);
0397     /* io-mmu-map the skb */
0398     descr->buf_addr = cpu_to_be32(dma_map_single(ctodev(card),
0399                              descr->skb->data,
0400                              GELIC_NET_MAX_MTU,
0401                              DMA_FROM_DEVICE));
0402     if (!descr->buf_addr) {
0403         dev_kfree_skb_any(descr->skb);
0404         descr->skb = NULL;
0405         dev_info(ctodev(card),
0406              "%s:Could not iommu-map rx buffer\n", __func__);
0407         gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
0408         return -ENOMEM;
0409     } else {
0410         gelic_descr_set_status(descr, GELIC_DESCR_DMA_CARDOWNED);
0411         return 0;
0412     }
0413 }
0414 
0415 /**
0416  * gelic_card_release_rx_chain - free all skb of rx descr
0417  * @card: card structure
0418  *
0419  */
0420 static void gelic_card_release_rx_chain(struct gelic_card *card)
0421 {
0422     struct gelic_descr *descr = card->rx_chain.head;
0423 
0424     do {
0425         if (descr->skb) {
0426             dma_unmap_single(ctodev(card),
0427                      be32_to_cpu(descr->buf_addr),
0428                      descr->skb->len,
0429                      DMA_FROM_DEVICE);
0430             descr->buf_addr = 0;
0431             dev_kfree_skb_any(descr->skb);
0432             descr->skb = NULL;
0433             gelic_descr_set_status(descr,
0434                            GELIC_DESCR_DMA_NOT_IN_USE);
0435         }
0436         descr = descr->next;
0437     } while (descr != card->rx_chain.head);
0438 }
0439 
0440 /**
0441  * gelic_card_fill_rx_chain - fills descriptors/skbs in the rx chains
0442  * @card: card structure
0443  *
0444  * fills all descriptors in the rx chain: allocates skbs
0445  * and iommu-maps them.
0446  * returns 0 on success, < 0 on failure
0447  */
0448 static int gelic_card_fill_rx_chain(struct gelic_card *card)
0449 {
0450     struct gelic_descr *descr = card->rx_chain.head;
0451     int ret;
0452 
0453     do {
0454         if (!descr->skb) {
0455             ret = gelic_descr_prepare_rx(card, descr);
0456             if (ret)
0457                 goto rewind;
0458         }
0459         descr = descr->next;
0460     } while (descr != card->rx_chain.head);
0461 
0462     return 0;
0463 rewind:
0464     gelic_card_release_rx_chain(card);
0465     return ret;
0466 }
0467 
0468 /**
0469  * gelic_card_alloc_rx_skbs - allocates rx skbs in rx descriptor chains
0470  * @card: card structure
0471  *
0472  * returns 0 on success, < 0 on failure
0473  */
0474 static int gelic_card_alloc_rx_skbs(struct gelic_card *card)
0475 {
0476     struct gelic_descr_chain *chain;
0477     int ret;
0478     chain = &card->rx_chain;
0479     ret = gelic_card_fill_rx_chain(card);
0480     chain->tail = card->rx_top->prev; /* point to the last */
0481     return ret;
0482 }
0483 
0484 /**
0485  * gelic_descr_release_tx - processes a used tx descriptor
0486  * @card: card structure
0487  * @descr: descriptor to release
0488  *
0489  * releases a used tx descriptor (unmapping, freeing of skb)
0490  */
0491 static void gelic_descr_release_tx(struct gelic_card *card,
0492                        struct gelic_descr *descr)
0493 {
0494     struct sk_buff *skb = descr->skb;
0495 
0496     BUG_ON(!(be32_to_cpu(descr->data_status) & GELIC_DESCR_TX_TAIL));
0497 
0498     dma_unmap_single(ctodev(card), be32_to_cpu(descr->buf_addr), skb->len,
0499              DMA_TO_DEVICE);
0500     dev_kfree_skb_any(skb);
0501 
0502     descr->buf_addr = 0;
0503     descr->buf_size = 0;
0504     descr->next_descr_addr = 0;
0505     descr->result_size = 0;
0506     descr->valid_size = 0;
0507     descr->data_status = 0;
0508     descr->data_error = 0;
0509     descr->skb = NULL;
0510 
0511     /* set descr status */
0512     gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
0513 }
0514 
0515 static void gelic_card_stop_queues(struct gelic_card *card)
0516 {
0517     netif_stop_queue(card->netdev[GELIC_PORT_ETHERNET_0]);
0518 
0519     if (card->netdev[GELIC_PORT_WIRELESS])
0520         netif_stop_queue(card->netdev[GELIC_PORT_WIRELESS]);
0521 }
0522 static void gelic_card_wake_queues(struct gelic_card *card)
0523 {
0524     netif_wake_queue(card->netdev[GELIC_PORT_ETHERNET_0]);
0525 
0526     if (card->netdev[GELIC_PORT_WIRELESS])
0527         netif_wake_queue(card->netdev[GELIC_PORT_WIRELESS]);
0528 }
0529 /**
0530  * gelic_card_release_tx_chain - processes sent tx descriptors
0531  * @card: adapter structure
0532  * @stop: net_stop sequence
0533  *
0534  * releases the tx descriptors that gelic has finished with
0535  */
0536 static void gelic_card_release_tx_chain(struct gelic_card *card, int stop)
0537 {
0538     struct gelic_descr_chain *tx_chain;
0539     enum gelic_descr_dma_status status;
0540     struct net_device *netdev;
0541     int release = 0;
0542 
0543     for (tx_chain = &card->tx_chain;
0544          tx_chain->head != tx_chain->tail && tx_chain->tail;
0545          tx_chain->tail = tx_chain->tail->next) {
0546         status = gelic_descr_get_status(tx_chain->tail);
0547         netdev = tx_chain->tail->skb->dev;
0548         switch (status) {
0549         case GELIC_DESCR_DMA_RESPONSE_ERROR:
0550         case GELIC_DESCR_DMA_PROTECTION_ERROR:
0551         case GELIC_DESCR_DMA_FORCE_END:
0552             if (printk_ratelimit())
0553                 dev_info(ctodev(card),
0554                      "%s: forcing end of tx descriptor " \
0555                      "with status %x\n",
0556                      __func__, status);
0557             netdev->stats.tx_dropped++;
0558             break;
0559 
0560         case GELIC_DESCR_DMA_COMPLETE:
0561             if (tx_chain->tail->skb) {
0562                 netdev->stats.tx_packets++;
0563                 netdev->stats.tx_bytes +=
0564                     tx_chain->tail->skb->len;
0565             }
0566             break;
0567 
0568         case GELIC_DESCR_DMA_CARDOWNED:
0569             /* pending tx request */
0570         default:
0571             /* any other value (== GELIC_DESCR_DMA_NOT_IN_USE) */
0572             if (!stop)
0573                 goto out;
0574         }
0575         gelic_descr_release_tx(card, tx_chain->tail);
0576         release ++;
0577     }
0578 out:
0579     if (!stop && release)
0580         gelic_card_wake_queues(card);
0581 }
0582 
0583 /**
0584  * gelic_net_set_multi - sets multicast addresses and promisc flags
0585  * @netdev: interface device structure
0586  *
0587  * gelic_net_set_multi configures multicast addresses as needed for the
0588  * netdev interface. It also sets up multicast, allmulti and promisc
0589  * flags appropriately
0590  */
0591 void gelic_net_set_multi(struct net_device *netdev)
0592 {
0593     struct gelic_card *card = netdev_card(netdev);
0594     struct netdev_hw_addr *ha;
0595     unsigned int i;
0596     uint8_t *p;
0597     u64 addr;
0598     int status;
0599 
0600     /* clear all multicast address */
0601     status = lv1_net_remove_multicast_address(bus_id(card), dev_id(card),
0602                           0, 1);
0603     if (status)
0604         dev_err(ctodev(card),
0605             "lv1_net_remove_multicast_address failed %d\n",
0606             status);
0607     /* set broadcast address */
0608     status = lv1_net_add_multicast_address(bus_id(card), dev_id(card),
0609                            GELIC_NET_BROADCAST_ADDR, 0);
0610     if (status)
0611         dev_err(ctodev(card),
0612             "lv1_net_add_multicast_address failed, %d\n",
0613             status);
0614 
0615     if ((netdev->flags & IFF_ALLMULTI) ||
0616         (netdev_mc_count(netdev) > GELIC_NET_MC_COUNT_MAX)) {
0617         status = lv1_net_add_multicast_address(bus_id(card),
0618                                dev_id(card),
0619                                0, 1);
0620         if (status)
0621             dev_err(ctodev(card),
0622                 "lv1_net_add_multicast_address failed, %d\n",
0623                 status);
0624         return;
0625     }
0626 
0627     /* set multicast addresses */
0628     netdev_for_each_mc_addr(ha, netdev) {
0629         addr = 0;
0630         p = ha->addr;
0631         for (i = 0; i < ETH_ALEN; i++) {
0632             addr <<= 8;
0633             addr |= *p++;
0634         }
0635         status = lv1_net_add_multicast_address(bus_id(card),
0636                                dev_id(card),
0637                                addr, 0);
0638         if (status)
0639             dev_err(ctodev(card),
0640                 "lv1_net_add_multicast_address failed, %d\n",
0641                 status);
0642     }
0643 }
0644 
0645 /**
0646  * gelic_net_stop - called upon ifconfig down
0647  * @netdev: interface device structure
0648  *
0649  * always returns 0
0650  */
0651 int gelic_net_stop(struct net_device *netdev)
0652 {
0653     struct gelic_card *card;
0654 
0655     pr_debug("%s: start\n", __func__);
0656 
0657     netif_stop_queue(netdev);
0658     netif_carrier_off(netdev);
0659 
0660     card = netdev_card(netdev);
0661     gelic_card_down(card);
0662 
0663     pr_debug("%s: done\n", __func__);
0664     return 0;
0665 }
0666 
0667 /**
0668  * gelic_card_get_next_tx_descr - returns the next available tx descriptor
0669  * @card: device structure to get descriptor from
0670  *
0671  * returns the address of the next descriptor, or NULL if not available.
0672  */
0673 static struct gelic_descr *
0674 gelic_card_get_next_tx_descr(struct gelic_card *card)
0675 {
0676     if (!card->tx_chain.head)
0677         return NULL;
0678     /*  see if the next descriptor is free */
0679     if (card->tx_chain.tail != card->tx_chain.head->next &&
0680         gelic_descr_get_status(card->tx_chain.head) ==
0681         GELIC_DESCR_DMA_NOT_IN_USE)
0682         return card->tx_chain.head;
0683     else
0684         return NULL;
0685 
0686 }
0687 
0688 /**
0689  * gelic_net_set_txdescr_cmdstat - sets the tx descriptor command field
0690  * @descr: descriptor structure to fill out
0691  * @skb: packet to consider
0692  *
0693  * fills out the command and status field of the descriptor structure,
0694  * depending on hardware checksum settings. This function assumes a wmb()
0695  * has executed before.
0696  */
0697 static void gelic_descr_set_tx_cmdstat(struct gelic_descr *descr,
0698                        struct sk_buff *skb)
0699 {
0700     if (skb->ip_summed != CHECKSUM_PARTIAL)
0701         descr->dmac_cmd_status =
0702             cpu_to_be32(GELIC_DESCR_DMA_CMD_NO_CHKSUM |
0703                     GELIC_DESCR_TX_DMA_FRAME_TAIL);
0704     else {
0705         /* is packet ip?
0706          * if yes: tcp? udp? */
0707         if (skb->protocol == htons(ETH_P_IP)) {
0708             if (ip_hdr(skb)->protocol == IPPROTO_TCP)
0709                 descr->dmac_cmd_status =
0710                 cpu_to_be32(GELIC_DESCR_DMA_CMD_TCP_CHKSUM |
0711                         GELIC_DESCR_TX_DMA_FRAME_TAIL);
0712 
0713             else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
0714                 descr->dmac_cmd_status =
0715                 cpu_to_be32(GELIC_DESCR_DMA_CMD_UDP_CHKSUM |
0716                         GELIC_DESCR_TX_DMA_FRAME_TAIL);
0717             else    /*
0718                  * the stack should checksum non-tcp and non-udp
0719                  * packets on his own: NETIF_F_IP_CSUM
0720                  */
0721                 descr->dmac_cmd_status =
0722                 cpu_to_be32(GELIC_DESCR_DMA_CMD_NO_CHKSUM |
0723                         GELIC_DESCR_TX_DMA_FRAME_TAIL);
0724         }
0725     }
0726 }
0727 
0728 static struct sk_buff *gelic_put_vlan_tag(struct sk_buff *skb,
0729                          unsigned short tag)
0730 {
0731     struct vlan_ethhdr *veth;
0732     static unsigned int c;
0733 
0734     if (skb_headroom(skb) < VLAN_HLEN) {
0735         struct sk_buff *sk_tmp = skb;
0736         pr_debug("%s: hd=%d c=%ud\n", __func__, skb_headroom(skb), c);
0737         skb = skb_realloc_headroom(sk_tmp, VLAN_HLEN);
0738         if (!skb)
0739             return NULL;
0740         dev_kfree_skb_any(sk_tmp);
0741     }
0742     veth = skb_push(skb, VLAN_HLEN);
0743 
0744     /* Move the mac addresses to the top of buffer */
0745     memmove(skb->data, skb->data + VLAN_HLEN, 2 * ETH_ALEN);
0746 
0747     veth->h_vlan_proto = cpu_to_be16(ETH_P_8021Q);
0748     veth->h_vlan_TCI = htons(tag);
0749 
0750     return skb;
0751 }
0752 
0753 /**
0754  * gelic_descr_prepare_tx - setup a descriptor for sending packets
0755  * @card: card structure
0756  * @descr: descriptor structure
0757  * @skb: packet to use
0758  *
0759  * returns 0 on success, <0 on failure.
0760  *
0761  */
0762 static int gelic_descr_prepare_tx(struct gelic_card *card,
0763                   struct gelic_descr *descr,
0764                   struct sk_buff *skb)
0765 {
0766     dma_addr_t buf;
0767 
0768     if (card->vlan_required) {
0769         struct sk_buff *skb_tmp;
0770         enum gelic_port_type type;
0771 
0772         type = netdev_port(skb->dev)->type;
0773         skb_tmp = gelic_put_vlan_tag(skb,
0774                          card->vlan[type].tx);
0775         if (!skb_tmp)
0776             return -ENOMEM;
0777         skb = skb_tmp;
0778     }
0779 
0780     buf = dma_map_single(ctodev(card), skb->data, skb->len, DMA_TO_DEVICE);
0781 
0782     if (!buf) {
0783         dev_err(ctodev(card),
0784             "dma map 2 failed (%p, %i). Dropping packet\n",
0785             skb->data, skb->len);
0786         return -ENOMEM;
0787     }
0788 
0789     descr->buf_addr = cpu_to_be32(buf);
0790     descr->buf_size = cpu_to_be32(skb->len);
0791     descr->skb = skb;
0792     descr->data_status = 0;
0793     descr->next_descr_addr = 0; /* terminate hw descr */
0794     gelic_descr_set_tx_cmdstat(descr, skb);
0795 
0796     /* bump free descriptor pointer */
0797     card->tx_chain.head = descr->next;
0798     return 0;
0799 }
0800 
0801 /**
0802  * gelic_card_kick_txdma - enables TX DMA processing
0803  * @card: card structure
0804  * @descr: descriptor address to enable TX processing at
0805  *
0806  */
0807 static int gelic_card_kick_txdma(struct gelic_card *card,
0808                  struct gelic_descr *descr)
0809 {
0810     int status = 0;
0811 
0812     if (card->tx_dma_progress)
0813         return 0;
0814 
0815     if (gelic_descr_get_status(descr) == GELIC_DESCR_DMA_CARDOWNED) {
0816         card->tx_dma_progress = 1;
0817         status = lv1_net_start_tx_dma(bus_id(card), dev_id(card),
0818                           descr->bus_addr, 0);
0819         if (status) {
0820             card->tx_dma_progress = 0;
0821             dev_info(ctodev(card), "lv1_net_start_txdma failed," \
0822                  "status=%d\n", status);
0823         }
0824     }
0825     return status;
0826 }
0827 
0828 /**
0829  * gelic_net_xmit - transmits a frame over the device
0830  * @skb: packet to send out
0831  * @netdev: interface device structure
0832  *
0833  * returns NETDEV_TX_OK on success, NETDEV_TX_BUSY on failure
0834  */
0835 netdev_tx_t gelic_net_xmit(struct sk_buff *skb, struct net_device *netdev)
0836 {
0837     struct gelic_card *card = netdev_card(netdev);
0838     struct gelic_descr *descr;
0839     int result;
0840     unsigned long flags;
0841 
0842     spin_lock_irqsave(&card->tx_lock, flags);
0843 
0844     gelic_card_release_tx_chain(card, 0);
0845 
0846     descr = gelic_card_get_next_tx_descr(card);
0847     if (!descr) {
0848         /*
0849          * no more descriptors free
0850          */
0851         gelic_card_stop_queues(card);
0852         spin_unlock_irqrestore(&card->tx_lock, flags);
0853         return NETDEV_TX_BUSY;
0854     }
0855 
0856     result = gelic_descr_prepare_tx(card, descr, skb);
0857     if (result) {
0858         /*
0859          * DMA map failed.  As chances are that failure
0860          * would continue, just release skb and return
0861          */
0862         netdev->stats.tx_dropped++;
0863         dev_kfree_skb_any(skb);
0864         spin_unlock_irqrestore(&card->tx_lock, flags);
0865         return NETDEV_TX_OK;
0866     }
0867     /*
0868      * link this prepared descriptor to previous one
0869      * to achieve high performance
0870      */
0871     descr->prev->next_descr_addr = cpu_to_be32(descr->bus_addr);
0872     /*
0873      * as hardware descriptor is modified in the above lines,
0874      * ensure that the hardware sees it
0875      */
0876     wmb();
0877     if (gelic_card_kick_txdma(card, descr)) {
0878         /*
0879          * kick failed.
0880          * release descriptor which was just prepared
0881          */
0882         netdev->stats.tx_dropped++;
0883         /* don't trigger BUG_ON() in gelic_descr_release_tx */
0884         descr->data_status = cpu_to_be32(GELIC_DESCR_TX_TAIL);
0885         gelic_descr_release_tx(card, descr);
0886         /* reset head */
0887         card->tx_chain.head = descr;
0888         /* reset hw termination */
0889         descr->prev->next_descr_addr = 0;
0890         dev_info(ctodev(card), "%s: kick failure\n", __func__);
0891     }
0892 
0893     spin_unlock_irqrestore(&card->tx_lock, flags);
0894     return NETDEV_TX_OK;
0895 }
0896 
0897 /**
0898  * gelic_net_pass_skb_up - takes an skb from a descriptor and passes it on
0899  * @descr: descriptor to process
0900  * @card: card structure
0901  * @netdev: net_device structure to be passed packet
0902  *
0903  * iommu-unmaps the skb, fills out skb structure and passes the data to the
0904  * stack. The descriptor state is not changed.
0905  */
0906 static void gelic_net_pass_skb_up(struct gelic_descr *descr,
0907                   struct gelic_card *card,
0908                   struct net_device *netdev)
0909 
0910 {
0911     struct sk_buff *skb = descr->skb;
0912     u32 data_status, data_error;
0913 
0914     data_status = be32_to_cpu(descr->data_status);
0915     data_error = be32_to_cpu(descr->data_error);
0916     /* unmap skb buffer */
0917     dma_unmap_single(ctodev(card), be32_to_cpu(descr->buf_addr),
0918              GELIC_NET_MAX_MTU,
0919              DMA_FROM_DEVICE);
0920 
0921     skb_put(skb, be32_to_cpu(descr->valid_size)?
0922         be32_to_cpu(descr->valid_size) :
0923         be32_to_cpu(descr->result_size));
0924     if (!descr->valid_size)
0925         dev_info(ctodev(card), "buffer full %x %x %x\n",
0926              be32_to_cpu(descr->result_size),
0927              be32_to_cpu(descr->buf_size),
0928              be32_to_cpu(descr->dmac_cmd_status));
0929 
0930     descr->skb = NULL;
0931     /*
0932      * the card put 2 bytes vlan tag in front
0933      * of the ethernet frame
0934      */
0935     skb_pull(skb, 2);
0936     skb->protocol = eth_type_trans(skb, netdev);
0937 
0938     /* checksum offload */
0939     if (netdev->features & NETIF_F_RXCSUM) {
0940         if ((data_status & GELIC_DESCR_DATA_STATUS_CHK_MASK) &&
0941             (!(data_error & GELIC_DESCR_DATA_ERROR_CHK_MASK)))
0942             skb->ip_summed = CHECKSUM_UNNECESSARY;
0943         else
0944             skb_checksum_none_assert(skb);
0945     } else
0946         skb_checksum_none_assert(skb);
0947 
0948     /* update netdevice statistics */
0949     netdev->stats.rx_packets++;
0950     netdev->stats.rx_bytes += skb->len;
0951 
0952     /* pass skb up to stack */
0953     netif_receive_skb(skb);
0954 }
0955 
0956 /**
0957  * gelic_card_decode_one_descr - processes an rx descriptor
0958  * @card: card structure
0959  *
0960  * returns 1 if a packet has been sent to the stack, otherwise 0
0961  *
0962  * processes an rx descriptor by iommu-unmapping the data buffer and passing
0963  * the packet up to the stack
0964  */
0965 static int gelic_card_decode_one_descr(struct gelic_card *card)
0966 {
0967     enum gelic_descr_dma_status status;
0968     struct gelic_descr_chain *chain = &card->rx_chain;
0969     struct gelic_descr *descr = chain->head;
0970     struct net_device *netdev = NULL;
0971     int dmac_chain_ended;
0972 
0973     status = gelic_descr_get_status(descr);
0974 
0975     if (status == GELIC_DESCR_DMA_CARDOWNED)
0976         return 0;
0977 
0978     if (status == GELIC_DESCR_DMA_NOT_IN_USE) {
0979         dev_dbg(ctodev(card), "dormant descr? %p\n", descr);
0980         return 0;
0981     }
0982 
0983     /* netdevice select */
0984     if (card->vlan_required) {
0985         unsigned int i;
0986         u16 vid;
0987         vid = *(u16 *)(descr->skb->data) & VLAN_VID_MASK;
0988         for (i = 0; i < GELIC_PORT_MAX; i++) {
0989             if (card->vlan[i].rx == vid) {
0990                 netdev = card->netdev[i];
0991                 break;
0992             }
0993         }
0994         if (GELIC_PORT_MAX <= i) {
0995             pr_info("%s: unknown packet vid=%x\n", __func__, vid);
0996             goto refill;
0997         }
0998     } else
0999         netdev = card->netdev[GELIC_PORT_ETHERNET_0];
1000 
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",
1005              status);
1006         netdev->stats.rx_dropped++;
1007         goto refill;
1008     }
1009 
1010     if (status == GELIC_DESCR_DMA_BUFFER_FULL) {
1011         /*
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
1016          * would occur.
1017          * Anyway this frame was longer than the MTU,
1018          * just drop it.
1019          */
1020         dev_info(ctodev(card), "overlength frame\n");
1021         goto refill;
1022     }
1023     /*
1024      * descriptors any other than FRAME_END here should
1025      * be treated as error.
1026      */
1027     if (status != GELIC_DESCR_DMA_FRAME_END) {
1028         dev_dbg(ctodev(card), "RX descriptor with state %x\n",
1029             status);
1030         goto refill;
1031     }
1032 
1033     /* ok, we've got a packet in descr */
1034     gelic_net_pass_skb_up(descr, card, netdev);
1035 refill:
1036 
1037     /* is the current descriptor terminated with next_descr == NULL? */
1038     dmac_chain_ended =
1039         be32_to_cpu(descr->dmac_cmd_status) &
1040         GELIC_DESCR_RX_DMA_CHAIN_END;
1041     /*
1042      * So that always DMAC can see the end
1043      * of the descriptor chain to avoid
1044      * from unwanted DMAC overrun.
1045      */
1046     descr->next_descr_addr = 0;
1047 
1048     /* change the descriptor state: */
1049     gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
1050 
1051     /*
1052      * this call can fail, but for now, just leave this
1053      * descriptor without skb
1054      */
1055     gelic_descr_prepare_rx(card, descr);
1056 
1057     chain->tail = descr;
1058     chain->head = descr->next;
1059 
1060     /*
1061      * Set this descriptor the end of the chain.
1062      */
1063     descr->prev->next_descr_addr = cpu_to_be32(descr->bus_addr);
1064 
1065     /*
1066      * If dmac chain was met, DMAC stopped.
1067      * thus re-enable it
1068      */
1069 
1070     if (dmac_chain_ended)
1071         gelic_card_enable_rxdmac(card);
1072 
1073     return 1;
1074 }
1075 
1076 /**
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
1080  *
1081  * returns the number of the processed packets
1082  *
1083  */
1084 static int gelic_net_poll(struct napi_struct *napi, int budget)
1085 {
1086     struct gelic_card *card = container_of(napi, struct gelic_card, napi);
1087     int packets_done = 0;
1088 
1089     while (packets_done < budget) {
1090         if (!gelic_card_decode_one_descr(card))
1091             break;
1092 
1093         packets_done++;
1094     }
1095 
1096     if (packets_done < budget) {
1097         napi_complete_done(napi, packets_done);
1098         gelic_card_rx_irq_on(card);
1099     }
1100     return packets_done;
1101 }
1102 
1103 /*
1104  * gelic_card_interrupt - event handler for gelic_net
1105  */
1106 static irqreturn_t gelic_card_interrupt(int irq, void *ptr)
1107 {
1108     unsigned long flags;
1109     struct gelic_card *card = ptr;
1110     u64 status;
1111 
1112     status = card->irq_status;
1113 
1114     if (!status)
1115         return IRQ_NONE;
1116 
1117     status &= card->irq_mask;
1118 
1119     if (status & GELIC_CARD_RXINT) {
1120         gelic_card_rx_irq_off(card);
1121         napi_schedule(&card->napi);
1122     }
1123 
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);
1131     }
1132 
1133     /* ether port status changed */
1134     if (status & GELIC_CARD_PORT_STATUS_CHANGED)
1135         gelic_card_get_ether_port_status(card, 1);
1136 
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);
1141 #endif
1142 
1143     return IRQ_HANDLED;
1144 }
1145 
1146 #ifdef CONFIG_NET_POLL_CONTROLLER
1147 /**
1148  * gelic_net_poll_controller - artificial interrupt for netconsole etc.
1149  * @netdev: interface device structure
1150  *
1151  * see Documentation/networking/netconsole.rst
1152  */
1153 void gelic_net_poll_controller(struct net_device *netdev)
1154 {
1155     struct gelic_card *card = netdev_card(netdev);
1156 
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);
1160 }
1161 #endif /* CONFIG_NET_POLL_CONTROLLER */
1162 
1163 /**
1164  * gelic_net_open - called upon ifconfig up
1165  * @netdev: interface device structure
1166  *
1167  * returns 0 on success, <0 on failure
1168  *
1169  * gelic_net_open allocates all the descriptors and memory needed for
1170  * operation, sets up multicast list and enables interrupts
1171  */
1172 int gelic_net_open(struct net_device *netdev)
1173 {
1174     struct gelic_card *card = netdev_card(netdev);
1175 
1176     dev_dbg(ctodev(card), " -> %s %p\n", __func__, netdev);
1177 
1178     gelic_card_up(card);
1179 
1180     netif_start_queue(netdev);
1181     gelic_card_get_ether_port_status(card, 1);
1182 
1183     dev_dbg(ctodev(card), " <- %s\n", __func__);
1184     return 0;
1185 }
1186 
1187 void gelic_net_get_drvinfo(struct net_device *netdev,
1188                struct ethtool_drvinfo *info)
1189 {
1190     strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
1191     strlcpy(info->version, DRV_VERSION, sizeof(info->version));
1192 }
1193 
1194 static int gelic_ether_get_link_ksettings(struct net_device *netdev,
1195                       struct ethtool_link_ksettings *cmd)
1196 {
1197     struct gelic_card *card = netdev_card(netdev);
1198     u32 supported, advertising;
1199 
1200     gelic_card_get_ether_port_status(card, 0);
1201 
1202     if (card->ether_port_status & GELIC_LV1_ETHER_FULL_DUPLEX)
1203         cmd->base.duplex = DUPLEX_FULL;
1204     else
1205         cmd->base.duplex = DUPLEX_HALF;
1206 
1207     switch (card->ether_port_status & GELIC_LV1_ETHER_SPEED_MASK) {
1208     case GELIC_LV1_ETHER_SPEED_10:
1209         cmd->base.speed = SPEED_10;
1210         break;
1211     case GELIC_LV1_ETHER_SPEED_100:
1212         cmd->base.speed = SPEED_100;
1213         break;
1214     case GELIC_LV1_ETHER_SPEED_1000:
1215         cmd->base.speed = SPEED_1000;
1216         break;
1217     default:
1218         pr_info("%s: speed unknown\n", __func__);
1219         cmd->base.speed = SPEED_10;
1220         break;
1221     }
1222 
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;
1230     } else {
1231         cmd->base.autoneg = AUTONEG_DISABLE;
1232         advertising &= ~ADVERTISED_Autoneg;
1233     }
1234     cmd->base.port = PORT_TP;
1235 
1236     ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
1237                         supported);
1238     ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
1239                         advertising);
1240 
1241     return 0;
1242 }
1243 
1244 static int
1245 gelic_ether_set_link_ksettings(struct net_device *netdev,
1246                    const struct ethtool_link_ksettings *cmd)
1247 {
1248     struct gelic_card *card = netdev_card(netdev);
1249     u64 mode;
1250     int ret;
1251 
1252     if (cmd->base.autoneg == AUTONEG_ENABLE) {
1253         mode = GELIC_LV1_ETHER_AUTO_NEG;
1254     } else {
1255         switch (cmd->base.speed) {
1256         case SPEED_10:
1257             mode = GELIC_LV1_ETHER_SPEED_10;
1258             break;
1259         case SPEED_100:
1260             mode = GELIC_LV1_ETHER_SPEED_100;
1261             break;
1262         case SPEED_1000:
1263             mode = GELIC_LV1_ETHER_SPEED_1000;
1264             break;
1265         default:
1266             return -EINVAL;
1267         }
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");
1272             return -EINVAL;
1273         }
1274     }
1275 
1276     ret = gelic_card_set_link_mode(card, mode);
1277 
1278     if (ret)
1279         return ret;
1280 
1281     return 0;
1282 }
1283 
1284 static void gelic_net_get_wol(struct net_device *netdev,
1285                   struct ethtool_wolinfo *wol)
1286 {
1287     if (0 <= ps3_compare_firmware_version(2, 2, 0))
1288         wol->supported = WAKE_MAGIC;
1289     else
1290         wol->supported = 0;
1291 
1292     wol->wolopts = ps3_sys_manager_get_wol() ? wol->supported : 0;
1293     memset(&wol->sopass, 0, sizeof(wol->sopass));
1294 }
1295 static int gelic_net_set_wol(struct net_device *netdev,
1296                  struct ethtool_wolinfo *wol)
1297 {
1298     int status;
1299     struct gelic_card *card;
1300     u64 v1, v2;
1301 
1302     if (ps3_compare_firmware_version(2, 2, 0) < 0 ||
1303         !capable(CAP_NET_ADMIN))
1304         return -EPERM;
1305 
1306     if (wol->wolopts & ~WAKE_MAGIC)
1307         return -EINVAL;
1308 
1309     card = netdev_card(netdev);
1310     if (wol->wolopts & WAKE_MAGIC) {
1311         status = lv1_net_control(bus_id(card), dev_id(card),
1312                      GELIC_LV1_SET_WOL,
1313                      GELIC_LV1_WOL_MAGIC_PACKET,
1314                      0, GELIC_LV1_WOL_MP_ENABLE,
1315                      &v1, &v2);
1316         if (status) {
1317             pr_info("%s: enabling WOL failed %d\n", __func__,
1318                 status);
1319             status = -EIO;
1320             goto done;
1321         }
1322         status = lv1_net_control(bus_id(card), dev_id(card),
1323                      GELIC_LV1_SET_WOL,
1324                      GELIC_LV1_WOL_ADD_MATCH_ADDR,
1325                      0, GELIC_LV1_WOL_MATCH_ALL,
1326                      &v1, &v2);
1327         if (!status)
1328             ps3_sys_manager_set_wol(1);
1329         else {
1330             pr_info("%s: enabling WOL filter failed %d\n",
1331                 __func__, status);
1332             status = -EIO;
1333         }
1334     } else {
1335         status = lv1_net_control(bus_id(card), dev_id(card),
1336                      GELIC_LV1_SET_WOL,
1337                      GELIC_LV1_WOL_MAGIC_PACKET,
1338                      0, GELIC_LV1_WOL_MP_DISABLE,
1339                      &v1, &v2);
1340         if (status) {
1341             pr_info("%s: disabling WOL failed %d\n", __func__,
1342                 status);
1343             status = -EIO;
1344             goto done;
1345         }
1346         status = lv1_net_control(bus_id(card), dev_id(card),
1347                      GELIC_LV1_SET_WOL,
1348                      GELIC_LV1_WOL_DELETE_MATCH_ADDR,
1349                      0, GELIC_LV1_WOL_MATCH_ALL,
1350                      &v1, &v2);
1351         if (!status)
1352             ps3_sys_manager_set_wol(0);
1353         else {
1354             pr_info("%s: removing WOL filter failed %d\n",
1355                 __func__, status);
1356             status = -EIO;
1357         }
1358     }
1359 done:
1360     return status;
1361 }
1362 
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,
1370 };
1371 
1372 /**
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
1376  *
1377  * called as task when tx hangs, resets interface (if interface is up)
1378  */
1379 static void gelic_net_tx_timeout_task(struct work_struct *work)
1380 {
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];
1384 
1385     dev_info(ctodev(card), "%s:Timed out. Restarting...\n", __func__);
1386 
1387     if (!(netdev->flags & IFF_UP))
1388         goto out;
1389 
1390     netif_device_detach(netdev);
1391     gelic_net_stop(netdev);
1392 
1393     gelic_net_open(netdev);
1394     netif_device_attach(netdev);
1395 
1396 out:
1397     atomic_dec(&card->tx_timeout_task_counter);
1398 }
1399 
1400 /**
1401  * gelic_net_tx_timeout - called when the tx timeout watchdog kicks in.
1402  * @netdev: interface device structure
1403  * @txqueue: unused
1404  *
1405  * called, if tx hangs. Schedules a task that resets the interface
1406  */
1407 void gelic_net_tx_timeout(struct net_device *netdev, unsigned int txqueue)
1408 {
1409     struct gelic_card *card;
1410 
1411     card = netdev_card(netdev);
1412     atomic_inc(&card->tx_timeout_task_counter);
1413     if (netdev->flags & IFF_UP)
1414         schedule_work(&card->tx_timeout_task);
1415     else
1416         atomic_dec(&card->tx_timeout_task_counter);
1417 }
1418 
1419 static const struct net_device_ops gelic_netdevice_ops = {
1420     .ndo_open = gelic_net_open,
1421     .ndo_stop = gelic_net_stop,
1422     .ndo_start_xmit = gelic_net_xmit,
1423     .ndo_set_rx_mode = gelic_net_set_multi,
1424     .ndo_tx_timeout = gelic_net_tx_timeout,
1425     .ndo_set_mac_address = eth_mac_addr,
1426     .ndo_validate_addr = eth_validate_addr,
1427 #ifdef CONFIG_NET_POLL_CONTROLLER
1428     .ndo_poll_controller = gelic_net_poll_controller,
1429 #endif
1430 };
1431 
1432 /**
1433  * gelic_ether_setup_netdev_ops - initialization of net_device operations
1434  * @netdev: net_device structure
1435  * @napi: napi structure
1436  *
1437  * fills out function pointers in the net_device structure
1438  */
1439 static void gelic_ether_setup_netdev_ops(struct net_device *netdev,
1440                      struct napi_struct *napi)
1441 {
1442     netdev->watchdog_timeo = GELIC_NET_WATCHDOG_TIMEOUT;
1443     /* NAPI */
1444     netif_napi_add(netdev, napi, gelic_net_poll, NAPI_POLL_WEIGHT);
1445     netdev->ethtool_ops = &gelic_ether_ethtool_ops;
1446     netdev->netdev_ops = &gelic_netdevice_ops;
1447 }
1448 
1449 /**
1450  * gelic_ether_setup_netdev - initialization of net_device
1451  * @netdev: net_device structure
1452  * @card: card structure
1453  *
1454  * Returns 0 on success or <0 on failure
1455  *
1456  * gelic_ether_setup_netdev initializes the net_device structure
1457  * and register it.
1458  **/
1459 int gelic_net_setup_netdev(struct net_device *netdev, struct gelic_card *card)
1460 {
1461     int status;
1462     u64 v1, v2;
1463 
1464     netdev->hw_features = NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
1465 
1466     netdev->features = NETIF_F_IP_CSUM;
1467     if (GELIC_CARD_RX_CSUM_DEFAULT)
1468         netdev->features |= NETIF_F_RXCSUM;
1469 
1470     status = lv1_net_control(bus_id(card), dev_id(card),
1471                  GELIC_LV1_GET_MAC_ADDRESS,
1472                  0, 0, 0, &v1, &v2);
1473     v1 <<= 16;
1474     if (status || !is_valid_ether_addr((u8 *)&v1)) {
1475         dev_info(ctodev(card),
1476              "%s:lv1_net_control GET_MAC_ADDR failed %d\n",
1477              __func__, status);
1478         return -EINVAL;
1479     }
1480     eth_hw_addr_set(netdev, (u8 *)&v1);
1481 
1482     if (card->vlan_required) {
1483         netdev->hard_header_len += VLAN_HLEN;
1484         /*
1485          * As vlan is internally used,
1486          * we can not receive vlan packets
1487          */
1488         netdev->features |= NETIF_F_VLAN_CHALLENGED;
1489     }
1490 
1491     /* MTU range: 64 - 1518 */
1492     netdev->min_mtu = GELIC_NET_MIN_MTU;
1493     netdev->max_mtu = GELIC_NET_MAX_MTU;
1494 
1495     status = register_netdev(netdev);
1496     if (status) {
1497         dev_err(ctodev(card), "%s:Couldn't register %s %d\n",
1498             __func__, netdev->name, status);
1499         return status;
1500     }
1501     dev_info(ctodev(card), "%s: MAC addr %pM\n",
1502          netdev->name, netdev->dev_addr);
1503 
1504     return 0;
1505 }
1506 
1507 /**
1508  * gelic_alloc_card_net - allocates net_device and card structure
1509  *
1510  * returns the card structure or NULL in case of errors
1511  *
1512  * the card and net_device structures are linked to each other
1513  */
1514 #define GELIC_ALIGN (32)
1515 static struct gelic_card *gelic_alloc_card_net(struct net_device **netdev)
1516 {
1517     struct gelic_card *card;
1518     struct gelic_port *port;
1519     void *p;
1520     size_t alloc_size;
1521     /*
1522      * gelic requires dma descriptor is 32 bytes aligned and
1523      * the hypervisor requires irq_status is 8 bytes aligned.
1524      */
1525     BUILD_BUG_ON(offsetof(struct gelic_card, irq_status) % 8);
1526     BUILD_BUG_ON(offsetof(struct gelic_card, descr) % 32);
1527     alloc_size =
1528         sizeof(struct gelic_card) +
1529         sizeof(struct gelic_descr) * GELIC_NET_RX_DESCRIPTORS +
1530         sizeof(struct gelic_descr) * GELIC_NET_TX_DESCRIPTORS +
1531         GELIC_ALIGN - 1;
1532 
1533     p  = kzalloc(alloc_size, GFP_KERNEL);
1534     if (!p)
1535         return NULL;
1536     card = PTR_ALIGN(p, GELIC_ALIGN);
1537     card->unalign = p;
1538 
1539     /*
1540      * alloc netdev
1541      */
1542     *netdev = alloc_etherdev(sizeof(struct gelic_port));
1543     if (!*netdev) {
1544         kfree(card->unalign);
1545         return NULL;
1546     }
1547     port = netdev_priv(*netdev);
1548 
1549     /* gelic_port */
1550     port->netdev = *netdev;
1551     port->card = card;
1552     port->type = GELIC_PORT_ETHERNET_0;
1553 
1554     /* gelic_card */
1555     card->netdev[GELIC_PORT_ETHERNET_0] = *netdev;
1556 
1557     INIT_WORK(&card->tx_timeout_task, gelic_net_tx_timeout_task);
1558     init_waitqueue_head(&card->waitq);
1559     atomic_set(&card->tx_timeout_task_counter, 0);
1560     mutex_init(&card->updown_lock);
1561     atomic_set(&card->users, 0);
1562 
1563     return card;
1564 }
1565 
1566 static void gelic_card_get_vlan_info(struct gelic_card *card)
1567 {
1568     u64 v1, v2;
1569     int status;
1570     unsigned int i;
1571     struct {
1572         int tx;
1573         int rx;
1574     } vlan_id_ix[2] = {
1575         [GELIC_PORT_ETHERNET_0] = {
1576             .tx = GELIC_LV1_VLAN_TX_ETHERNET_0,
1577             .rx = GELIC_LV1_VLAN_RX_ETHERNET_0
1578         },
1579         [GELIC_PORT_WIRELESS] = {
1580             .tx = GELIC_LV1_VLAN_TX_WIRELESS,
1581             .rx = GELIC_LV1_VLAN_RX_WIRELESS
1582         }
1583     };
1584 
1585     for (i = 0; i < ARRAY_SIZE(vlan_id_ix); i++) {
1586         /* tx tag */
1587         status = lv1_net_control(bus_id(card), dev_id(card),
1588                      GELIC_LV1_GET_VLAN_ID,
1589                      vlan_id_ix[i].tx,
1590                      0, 0, &v1, &v2);
1591         if (status || !v1) {
1592             if (status != LV1_NO_ENTRY)
1593                 dev_dbg(ctodev(card),
1594                     "get vlan id for tx(%d) failed(%d)\n",
1595                     vlan_id_ix[i].tx, status);
1596             card->vlan[i].tx = 0;
1597             card->vlan[i].rx = 0;
1598             continue;
1599         }
1600         card->vlan[i].tx = (u16)v1;
1601 
1602         /* rx tag */
1603         status = lv1_net_control(bus_id(card), dev_id(card),
1604                      GELIC_LV1_GET_VLAN_ID,
1605                      vlan_id_ix[i].rx,
1606                      0, 0, &v1, &v2);
1607         if (status || !v1) {
1608             if (status != LV1_NO_ENTRY)
1609                 dev_info(ctodev(card),
1610                      "get vlan id for rx(%d) failed(%d)\n",
1611                      vlan_id_ix[i].rx, status);
1612             card->vlan[i].tx = 0;
1613             card->vlan[i].rx = 0;
1614             continue;
1615         }
1616         card->vlan[i].rx = (u16)v1;
1617 
1618         dev_dbg(ctodev(card), "vlan_id[%d] tx=%02x rx=%02x\n",
1619             i, card->vlan[i].tx, card->vlan[i].rx);
1620     }
1621 
1622     if (card->vlan[GELIC_PORT_ETHERNET_0].tx) {
1623         BUG_ON(!card->vlan[GELIC_PORT_WIRELESS].tx);
1624         card->vlan_required = 1;
1625     } else
1626         card->vlan_required = 0;
1627 
1628     /* check wirelss capable firmware */
1629     if (ps3_compare_firmware_version(1, 6, 0) < 0) {
1630         card->vlan[GELIC_PORT_WIRELESS].tx = 0;
1631         card->vlan[GELIC_PORT_WIRELESS].rx = 0;
1632     }
1633 
1634     dev_info(ctodev(card), "internal vlan %s\n",
1635          card->vlan_required? "enabled" : "disabled");
1636 }
1637 /*
1638  * ps3_gelic_driver_probe - add a device to the control of this driver
1639  */
1640 static int ps3_gelic_driver_probe(struct ps3_system_bus_device *dev)
1641 {
1642     struct gelic_card *card;
1643     struct net_device *netdev;
1644     int result;
1645 
1646     pr_debug("%s: called\n", __func__);
1647 
1648     udbg_shutdown_ps3gelic();
1649 
1650     result = ps3_open_hv_device(dev);
1651 
1652     if (result) {
1653         dev_dbg(&dev->core, "%s:ps3_open_hv_device failed\n",
1654             __func__);
1655         goto fail_open;
1656     }
1657 
1658     result = ps3_dma_region_create(dev->d_region);
1659 
1660     if (result) {
1661         dev_dbg(&dev->core, "%s:ps3_dma_region_create failed(%d)\n",
1662             __func__, result);
1663         BUG_ON("check region type");
1664         goto fail_dma_region;
1665     }
1666 
1667     /* alloc card/netdevice */
1668     card = gelic_alloc_card_net(&netdev);
1669     if (!card) {
1670         dev_info(&dev->core, "%s:gelic_net_alloc_card failed\n",
1671              __func__);
1672         result = -ENOMEM;
1673         goto fail_alloc_card;
1674     }
1675     ps3_system_bus_set_drvdata(dev, card);
1676     card->dev = dev;
1677 
1678     /* get internal vlan info */
1679     gelic_card_get_vlan_info(card);
1680 
1681     card->link_mode = GELIC_LV1_ETHER_AUTO_NEG;
1682 
1683     /* setup interrupt */
1684     result = lv1_net_set_interrupt_status_indicator(bus_id(card),
1685                             dev_id(card),
1686         ps3_mm_phys_to_lpar(__pa(&card->irq_status)),
1687         0);
1688 
1689     if (result) {
1690         dev_dbg(&dev->core,
1691             "%s:set_interrupt_status_indicator failed: %s\n",
1692             __func__, ps3_result(result));
1693         result = -EIO;
1694         goto fail_status_indicator;
1695     }
1696 
1697     result = ps3_sb_event_receive_port_setup(dev, PS3_BINDING_CPU_ANY,
1698         &card->irq);
1699 
1700     if (result) {
1701         dev_info(ctodev(card),
1702              "%s:gelic_net_open_device failed (%d)\n",
1703              __func__, result);
1704         result = -EPERM;
1705         goto fail_alloc_irq;
1706     }
1707     result = request_irq(card->irq, gelic_card_interrupt,
1708                  0, netdev->name, card);
1709 
1710     if (result) {
1711         dev_info(ctodev(card), "%s:request_irq failed (%d)\n",
1712             __func__, result);
1713         goto fail_request_irq;
1714     }
1715 
1716     /* setup card structure */
1717     card->irq_mask = GELIC_CARD_RXINT | GELIC_CARD_TXINT |
1718         GELIC_CARD_PORT_STATUS_CHANGED;
1719 
1720 
1721     result = gelic_card_init_chain(card, &card->tx_chain,
1722                        card->descr, GELIC_NET_TX_DESCRIPTORS);
1723     if (result)
1724         goto fail_alloc_tx;
1725     result = gelic_card_init_chain(card, &card->rx_chain,
1726                        card->descr + GELIC_NET_TX_DESCRIPTORS,
1727                        GELIC_NET_RX_DESCRIPTORS);
1728     if (result)
1729         goto fail_alloc_rx;
1730 
1731     /* head of chain */
1732     card->tx_top = card->tx_chain.head;
1733     card->rx_top = card->rx_chain.head;
1734     dev_dbg(ctodev(card), "descr rx %p, tx %p, size %#lx, num %#x\n",
1735         card->rx_top, card->tx_top, sizeof(struct gelic_descr),
1736         GELIC_NET_RX_DESCRIPTORS);
1737     /* allocate rx skbs */
1738     result = gelic_card_alloc_rx_skbs(card);
1739     if (result)
1740         goto fail_alloc_skbs;
1741 
1742     spin_lock_init(&card->tx_lock);
1743     card->tx_dma_progress = 0;
1744 
1745     /* setup net_device structure */
1746     netdev->irq = card->irq;
1747     SET_NETDEV_DEV(netdev, &card->dev->core);
1748     gelic_ether_setup_netdev_ops(netdev, &card->napi);
1749     result = gelic_net_setup_netdev(netdev, card);
1750     if (result) {
1751         dev_dbg(&dev->core, "%s: setup_netdev failed %d\n",
1752             __func__, result);
1753         goto fail_setup_netdev;
1754     }
1755 
1756 #ifdef CONFIG_GELIC_WIRELESS
1757     result = gelic_wl_driver_probe(card);
1758     if (result) {
1759         dev_dbg(&dev->core, "%s: WL init failed\n", __func__);
1760         goto fail_setup_netdev;
1761     }
1762 #endif
1763     pr_debug("%s: done\n", __func__);
1764     return 0;
1765 
1766 fail_setup_netdev:
1767 fail_alloc_skbs:
1768     gelic_card_free_chain(card, card->rx_chain.head);
1769 fail_alloc_rx:
1770     gelic_card_free_chain(card, card->tx_chain.head);
1771 fail_alloc_tx:
1772     free_irq(card->irq, card);
1773     netdev->irq = 0;
1774 fail_request_irq:
1775     ps3_sb_event_receive_port_destroy(dev, card->irq);
1776 fail_alloc_irq:
1777     lv1_net_set_interrupt_status_indicator(bus_id(card),
1778                            bus_id(card),
1779                            0, 0);
1780 fail_status_indicator:
1781     ps3_system_bus_set_drvdata(dev, NULL);
1782     kfree(netdev_card(netdev)->unalign);
1783     free_netdev(netdev);
1784 fail_alloc_card:
1785     ps3_dma_region_free(dev->d_region);
1786 fail_dma_region:
1787     ps3_close_hv_device(dev);
1788 fail_open:
1789     return result;
1790 }
1791 
1792 /*
1793  * ps3_gelic_driver_remove - remove a device from the control of this driver
1794  */
1795 
1796 static void ps3_gelic_driver_remove(struct ps3_system_bus_device *dev)
1797 {
1798     struct gelic_card *card = ps3_system_bus_get_drvdata(dev);
1799     struct net_device *netdev0;
1800     pr_debug("%s: called\n", __func__);
1801 
1802     /* set auto-negotiation */
1803     gelic_card_set_link_mode(card, GELIC_LV1_ETHER_AUTO_NEG);
1804 
1805 #ifdef CONFIG_GELIC_WIRELESS
1806     gelic_wl_driver_remove(card);
1807 #endif
1808     /* stop interrupt */
1809     gelic_card_set_irq_mask(card, 0);
1810 
1811     /* turn off DMA, force end */
1812     gelic_card_disable_rxdmac(card);
1813     gelic_card_disable_txdmac(card);
1814 
1815     /* release chains */
1816     gelic_card_release_tx_chain(card, 1);
1817     gelic_card_release_rx_chain(card);
1818 
1819     gelic_card_free_chain(card, card->tx_top);
1820     gelic_card_free_chain(card, card->rx_top);
1821 
1822     netdev0 = card->netdev[GELIC_PORT_ETHERNET_0];
1823     /* disconnect event port */
1824     free_irq(card->irq, card);
1825     netdev0->irq = 0;
1826     ps3_sb_event_receive_port_destroy(card->dev, card->irq);
1827 
1828     wait_event(card->waitq,
1829            atomic_read(&card->tx_timeout_task_counter) == 0);
1830 
1831     lv1_net_set_interrupt_status_indicator(bus_id(card), dev_id(card),
1832                            0 , 0);
1833 
1834     unregister_netdev(netdev0);
1835     kfree(netdev_card(netdev0)->unalign);
1836     free_netdev(netdev0);
1837 
1838     ps3_system_bus_set_drvdata(dev, NULL);
1839 
1840     ps3_dma_region_free(dev->d_region);
1841 
1842     ps3_close_hv_device(dev);
1843 
1844     pr_debug("%s: done\n", __func__);
1845 }
1846 
1847 static struct ps3_system_bus_driver ps3_gelic_driver = {
1848     .match_id = PS3_MATCH_ID_GELIC,
1849     .probe = ps3_gelic_driver_probe,
1850     .remove = ps3_gelic_driver_remove,
1851     .shutdown = ps3_gelic_driver_remove,
1852     .core.name = "ps3_gelic_driver",
1853     .core.owner = THIS_MODULE,
1854 };
1855 
1856 static int __init ps3_gelic_driver_init (void)
1857 {
1858     return firmware_has_feature(FW_FEATURE_PS3_LV1)
1859         ? ps3_system_bus_driver_register(&ps3_gelic_driver)
1860         : -ENODEV;
1861 }
1862 
1863 static void __exit ps3_gelic_driver_exit (void)
1864 {
1865     ps3_system_bus_driver_unregister(&ps3_gelic_driver);
1866 }
1867 
1868 module_init(ps3_gelic_driver_init);
1869 module_exit(ps3_gelic_driver_exit);
1870 
1871 MODULE_ALIAS(PS3_MODULE_ALIAS_GELIC);
1872