2 * PS3 gelic network driver.
4 * Copyright (C) 2007 Sony Computer Entertainment Inc.
5 * Copyright 2006, 2007 Sony Corporation
7 * This file is based on: spider_net.c
9 * (C) Copyright IBM Corp. 2005
11 * Authors : Utz Bacher <utz.bacher@de.ibm.com>
12 * Jens Osterkamp <Jens.Osterkamp@de.ibm.com>
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2, or (at your option)
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31 #include <linux/kernel.h>
32 #include <linux/module.h>
34 #include <linux/etherdevice.h>
35 #include <linux/ethtool.h>
36 #include <linux/if_vlan.h>
40 #include <linux/tcp.h>
42 #include <linux/dma-mapping.h>
43 #include <net/checksum.h>
44 #include <asm/firmware.h>
46 #include <asm/lv1call.h>
48 #include "ps3_gelic_net.h"
50 #define DRV_NAME "Gelic Network Driver"
51 #define DRV_VERSION "1.0"
53 MODULE_AUTHOR("SCE Inc.");
54 MODULE_DESCRIPTION("Gelic Network driver");
55 MODULE_LICENSE("GPL");
57 static inline struct device *ctodev(struct gelic_card *card)
59 return &card->dev->core;
61 static inline u64 bus_id(struct gelic_card *card)
63 return card->dev->bus_id;
65 static inline u64 dev_id(struct gelic_card *card)
67 return card->dev->dev_id;
71 static int gelic_card_set_irq_mask(struct gelic_card *card, u64 mask)
75 status = lv1_net_set_interrupt_mask(bus_id(card), dev_id(card),
78 dev_info(ctodev(card),
79 "lv1_net_set_interrupt_mask failed %d\n", status);
82 static inline void gelic_card_rx_irq_on(struct gelic_card *card)
84 gelic_card_set_irq_mask(card, card->ghiintmask | GELIC_CARD_RXINT);
86 static inline void gelic_card_rx_irq_off(struct gelic_card *card)
88 gelic_card_set_irq_mask(card, card->ghiintmask & ~GELIC_CARD_RXINT);
91 * gelic_descr_get_status -- returns the status of a descriptor
92 * @descr: descriptor to look at
94 * returns the status as in the dmac_cmd_status field of the descriptor
96 static enum gelic_descr_dma_status
97 gelic_descr_get_status(struct gelic_descr *descr)
99 return be32_to_cpu(descr->dmac_cmd_status) & GELIC_DESCR_DMA_STAT_MASK;
103 * gelic_descr_set_status -- sets the status of a descriptor
104 * @descr: descriptor to change
105 * @status: status to set in the descriptor
107 * changes the status to the specified value. Doesn't change other bits
110 static void gelic_descr_set_status(struct gelic_descr *descr,
111 enum gelic_descr_dma_status status)
113 descr->dmac_cmd_status = cpu_to_be32(status |
114 (be32_to_cpu(descr->dmac_cmd_status) &
115 ~GELIC_DESCR_DMA_STAT_MASK));
117 * dma_cmd_status field is used to indicate whether the descriptor
119 * Usually caller of this function wants to inform that to the
120 * hardware, so we assure here the hardware sees the change.
126 * gelic_card_free_chain - free descriptor chain
127 * @card: card structure
128 * @descr_in: address of desc
130 static void gelic_card_free_chain(struct gelic_card *card,
131 struct gelic_descr *descr_in)
133 struct gelic_descr *descr;
135 for (descr = descr_in; descr && descr->bus_addr; descr = descr->next) {
136 dma_unmap_single(ctodev(card), descr->bus_addr,
137 GELIC_DESCR_SIZE, DMA_BIDIRECTIONAL);
143 * gelic_card_init_chain - links descriptor chain
144 * @card: card structure
145 * @chain: address of chain
146 * @start_descr: address of descriptor array
147 * @no: number of descriptors
149 * we manage a circular list that mirrors the hardware structure,
150 * except that the hardware uses bus addresses.
152 * returns 0 on success, <0 on failure
154 static int gelic_card_init_chain(struct gelic_card *card,
155 struct gelic_descr_chain *chain,
156 struct gelic_descr *start_descr, int no)
159 struct gelic_descr *descr;
162 memset(descr, 0, sizeof(*descr) * no);
164 /* set up the hardware pointers in each descriptor */
165 for (i = 0; i < no; i++, descr++) {
166 gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
168 dma_map_single(ctodev(card), descr,
172 if (!descr->bus_addr)
175 descr->next = descr + 1;
176 descr->prev = descr - 1;
178 /* make them as ring */
179 (descr - 1)->next = start_descr;
180 start_descr->prev = (descr - 1);
182 /* chain bus addr of hw descriptor */
184 for (i = 0; i < no; i++, descr++) {
185 descr->next_descr_addr = cpu_to_be32(descr->next->bus_addr);
188 chain->head = start_descr;
189 chain->tail = start_descr;
191 /* do not chain last hw descriptor */
192 (descr - 1)->next_descr_addr = 0;
197 for (i--, descr--; 0 <= i; i--, descr--)
199 dma_unmap_single(ctodev(card), descr->bus_addr,
206 * gelic_descr_prepare_rx - reinitializes a rx descriptor
207 * @card: card structure
208 * @descr: descriptor to re-init
210 * return 0 on succes, <0 on failure
212 * allocates a new rx skb, iommu-maps it and attaches it to the descriptor.
213 * Activate the descriptor state-wise
215 static int gelic_descr_prepare_rx(struct gelic_card *card,
216 struct gelic_descr *descr)
219 unsigned int bufsize;
221 if (gelic_descr_get_status(descr) != GELIC_DESCR_DMA_NOT_IN_USE)
222 dev_info(ctodev(card), "%s: ERROR status \n", __func__);
224 /* we need to round up the buffer size to a multiple of 128 */
225 bufsize = ALIGN(GELIC_NET_MAX_MTU, GELIC_NET_RXBUF_ALIGN);
227 /* and we need to have it 128 byte aligned, therefore we allocate a
229 descr->skb = netdev_alloc_skb(card->netdev,
230 bufsize + GELIC_NET_RXBUF_ALIGN - 1);
232 descr->buf_addr = 0; /* tell DMAC don't touch memory */
233 dev_info(ctodev(card),
234 "%s:allocate skb failed !!\n", __func__);
237 descr->buf_size = cpu_to_be32(bufsize);
238 descr->dmac_cmd_status = 0;
239 descr->result_size = 0;
240 descr->valid_size = 0;
241 descr->data_error = 0;
243 offset = ((unsigned long)descr->skb->data) &
244 (GELIC_NET_RXBUF_ALIGN - 1);
246 skb_reserve(descr->skb, GELIC_NET_RXBUF_ALIGN - offset);
247 /* io-mmu-map the skb */
248 descr->buf_addr = cpu_to_be32(dma_map_single(ctodev(card),
252 if (!descr->buf_addr) {
253 dev_kfree_skb_any(descr->skb);
255 dev_info(ctodev(card),
256 "%s:Could not iommu-map rx buffer\n", __func__);
257 gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
260 gelic_descr_set_status(descr, GELIC_DESCR_DMA_CARDOWNED);
266 * gelic_card_release_rx_chain - free all skb of rx descr
267 * @card: card structure
270 static void gelic_card_release_rx_chain(struct gelic_card *card)
272 struct gelic_descr *descr = card->rx_chain.head;
276 dma_unmap_single(ctodev(card),
277 be32_to_cpu(descr->buf_addr),
281 dev_kfree_skb_any(descr->skb);
283 gelic_descr_set_status(descr,
284 GELIC_DESCR_DMA_NOT_IN_USE);
287 } while (descr != card->rx_chain.head);
291 * gelic_card_fill_rx_chain - fills descriptors/skbs in the rx chains
292 * @card: card structure
294 * fills all descriptors in the rx chain: allocates skbs
295 * and iommu-maps them.
296 * returns 0 on success, < 0 on failure
298 static int gelic_card_fill_rx_chain(struct gelic_card *card)
300 struct gelic_descr *descr = card->rx_chain.head;
305 ret = gelic_descr_prepare_rx(card, descr);
310 } while (descr != card->rx_chain.head);
314 gelic_card_release_rx_chain(card);
319 * gelic_card_alloc_rx_skbs - allocates rx skbs in rx descriptor chains
320 * @card: card structure
322 * returns 0 on success, < 0 on failure
324 static int gelic_card_alloc_rx_skbs(struct gelic_card *card)
326 struct gelic_descr_chain *chain;
328 chain = &card->rx_chain;
329 ret = gelic_card_fill_rx_chain(card);
330 chain->head = card->rx_top->prev; /* point to the last */
335 * gelic_descr_release_tx - processes a used tx descriptor
336 * @card: card structure
337 * @descr: descriptor to release
339 * releases a used tx descriptor (unmapping, freeing of skb)
341 static void gelic_descr_release_tx(struct gelic_card *card,
342 struct gelic_descr *descr)
344 struct sk_buff *skb = descr->skb;
347 BUG_ON(!(be32_to_cpu(descr->data_status) &
348 (1 << GELIC_DESCR_TX_DMA_FRAME_TAIL)));
350 dma_unmap_single(ctodev(card),
351 be32_to_cpu(descr->buf_addr), skb->len, DMA_TO_DEVICE);
352 dev_kfree_skb_any(skb);
356 descr->next_descr_addr = 0;
357 descr->result_size = 0;
358 descr->valid_size = 0;
359 descr->data_status = 0;
360 descr->data_error = 0;
363 /* set descr status */
364 gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
368 * gelic_card_release_tx_chain - processes sent tx descriptors
369 * @card: adapter structure
370 * @stop: net_stop sequence
372 * releases the tx descriptors that gelic has finished with
374 static void gelic_card_release_tx_chain(struct gelic_card *card, int stop)
376 struct gelic_descr_chain *tx_chain;
377 enum gelic_descr_dma_status status;
380 for (tx_chain = &card->tx_chain;
381 tx_chain->head != tx_chain->tail && tx_chain->tail;
382 tx_chain->tail = tx_chain->tail->next) {
383 status = gelic_descr_get_status(tx_chain->tail);
385 case GELIC_DESCR_DMA_RESPONSE_ERROR:
386 case GELIC_DESCR_DMA_PROTECTION_ERROR:
387 case GELIC_DESCR_DMA_FORCE_END:
388 if (printk_ratelimit())
389 dev_info(ctodev(card),
390 "%s: forcing end of tx descriptor " \
393 card->netdev->stats.tx_dropped++;
396 case GELIC_DESCR_DMA_COMPLETE:
397 if (tx_chain->tail->skb) {
398 card->netdev->stats.tx_packets++;
399 card->netdev->stats.tx_bytes +=
400 tx_chain->tail->skb->len;
404 case GELIC_DESCR_DMA_CARDOWNED:
405 /* pending tx request */
407 /* any other value (== GELIC_DESCR_DMA_NOT_IN_USE) */
411 gelic_descr_release_tx(card, tx_chain->tail);
415 if (!stop && release)
416 netif_wake_queue(card->netdev);
420 * gelic_net_set_multi - sets multicast addresses and promisc flags
421 * @netdev: interface device structure
423 * gelic_net_set_multi configures multicast addresses as needed for the
424 * netdev interface. It also sets up multicast, allmulti and promisc
425 * flags appropriately
427 static void gelic_net_set_multi(struct net_device *netdev)
429 struct gelic_card *card = netdev_priv(netdev);
430 struct dev_mc_list *mc;
436 /* clear all multicast address */
437 status = lv1_net_remove_multicast_address(bus_id(card), dev_id(card),
440 dev_err(ctodev(card),
441 "lv1_net_remove_multicast_address failed %d\n",
443 /* set broadcast address */
444 status = lv1_net_add_multicast_address(bus_id(card), dev_id(card),
445 GELIC_NET_BROADCAST_ADDR, 0);
447 dev_err(ctodev(card),
448 "lv1_net_add_multicast_address failed, %d\n",
451 if (netdev->flags & IFF_ALLMULTI
452 || netdev->mc_count > GELIC_NET_MC_COUNT_MAX) { /* list max */
453 status = lv1_net_add_multicast_address(bus_id(card),
457 dev_err(ctodev(card),
458 "lv1_net_add_multicast_address failed, %d\n",
463 /* set multicast address */
464 for (mc = netdev->mc_list; mc; mc = mc->next) {
467 for (i = 0; i < ETH_ALEN; i++) {
471 status = lv1_net_add_multicast_address(bus_id(card),
475 dev_err(ctodev(card),
476 "lv1_net_add_multicast_address failed, %d\n",
482 * gelic_card_enable_rxdmac - enables the receive DMA controller
483 * @card: card structure
485 * gelic_card_enable_rxdmac enables the DMA controller by setting RX_DMA_EN
486 * in the GDADMACCNTR register
488 static inline void gelic_card_enable_rxdmac(struct gelic_card *card)
492 status = lv1_net_start_rx_dma(bus_id(card), dev_id(card),
493 card->rx_chain.tail->bus_addr, 0);
495 dev_info(ctodev(card),
496 "lv1_net_start_rx_dma failed, status=%d\n", status);
500 * gelic_card_disable_rxdmac - disables the receive DMA controller
501 * @card: card structure
503 * gelic_card_disable_rxdmac terminates processing on the DMA controller by
504 * turing off DMA and issueing a force end
506 static inline void gelic_card_disable_rxdmac(struct gelic_card *card)
510 /* this hvc blocks until the DMA in progress really stopped */
511 status = lv1_net_stop_rx_dma(bus_id(card), dev_id(card), 0);
513 dev_err(ctodev(card),
514 "lv1_net_stop_rx_dma faild, %d\n", status);
518 * gelic_card_disable_txdmac - disables the transmit DMA controller
519 * @card: card structure
521 * gelic_card_disable_txdmac terminates processing on the DMA controller by
522 * turing off DMA and issueing a force end
524 static inline void gelic_card_disable_txdmac(struct gelic_card *card)
528 /* this hvc blocks until the DMA in progress really stopped */
529 status = lv1_net_stop_tx_dma(bus_id(card), dev_id(card), 0);
531 dev_err(ctodev(card),
532 "lv1_net_stop_tx_dma faild, status=%d\n", status);
536 * gelic_net_stop - called upon ifconfig down
537 * @netdev: interface device structure
541 static int gelic_net_stop(struct net_device *netdev)
543 struct gelic_card *card = netdev_priv(netdev);
545 napi_disable(&card->napi);
546 netif_stop_queue(netdev);
548 /* turn off DMA, force end */
549 gelic_card_disable_rxdmac(card);
550 gelic_card_disable_txdmac(card);
552 gelic_card_set_irq_mask(card, 0);
554 /* disconnect event port */
555 free_irq(card->netdev->irq, card->netdev);
556 ps3_sb_event_receive_port_destroy(card->dev, card->netdev->irq);
557 card->netdev->irq = NO_IRQ;
559 netif_carrier_off(netdev);
562 gelic_card_release_tx_chain(card, 1);
563 gelic_card_release_rx_chain(card);
565 gelic_card_free_chain(card, card->tx_top);
566 gelic_card_free_chain(card, card->rx_top);
572 * gelic_card_get_next_tx_descr - returns the next available tx descriptor
573 * @card: device structure to get descriptor from
575 * returns the address of the next descriptor, or NULL if not available.
577 static struct gelic_descr *
578 gelic_card_get_next_tx_descr(struct gelic_card *card)
580 if (!card->tx_chain.head)
582 /* see if the next descriptor is free */
583 if (card->tx_chain.tail != card->tx_chain.head->next &&
584 gelic_descr_get_status(card->tx_chain.head) ==
585 GELIC_DESCR_DMA_NOT_IN_USE)
586 return card->tx_chain.head;
593 * gelic_descr_set_tx_cmdstat - sets the tx descriptor command field
594 * @descr: descriptor structure to fill out
595 * @skb: packet to consider
597 * fills out the command and status field of the descriptor structure,
598 * depending on hardware checksum settings. This function assumes a wmb()
599 * has executed before.
601 static void gelic_descr_set_tx_cmdstat(struct gelic_descr *descr,
604 if (skb->ip_summed != CHECKSUM_PARTIAL)
605 descr->dmac_cmd_status =
606 cpu_to_be32(GELIC_DESCR_DMA_CMD_NO_CHKSUM |
607 GELIC_DESCR_TX_DMA_FRAME_TAIL);
610 * if yes: tcp? udp? */
611 if (skb->protocol == htons(ETH_P_IP)) {
612 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
613 descr->dmac_cmd_status =
614 cpu_to_be32(GELIC_DESCR_DMA_CMD_TCP_CHKSUM |
615 GELIC_DESCR_TX_DMA_FRAME_TAIL);
617 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
618 descr->dmac_cmd_status =
619 cpu_to_be32(GELIC_DESCR_DMA_CMD_UDP_CHKSUM |
620 GELIC_DESCR_TX_DMA_FRAME_TAIL);
622 * the stack should checksum non-tcp and non-udp
623 * packets on his own: NETIF_F_IP_CSUM
625 descr->dmac_cmd_status =
626 cpu_to_be32(GELIC_DESCR_DMA_CMD_NO_CHKSUM |
627 GELIC_DESCR_TX_DMA_FRAME_TAIL);
632 static inline struct sk_buff *gelic_put_vlan_tag(struct sk_buff *skb,
635 struct vlan_ethhdr *veth;
636 static unsigned int c;
638 if (skb_headroom(skb) < VLAN_HLEN) {
639 struct sk_buff *sk_tmp = skb;
640 pr_debug("%s: hd=%d c=%ud\n", __func__, skb_headroom(skb), c);
641 skb = skb_realloc_headroom(sk_tmp, VLAN_HLEN);
644 dev_kfree_skb_any(sk_tmp);
646 veth = (struct vlan_ethhdr *)skb_push(skb, VLAN_HLEN);
648 /* Move the mac addresses to the top of buffer */
649 memmove(skb->data, skb->data + VLAN_HLEN, 2 * ETH_ALEN);
651 veth->h_vlan_proto = __constant_htons(ETH_P_8021Q);
652 veth->h_vlan_TCI = htons(tag);
658 * gelic_descr_prepare_tx - get dma address of skb_data
659 * @card: card structure
660 * @descr: descriptor structure
661 * @skb: packet to use
663 * returns 0 on success, <0 on failure.
666 static int gelic_descr_prepare_tx(struct gelic_card *card,
667 struct gelic_descr *descr,
672 if (card->vlan_index != -1) {
673 struct sk_buff *skb_tmp;
674 skb_tmp = gelic_put_vlan_tag(skb,
675 card->vlan_id[card->vlan_index]);
681 buf = dma_map_single(ctodev(card), skb->data, skb->len, DMA_TO_DEVICE);
684 dev_err(ctodev(card),
685 "dma map 2 failed (%p, %i). Dropping packet\n",
686 skb->data, skb->len);
690 descr->buf_addr = cpu_to_be32(buf);
691 descr->buf_size = cpu_to_be32(skb->len);
693 descr->data_status = 0;
694 descr->next_descr_addr = 0; /* terminate hw descr */
695 gelic_descr_set_tx_cmdstat(descr, skb);
697 /* bump free descriptor pointer */
698 card->tx_chain.head = descr->next;
703 * gelic_card_kick_txdma - enables TX DMA processing
704 * @card: card structure
705 * @descr: descriptor address to enable TX processing at
708 static int gelic_card_kick_txdma(struct gelic_card *card,
709 struct gelic_descr *descr)
713 if (card->tx_dma_progress)
716 if (gelic_descr_get_status(descr) == GELIC_DESCR_DMA_CARDOWNED) {
717 card->tx_dma_progress = 1;
718 status = lv1_net_start_tx_dma(bus_id(card), dev_id(card),
721 dev_info(ctodev(card), "lv1_net_start_txdma failed," \
722 "status=%d\n", status);
728 * gelic_net_xmit - transmits a frame over the device
729 * @skb: packet to send out
730 * @netdev: interface device structure
732 * returns 0 on success, <0 on failure
734 static int gelic_net_xmit(struct sk_buff *skb, struct net_device *netdev)
736 struct gelic_card *card = netdev_priv(netdev);
737 struct gelic_descr *descr;
741 spin_lock_irqsave(&card->tx_dma_lock, flags);
743 gelic_card_release_tx_chain(card, 0);
745 descr = gelic_card_get_next_tx_descr(card);
748 * no more descriptors free
750 netif_stop_queue(netdev);
751 spin_unlock_irqrestore(&card->tx_dma_lock, flags);
752 return NETDEV_TX_BUSY;
755 result = gelic_descr_prepare_tx(card, descr, skb);
758 * DMA map failed. As chanses are that failure
759 * would continue, just release skb and return
761 card->netdev->stats.tx_dropped++;
762 dev_kfree_skb_any(skb);
763 spin_unlock_irqrestore(&card->tx_dma_lock, flags);
767 * link this prepared descriptor to previous one
768 * to achieve high performance
770 descr->prev->next_descr_addr = cpu_to_be32(descr->bus_addr);
772 * as hardware descriptor is modified in the above lines,
773 * ensure that the hardware sees it
776 if (gelic_card_kick_txdma(card, descr)) {
779 * release descriptors which were just prepared
781 card->netdev->stats.tx_dropped++;
782 gelic_descr_release_tx(card, descr);
783 gelic_descr_release_tx(card, descr->next);
784 card->tx_chain.tail = descr->next->next;
785 dev_info(ctodev(card), "%s: kick failure\n", __func__);
787 /* OK, DMA started/reserved */
788 netdev->trans_start = jiffies;
791 spin_unlock_irqrestore(&card->tx_dma_lock, flags);
796 * gelic_net_pass_skb_up - takes an skb from a descriptor and passes it on
797 * @descr: descriptor to process
798 * @card: card structure
800 * iommu-unmaps the skb, fills out skb structure and passes the data to the
801 * stack. The descriptor state is not changed.
803 static void gelic_net_pass_skb_up(struct gelic_descr *descr,
804 struct gelic_card *card)
807 struct net_device *netdev;
808 u32 data_status, data_error;
810 data_status = be32_to_cpu(descr->data_status);
811 data_error = be32_to_cpu(descr->data_error);
812 netdev = card->netdev;
813 /* unmap skb buffer */
815 dma_unmap_single(ctodev(card),
816 be32_to_cpu(descr->buf_addr), GELIC_NET_MAX_MTU,
819 skb_put(skb, descr->valid_size ?
820 be32_to_cpu(descr->valid_size) :
821 be32_to_cpu(descr->result_size));
822 if (!descr->valid_size)
823 dev_info(ctodev(card), "buffer full %x %x %x\n",
824 be32_to_cpu(descr->result_size),
825 be32_to_cpu(descr->buf_size),
826 be32_to_cpu(descr->dmac_cmd_status));
830 * the card put 2 bytes vlan tag in front
831 * of the ethernet frame
834 skb->protocol = eth_type_trans(skb, netdev);
836 /* checksum offload */
838 if ((data_status & GELIC_DESCR_DATA_STATUS_CHK_MASK) &&
839 (!(data_error & GELIC_DESCR_DATA_ERROR_CHK_MASK)))
840 skb->ip_summed = CHECKSUM_UNNECESSARY;
842 skb->ip_summed = CHECKSUM_NONE;
844 skb->ip_summed = CHECKSUM_NONE;
846 /* update netdevice statistics */
847 card->netdev->stats.rx_packets++;
848 card->netdev->stats.rx_bytes += skb->len;
850 /* pass skb up to stack */
851 netif_receive_skb(skb);
855 * gelic_card_decode_one_descr - processes an rx descriptor
856 * @card: card structure
858 * returns 1 if a packet has been sent to the stack, otherwise 0
860 * processes an rx descriptor by iommu-unmapping the data buffer and passing
861 * the packet up to the stack
863 static int gelic_card_decode_one_descr(struct gelic_card *card)
865 enum gelic_descr_dma_status status;
866 struct gelic_descr_chain *chain = &card->rx_chain;
867 struct gelic_descr *descr = chain->tail;
868 int dmac_chain_ended;
870 status = gelic_descr_get_status(descr);
871 /* is this descriptor terminated with next_descr == NULL? */
873 be32_to_cpu(descr->dmac_cmd_status) &
874 GELIC_DESCR_RX_DMA_CHAIN_END;
876 if (status == GELIC_DESCR_DMA_CARDOWNED)
879 if (status == GELIC_DESCR_DMA_NOT_IN_USE) {
880 dev_dbg(ctodev(card), "dormant descr? %p\n", descr);
884 if ((status == GELIC_DESCR_DMA_RESPONSE_ERROR) ||
885 (status == GELIC_DESCR_DMA_PROTECTION_ERROR) ||
886 (status == GELIC_DESCR_DMA_FORCE_END)) {
887 dev_info(ctodev(card), "dropping RX descriptor with state %x\n",
889 card->netdev->stats.rx_dropped++;
893 if (status == GELIC_DESCR_DMA_BUFFER_FULL) {
895 * Buffer full would occur if and only if
896 * the frame length was longer than the size of this
897 * descriptor's buffer. If the frame length was equal
898 * to or shorter than buffer'size, FRAME_END condition
900 * Anyway this frame was longer than the MTU,
903 dev_info(ctodev(card), "overlength frame\n");
907 * descriptoers any other than FRAME_END here should
908 * be treated as error.
910 if (status != GELIC_DESCR_DMA_FRAME_END) {
911 dev_dbg(ctodev(card), "RX descriptor with state %x\n",
916 /* ok, we've got a packet in descr */
917 gelic_net_pass_skb_up(descr, card);
920 * So that always DMAC can see the end
921 * of the descriptor chain to avoid
922 * from unwanted DMAC overrun.
924 descr->next_descr_addr = 0;
926 /* change the descriptor state: */
927 gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
930 * this call can fail, but for now, just leave this
931 * decriptor without skb
933 gelic_descr_prepare_rx(card, descr);
936 chain->tail = descr->next;
939 * Set this descriptor the end of the chain.
941 descr->prev->next_descr_addr = cpu_to_be32(descr->bus_addr);
944 * If dmac chain was met, DMAC stopped.
947 if (dmac_chain_ended) {
948 card->rx_dma_restart_required = 1;
949 dev_dbg(ctodev(card), "reenable rx dma scheduled\n");
956 * gelic_net_poll - NAPI poll function called by the stack to return packets
957 * @netdev: interface device structure
958 * @budget: number of packets we can pass to the stack at most
960 * returns 0 if no more packets available to the driver/stack. Returns 1,
961 * if the quota is exceeded, but the driver has still packets.
964 static int gelic_net_poll(struct napi_struct *napi, int budget)
966 struct gelic_card *card = container_of(napi, struct gelic_card, napi);
967 struct net_device *netdev = card->netdev;
968 int packets_done = 0;
970 while (packets_done < budget) {
971 if (!gelic_card_decode_one_descr(card))
977 if (packets_done < budget) {
978 netif_rx_complete(netdev, napi);
979 gelic_card_rx_irq_on(card);
984 * gelic_net_change_mtu - changes the MTU of an interface
985 * @netdev: interface device structure
986 * @new_mtu: new MTU value
988 * returns 0 on success, <0 on failure
990 static int gelic_net_change_mtu(struct net_device *netdev, int new_mtu)
992 /* no need to re-alloc skbs or so -- the max mtu is about 2.3k
993 * and mtu is outbound only anyway */
994 if ((new_mtu < GELIC_NET_MIN_MTU) ||
995 (new_mtu > GELIC_NET_MAX_MTU)) {
998 netdev->mtu = new_mtu;
1003 * gelic_card_interrupt - event handler for gelic_net
1005 static irqreturn_t gelic_card_interrupt(int irq, void *ptr)
1007 unsigned long flags;
1008 struct net_device *netdev = ptr;
1009 struct gelic_card *card = netdev_priv(netdev);
1012 status = card->irq_status;
1017 if (card->rx_dma_restart_required) {
1018 card->rx_dma_restart_required = 0;
1019 gelic_card_enable_rxdmac(card);
1022 if (status & GELIC_CARD_RXINT) {
1023 gelic_card_rx_irq_off(card);
1024 netif_rx_schedule(netdev, &card->napi);
1027 if (status & GELIC_CARD_TXINT) {
1028 spin_lock_irqsave(&card->tx_dma_lock, flags);
1029 card->tx_dma_progress = 0;
1030 gelic_card_release_tx_chain(card, 0);
1031 /* kick outstanding tx descriptor if any */
1032 gelic_card_kick_txdma(card, card->tx_chain.tail);
1033 spin_unlock_irqrestore(&card->tx_dma_lock, flags);
1038 #ifdef CONFIG_NET_POLL_CONTROLLER
1040 * gelic_net_poll_controller - artificial interrupt for netconsole etc.
1041 * @netdev: interface device structure
1043 * see Documentation/networking/netconsole.txt
1045 static void gelic_net_poll_controller(struct net_device *netdev)
1047 struct gelic_card *card = netdev_priv(netdev);
1049 gelic_card_set_irq_mask(card, 0);
1050 gelic_card_interrupt(netdev->irq, netdev);
1051 gelic_card_set_irq_mask(card, card->ghiintmask);
1053 #endif /* CONFIG_NET_POLL_CONTROLLER */
1056 * gelic_card_open - open device and map dma region
1057 * @card: card structure
1059 static int gelic_card_open(struct gelic_card *card)
1063 result = ps3_sb_event_receive_port_setup(card->dev, PS3_BINDING_CPU_ANY,
1064 &card->netdev->irq);
1067 dev_info(ctodev(card),
1068 "%s:%d: recieve_port_setup failed (%d)\n",
1069 __func__, __LINE__, result);
1071 goto fail_alloc_irq;
1074 result = request_irq(card->netdev->irq, gelic_card_interrupt,
1075 IRQF_DISABLED, card->netdev->name, card->netdev);
1078 dev_info(ctodev(card), "%s:%d: request_irq failed (%d)\n",
1079 __func__, __LINE__, result);
1080 goto fail_request_irq;
1086 ps3_sb_event_receive_port_destroy(card->dev, card->netdev->irq);
1087 card->netdev->irq = NO_IRQ;
1094 * gelic_net_open - called upon ifonfig up
1095 * @netdev: interface device structure
1097 * returns 0 on success, <0 on failure
1099 * gelic_net_open allocates all the descriptors and memory needed for
1100 * operation, sets up multicast list and enables interrupts
1102 static int gelic_net_open(struct net_device *netdev)
1104 struct gelic_card *card = netdev_priv(netdev);
1106 dev_dbg(ctodev(card), " -> %s:%d\n", __func__, __LINE__);
1108 gelic_card_open(card);
1110 if (gelic_card_init_chain(card, &card->tx_chain,
1111 card->descr, GELIC_NET_TX_DESCRIPTORS))
1112 goto alloc_tx_failed;
1113 if (gelic_card_init_chain(card, &card->rx_chain,
1114 card->descr + GELIC_NET_TX_DESCRIPTORS,
1115 GELIC_NET_RX_DESCRIPTORS))
1116 goto alloc_rx_failed;
1119 card->tx_top = card->tx_chain.head;
1120 card->rx_top = card->rx_chain.head;
1121 dev_dbg(ctodev(card), "descr rx %p, tx %p, size %#lx, num %#x\n",
1122 card->rx_top, card->tx_top, sizeof(struct gelic_descr),
1123 GELIC_NET_RX_DESCRIPTORS);
1124 /* allocate rx skbs */
1125 if (gelic_card_alloc_rx_skbs(card))
1126 goto alloc_skbs_failed;
1128 napi_enable(&card->napi);
1130 card->tx_dma_progress = 0;
1131 card->ghiintmask = GELIC_CARD_RXINT | GELIC_CARD_TXINT;
1133 gelic_card_set_irq_mask(card, card->ghiintmask);
1134 gelic_card_enable_rxdmac(card);
1136 netif_start_queue(netdev);
1137 netif_carrier_on(netdev);
1142 gelic_card_free_chain(card, card->rx_top);
1144 gelic_card_free_chain(card, card->tx_top);
1149 static void gelic_net_get_drvinfo(struct net_device *netdev,
1150 struct ethtool_drvinfo *info)
1152 strncpy(info->driver, DRV_NAME, sizeof(info->driver) - 1);
1153 strncpy(info->version, DRV_VERSION, sizeof(info->version) - 1);
1156 static int gelic_ether_get_settings(struct net_device *netdev,
1157 struct ethtool_cmd *cmd)
1159 struct gelic_card *card = netdev_priv(netdev);
1164 speed = duplex = -1;
1165 status = lv1_net_control(bus_id(card), dev_id(card),
1166 GELIC_LV1_GET_ETH_PORT_STATUS,
1167 GELIC_LV1_VLAN_TX_ETHERNET, 0, 0,
1172 if (v1 & GELIC_LV1_ETHER_FULL_DUPLEX) {
1173 duplex = DUPLEX_FULL;
1175 duplex = DUPLEX_HALF;
1178 if (v1 & GELIC_LV1_ETHER_SPEED_10) {
1180 } else if (v1 & GELIC_LV1_ETHER_SPEED_100) {
1182 } else if (v1 & GELIC_LV1_ETHER_SPEED_1000) {
1186 cmd->supported = SUPPORTED_TP | SUPPORTED_Autoneg |
1187 SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full |
1188 SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full |
1189 SUPPORTED_1000baseT_Half | SUPPORTED_1000baseT_Full;
1190 cmd->advertising = cmd->supported;
1192 cmd->duplex = duplex;
1193 cmd->autoneg = AUTONEG_ENABLE; /* always enabled */
1194 cmd->port = PORT_TP;
1199 static int gelic_net_nway_reset(struct net_device *netdev)
1201 if (netif_running(netdev)) {
1202 gelic_net_stop(netdev);
1203 gelic_net_open(netdev);
1208 static u32 gelic_net_get_rx_csum(struct net_device *netdev)
1210 struct gelic_card *card = netdev_priv(netdev);
1212 return card->rx_csum;
1215 static int gelic_net_set_rx_csum(struct net_device *netdev, u32 data)
1217 struct gelic_card *card = netdev_priv(netdev);
1219 card->rx_csum = data;
1223 static struct ethtool_ops gelic_net_ethtool_ops = {
1224 .get_drvinfo = gelic_net_get_drvinfo,
1225 .get_settings = gelic_ether_get_settings,
1226 .get_link = ethtool_op_get_link,
1227 .nway_reset = gelic_net_nway_reset,
1228 .get_tx_csum = ethtool_op_get_tx_csum,
1229 .set_tx_csum = ethtool_op_set_tx_csum,
1230 .get_rx_csum = gelic_net_get_rx_csum,
1231 .set_rx_csum = gelic_net_set_rx_csum,
1235 * gelic_net_tx_timeout_task - task scheduled by the watchdog timeout
1236 * function (to be called not under interrupt status)
1237 * @work: work is context of tx timout task
1239 * called as task when tx hangs, resets interface (if interface is up)
1241 static void gelic_net_tx_timeout_task(struct work_struct *work)
1243 struct gelic_card *card =
1244 container_of(work, struct gelic_card, tx_timeout_task);
1245 struct net_device *netdev = card->netdev;
1247 dev_info(ctodev(card), "%s:Timed out. Restarting... \n", __func__);
1249 if (!(netdev->flags & IFF_UP))
1252 netif_device_detach(netdev);
1253 gelic_net_stop(netdev);
1255 gelic_net_open(netdev);
1256 netif_device_attach(netdev);
1259 atomic_dec(&card->tx_timeout_task_counter);
1263 * gelic_net_tx_timeout - called when the tx timeout watchdog kicks in.
1264 * @netdev: interface device structure
1266 * called, if tx hangs. Schedules a task that resets the interface
1268 static void gelic_net_tx_timeout(struct net_device *netdev)
1270 struct gelic_card *card;
1272 card = netdev_priv(netdev);
1273 atomic_inc(&card->tx_timeout_task_counter);
1274 if (netdev->flags & IFF_UP)
1275 schedule_work(&card->tx_timeout_task);
1277 atomic_dec(&card->tx_timeout_task_counter);
1281 * gelic_ether_setup_netdev_ops - initialization of net_device operations
1282 * @netdev: net_device structure
1284 * fills out function pointers in the net_device structure
1286 static void gelic_ether_setup_netdev_ops(struct net_device *netdev)
1288 netdev->open = &gelic_net_open;
1289 netdev->stop = &gelic_net_stop;
1290 netdev->hard_start_xmit = &gelic_net_xmit;
1291 netdev->set_multicast_list = &gelic_net_set_multi;
1292 netdev->change_mtu = &gelic_net_change_mtu;
1294 netdev->tx_timeout = &gelic_net_tx_timeout;
1295 netdev->watchdog_timeo = GELIC_NET_WATCHDOG_TIMEOUT;
1296 netdev->ethtool_ops = &gelic_net_ethtool_ops;
1300 * gelic_net_setup_netdev - initialization of net_device
1301 * @card: card structure
1303 * Returns 0 on success or <0 on failure
1305 * gelic_net_setup_netdev initializes the net_device structure
1307 static int gelic_net_setup_netdev(struct gelic_card *card)
1309 struct net_device *netdev = card->netdev;
1310 struct sockaddr addr;
1314 DECLARE_MAC_BUF(mac);
1316 SET_NETDEV_DEV(netdev, &card->dev->core);
1317 spin_lock_init(&card->tx_dma_lock);
1319 card->rx_csum = GELIC_NET_RX_CSUM_DEFAULT;
1321 gelic_ether_setup_netdev_ops(netdev);
1323 netif_napi_add(netdev, &card->napi,
1324 gelic_net_poll, GELIC_NET_NAPI_WEIGHT);
1326 netdev->features = NETIF_F_IP_CSUM;
1328 status = lv1_net_control(bus_id(card), dev_id(card),
1329 GELIC_LV1_GET_MAC_ADDRESS,
1331 if (status || !is_valid_ether_addr((u8 *)&v1)) {
1332 dev_info(ctodev(card),
1333 "%s:lv1_net_control GET_MAC_ADDR failed %d\n",
1338 memcpy(addr.sa_data, &v1, ETH_ALEN);
1339 memcpy(netdev->dev_addr, addr.sa_data, ETH_ALEN);
1340 dev_info(ctodev(card), "MAC addr %s\n",
1341 print_mac(mac, netdev->dev_addr));
1343 card->vlan_index = -1; /* no vlan */
1344 for (i = 0; i < GELIC_NET_VLAN_MAX; i++) {
1345 status = lv1_net_control(bus_id(card), dev_id(card),
1346 GELIC_LV1_GET_VLAN_ID,
1347 i + 1, /* index; one based */
1349 if (status == LV1_NO_ENTRY) {
1350 dev_dbg(ctodev(card),
1351 "GELIC_VLAN_ID no entry:%d, VLAN disabled\n",
1353 card->vlan_id[i] = 0;
1354 } else if (status) {
1355 dev_dbg(ctodev(card),
1356 "%s:get vlan id faild, status=%d\n",
1358 card->vlan_id[i] = 0;
1360 card->vlan_id[i] = (u32)v1;
1361 dev_dbg(ctodev(card), "vlan_id:%d, %lx\n", i, v1);
1365 if (card->vlan_id[GELIC_LV1_VLAN_TX_ETHERNET - 1]) {
1366 card->vlan_index = GELIC_LV1_VLAN_TX_ETHERNET - 1;
1367 netdev->hard_header_len += VLAN_HLEN;
1370 status = register_netdev(netdev);
1372 dev_err(ctodev(card), "%s:Couldn't register net_device: %d\n",
1381 * gelic_alloc_card_net - allocates net_device and card structure
1383 * returns the card structure or NULL in case of errors
1385 * the card and net_device structures are linked to each other
1387 static struct gelic_card *gelic_alloc_card_net(void)
1389 struct net_device *netdev;
1390 struct gelic_card *card;
1393 alloc_size = sizeof(*card) +
1394 sizeof(struct gelic_descr) * GELIC_NET_RX_DESCRIPTORS +
1395 sizeof(struct gelic_descr) * GELIC_NET_TX_DESCRIPTORS;
1397 * we assume private data is allocated 32 bytes (or more) aligned
1398 * so that gelic_descr should be 32 bytes aligned.
1399 * Current alloc_etherdev() does do it because NETDEV_ALIGN
1401 * check this assumption here.
1403 BUILD_BUG_ON(NETDEV_ALIGN < 32);
1404 BUILD_BUG_ON(offsetof(struct gelic_card, irq_status) % 8);
1405 BUILD_BUG_ON(offsetof(struct gelic_card, descr) % 32);
1407 netdev = alloc_etherdev(alloc_size);
1411 card = netdev_priv(netdev);
1412 card->netdev = netdev;
1413 INIT_WORK(&card->tx_timeout_task, gelic_net_tx_timeout_task);
1414 init_waitqueue_head(&card->waitq);
1415 atomic_set(&card->tx_timeout_task_counter, 0);
1421 * ps3_gelic_driver_probe - add a device to the control of this driver
1423 static int ps3_gelic_driver_probe(struct ps3_system_bus_device *dev)
1425 struct gelic_card *card = gelic_alloc_card_net();
1429 dev_info(&dev->core, "gelic_net_alloc_card failed\n");
1431 goto fail_alloc_card;
1434 ps3_system_bus_set_driver_data(dev, card);
1437 result = ps3_open_hv_device(dev);
1440 dev_dbg(&dev->core, "ps3_open_hv_device failed\n");
1444 result = ps3_dma_region_create(dev->d_region);
1447 dev_dbg(&dev->core, "ps3_dma_region_create failed(%d)\n",
1449 BUG_ON("check region type");
1450 goto fail_dma_region;
1453 result = lv1_net_set_interrupt_status_indicator(bus_id(card),
1455 ps3_mm_phys_to_lpar(__pa(&card->irq_status)),
1460 "lv1_net_set_interrupt_status_indicator failed: %s\n",
1461 ps3_result(result));
1463 goto fail_status_indicator;
1466 result = gelic_net_setup_netdev(card);
1469 dev_dbg(&dev->core, "%s:%d: ps3_dma_region_create failed: "
1470 "(%d)\n", __func__, __LINE__, result);
1471 goto fail_setup_netdev;
1477 lv1_net_set_interrupt_status_indicator(bus_id(card),
1480 fail_status_indicator:
1481 ps3_dma_region_free(dev->d_region);
1483 ps3_close_hv_device(dev);
1485 ps3_system_bus_set_driver_data(dev, NULL);
1486 free_netdev(card->netdev);
1492 * ps3_gelic_driver_remove - remove a device from the control of this driver
1495 static int ps3_gelic_driver_remove(struct ps3_system_bus_device *dev)
1497 struct gelic_card *card = ps3_system_bus_get_driver_data(dev);
1499 wait_event(card->waitq,
1500 atomic_read(&card->tx_timeout_task_counter) == 0);
1502 lv1_net_set_interrupt_status_indicator(bus_id(card), dev_id(card),
1505 unregister_netdev(card->netdev);
1506 free_netdev(card->netdev);
1508 ps3_system_bus_set_driver_data(dev, NULL);
1510 ps3_dma_region_free(dev->d_region);
1512 ps3_close_hv_device(dev);
1517 static struct ps3_system_bus_driver ps3_gelic_driver = {
1518 .match_id = PS3_MATCH_ID_GELIC,
1519 .probe = ps3_gelic_driver_probe,
1520 .remove = ps3_gelic_driver_remove,
1521 .shutdown = ps3_gelic_driver_remove,
1522 .core.name = "ps3_gelic_driver",
1523 .core.owner = THIS_MODULE,
1526 static int __init ps3_gelic_driver_init (void)
1528 return firmware_has_feature(FW_FEATURE_PS3_LV1)
1529 ? ps3_system_bus_driver_register(&ps3_gelic_driver)
1533 static void __exit ps3_gelic_driver_exit (void)
1535 ps3_system_bus_driver_unregister(&ps3_gelic_driver);
1538 module_init(ps3_gelic_driver_init);
1539 module_exit(ps3_gelic_driver_exit);
1541 MODULE_ALIAS(PS3_MODULE_ALIAS_GELIC);