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_net_card *card)
59 return &card->dev->core;
61 static inline unsigned int bus_id(struct gelic_net_card *card)
63 return card->dev->bus_id;
65 static inline unsigned int dev_id(struct gelic_net_card *card)
67 return card->dev->dev_id;
71 static int gelic_net_set_irq_mask(struct gelic_net_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_net_rx_irq_on(struct gelic_net_card *card)
84 gelic_net_set_irq_mask(card, card->ghiintmask | GELIC_NET_RXINT);
86 static inline void gelic_net_rx_irq_off(struct gelic_net_card *card)
88 gelic_net_set_irq_mask(card, card->ghiintmask & ~GELIC_NET_RXINT);
91 * gelic_net_get_descr_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_net_descr_status
97 gelic_net_get_descr_status(struct gelic_net_descr *descr)
101 cmd_status = descr->dmac_cmd_status;
102 cmd_status >>= GELIC_NET_DESCR_IND_PROC_SHIFT;
107 * gelic_net_set_descr_status -- sets the status of a descriptor
108 * @descr: descriptor to change
109 * @status: status to set in the descriptor
111 * changes the status to the specified value. Doesn't change other bits
114 static void gelic_net_set_descr_status(struct gelic_net_descr *descr,
115 enum gelic_net_descr_status status)
119 /* read the status */
120 cmd_status = descr->dmac_cmd_status;
121 /* clean the upper 4 bits */
122 cmd_status &= GELIC_NET_DESCR_IND_PROC_MASKO;
123 /* add the status to it */
124 cmd_status |= ((u32)status) << GELIC_NET_DESCR_IND_PROC_SHIFT;
125 /* and write it back */
126 descr->dmac_cmd_status = cmd_status;
128 * dma_cmd_status field is used to indicate whether the descriptor
130 * Usually caller of this function wants to inform that to the
131 * hardware, so we assure here the hardware sees the change.
137 * gelic_net_free_chain - free descriptor chain
138 * @card: card structure
139 * @descr_in: address of desc
141 static void gelic_net_free_chain(struct gelic_net_card *card,
142 struct gelic_net_descr *descr_in)
144 struct gelic_net_descr *descr;
146 for (descr = descr_in; descr && descr->bus_addr; descr = descr->next) {
147 dma_unmap_single(ctodev(card), descr->bus_addr,
148 GELIC_NET_DESCR_SIZE, DMA_BIDIRECTIONAL);
154 * gelic_net_init_chain - links descriptor chain
155 * @card: card structure
156 * @chain: address of chain
157 * @start_descr: address of descriptor array
158 * @no: number of descriptors
160 * we manage a circular list that mirrors the hardware structure,
161 * except that the hardware uses bus addresses.
163 * returns 0 on success, <0 on failure
165 static int gelic_net_init_chain(struct gelic_net_card *card,
166 struct gelic_net_descr_chain *chain,
167 struct gelic_net_descr *start_descr, int no)
170 struct gelic_net_descr *descr;
173 memset(descr, 0, sizeof(*descr) * no);
175 /* set up the hardware pointers in each descriptor */
176 for (i = 0; i < no; i++, descr++) {
177 gelic_net_set_descr_status(descr, GELIC_NET_DESCR_NOT_IN_USE);
179 dma_map_single(ctodev(card), descr,
180 GELIC_NET_DESCR_SIZE,
183 if (!descr->bus_addr)
186 descr->next = descr + 1;
187 descr->prev = descr - 1;
189 /* make them as ring */
190 (descr - 1)->next = start_descr;
191 start_descr->prev = (descr - 1);
193 /* chain bus addr of hw descriptor */
195 for (i = 0; i < no; i++, descr++) {
196 descr->next_descr_addr = descr->next->bus_addr;
199 chain->head = start_descr;
200 chain->tail = start_descr;
202 /* do not chain last hw descriptor */
203 (descr - 1)->next_descr_addr = 0;
208 for (i--, descr--; 0 <= i; i--, descr--)
210 dma_unmap_single(ctodev(card), descr->bus_addr,
211 GELIC_NET_DESCR_SIZE,
217 * gelic_net_prepare_rx_descr - reinitializes a rx descriptor
218 * @card: card structure
219 * @descr: descriptor to re-init
221 * return 0 on succes, <0 on failure
223 * allocates a new rx skb, iommu-maps it and attaches it to the descriptor.
224 * Activate the descriptor state-wise
226 static int gelic_net_prepare_rx_descr(struct gelic_net_card *card,
227 struct gelic_net_descr *descr)
230 unsigned int bufsize;
232 if (gelic_net_get_descr_status(descr) != GELIC_NET_DESCR_NOT_IN_USE) {
233 dev_info(ctodev(card), "%s: ERROR status \n", __func__);
235 /* we need to round up the buffer size to a multiple of 128 */
236 bufsize = ALIGN(GELIC_NET_MAX_MTU, GELIC_NET_RXBUF_ALIGN);
238 /* and we need to have it 128 byte aligned, therefore we allocate a
240 descr->skb = netdev_alloc_skb(card->netdev,
241 bufsize + GELIC_NET_RXBUF_ALIGN - 1);
243 descr->buf_addr = 0; /* tell DMAC don't touch memory */
244 dev_info(ctodev(card),
245 "%s:allocate skb failed !!\n", __func__);
248 descr->buf_size = bufsize;
249 descr->dmac_cmd_status = 0;
250 descr->result_size = 0;
251 descr->valid_size = 0;
252 descr->data_error = 0;
254 offset = ((unsigned long)descr->skb->data) &
255 (GELIC_NET_RXBUF_ALIGN - 1);
257 skb_reserve(descr->skb, GELIC_NET_RXBUF_ALIGN - offset);
258 /* io-mmu-map the skb */
259 descr->buf_addr = dma_map_single(ctodev(card), descr->skb->data,
262 if (!descr->buf_addr) {
263 dev_kfree_skb_any(descr->skb);
265 dev_info(ctodev(card),
266 "%s:Could not iommu-map rx buffer\n", __func__);
267 gelic_net_set_descr_status(descr, GELIC_NET_DESCR_NOT_IN_USE);
270 gelic_net_set_descr_status(descr, GELIC_NET_DESCR_CARDOWNED);
276 * gelic_net_release_rx_chain - free all skb of rx descr
277 * @card: card structure
280 static void gelic_net_release_rx_chain(struct gelic_net_card *card)
282 struct gelic_net_descr *descr = card->rx_chain.head;
286 dma_unmap_single(ctodev(card),
291 dev_kfree_skb_any(descr->skb);
293 gelic_net_set_descr_status(descr,
294 GELIC_NET_DESCR_NOT_IN_USE);
297 } while (descr != card->rx_chain.head);
301 * gelic_net_fill_rx_chain - fills descriptors/skbs in the rx chains
302 * @card: card structure
304 * fills all descriptors in the rx chain: allocates skbs
305 * and iommu-maps them.
306 * returns 0 on success, <0 on failure
308 static int gelic_net_fill_rx_chain(struct gelic_net_card *card)
310 struct gelic_net_descr *descr = card->rx_chain.head;
315 ret = gelic_net_prepare_rx_descr(card, descr);
320 } while (descr != card->rx_chain.head);
324 gelic_net_release_rx_chain(card);
329 * gelic_net_alloc_rx_skbs - allocates rx skbs in rx descriptor chains
330 * @card: card structure
332 * returns 0 on success, <0 on failure
334 static int gelic_net_alloc_rx_skbs(struct gelic_net_card *card)
336 struct gelic_net_descr_chain *chain;
338 chain = &card->rx_chain;
339 ret = gelic_net_fill_rx_chain(card);
340 chain->head = card->rx_top->prev; /* point to the last */
345 * gelic_net_release_tx_descr - processes a used tx descriptor
346 * @card: card structure
347 * @descr: descriptor to release
349 * releases a used tx descriptor (unmapping, freeing of skb)
351 static void gelic_net_release_tx_descr(struct gelic_net_card *card,
352 struct gelic_net_descr *descr)
354 struct sk_buff *skb = descr->skb;
356 BUG_ON(!(descr->data_status & (1 << GELIC_NET_TXDESC_TAIL)));
358 dma_unmap_single(ctodev(card), descr->buf_addr, skb->len,
360 dev_kfree_skb_any(skb);
364 descr->next_descr_addr = 0;
365 descr->result_size = 0;
366 descr->valid_size = 0;
367 descr->data_status = 0;
368 descr->data_error = 0;
371 /* set descr status */
372 gelic_net_set_descr_status(descr, GELIC_NET_DESCR_NOT_IN_USE);
376 * gelic_net_release_tx_chain - processes sent tx descriptors
377 * @card: adapter structure
378 * @stop: net_stop sequence
380 * releases the tx descriptors that gelic has finished with
382 static void gelic_net_release_tx_chain(struct gelic_net_card *card, int stop)
384 struct gelic_net_descr_chain *tx_chain;
385 enum gelic_net_descr_status status;
388 for (tx_chain = &card->tx_chain;
389 tx_chain->head != tx_chain->tail && tx_chain->tail;
390 tx_chain->tail = tx_chain->tail->next) {
391 status = gelic_net_get_descr_status(tx_chain->tail);
393 case GELIC_NET_DESCR_RESPONSE_ERROR:
394 case GELIC_NET_DESCR_PROTECTION_ERROR:
395 case GELIC_NET_DESCR_FORCE_END:
396 if (printk_ratelimit())
397 dev_info(ctodev(card),
398 "%s: forcing end of tx descriptor " \
401 card->netdev->stats.tx_dropped++;
404 case GELIC_NET_DESCR_COMPLETE:
405 if (tx_chain->tail->skb) {
406 card->netdev->stats.tx_packets++;
407 card->netdev->stats.tx_bytes +=
408 tx_chain->tail->skb->len;
412 case GELIC_NET_DESCR_CARDOWNED:
413 /* pending tx request */
415 /* any other value (== GELIC_NET_DESCR_NOT_IN_USE) */
419 gelic_net_release_tx_descr(card, tx_chain->tail);
423 if (!stop && release)
424 netif_wake_queue(card->netdev);
428 * gelic_net_set_multi - sets multicast addresses and promisc flags
429 * @netdev: interface device structure
431 * gelic_net_set_multi configures multicast addresses as needed for the
432 * netdev interface. It also sets up multicast, allmulti and promisc
433 * flags appropriately
435 static void gelic_net_set_multi(struct net_device *netdev)
437 struct gelic_net_card *card = netdev_priv(netdev);
438 struct dev_mc_list *mc;
444 /* clear all multicast address */
445 status = lv1_net_remove_multicast_address(bus_id(card), dev_id(card),
448 dev_err(ctodev(card),
449 "lv1_net_remove_multicast_address failed %d\n",
451 /* set broadcast address */
452 status = lv1_net_add_multicast_address(bus_id(card), dev_id(card),
453 GELIC_NET_BROADCAST_ADDR, 0);
455 dev_err(ctodev(card),
456 "lv1_net_add_multicast_address failed, %d\n",
459 if (netdev->flags & IFF_ALLMULTI
460 || netdev->mc_count > GELIC_NET_MC_COUNT_MAX) { /* list max */
461 status = lv1_net_add_multicast_address(bus_id(card),
465 dev_err(ctodev(card),
466 "lv1_net_add_multicast_address failed, %d\n",
471 /* set multicast address */
472 for (mc = netdev->mc_list; mc; mc = mc->next) {
475 for (i = 0; i < ETH_ALEN; i++) {
479 status = lv1_net_add_multicast_address(bus_id(card),
483 dev_err(ctodev(card),
484 "lv1_net_add_multicast_address failed, %d\n",
490 * gelic_net_enable_rxdmac - enables the receive DMA controller
491 * @card: card structure
493 * gelic_net_enable_rxdmac enables the DMA controller by setting RX_DMA_EN
494 * in the GDADMACCNTR register
496 static inline void gelic_net_enable_rxdmac(struct gelic_net_card *card)
500 status = lv1_net_start_rx_dma(bus_id(card), dev_id(card),
501 card->rx_chain.tail->bus_addr, 0);
503 dev_info(ctodev(card),
504 "lv1_net_start_rx_dma failed, status=%d\n", status);
508 * gelic_net_disable_rxdmac - disables the receive DMA controller
509 * @card: card structure
511 * gelic_net_disable_rxdmac terminates processing on the DMA controller by
512 * turing off DMA and issueing a force end
514 static inline void gelic_net_disable_rxdmac(struct gelic_net_card *card)
518 /* this hvc blocks until the DMA in progress really stopped */
519 status = lv1_net_stop_rx_dma(bus_id(card), dev_id(card), 0);
521 dev_err(ctodev(card),
522 "lv1_net_stop_rx_dma faild, %d\n", status);
526 * gelic_net_disable_txdmac - disables the transmit DMA controller
527 * @card: card structure
529 * gelic_net_disable_txdmac terminates processing on the DMA controller by
530 * turing off DMA and issueing a force end
532 static inline void gelic_net_disable_txdmac(struct gelic_net_card *card)
536 /* this hvc blocks until the DMA in progress really stopped */
537 status = lv1_net_stop_tx_dma(bus_id(card), dev_id(card), 0);
539 dev_err(ctodev(card),
540 "lv1_net_stop_tx_dma faild, status=%d\n", status);
544 * gelic_net_stop - called upon ifconfig down
545 * @netdev: interface device structure
549 static int gelic_net_stop(struct net_device *netdev)
551 struct gelic_net_card *card = netdev_priv(netdev);
553 napi_disable(&card->napi);
554 netif_stop_queue(netdev);
556 /* turn off DMA, force end */
557 gelic_net_disable_rxdmac(card);
558 gelic_net_disable_txdmac(card);
560 gelic_net_set_irq_mask(card, 0);
562 /* disconnect event port */
563 free_irq(card->netdev->irq, card->netdev);
564 ps3_sb_event_receive_port_destroy(card->dev, card->netdev->irq);
565 card->netdev->irq = NO_IRQ;
567 netif_carrier_off(netdev);
570 gelic_net_release_tx_chain(card, 1);
571 gelic_net_release_rx_chain(card);
573 gelic_net_free_chain(card, card->tx_top);
574 gelic_net_free_chain(card, card->rx_top);
580 * gelic_net_get_next_tx_descr - returns the next available tx descriptor
581 * @card: device structure to get descriptor from
583 * returns the address of the next descriptor, or NULL if not available.
585 static struct gelic_net_descr *
586 gelic_net_get_next_tx_descr(struct gelic_net_card *card)
588 if (!card->tx_chain.head)
590 /* see if the next descriptor is free */
591 if (card->tx_chain.tail != card->tx_chain.head->next &&
592 gelic_net_get_descr_status(card->tx_chain.head) ==
593 GELIC_NET_DESCR_NOT_IN_USE)
594 return card->tx_chain.head;
601 * gelic_net_set_txdescr_cmdstat - sets the tx descriptor command field
602 * @descr: descriptor structure to fill out
603 * @skb: packet to consider
605 * fills out the command and status field of the descriptor structure,
606 * depending on hardware checksum settings. This function assumes a wmb()
607 * has executed before.
609 static void gelic_net_set_txdescr_cmdstat(struct gelic_net_descr *descr,
612 if (skb->ip_summed != CHECKSUM_PARTIAL)
613 descr->dmac_cmd_status = GELIC_NET_DMAC_CMDSTAT_NOCS |
614 GELIC_NET_DMAC_CMDSTAT_END_FRAME;
617 * if yes: tcp? udp? */
618 if (skb->protocol == htons(ETH_P_IP)) {
619 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
620 descr->dmac_cmd_status =
621 GELIC_NET_DMAC_CMDSTAT_TCPCS |
622 GELIC_NET_DMAC_CMDSTAT_END_FRAME;
624 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
625 descr->dmac_cmd_status =
626 GELIC_NET_DMAC_CMDSTAT_UDPCS |
627 GELIC_NET_DMAC_CMDSTAT_END_FRAME;
629 * the stack should checksum non-tcp and non-udp
630 * packets on his own: NETIF_F_IP_CSUM
632 descr->dmac_cmd_status =
633 GELIC_NET_DMAC_CMDSTAT_NOCS |
634 GELIC_NET_DMAC_CMDSTAT_END_FRAME;
639 static inline struct sk_buff *gelic_put_vlan_tag(struct sk_buff *skb,
642 struct vlan_ethhdr *veth;
643 static unsigned int c;
645 if (skb_headroom(skb) < VLAN_HLEN) {
646 struct sk_buff *sk_tmp = skb;
647 pr_debug("%s: hd=%d c=%ud\n", __func__, skb_headroom(skb), c);
648 skb = skb_realloc_headroom(sk_tmp, VLAN_HLEN);
651 dev_kfree_skb_any(sk_tmp);
653 veth = (struct vlan_ethhdr *)skb_push(skb, VLAN_HLEN);
655 /* Move the mac addresses to the top of buffer */
656 memmove(skb->data, skb->data + VLAN_HLEN, 2 * ETH_ALEN);
658 veth->h_vlan_proto = __constant_htons(ETH_P_8021Q);
659 veth->h_vlan_TCI = htons(tag);
665 * gelic_net_prepare_tx_descr_v - get dma address of skb_data
666 * @card: card structure
667 * @descr: descriptor structure
668 * @skb: packet to use
670 * returns 0 on success, <0 on failure.
673 static int gelic_net_prepare_tx_descr_v(struct gelic_net_card *card,
674 struct gelic_net_descr *descr,
679 if (card->vlan_index != -1) {
680 struct sk_buff *skb_tmp;
681 skb_tmp = gelic_put_vlan_tag(skb,
682 card->vlan_id[card->vlan_index]);
688 buf = dma_map_single(ctodev(card), skb->data, skb->len, DMA_TO_DEVICE);
691 dev_err(ctodev(card),
692 "dma map 2 failed (%p, %i). Dropping packet\n",
693 skb->data, skb->len);
697 descr->buf_addr = buf;
698 descr->buf_size = skb->len;
700 descr->data_status = 0;
701 descr->next_descr_addr = 0; /* terminate hw descr */
702 gelic_net_set_txdescr_cmdstat(descr, skb);
704 /* bump free descriptor pointer */
705 card->tx_chain.head = descr->next;
710 * gelic_net_kick_txdma - enables TX DMA processing
711 * @card: card structure
712 * @descr: descriptor address to enable TX processing at
715 static int gelic_net_kick_txdma(struct gelic_net_card *card,
716 struct gelic_net_descr *descr)
721 if (card->tx_dma_progress)
724 if (gelic_net_get_descr_status(descr) == GELIC_NET_DESCR_CARDOWNED) {
725 card->tx_dma_progress = 1;
726 /* sometimes we need retry here */
728 status = lv1_net_start_tx_dma(bus_id(card),
735 dev_info(ctodev(card), "lv1_net_start_txdma failed," \
737 status, card->irq_status);
743 * gelic_net_xmit - transmits a frame over the device
744 * @skb: packet to send out
745 * @netdev: interface device structure
747 * returns 0 on success, <0 on failure
749 static int gelic_net_xmit(struct sk_buff *skb, struct net_device *netdev)
751 struct gelic_net_card *card = netdev_priv(netdev);
752 struct gelic_net_descr *descr;
756 spin_lock_irqsave(&card->tx_dma_lock, flags);
758 gelic_net_release_tx_chain(card, 0);
760 descr = gelic_net_get_next_tx_descr(card);
763 * no more descriptors free
765 netif_stop_queue(netdev);
766 spin_unlock_irqrestore(&card->tx_dma_lock, flags);
767 return NETDEV_TX_BUSY;
770 result = gelic_net_prepare_tx_descr_v(card, descr, skb);
773 * DMA map failed. As chanses are that failure
774 * would continue, just release skb and return
776 card->netdev->stats.tx_dropped++;
777 dev_kfree_skb_any(skb);
778 spin_unlock_irqrestore(&card->tx_dma_lock, flags);
782 * link this prepared descriptor to previous one
783 * to achieve high performance
785 descr->prev->next_descr_addr = descr->bus_addr;
787 * as hardware descriptor is modified in the above lines,
788 * ensure that the hardware sees it
791 if (gelic_net_kick_txdma(card, descr)) {
794 * release descriptors which were just prepared
796 card->netdev->stats.tx_dropped++;
797 gelic_net_release_tx_descr(card, descr);
798 gelic_net_release_tx_descr(card, descr->next);
799 card->tx_chain.tail = descr->next->next;
800 dev_info(ctodev(card), "%s: kick failure\n", __func__);
802 /* OK, DMA started/reserved */
803 netdev->trans_start = jiffies;
806 spin_unlock_irqrestore(&card->tx_dma_lock, flags);
811 * gelic_net_pass_skb_up - takes an skb from a descriptor and passes it on
812 * @descr: descriptor to process
813 * @card: card structure
815 * iommu-unmaps the skb, fills out skb structure and passes the data to the
816 * stack. The descriptor state is not changed.
818 static void gelic_net_pass_skb_up(struct gelic_net_descr *descr,
819 struct gelic_net_card *card)
822 struct net_device *netdev;
823 u32 data_status, data_error;
825 data_status = descr->data_status;
826 data_error = descr->data_error;
827 netdev = card->netdev;
828 /* unmap skb buffer */
830 dma_unmap_single(ctodev(card), descr->buf_addr, GELIC_NET_MAX_MTU,
833 skb_put(skb, descr->valid_size? descr->valid_size : descr->result_size);
834 if (!descr->valid_size)
835 dev_info(ctodev(card), "buffer full %x %x %x\n",
836 descr->result_size, descr->buf_size,
837 descr->dmac_cmd_status);
841 * the card put 2 bytes vlan tag in front
842 * of the ethernet frame
845 skb->protocol = eth_type_trans(skb, netdev);
847 /* checksum offload */
849 if ((data_status & GELIC_NET_DATA_STATUS_CHK_MASK) &&
850 (!(data_error & GELIC_NET_DATA_ERROR_CHK_MASK)))
851 skb->ip_summed = CHECKSUM_UNNECESSARY;
853 skb->ip_summed = CHECKSUM_NONE;
855 skb->ip_summed = CHECKSUM_NONE;
857 /* update netdevice statistics */
858 card->netdev->stats.rx_packets++;
859 card->netdev->stats.rx_bytes += skb->len;
861 /* pass skb up to stack */
862 netif_receive_skb(skb);
866 * gelic_net_decode_one_descr - processes an rx descriptor
867 * @card: card structure
869 * returns 1 if a packet has been sent to the stack, otherwise 0
871 * processes an rx descriptor by iommu-unmapping the data buffer and passing
872 * the packet up to the stack
874 static int gelic_net_decode_one_descr(struct gelic_net_card *card)
876 enum gelic_net_descr_status status;
877 struct gelic_net_descr_chain *chain = &card->rx_chain;
878 struct gelic_net_descr *descr = chain->tail;
879 int dmac_chain_ended;
881 status = gelic_net_get_descr_status(descr);
882 /* is this descriptor terminated with next_descr == NULL? */
884 descr->dmac_cmd_status & GELIC_NET_DMAC_CMDSTAT_RXDCEIS;
886 if (status == GELIC_NET_DESCR_CARDOWNED)
889 if (status == GELIC_NET_DESCR_NOT_IN_USE) {
890 dev_dbg(ctodev(card), "dormant descr? %p\n", descr);
894 if ((status == GELIC_NET_DESCR_RESPONSE_ERROR) ||
895 (status == GELIC_NET_DESCR_PROTECTION_ERROR) ||
896 (status == GELIC_NET_DESCR_FORCE_END)) {
897 dev_info(ctodev(card), "dropping RX descriptor with state %x\n",
899 card->netdev->stats.rx_dropped++;
903 if (status == GELIC_NET_DESCR_BUFFER_FULL) {
905 * Buffer full would occur if and only if
906 * the frame length was longer than the size of this
907 * descriptor's buffer. If the frame length was equal
908 * to or shorter than buffer'size, FRAME_END condition
910 * Anyway this frame was longer than the MTU,
913 dev_info(ctodev(card), "overlength frame\n");
917 * descriptoers any other than FRAME_END here should
918 * be treated as error.
920 if (status != GELIC_NET_DESCR_FRAME_END) {
921 dev_dbg(ctodev(card), "RX descriptor with state %x\n",
926 /* ok, we've got a packet in descr */
927 gelic_net_pass_skb_up(descr, card);
930 * So that always DMAC can see the end
931 * of the descriptor chain to avoid
932 * from unwanted DMAC overrun.
934 descr->next_descr_addr = 0;
936 /* change the descriptor state: */
937 gelic_net_set_descr_status(descr, GELIC_NET_DESCR_NOT_IN_USE);
940 * this call can fail, but for now, just leave this
941 * decriptor without skb
943 gelic_net_prepare_rx_descr(card, descr);
946 chain->tail = descr->next;
949 * Set this descriptor the end of the chain.
951 descr->prev->next_descr_addr = descr->bus_addr;
954 * If dmac chain was met, DMAC stopped.
957 if (dmac_chain_ended) {
958 card->rx_dma_restart_required = 1;
959 dev_dbg(ctodev(card), "reenable rx dma scheduled\n");
966 * gelic_net_poll - NAPI poll function called by the stack to return packets
967 * @netdev: interface device structure
968 * @budget: number of packets we can pass to the stack at most
970 * returns 0 if no more packets available to the driver/stack. Returns 1,
971 * if the quota is exceeded, but the driver has still packets.
974 static int gelic_net_poll(struct napi_struct *napi, int budget)
976 struct gelic_net_card *card = container_of(napi, struct gelic_net_card, napi);
977 struct net_device *netdev = card->netdev;
978 int packets_done = 0;
980 while (packets_done < budget) {
981 if (!gelic_net_decode_one_descr(card))
987 if (packets_done < budget) {
988 netif_rx_complete(netdev, napi);
989 gelic_net_rx_irq_on(card);
994 * gelic_net_change_mtu - changes the MTU of an interface
995 * @netdev: interface device structure
996 * @new_mtu: new MTU value
998 * returns 0 on success, <0 on failure
1000 static int gelic_net_change_mtu(struct net_device *netdev, int new_mtu)
1002 /* no need to re-alloc skbs or so -- the max mtu is about 2.3k
1003 * and mtu is outbound only anyway */
1004 if ((new_mtu < GELIC_NET_MIN_MTU) ||
1005 (new_mtu > GELIC_NET_MAX_MTU)) {
1008 netdev->mtu = new_mtu;
1013 * gelic_net_interrupt - event handler for gelic_net
1015 static irqreturn_t gelic_net_interrupt(int irq, void *ptr)
1017 unsigned long flags;
1018 struct net_device *netdev = ptr;
1019 struct gelic_net_card *card = netdev_priv(netdev);
1022 status = card->irq_status;
1027 if (card->rx_dma_restart_required) {
1028 card->rx_dma_restart_required = 0;
1029 gelic_net_enable_rxdmac(card);
1032 if (status & GELIC_NET_RXINT) {
1033 gelic_net_rx_irq_off(card);
1034 netif_rx_schedule(netdev, &card->napi);
1037 if (status & GELIC_NET_TXINT) {
1038 spin_lock_irqsave(&card->tx_dma_lock, flags);
1039 card->tx_dma_progress = 0;
1040 gelic_net_release_tx_chain(card, 0);
1041 /* kick outstanding tx descriptor if any */
1042 gelic_net_kick_txdma(card, card->tx_chain.tail);
1043 spin_unlock_irqrestore(&card->tx_dma_lock, flags);
1048 #ifdef CONFIG_NET_POLL_CONTROLLER
1050 * gelic_net_poll_controller - artificial interrupt for netconsole etc.
1051 * @netdev: interface device structure
1053 * see Documentation/networking/netconsole.txt
1055 static void gelic_net_poll_controller(struct net_device *netdev)
1057 struct gelic_net_card *card = netdev_priv(netdev);
1059 gelic_net_set_irq_mask(card, 0);
1060 gelic_net_interrupt(netdev->irq, netdev);
1061 gelic_net_set_irq_mask(card, card->ghiintmask);
1063 #endif /* CONFIG_NET_POLL_CONTROLLER */
1066 * gelic_net_open_device - open device and map dma region
1067 * @card: card structure
1069 static int gelic_net_open_device(struct gelic_net_card *card)
1073 result = ps3_sb_event_receive_port_setup(card->dev, PS3_BINDING_CPU_ANY,
1074 &card->netdev->irq);
1077 dev_info(ctodev(card),
1078 "%s:%d: gelic_net_open_device failed (%d)\n",
1079 __func__, __LINE__, result);
1081 goto fail_alloc_irq;
1084 result = request_irq(card->netdev->irq, gelic_net_interrupt,
1085 IRQF_DISABLED, card->netdev->name, card->netdev);
1088 dev_info(ctodev(card), "%s:%d: request_irq failed (%d)\n",
1089 __func__, __LINE__, result);
1090 goto fail_request_irq;
1096 ps3_sb_event_receive_port_destroy(card->dev, card->netdev->irq);
1097 card->netdev->irq = NO_IRQ;
1104 * gelic_net_open - called upon ifonfig up
1105 * @netdev: interface device structure
1107 * returns 0 on success, <0 on failure
1109 * gelic_net_open allocates all the descriptors and memory needed for
1110 * operation, sets up multicast list and enables interrupts
1112 static int gelic_net_open(struct net_device *netdev)
1114 struct gelic_net_card *card = netdev_priv(netdev);
1116 dev_dbg(ctodev(card), " -> %s:%d\n", __func__, __LINE__);
1118 gelic_net_open_device(card);
1120 if (gelic_net_init_chain(card, &card->tx_chain,
1121 card->descr, GELIC_NET_TX_DESCRIPTORS))
1122 goto alloc_tx_failed;
1123 if (gelic_net_init_chain(card, &card->rx_chain,
1124 card->descr + GELIC_NET_TX_DESCRIPTORS,
1125 GELIC_NET_RX_DESCRIPTORS))
1126 goto alloc_rx_failed;
1129 card->tx_top = card->tx_chain.head;
1130 card->rx_top = card->rx_chain.head;
1131 dev_dbg(ctodev(card), "descr rx %p, tx %p, size %#lx, num %#x\n",
1132 card->rx_top, card->tx_top, sizeof(struct gelic_net_descr),
1133 GELIC_NET_RX_DESCRIPTORS);
1134 /* allocate rx skbs */
1135 if (gelic_net_alloc_rx_skbs(card))
1136 goto alloc_skbs_failed;
1138 napi_enable(&card->napi);
1140 card->tx_dma_progress = 0;
1141 card->ghiintmask = GELIC_NET_RXINT | GELIC_NET_TXINT;
1143 gelic_net_set_irq_mask(card, card->ghiintmask);
1144 gelic_net_enable_rxdmac(card);
1146 netif_start_queue(netdev);
1147 netif_carrier_on(netdev);
1152 gelic_net_free_chain(card, card->rx_top);
1154 gelic_net_free_chain(card, card->tx_top);
1159 static void gelic_net_get_drvinfo (struct net_device *netdev,
1160 struct ethtool_drvinfo *info)
1162 strncpy(info->driver, DRV_NAME, sizeof(info->driver) - 1);
1163 strncpy(info->version, DRV_VERSION, sizeof(info->version) - 1);
1166 static int gelic_net_get_settings(struct net_device *netdev,
1167 struct ethtool_cmd *cmd)
1169 struct gelic_net_card *card = netdev_priv(netdev);
1174 speed = duplex = -1;
1175 status = lv1_net_control(bus_id(card), dev_id(card),
1176 GELIC_NET_GET_ETH_PORT_STATUS, GELIC_NET_PORT, 0, 0,
1181 if (v1 & GELIC_NET_FULL_DUPLEX) {
1182 duplex = DUPLEX_FULL;
1184 duplex = DUPLEX_HALF;
1187 if (v1 & GELIC_NET_SPEED_10 ) {
1189 } else if (v1 & GELIC_NET_SPEED_100) {
1191 } else if (v1 & GELIC_NET_SPEED_1000) {
1195 cmd->supported = SUPPORTED_TP | SUPPORTED_Autoneg |
1196 SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full |
1197 SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full |
1198 SUPPORTED_1000baseT_Half | SUPPORTED_1000baseT_Full;
1199 cmd->advertising = cmd->supported;
1201 cmd->duplex = duplex;
1202 cmd->autoneg = AUTONEG_ENABLE; /* always enabled */
1203 cmd->port = PORT_TP;
1208 static u32 gelic_net_get_link(struct net_device *netdev)
1210 struct gelic_net_card *card = netdev_priv(netdev);
1215 status = lv1_net_control(bus_id(card), dev_id(card),
1216 GELIC_NET_GET_ETH_PORT_STATUS, GELIC_NET_PORT, 0, 0,
1219 return 0; /* link down */
1221 if (v1 & GELIC_NET_LINK_UP)
1229 static int gelic_net_nway_reset(struct net_device *netdev)
1231 if (netif_running(netdev)) {
1232 gelic_net_stop(netdev);
1233 gelic_net_open(netdev);
1238 static u32 gelic_net_get_tx_csum(struct net_device *netdev)
1240 return (netdev->features & NETIF_F_IP_CSUM) != 0;
1243 static int gelic_net_set_tx_csum(struct net_device *netdev, u32 data)
1246 netdev->features |= NETIF_F_IP_CSUM;
1248 netdev->features &= ~NETIF_F_IP_CSUM;
1253 static u32 gelic_net_get_rx_csum(struct net_device *netdev)
1255 struct gelic_net_card *card = netdev_priv(netdev);
1257 return card->rx_csum;
1260 static int gelic_net_set_rx_csum(struct net_device *netdev, u32 data)
1262 struct gelic_net_card *card = netdev_priv(netdev);
1264 card->rx_csum = data;
1268 static struct ethtool_ops gelic_net_ethtool_ops = {
1269 .get_drvinfo = gelic_net_get_drvinfo,
1270 .get_settings = gelic_net_get_settings,
1271 .get_link = gelic_net_get_link,
1272 .nway_reset = gelic_net_nway_reset,
1273 .get_tx_csum = gelic_net_get_tx_csum,
1274 .set_tx_csum = gelic_net_set_tx_csum,
1275 .get_rx_csum = gelic_net_get_rx_csum,
1276 .set_rx_csum = gelic_net_set_rx_csum,
1280 * gelic_net_tx_timeout_task - task scheduled by the watchdog timeout
1281 * function (to be called not under interrupt status)
1282 * @work: work is context of tx timout task
1284 * called as task when tx hangs, resets interface (if interface is up)
1286 static void gelic_net_tx_timeout_task(struct work_struct *work)
1288 struct gelic_net_card *card =
1289 container_of(work, struct gelic_net_card, tx_timeout_task);
1290 struct net_device *netdev = card->netdev;
1292 dev_info(ctodev(card), "%s:Timed out. Restarting... \n", __func__);
1294 if (!(netdev->flags & IFF_UP))
1297 netif_device_detach(netdev);
1298 gelic_net_stop(netdev);
1300 gelic_net_open(netdev);
1301 netif_device_attach(netdev);
1304 atomic_dec(&card->tx_timeout_task_counter);
1308 * gelic_net_tx_timeout - called when the tx timeout watchdog kicks in.
1309 * @netdev: interface device structure
1311 * called, if tx hangs. Schedules a task that resets the interface
1313 static void gelic_net_tx_timeout(struct net_device *netdev)
1315 struct gelic_net_card *card;
1317 card = netdev_priv(netdev);
1318 atomic_inc(&card->tx_timeout_task_counter);
1319 if (netdev->flags & IFF_UP)
1320 schedule_work(&card->tx_timeout_task);
1322 atomic_dec(&card->tx_timeout_task_counter);
1326 * gelic_net_setup_netdev_ops - initialization of net_device operations
1327 * @netdev: net_device structure
1329 * fills out function pointers in the net_device structure
1331 static void gelic_net_setup_netdev_ops(struct net_device *netdev)
1333 netdev->open = &gelic_net_open;
1334 netdev->stop = &gelic_net_stop;
1335 netdev->hard_start_xmit = &gelic_net_xmit;
1336 netdev->set_multicast_list = &gelic_net_set_multi;
1337 netdev->change_mtu = &gelic_net_change_mtu;
1339 netdev->tx_timeout = &gelic_net_tx_timeout;
1340 netdev->watchdog_timeo = GELIC_NET_WATCHDOG_TIMEOUT;
1341 netdev->ethtool_ops = &gelic_net_ethtool_ops;
1345 * gelic_net_setup_netdev - initialization of net_device
1346 * @card: card structure
1348 * Returns 0 on success or <0 on failure
1350 * gelic_net_setup_netdev initializes the net_device structure
1352 static int gelic_net_setup_netdev(struct gelic_net_card *card)
1354 struct net_device *netdev = card->netdev;
1355 struct sockaddr addr;
1360 SET_MODULE_OWNER(netdev);
1361 SET_NETDEV_DEV(netdev, &card->dev->core);
1362 spin_lock_init(&card->tx_dma_lock);
1364 card->rx_csum = GELIC_NET_RX_CSUM_DEFAULT;
1366 gelic_net_setup_netdev_ops(netdev);
1368 netif_napi_add(netdev, &card->napi,
1369 gelic_net_poll, GELIC_NET_NAPI_WEIGHT);
1371 netdev->features = NETIF_F_IP_CSUM;
1373 status = lv1_net_control(bus_id(card), dev_id(card),
1374 GELIC_NET_GET_MAC_ADDRESS,
1376 if (status || !is_valid_ether_addr((u8 *)&v1)) {
1377 dev_info(ctodev(card),
1378 "%s:lv1_net_control GET_MAC_ADDR failed %d\n",
1383 memcpy(addr.sa_data, &v1, ETH_ALEN);
1384 memcpy(netdev->dev_addr, addr.sa_data, ETH_ALEN);
1385 dev_info(ctodev(card), "MAC addr %02x:%02x:%02x:%02x:%02x:%02x\n",
1386 netdev->dev_addr[0], netdev->dev_addr[1],
1387 netdev->dev_addr[2], netdev->dev_addr[3],
1388 netdev->dev_addr[4], netdev->dev_addr[5]);
1390 card->vlan_index = -1; /* no vlan */
1391 for (i = 0; i < GELIC_NET_VLAN_MAX; i++) {
1392 status = lv1_net_control(bus_id(card), dev_id(card),
1393 GELIC_NET_GET_VLAN_ID,
1394 i + 1, /* index; one based */
1396 if (status == GELIC_NET_VLAN_NO_ENTRY) {
1397 dev_dbg(ctodev(card),
1398 "GELIC_VLAN_ID no entry:%d, VLAN disabled\n",
1400 card->vlan_id[i] = 0;
1401 } else if (status) {
1402 dev_dbg(ctodev(card),
1403 "%s:GELIC_NET_VLAN_ID faild, status=%d\n",
1405 card->vlan_id[i] = 0;
1407 card->vlan_id[i] = (u32)v1;
1408 dev_dbg(ctodev(card), "vlan_id:%d, %lx\n", i, v1);
1412 if (card->vlan_id[GELIC_NET_VLAN_WIRED - 1]) {
1413 card->vlan_index = GELIC_NET_VLAN_WIRED - 1;
1414 netdev->hard_header_len += VLAN_HLEN;
1417 status = register_netdev(netdev);
1419 dev_err(ctodev(card), "%s:Couldn't register net_device: %d\n",
1428 * gelic_net_alloc_card - allocates net_device and card structure
1430 * returns the card structure or NULL in case of errors
1432 * the card and net_device structures are linked to each other
1434 static struct gelic_net_card *gelic_net_alloc_card(void)
1436 struct net_device *netdev;
1437 struct gelic_net_card *card;
1440 alloc_size = sizeof (*card) +
1441 sizeof (struct gelic_net_descr) * GELIC_NET_RX_DESCRIPTORS +
1442 sizeof (struct gelic_net_descr) * GELIC_NET_TX_DESCRIPTORS;
1444 * we assume private data is allocated 32 bytes (or more) aligned
1445 * so that gelic_net_descr should be 32 bytes aligned.
1446 * Current alloc_etherdev() does do it because NETDEV_ALIGN
1448 * check this assumption here.
1450 BUILD_BUG_ON(NETDEV_ALIGN < 32);
1451 BUILD_BUG_ON(offsetof(struct gelic_net_card, irq_status) % 8);
1452 BUILD_BUG_ON(offsetof(struct gelic_net_card, descr) % 32);
1454 netdev = alloc_etherdev(alloc_size);
1458 card = netdev_priv(netdev);
1459 card->netdev = netdev;
1460 INIT_WORK(&card->tx_timeout_task, gelic_net_tx_timeout_task);
1461 init_waitqueue_head(&card->waitq);
1462 atomic_set(&card->tx_timeout_task_counter, 0);
1468 * ps3_gelic_driver_probe - add a device to the control of this driver
1470 static int ps3_gelic_driver_probe (struct ps3_system_bus_device *dev)
1472 struct gelic_net_card *card = gelic_net_alloc_card();
1476 dev_info(&dev->core, "gelic_net_alloc_card failed\n");
1478 goto fail_alloc_card;
1481 ps3_system_bus_set_driver_data(dev, card);
1484 result = ps3_open_hv_device(dev);
1487 dev_dbg(&dev->core, "ps3_open_hv_device failed\n");
1491 result = ps3_dma_region_create(dev->d_region);
1494 dev_dbg(&dev->core, "ps3_dma_region_create failed(%d)\n",
1496 BUG_ON("check region type");
1497 goto fail_dma_region;
1500 result = lv1_net_set_interrupt_status_indicator(bus_id(card),
1502 ps3_mm_phys_to_lpar(__pa(&card->irq_status)),
1507 "lv1_net_set_interrupt_status_indicator failed: %s\n",
1508 ps3_result(result));
1510 goto fail_status_indicator;
1513 result = gelic_net_setup_netdev(card);
1516 dev_dbg(&dev->core, "%s:%d: ps3_dma_region_create failed: "
1517 "(%d)\n", __func__, __LINE__, result);
1518 goto fail_setup_netdev;
1524 lv1_net_set_interrupt_status_indicator(bus_id(card),
1527 fail_status_indicator:
1528 ps3_dma_region_free(dev->d_region);
1530 ps3_close_hv_device(dev);
1532 ps3_system_bus_set_driver_data(dev, NULL);
1533 free_netdev(card->netdev);
1539 * ps3_gelic_driver_remove - remove a device from the control of this driver
1542 static int ps3_gelic_driver_remove (struct ps3_system_bus_device *dev)
1544 struct gelic_net_card *card = ps3_system_bus_get_driver_data(dev);
1546 wait_event(card->waitq,
1547 atomic_read(&card->tx_timeout_task_counter) == 0);
1549 lv1_net_set_interrupt_status_indicator(bus_id(card), dev_id(card),
1552 unregister_netdev(card->netdev);
1553 free_netdev(card->netdev);
1555 ps3_system_bus_set_driver_data(dev, NULL);
1557 ps3_dma_region_free(dev->d_region);
1559 ps3_close_hv_device(dev);
1564 static struct ps3_system_bus_driver ps3_gelic_driver = {
1565 .match_id = PS3_MATCH_ID_GELIC,
1566 .probe = ps3_gelic_driver_probe,
1567 .remove = ps3_gelic_driver_remove,
1568 .shutdown = ps3_gelic_driver_remove,
1569 .core.name = "ps3_gelic_driver",
1570 .core.owner = THIS_MODULE,
1573 static int __init ps3_gelic_driver_init (void)
1575 return firmware_has_feature(FW_FEATURE_PS3_LV1)
1576 ? ps3_system_bus_driver_register(&ps3_gelic_driver)
1580 static void __exit ps3_gelic_driver_exit (void)
1582 ps3_system_bus_driver_unregister(&ps3_gelic_driver);
1585 module_init (ps3_gelic_driver_init);
1586 module_exit (ps3_gelic_driver_exit);
1588 MODULE_ALIAS(PS3_MODULE_ALIAS_GELIC);