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)
357 if (descr->data_status & (1 << GELIC_NET_TXDESC_TAIL)) {
360 dma_unmap_single(ctodev(card), descr->buf_addr, skb->len,
362 dev_kfree_skb_any(skb);
364 dma_unmap_single(ctodev(card), descr->buf_addr,
365 descr->buf_size, DMA_TO_DEVICE);
370 descr->next_descr_addr = 0;
371 descr->result_size = 0;
372 descr->valid_size = 0;
373 descr->data_status = 0;
374 descr->data_error = 0;
377 /* set descr status */
378 gelic_net_set_descr_status(descr, GELIC_NET_DESCR_NOT_IN_USE);
382 * gelic_net_release_tx_chain - processes sent tx descriptors
383 * @card: adapter structure
384 * @stop: net_stop sequence
386 * releases the tx descriptors that gelic has finished with
388 static void gelic_net_release_tx_chain(struct gelic_net_card *card, int stop)
390 struct gelic_net_descr_chain *tx_chain;
391 enum gelic_net_descr_status status;
394 for (tx_chain = &card->tx_chain;
395 tx_chain->head != tx_chain->tail && tx_chain->tail;
396 tx_chain->tail = tx_chain->tail->next) {
397 status = gelic_net_get_descr_status(tx_chain->tail);
399 case GELIC_NET_DESCR_RESPONSE_ERROR:
400 case GELIC_NET_DESCR_PROTECTION_ERROR:
401 case GELIC_NET_DESCR_FORCE_END:
402 if (printk_ratelimit())
403 dev_info(ctodev(card),
404 "%s: forcing end of tx descriptor " \
407 card->netdev->stats.tx_dropped++;
410 case GELIC_NET_DESCR_COMPLETE:
411 if (tx_chain->tail->skb) {
412 card->netdev->stats.tx_packets++;
413 card->netdev->stats.tx_bytes +=
414 tx_chain->tail->skb->len;
418 case GELIC_NET_DESCR_CARDOWNED:
419 /* pending tx request */
421 /* any other value (== GELIC_NET_DESCR_NOT_IN_USE) */
425 gelic_net_release_tx_descr(card, tx_chain->tail);
429 if (!stop && (2 < release))
430 netif_wake_queue(card->netdev);
434 * gelic_net_set_multi - sets multicast addresses and promisc flags
435 * @netdev: interface device structure
437 * gelic_net_set_multi configures multicast addresses as needed for the
438 * netdev interface. It also sets up multicast, allmulti and promisc
439 * flags appropriately
441 static void gelic_net_set_multi(struct net_device *netdev)
443 struct gelic_net_card *card = netdev_priv(netdev);
444 struct dev_mc_list *mc;
450 /* clear all multicast address */
451 status = lv1_net_remove_multicast_address(bus_id(card), dev_id(card),
454 dev_err(ctodev(card),
455 "lv1_net_remove_multicast_address failed %d\n",
457 /* set broadcast address */
458 status = lv1_net_add_multicast_address(bus_id(card), dev_id(card),
459 GELIC_NET_BROADCAST_ADDR, 0);
461 dev_err(ctodev(card),
462 "lv1_net_add_multicast_address failed, %d\n",
465 if (netdev->flags & IFF_ALLMULTI
466 || netdev->mc_count > GELIC_NET_MC_COUNT_MAX) { /* list max */
467 status = lv1_net_add_multicast_address(bus_id(card),
471 dev_err(ctodev(card),
472 "lv1_net_add_multicast_address failed, %d\n",
477 /* set multicast address */
478 for (mc = netdev->mc_list; mc; mc = mc->next) {
481 for (i = 0; i < ETH_ALEN; i++) {
485 status = lv1_net_add_multicast_address(bus_id(card),
489 dev_err(ctodev(card),
490 "lv1_net_add_multicast_address failed, %d\n",
496 * gelic_net_enable_rxdmac - enables the receive DMA controller
497 * @card: card structure
499 * gelic_net_enable_rxdmac enables the DMA controller by setting RX_DMA_EN
500 * in the GDADMACCNTR register
502 static inline void gelic_net_enable_rxdmac(struct gelic_net_card *card)
506 status = lv1_net_start_rx_dma(bus_id(card), dev_id(card),
507 card->rx_chain.tail->bus_addr, 0);
509 dev_info(ctodev(card),
510 "lv1_net_start_rx_dma failed, status=%d\n", status);
514 * gelic_net_disable_rxdmac - disables the receive DMA controller
515 * @card: card structure
517 * gelic_net_disable_rxdmac terminates processing on the DMA controller by
518 * turing off DMA and issueing a force end
520 static inline void gelic_net_disable_rxdmac(struct gelic_net_card *card)
524 /* this hvc blocks until the DMA in progress really stopped */
525 status = lv1_net_stop_rx_dma(bus_id(card), dev_id(card), 0);
527 dev_err(ctodev(card),
528 "lv1_net_stop_rx_dma faild, %d\n", status);
532 * gelic_net_disable_txdmac - disables the transmit DMA controller
533 * @card: card structure
535 * gelic_net_disable_txdmac terminates processing on the DMA controller by
536 * turing off DMA and issueing a force end
538 static inline void gelic_net_disable_txdmac(struct gelic_net_card *card)
542 /* this hvc blocks until the DMA in progress really stopped */
543 status = lv1_net_stop_tx_dma(bus_id(card), dev_id(card), 0);
545 dev_err(ctodev(card),
546 "lv1_net_stop_tx_dma faild, status=%d\n", status);
550 * gelic_net_stop - called upon ifconfig down
551 * @netdev: interface device structure
555 static int gelic_net_stop(struct net_device *netdev)
557 struct gelic_net_card *card = netdev_priv(netdev);
559 netif_poll_disable(netdev);
560 netif_stop_queue(netdev);
562 /* turn off DMA, force end */
563 gelic_net_disable_rxdmac(card);
564 gelic_net_disable_txdmac(card);
566 gelic_net_set_irq_mask(card, 0);
568 /* disconnect event port */
569 free_irq(card->netdev->irq, card->netdev);
570 ps3_sb_event_receive_port_destroy(card->dev, card->netdev->irq);
571 card->netdev->irq = NO_IRQ;
573 netif_carrier_off(netdev);
576 gelic_net_release_tx_chain(card, 1);
577 gelic_net_release_rx_chain(card);
579 gelic_net_free_chain(card, card->tx_top);
580 gelic_net_free_chain(card, card->rx_top);
586 * gelic_net_get_next_tx_descr - returns the next available tx descriptor
587 * @card: device structure to get descriptor from
589 * returns the address of the next descriptor, or NULL if not available.
591 static struct gelic_net_descr *
592 gelic_net_get_next_tx_descr(struct gelic_net_card *card)
594 if (!card->tx_chain.head)
596 /* see if we can two consecutive free descrs */
597 if (card->tx_chain.tail != card->tx_chain.head->next &&
598 gelic_net_get_descr_status(card->tx_chain.head) ==
599 GELIC_NET_DESCR_NOT_IN_USE &&
600 card->tx_chain.tail != card->tx_chain.head->next->next &&
601 gelic_net_get_descr_status(card->tx_chain.head->next) ==
602 GELIC_NET_DESCR_NOT_IN_USE )
603 return card->tx_chain.head;
610 * gelic_net_set_txdescr_cmdstat - sets the tx descriptor command field
611 * @descr: descriptor structure to fill out
612 * @skb: packet to consider
613 * @middle: middle of frame
615 * fills out the command and status field of the descriptor structure,
616 * depending on hardware checksum settings. This function assumes a wmb()
617 * has executed before.
619 static void gelic_net_set_txdescr_cmdstat(struct gelic_net_descr *descr,
620 struct sk_buff *skb, int middle)
627 eofr = GELIC_NET_DMAC_CMDSTAT_END_FRAME;
629 if (skb->ip_summed != CHECKSUM_PARTIAL)
630 descr->dmac_cmd_status = GELIC_NET_DMAC_CMDSTAT_NOCS | eofr;
633 * if yes: tcp? udp? */
634 if (skb->protocol == htons(ETH_P_IP)) {
635 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
636 descr->dmac_cmd_status =
637 GELIC_NET_DMAC_CMDSTAT_TCPCS | eofr;
638 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
639 descr->dmac_cmd_status =
640 GELIC_NET_DMAC_CMDSTAT_UDPCS | eofr;
642 * the stack should checksum non-tcp and non-udp
643 * packets on his own: NETIF_F_IP_CSUM
645 descr->dmac_cmd_status =
646 GELIC_NET_DMAC_CMDSTAT_NOCS | eofr;
652 * gelic_net_prepare_tx_descr_v - get dma address of skb_data
653 * @card: card structure
654 * @descr: descriptor structure
655 * @skb: packet to use
657 * returns 0 on success, <0 on failure.
660 static int gelic_net_prepare_tx_descr_v(struct gelic_net_card *card,
661 struct gelic_net_descr *descr,
665 unsigned int vlan_len;
666 struct gelic_net_descr *sec_descr = descr->next;
668 if (skb->len < GELIC_NET_VLAN_POS)
671 vlan_len = GELIC_NET_VLAN_POS;
672 memcpy(&descr->vlan, skb->data, vlan_len);
673 if (card->vlan_index != -1) {
674 /* internal vlan tag used */
675 descr->vlan.h_vlan_proto = htons(ETH_P_8021Q); /* vlan 0x8100*/
676 descr->vlan.h_vlan_TCI = htons(card->vlan_id[card->vlan_index]);
677 vlan_len += VLAN_HLEN; /* added for above two lines */
681 buf[0] = dma_map_single(ctodev(card), &descr->vlan,
682 vlan_len, DMA_TO_DEVICE);
685 dev_err(ctodev(card),
686 "dma map 1 failed (%p, %i). Dropping packet\n",
687 skb->data, vlan_len);
691 buf[1] = dma_map_single(ctodev(card), skb->data + GELIC_NET_VLAN_POS,
692 skb->len - GELIC_NET_VLAN_POS,
696 dev_err(ctodev(card),
697 "dma map 2 failed (%p, %i). Dropping packet\n",
698 skb->data + GELIC_NET_VLAN_POS,
699 skb->len - GELIC_NET_VLAN_POS);
700 dma_unmap_single(ctodev(card), buf[0], vlan_len,
706 descr->buf_addr = buf[0];
707 descr->buf_size = vlan_len;
708 descr->skb = NULL; /* not used */
709 descr->data_status = 0;
710 descr->next_descr_addr = descr->next->bus_addr;
711 gelic_net_set_txdescr_cmdstat(descr, skb, 1); /* not the frame end */
714 sec_descr->buf_addr = buf[1];
715 sec_descr->buf_size = skb->len - GELIC_NET_VLAN_POS;
716 sec_descr->skb = skb;
717 sec_descr->data_status = 0;
718 sec_descr->next_descr_addr = 0; /* terminate hw descr */
719 gelic_net_set_txdescr_cmdstat(sec_descr, skb, 0);
721 /* bump free descriptor pointer */
722 card->tx_chain.head = sec_descr->next;
727 * gelic_net_kick_txdma - enables TX DMA processing
728 * @card: card structure
729 * @descr: descriptor address to enable TX processing at
732 static int gelic_net_kick_txdma(struct gelic_net_card *card,
733 struct gelic_net_descr *descr)
738 if (card->tx_dma_progress)
741 if (gelic_net_get_descr_status(descr) == GELIC_NET_DESCR_CARDOWNED) {
742 card->tx_dma_progress = 1;
743 /* sometimes we need retry here */
745 status = lv1_net_start_tx_dma(bus_id(card),
752 dev_info(ctodev(card), "lv1_net_start_txdma failed," \
754 status, card->irq_status);
760 * gelic_net_xmit - transmits a frame over the device
761 * @skb: packet to send out
762 * @netdev: interface device structure
764 * returns 0 on success, <0 on failure
766 static int gelic_net_xmit(struct sk_buff *skb, struct net_device *netdev)
768 struct gelic_net_card *card = netdev_priv(netdev);
769 struct gelic_net_descr *descr;
773 spin_lock_irqsave(&card->tx_dma_lock, flags);
775 gelic_net_release_tx_chain(card, 0);
777 descr = gelic_net_get_next_tx_descr(card);
780 * no more descriptors free
782 netif_stop_queue(netdev);
783 spin_unlock_irqrestore(&card->tx_dma_lock, flags);
784 return NETDEV_TX_BUSY;
787 result = gelic_net_prepare_tx_descr_v(card, descr, skb);
790 * DMA map failed. As chanses are that failure
791 * would continue, just release skb and return
793 card->netdev->stats.tx_dropped++;
794 dev_kfree_skb_any(skb);
795 spin_unlock_irqrestore(&card->tx_dma_lock, flags);
799 * link this prepared descriptor to previous one
800 * to achieve high performance
802 descr->prev->next_descr_addr = descr->bus_addr;
804 * as hardware descriptor is modified in the above lines,
805 * ensure that the hardware sees it
808 if (gelic_net_kick_txdma(card, descr)) {
811 * release descriptors which were just prepared
813 card->netdev->stats.tx_dropped++;
814 gelic_net_release_tx_descr(card, descr);
815 gelic_net_release_tx_descr(card, descr->next);
816 card->tx_chain.tail = descr->next->next;
817 dev_info(ctodev(card), "%s: kick failure\n", __func__);
819 /* OK, DMA started/reserved */
820 netdev->trans_start = jiffies;
823 spin_unlock_irqrestore(&card->tx_dma_lock, flags);
828 * gelic_net_pass_skb_up - takes an skb from a descriptor and passes it on
829 * @descr: descriptor to process
830 * @card: card structure
832 * iommu-unmaps the skb, fills out skb structure and passes the data to the
833 * stack. The descriptor state is not changed.
835 static void gelic_net_pass_skb_up(struct gelic_net_descr *descr,
836 struct gelic_net_card *card)
839 struct net_device *netdev;
840 u32 data_status, data_error;
842 data_status = descr->data_status;
843 data_error = descr->data_error;
844 netdev = card->netdev;
845 /* unmap skb buffer */
847 dma_unmap_single(ctodev(card), descr->buf_addr, GELIC_NET_MAX_MTU,
850 skb_put(skb, descr->valid_size? descr->valid_size : descr->result_size);
851 if (!descr->valid_size)
852 dev_info(ctodev(card), "buffer full %x %x %x\n",
853 descr->result_size, descr->buf_size,
854 descr->dmac_cmd_status);
858 * the card put 2 bytes vlan tag in front
859 * of the ethernet frame
862 skb->protocol = eth_type_trans(skb, netdev);
864 /* checksum offload */
866 if ((data_status & GELIC_NET_DATA_STATUS_CHK_MASK) &&
867 (!(data_error & GELIC_NET_DATA_ERROR_CHK_MASK)))
868 skb->ip_summed = CHECKSUM_UNNECESSARY;
870 skb->ip_summed = CHECKSUM_NONE;
872 skb->ip_summed = CHECKSUM_NONE;
874 /* update netdevice statistics */
875 card->netdev->stats.rx_packets++;
876 card->netdev->stats.rx_bytes += skb->len;
878 /* pass skb up to stack */
879 netif_receive_skb(skb);
883 * gelic_net_decode_one_descr - processes an rx descriptor
884 * @card: card structure
886 * returns 1 if a packet has been sent to the stack, otherwise 0
888 * processes an rx descriptor by iommu-unmapping the data buffer and passing
889 * the packet up to the stack
891 static int gelic_net_decode_one_descr(struct gelic_net_card *card)
893 enum gelic_net_descr_status status;
894 struct gelic_net_descr_chain *chain = &card->rx_chain;
895 struct gelic_net_descr *descr = chain->tail;
896 int dmac_chain_ended;
898 status = gelic_net_get_descr_status(descr);
899 /* is this descriptor terminated with next_descr == NULL? */
901 descr->dmac_cmd_status & GELIC_NET_DMAC_CMDSTAT_RXDCEIS;
903 if (status == GELIC_NET_DESCR_CARDOWNED)
906 if (status == GELIC_NET_DESCR_NOT_IN_USE) {
907 dev_dbg(ctodev(card), "dormant descr? %p\n", descr);
911 if ((status == GELIC_NET_DESCR_RESPONSE_ERROR) ||
912 (status == GELIC_NET_DESCR_PROTECTION_ERROR) ||
913 (status == GELIC_NET_DESCR_FORCE_END)) {
914 dev_info(ctodev(card), "dropping RX descriptor with state %x\n",
916 card->netdev->stats.rx_dropped++;
920 if (status == GELIC_NET_DESCR_BUFFER_FULL) {
922 * Buffer full would occur if and only if
923 * the frame length was longer than the size of this
924 * descriptor's buffer. If the frame length was equal
925 * to or shorter than buffer'size, FRAME_END condition
927 * Anyway this frame was longer than the MTU,
930 dev_info(ctodev(card), "overlength frame\n");
934 * descriptoers any other than FRAME_END here should
935 * be treated as error.
937 if (status != GELIC_NET_DESCR_FRAME_END) {
938 dev_dbg(ctodev(card), "RX descriptor with state %x\n",
943 /* ok, we've got a packet in descr */
944 gelic_net_pass_skb_up(descr, card);
947 * So that always DMAC can see the end
948 * of the descriptor chain to avoid
949 * from unwanted DMAC overrun.
951 descr->next_descr_addr = 0;
953 /* change the descriptor state: */
954 gelic_net_set_descr_status(descr, GELIC_NET_DESCR_NOT_IN_USE);
957 * this call can fail, but for now, just leave this
958 * decriptor without skb
960 gelic_net_prepare_rx_descr(card, descr);
963 chain->tail = descr->next;
966 * Set this descriptor the end of the chain.
968 descr->prev->next_descr_addr = descr->bus_addr;
971 * If dmac chain was met, DMAC stopped.
974 if (dmac_chain_ended) {
975 card->rx_dma_restart_required = 1;
976 dev_dbg(ctodev(card), "reenable rx dma scheduled\n");
983 * gelic_net_poll - NAPI poll function called by the stack to return packets
984 * @netdev: interface device structure
985 * @budget: number of packets we can pass to the stack at most
987 * returns 0 if no more packets available to the driver/stack. Returns 1,
988 * if the quota is exceeded, but the driver has still packets.
991 static int gelic_net_poll(struct net_device *netdev, int *budget)
993 struct gelic_net_card *card = netdev_priv(netdev);
994 int packets_to_do, packets_done = 0;
995 int no_more_packets = 0;
997 packets_to_do = min(*budget, netdev->quota);
999 while (packets_to_do) {
1000 if (gelic_net_decode_one_descr(card)) {
1004 /* no more packets for the stack */
1005 no_more_packets = 1;
1009 netdev->quota -= packets_done;
1010 *budget -= packets_done;
1011 if (no_more_packets) {
1012 netif_rx_complete(netdev);
1013 gelic_net_rx_irq_on(card);
1019 * gelic_net_change_mtu - changes the MTU of an interface
1020 * @netdev: interface device structure
1021 * @new_mtu: new MTU value
1023 * returns 0 on success, <0 on failure
1025 static int gelic_net_change_mtu(struct net_device *netdev, int new_mtu)
1027 /* no need to re-alloc skbs or so -- the max mtu is about 2.3k
1028 * and mtu is outbound only anyway */
1029 if ((new_mtu < GELIC_NET_MIN_MTU) ||
1030 (new_mtu > GELIC_NET_MAX_MTU)) {
1033 netdev->mtu = new_mtu;
1038 * gelic_net_interrupt - event handler for gelic_net
1040 static irqreturn_t gelic_net_interrupt(int irq, void *ptr)
1042 unsigned long flags;
1043 struct net_device *netdev = ptr;
1044 struct gelic_net_card *card = netdev_priv(netdev);
1047 status = card->irq_status;
1052 if (card->rx_dma_restart_required) {
1053 card->rx_dma_restart_required = 0;
1054 gelic_net_enable_rxdmac(card);
1057 if (status & GELIC_NET_RXINT) {
1058 gelic_net_rx_irq_off(card);
1059 netif_rx_schedule(netdev);
1062 if (status & GELIC_NET_TXINT) {
1063 spin_lock_irqsave(&card->tx_dma_lock, flags);
1064 card->tx_dma_progress = 0;
1065 gelic_net_release_tx_chain(card, 0);
1066 /* kick outstanding tx descriptor if any */
1067 gelic_net_kick_txdma(card, card->tx_chain.tail);
1068 spin_unlock_irqrestore(&card->tx_dma_lock, flags);
1073 #ifdef CONFIG_NET_POLL_CONTROLLER
1075 * gelic_net_poll_controller - artificial interrupt for netconsole etc.
1076 * @netdev: interface device structure
1078 * see Documentation/networking/netconsole.txt
1080 static void gelic_net_poll_controller(struct net_device *netdev)
1082 struct gelic_net_card *card = netdev_priv(netdev);
1084 gelic_net_set_irq_mask(card, 0);
1085 gelic_net_interrupt(netdev->irq, netdev);
1086 gelic_net_set_irq_mask(card, card->ghiintmask);
1088 #endif /* CONFIG_NET_POLL_CONTROLLER */
1091 * gelic_net_open_device - open device and map dma region
1092 * @card: card structure
1094 static int gelic_net_open_device(struct gelic_net_card *card)
1098 result = ps3_sb_event_receive_port_setup(card->dev, PS3_BINDING_CPU_ANY,
1099 &card->netdev->irq);
1102 dev_info(ctodev(card),
1103 "%s:%d: gelic_net_open_device failed (%d)\n",
1104 __func__, __LINE__, result);
1106 goto fail_alloc_irq;
1109 result = request_irq(card->netdev->irq, gelic_net_interrupt,
1110 IRQF_DISABLED, card->netdev->name, card->netdev);
1113 dev_info(ctodev(card), "%s:%d: request_irq failed (%d)\n",
1114 __func__, __LINE__, result);
1115 goto fail_request_irq;
1121 ps3_sb_event_receive_port_destroy(card->dev, card->netdev->irq);
1122 card->netdev->irq = NO_IRQ;
1129 * gelic_net_open - called upon ifonfig up
1130 * @netdev: interface device structure
1132 * returns 0 on success, <0 on failure
1134 * gelic_net_open allocates all the descriptors and memory needed for
1135 * operation, sets up multicast list and enables interrupts
1137 static int gelic_net_open(struct net_device *netdev)
1139 struct gelic_net_card *card = netdev_priv(netdev);
1141 dev_dbg(ctodev(card), " -> %s:%d\n", __func__, __LINE__);
1143 gelic_net_open_device(card);
1145 if (gelic_net_init_chain(card, &card->tx_chain,
1146 card->descr, GELIC_NET_TX_DESCRIPTORS))
1147 goto alloc_tx_failed;
1148 if (gelic_net_init_chain(card, &card->rx_chain,
1149 card->descr + GELIC_NET_TX_DESCRIPTORS,
1150 GELIC_NET_RX_DESCRIPTORS))
1151 goto alloc_rx_failed;
1154 card->tx_top = card->tx_chain.head;
1155 card->rx_top = card->rx_chain.head;
1156 dev_dbg(ctodev(card), "descr rx %p, tx %p, size %#lx, num %#x\n",
1157 card->rx_top, card->tx_top, sizeof(struct gelic_net_descr),
1158 GELIC_NET_RX_DESCRIPTORS);
1159 /* allocate rx skbs */
1160 if (gelic_net_alloc_rx_skbs(card))
1161 goto alloc_skbs_failed;
1163 card->tx_dma_progress = 0;
1164 card->ghiintmask = GELIC_NET_RXINT | GELIC_NET_TXINT;
1166 gelic_net_set_irq_mask(card, card->ghiintmask);
1167 gelic_net_enable_rxdmac(card);
1169 netif_start_queue(netdev);
1170 netif_carrier_on(netdev);
1175 gelic_net_free_chain(card, card->rx_top);
1177 gelic_net_free_chain(card, card->tx_top);
1182 static void gelic_net_get_drvinfo (struct net_device *netdev,
1183 struct ethtool_drvinfo *info)
1185 strncpy(info->driver, DRV_NAME, sizeof(info->driver) - 1);
1186 strncpy(info->version, DRV_VERSION, sizeof(info->version) - 1);
1189 static int gelic_net_get_settings(struct net_device *netdev,
1190 struct ethtool_cmd *cmd)
1192 struct gelic_net_card *card = netdev_priv(netdev);
1197 speed = duplex = -1;
1198 status = lv1_net_control(bus_id(card), dev_id(card),
1199 GELIC_NET_GET_ETH_PORT_STATUS, GELIC_NET_PORT, 0, 0,
1204 if (v1 & GELIC_NET_FULL_DUPLEX) {
1205 duplex = DUPLEX_FULL;
1207 duplex = DUPLEX_HALF;
1210 if (v1 & GELIC_NET_SPEED_10 ) {
1212 } else if (v1 & GELIC_NET_SPEED_100) {
1214 } else if (v1 & GELIC_NET_SPEED_1000) {
1218 cmd->supported = SUPPORTED_TP | SUPPORTED_Autoneg |
1219 SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full |
1220 SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full |
1221 SUPPORTED_1000baseT_Half | SUPPORTED_1000baseT_Full;
1222 cmd->advertising = cmd->supported;
1224 cmd->duplex = duplex;
1225 cmd->autoneg = AUTONEG_ENABLE; /* always enabled */
1226 cmd->port = PORT_TP;
1231 static u32 gelic_net_get_link(struct net_device *netdev)
1233 struct gelic_net_card *card = netdev_priv(netdev);
1238 status = lv1_net_control(bus_id(card), dev_id(card),
1239 GELIC_NET_GET_ETH_PORT_STATUS, GELIC_NET_PORT, 0, 0,
1242 return 0; /* link down */
1244 if (v1 & GELIC_NET_LINK_UP)
1252 static int gelic_net_nway_reset(struct net_device *netdev)
1254 if (netif_running(netdev)) {
1255 gelic_net_stop(netdev);
1256 gelic_net_open(netdev);
1261 static u32 gelic_net_get_tx_csum(struct net_device *netdev)
1263 return (netdev->features & NETIF_F_IP_CSUM) != 0;
1266 static int gelic_net_set_tx_csum(struct net_device *netdev, u32 data)
1269 netdev->features |= NETIF_F_IP_CSUM;
1271 netdev->features &= ~NETIF_F_IP_CSUM;
1276 static u32 gelic_net_get_rx_csum(struct net_device *netdev)
1278 struct gelic_net_card *card = netdev_priv(netdev);
1280 return card->rx_csum;
1283 static int gelic_net_set_rx_csum(struct net_device *netdev, u32 data)
1285 struct gelic_net_card *card = netdev_priv(netdev);
1287 card->rx_csum = data;
1291 static struct ethtool_ops gelic_net_ethtool_ops = {
1292 .get_drvinfo = gelic_net_get_drvinfo,
1293 .get_settings = gelic_net_get_settings,
1294 .get_link = gelic_net_get_link,
1295 .nway_reset = gelic_net_nway_reset,
1296 .get_tx_csum = gelic_net_get_tx_csum,
1297 .set_tx_csum = gelic_net_set_tx_csum,
1298 .get_rx_csum = gelic_net_get_rx_csum,
1299 .set_rx_csum = gelic_net_set_rx_csum,
1303 * gelic_net_tx_timeout_task - task scheduled by the watchdog timeout
1304 * function (to be called not under interrupt status)
1305 * @work: work is context of tx timout task
1307 * called as task when tx hangs, resets interface (if interface is up)
1309 static void gelic_net_tx_timeout_task(struct work_struct *work)
1311 struct gelic_net_card *card =
1312 container_of(work, struct gelic_net_card, tx_timeout_task);
1313 struct net_device *netdev = card->netdev;
1315 dev_info(ctodev(card), "%s:Timed out. Restarting... \n", __func__);
1317 if (!(netdev->flags & IFF_UP))
1320 netif_device_detach(netdev);
1321 gelic_net_stop(netdev);
1323 gelic_net_open(netdev);
1324 netif_device_attach(netdev);
1327 atomic_dec(&card->tx_timeout_task_counter);
1331 * gelic_net_tx_timeout - called when the tx timeout watchdog kicks in.
1332 * @netdev: interface device structure
1334 * called, if tx hangs. Schedules a task that resets the interface
1336 static void gelic_net_tx_timeout(struct net_device *netdev)
1338 struct gelic_net_card *card;
1340 card = netdev_priv(netdev);
1341 atomic_inc(&card->tx_timeout_task_counter);
1342 if (netdev->flags & IFF_UP)
1343 schedule_work(&card->tx_timeout_task);
1345 atomic_dec(&card->tx_timeout_task_counter);
1349 * gelic_net_setup_netdev_ops - initialization of net_device operations
1350 * @netdev: net_device structure
1352 * fills out function pointers in the net_device structure
1354 static void gelic_net_setup_netdev_ops(struct net_device *netdev)
1356 netdev->open = &gelic_net_open;
1357 netdev->stop = &gelic_net_stop;
1358 netdev->hard_start_xmit = &gelic_net_xmit;
1359 netdev->set_multicast_list = &gelic_net_set_multi;
1360 netdev->change_mtu = &gelic_net_change_mtu;
1362 netdev->tx_timeout = &gelic_net_tx_timeout;
1363 netdev->watchdog_timeo = GELIC_NET_WATCHDOG_TIMEOUT;
1365 netdev->poll = &gelic_net_poll;
1366 netdev->weight = GELIC_NET_NAPI_WEIGHT;
1367 netdev->ethtool_ops = &gelic_net_ethtool_ops;
1371 * gelic_net_setup_netdev - initialization of net_device
1372 * @card: card structure
1374 * Returns 0 on success or <0 on failure
1376 * gelic_net_setup_netdev initializes the net_device structure
1378 static int gelic_net_setup_netdev(struct gelic_net_card *card)
1380 struct net_device *netdev = card->netdev;
1381 struct sockaddr addr;
1386 SET_MODULE_OWNER(netdev);
1387 SET_NETDEV_DEV(netdev, &card->dev->core);
1388 spin_lock_init(&card->tx_dma_lock);
1390 card->rx_csum = GELIC_NET_RX_CSUM_DEFAULT;
1392 gelic_net_setup_netdev_ops(netdev);
1394 netdev->features = NETIF_F_IP_CSUM;
1396 status = lv1_net_control(bus_id(card), dev_id(card),
1397 GELIC_NET_GET_MAC_ADDRESS,
1399 if (status || !is_valid_ether_addr((u8 *)&v1)) {
1400 dev_info(ctodev(card),
1401 "%s:lv1_net_control GET_MAC_ADDR failed %d\n",
1406 memcpy(addr.sa_data, &v1, ETH_ALEN);
1407 memcpy(netdev->dev_addr, addr.sa_data, ETH_ALEN);
1408 dev_info(ctodev(card), "MAC addr %02x:%02x:%02x:%02x:%02x:%02x\n",
1409 netdev->dev_addr[0], netdev->dev_addr[1],
1410 netdev->dev_addr[2], netdev->dev_addr[3],
1411 netdev->dev_addr[4], netdev->dev_addr[5]);
1413 card->vlan_index = -1; /* no vlan */
1414 for (i = 0; i < GELIC_NET_VLAN_MAX; i++) {
1415 status = lv1_net_control(bus_id(card), dev_id(card),
1416 GELIC_NET_GET_VLAN_ID,
1417 i + 1, /* index; one based */
1419 if (status == GELIC_NET_VLAN_NO_ENTRY) {
1420 dev_dbg(ctodev(card),
1421 "GELIC_VLAN_ID no entry:%d, VLAN disabled\n",
1423 card->vlan_id[i] = 0;
1424 } else if (status) {
1425 dev_dbg(ctodev(card),
1426 "%s:GELIC_NET_VLAN_ID faild, status=%d\n",
1428 card->vlan_id[i] = 0;
1430 card->vlan_id[i] = (u32)v1;
1431 dev_dbg(ctodev(card), "vlan_id:%d, %lx\n", i, v1);
1434 if (card->vlan_id[GELIC_NET_VLAN_WIRED - 1])
1435 card->vlan_index = GELIC_NET_VLAN_WIRED - 1;
1437 status = register_netdev(netdev);
1439 dev_err(ctodev(card), "%s:Couldn't register net_device: %d\n",
1448 * gelic_net_alloc_card - allocates net_device and card structure
1450 * returns the card structure or NULL in case of errors
1452 * the card and net_device structures are linked to each other
1454 static struct gelic_net_card *gelic_net_alloc_card(void)
1456 struct net_device *netdev;
1457 struct gelic_net_card *card;
1460 alloc_size = sizeof (*card) +
1461 sizeof (struct gelic_net_descr) * GELIC_NET_RX_DESCRIPTORS +
1462 sizeof (struct gelic_net_descr) * GELIC_NET_TX_DESCRIPTORS;
1464 * we assume private data is allocated 32 bytes (or more) aligned
1465 * so that gelic_net_descr should be 32 bytes aligned.
1466 * Current alloc_etherdev() does do it because NETDEV_ALIGN
1468 * check this assumption here.
1470 BUILD_BUG_ON(NETDEV_ALIGN < 32);
1471 BUILD_BUG_ON(offsetof(struct gelic_net_card, irq_status) % 8);
1472 BUILD_BUG_ON(offsetof(struct gelic_net_card, descr) % 32);
1474 netdev = alloc_etherdev(alloc_size);
1478 card = netdev_priv(netdev);
1479 card->netdev = netdev;
1480 INIT_WORK(&card->tx_timeout_task, gelic_net_tx_timeout_task);
1481 init_waitqueue_head(&card->waitq);
1482 atomic_set(&card->tx_timeout_task_counter, 0);
1488 * ps3_gelic_driver_probe - add a device to the control of this driver
1490 static int ps3_gelic_driver_probe (struct ps3_system_bus_device *dev)
1492 struct gelic_net_card *card = gelic_net_alloc_card();
1496 dev_info(&dev->core, "gelic_net_alloc_card failed\n");
1498 goto fail_alloc_card;
1501 ps3_system_bus_set_driver_data(dev, card);
1504 result = ps3_open_hv_device(dev);
1507 dev_dbg(&dev->core, "ps3_open_hv_device failed\n");
1511 result = ps3_dma_region_create(dev->d_region);
1514 dev_dbg(&dev->core, "ps3_dma_region_create failed(%d)\n",
1516 BUG_ON("check region type");
1517 goto fail_dma_region;
1520 result = lv1_net_set_interrupt_status_indicator(bus_id(card),
1522 ps3_mm_phys_to_lpar(__pa(&card->irq_status)),
1527 "lv1_net_set_interrupt_status_indicator failed: %s\n",
1528 ps3_result(result));
1530 goto fail_status_indicator;
1533 result = gelic_net_setup_netdev(card);
1536 dev_dbg(&dev->core, "%s:%d: ps3_dma_region_create failed: "
1537 "(%d)\n", __func__, __LINE__, result);
1538 goto fail_setup_netdev;
1544 lv1_net_set_interrupt_status_indicator(bus_id(card),
1547 fail_status_indicator:
1548 ps3_dma_region_free(dev->d_region);
1550 ps3_close_hv_device(dev);
1552 ps3_system_bus_set_driver_data(dev, NULL);
1553 free_netdev(card->netdev);
1559 * ps3_gelic_driver_remove - remove a device from the control of this driver
1562 static int ps3_gelic_driver_remove (struct ps3_system_bus_device *dev)
1564 struct gelic_net_card *card = ps3_system_bus_get_driver_data(dev);
1566 wait_event(card->waitq,
1567 atomic_read(&card->tx_timeout_task_counter) == 0);
1569 lv1_net_set_interrupt_status_indicator(bus_id(card), dev_id(card),
1572 unregister_netdev(card->netdev);
1573 free_netdev(card->netdev);
1575 ps3_system_bus_set_driver_data(dev, NULL);
1577 ps3_dma_region_free(dev->d_region);
1579 ps3_close_hv_device(dev);
1584 static struct ps3_system_bus_driver ps3_gelic_driver = {
1585 .match_id = PS3_MATCH_ID_GELIC,
1586 .probe = ps3_gelic_driver_probe,
1587 .remove = ps3_gelic_driver_remove,
1588 .shutdown = ps3_gelic_driver_remove,
1589 .core.name = "ps3_gelic_driver",
1590 .core.owner = THIS_MODULE,
1593 static int __init ps3_gelic_driver_init (void)
1595 return firmware_has_feature(FW_FEATURE_PS3_LV1)
1596 ? ps3_system_bus_driver_register(&ps3_gelic_driver)
1600 static void __exit ps3_gelic_driver_exit (void)
1602 ps3_system_bus_driver_unregister(&ps3_gelic_driver);
1605 module_init (ps3_gelic_driver_init);
1606 module_exit (ps3_gelic_driver_exit);
1608 MODULE_ALIAS(PS3_MODULE_ALIAS_GELIC);