1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * PS3 gelic network driver.
5 * Copyright (C) 2007 Sony Computer Entertainment Inc.
6 * Copyright 2006, 2007 Sony Corporation
8 * This file is based on: spider_net.c
10 * (C) Copyright IBM Corp. 2005
12 * Authors : Utz Bacher <utz.bacher@de.ibm.com>
13 * Jens Osterkamp <Jens.Osterkamp@de.ibm.com>
18 #include <linux/interrupt.h>
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/slab.h>
23 #include <linux/etherdevice.h>
24 #include <linux/ethtool.h>
25 #include <linux/if_vlan.h>
29 #include <linux/tcp.h>
31 #include <linux/dma-mapping.h>
32 #include <net/checksum.h>
33 #include <asm/firmware.h>
35 #include <asm/lv1call.h>
37 #include "ps3_gelic_net.h"
38 #include "ps3_gelic_wireless.h"
40 #define DRV_NAME "Gelic Network Driver"
41 #define DRV_VERSION "2.0"
43 MODULE_AUTHOR("SCE Inc.");
44 MODULE_DESCRIPTION("Gelic Network driver");
45 MODULE_LICENSE("GPL");
49 int gelic_card_set_irq_mask(struct gelic_card
*card
, u64 mask
)
53 status
= lv1_net_set_interrupt_mask(bus_id(card
), dev_id(card
),
56 dev_info(ctodev(card
),
57 "%s failed %d\n", __func__
, status
);
61 static void gelic_card_rx_irq_on(struct gelic_card
*card
)
63 card
->irq_mask
|= GELIC_CARD_RXINT
;
64 gelic_card_set_irq_mask(card
, card
->irq_mask
);
66 static void gelic_card_rx_irq_off(struct gelic_card
*card
)
68 card
->irq_mask
&= ~GELIC_CARD_RXINT
;
69 gelic_card_set_irq_mask(card
, card
->irq_mask
);
72 static void gelic_card_get_ether_port_status(struct gelic_card
*card
,
76 struct net_device
*ether_netdev
;
78 lv1_net_control(bus_id(card
), dev_id(card
),
79 GELIC_LV1_GET_ETH_PORT_STATUS
,
80 GELIC_LV1_VLAN_TX_ETHERNET_0
, 0, 0,
81 &card
->ether_port_status
, &v2
);
84 ether_netdev
= card
->netdev
[GELIC_PORT_ETHERNET_0
];
85 if (card
->ether_port_status
& GELIC_LV1_ETHER_LINK_UP
)
86 netif_carrier_on(ether_netdev
);
88 netif_carrier_off(ether_netdev
);
93 * gelic_descr_get_status -- returns the status of a descriptor
94 * @descr: descriptor to look at
96 * returns the status as in the hw_regs.dmac_cmd_status field of the descriptor
98 static enum gelic_descr_dma_status
99 gelic_descr_get_status(struct gelic_descr
*descr
)
101 return be32_to_cpu(descr
->hw_regs
.dmac_cmd_status
) &
102 GELIC_DESCR_DMA_STAT_MASK
;
105 static int gelic_card_set_link_mode(struct gelic_card
*card
, int mode
)
110 status
= lv1_net_control(bus_id(card
), dev_id(card
),
111 GELIC_LV1_SET_NEGOTIATION_MODE
,
112 GELIC_LV1_PHY_ETHERNET_0
, mode
, 0, &v1
, &v2
);
114 pr_info("%s: failed setting negotiation mode %d\n", __func__
,
119 card
->link_mode
= mode
;
124 * gelic_card_disable_txdmac - disables the transmit DMA controller
125 * @card: card structure
127 * gelic_card_disable_txdmac terminates processing on the DMA controller by
128 * turing off DMA and issuing a force end
130 static void gelic_card_disable_txdmac(struct gelic_card
*card
)
134 /* this hvc blocks until the DMA in progress really stopped */
135 status
= lv1_net_stop_tx_dma(bus_id(card
), dev_id(card
));
137 dev_err(ctodev(card
),
138 "lv1_net_stop_tx_dma failed, status=%d\n", status
);
142 * gelic_card_enable_rxdmac - enables the receive DMA controller
143 * @card: card structure
145 * gelic_card_enable_rxdmac enables the DMA controller by setting RX_DMA_EN
146 * in the GDADMACCNTR register
148 static void gelic_card_enable_rxdmac(struct gelic_card
*card
)
153 if (gelic_descr_get_status(card
->rx_chain
.head
) !=
154 GELIC_DESCR_DMA_CARDOWNED
) {
155 printk(KERN_ERR
"%s: status=%x\n", __func__
,
156 be32_to_cpu(card
->rx_chain
.head
->hw_regs
.dmac_cmd_status
));
157 printk(KERN_ERR
"%s: nextphy=%x\n", __func__
,
158 be32_to_cpu(card
->rx_chain
.head
->hw_regs
.next_descr_addr
));
159 printk(KERN_ERR
"%s: head=%p\n", __func__
,
160 card
->rx_chain
.head
);
163 status
= lv1_net_start_rx_dma(bus_id(card
), dev_id(card
),
164 card
->rx_chain
.head
->link
.cpu_addr
, 0);
166 dev_info(ctodev(card
),
167 "lv1_net_start_rx_dma failed, status=%d\n", status
);
171 * gelic_card_disable_rxdmac - disables the receive DMA controller
172 * @card: card structure
174 * gelic_card_disable_rxdmac terminates processing on the DMA controller by
175 * turing off DMA and issuing a force end
177 static void gelic_card_disable_rxdmac(struct gelic_card
*card
)
181 /* this hvc blocks until the DMA in progress really stopped */
182 status
= lv1_net_stop_rx_dma(bus_id(card
), dev_id(card
));
184 dev_err(ctodev(card
),
185 "lv1_net_stop_rx_dma failed, %d\n", status
);
189 * gelic_descr_set_status -- sets the status of a descriptor
190 * @descr: descriptor to change
191 * @status: status to set in the descriptor
193 * changes the status to the specified value. Doesn't change other bits
196 static void gelic_descr_set_status(struct gelic_descr
*descr
,
197 enum gelic_descr_dma_status status
)
199 descr
->hw_regs
.dmac_cmd_status
= cpu_to_be32(status
|
200 (be32_to_cpu(descr
->hw_regs
.dmac_cmd_status
) &
201 ~GELIC_DESCR_DMA_STAT_MASK
));
203 * dma_cmd_status field is used to indicate whether the descriptor
205 * Usually caller of this function wants to inform that to the
206 * hardware, so we assure here the hardware sees the change.
212 * gelic_card_reset_chain - reset status of a descriptor chain
213 * @card: card structure
214 * @chain: address of chain
215 * @start_descr: address of descriptor array
217 * Reset the status of dma descriptors to ready state
218 * and re-initialize the hardware chain for later use
220 static void gelic_card_reset_chain(struct gelic_card
*card
,
221 struct gelic_descr_chain
*chain
,
222 struct gelic_descr
*start_descr
)
224 struct gelic_descr
*descr
;
226 for (descr
= start_descr
; start_descr
!= descr
->next
; descr
++) {
227 gelic_descr_set_status(descr
, GELIC_DESCR_DMA_CARDOWNED
);
228 descr
->hw_regs
.next_descr_addr
229 = cpu_to_be32(descr
->next
->link
.cpu_addr
);
232 chain
->head
= start_descr
;
233 chain
->tail
= (descr
- 1);
235 (descr
- 1)->hw_regs
.next_descr_addr
= 0;
238 void gelic_card_up(struct gelic_card
*card
)
240 pr_debug("%s: called\n", __func__
);
241 mutex_lock(&card
->updown_lock
);
242 if (atomic_inc_return(&card
->users
) == 1) {
243 pr_debug("%s: real do\n", __func__
);
245 gelic_card_set_irq_mask(card
, card
->irq_mask
);
247 gelic_card_enable_rxdmac(card
);
249 napi_enable(&card
->napi
);
251 mutex_unlock(&card
->updown_lock
);
252 pr_debug("%s: done\n", __func__
);
255 void gelic_card_down(struct gelic_card
*card
)
258 pr_debug("%s: called\n", __func__
);
259 mutex_lock(&card
->updown_lock
);
260 if (atomic_dec_if_positive(&card
->users
) == 0) {
261 pr_debug("%s: real do\n", __func__
);
262 napi_disable(&card
->napi
);
264 * Disable irq. Wireless interrupts will
265 * be disabled later if any
267 mask
= card
->irq_mask
& (GELIC_CARD_WLAN_EVENT_RECEIVED
|
268 GELIC_CARD_WLAN_COMMAND_COMPLETED
);
269 gelic_card_set_irq_mask(card
, mask
);
271 gelic_card_disable_rxdmac(card
);
272 gelic_card_reset_chain(card
, &card
->rx_chain
,
273 card
->descr
+ GELIC_NET_TX_DESCRIPTORS
);
275 gelic_card_disable_txdmac(card
);
277 mutex_unlock(&card
->updown_lock
);
278 pr_debug("%s: done\n", __func__
);
282 * gelic_card_free_chain - free descriptor chain
283 * @card: card structure
284 * @descr_in: address of desc
286 static void gelic_card_free_chain(struct gelic_card
*card
,
287 struct gelic_descr
*descr_in
)
289 struct gelic_descr
*descr
;
291 for (descr
= descr_in
; descr
&& descr
->link
.cpu_addr
;
292 descr
= descr
->next
) {
293 dma_unmap_single(ctodev(card
), descr
->link
.cpu_addr
,
294 descr
->link
.size
, DMA_BIDIRECTIONAL
);
295 descr
->link
.cpu_addr
= 0;
296 descr
->link
.size
= 0;
301 * gelic_card_init_chain - links descriptor chain
302 * @card: card structure
303 * @chain: address of chain
304 * @start_descr: address of descriptor array
305 * @no: number of descriptors
307 * we manage a circular list that mirrors the hardware structure,
308 * except that the hardware uses bus addresses.
310 * returns 0 on success, <0 on failure
312 static int gelic_card_init_chain(struct gelic_card
*card
,
313 struct gelic_descr_chain
*chain
,
314 struct gelic_descr
*start_descr
, int no
)
317 struct gelic_descr
*descr
;
320 memset(descr
, 0, sizeof(*descr
) * no
);
322 /* set up the hardware pointers in each descriptor */
323 for (i
= 0; i
< no
; i
++, descr
++) {
324 gelic_descr_set_status(descr
, GELIC_DESCR_DMA_NOT_IN_USE
);
326 descr
->link
.size
= sizeof(struct gelic_hw_regs
);
327 descr
->link
.cpu_addr
= dma_map_single(ctodev(card
), descr
,
328 descr
->link
.size
, DMA_BIDIRECTIONAL
);
330 if (dma_mapping_error(ctodev(card
), descr
->link
.cpu_addr
)) {
331 for (i
--, descr
--; 0 <= i
; i
--, descr
--) {
332 dma_unmap_single(ctodev(card
),
333 descr
->link
.cpu_addr
, descr
->link
.size
,
339 descr
->next
= descr
+ 1;
340 descr
->prev
= descr
- 1;
342 /* make them as ring */
343 (descr
- 1)->next
= start_descr
;
344 start_descr
->prev
= (descr
- 1);
346 /* chain bus addr of hw descriptor */
348 for (i
= 0; i
< no
; i
++, descr
++) {
349 descr
->hw_regs
.next_descr_addr
=
350 cpu_to_be32(descr
->next
->link
.cpu_addr
);
353 chain
->head
= start_descr
;
354 chain
->tail
= start_descr
;
356 /* do not chain last hw descriptor */
357 (descr
- 1)->hw_regs
.next_descr_addr
= 0;
363 * gelic_descr_prepare_rx - reinitializes a rx descriptor
364 * @card: card structure
365 * @descr: descriptor to re-init
367 * return 0 on success, <0 on failure
369 * allocates a new rx skb, iommu-maps it and attaches it to the descriptor.
370 * Activate the descriptor state-wise
372 * Gelic RX sk_buffs must be aligned to GELIC_NET_RXBUF_ALIGN and the length
373 * must be a multiple of GELIC_NET_RXBUF_ALIGN.
375 static int gelic_descr_prepare_rx(struct gelic_card
*card
,
376 struct gelic_descr
*descr
)
378 static const unsigned int rx_skb_size
=
379 ALIGN(GELIC_NET_MAX_FRAME
, GELIC_NET_RXBUF_ALIGN
) +
380 GELIC_NET_RXBUF_ALIGN
- 1;
384 if (gelic_descr_get_status(descr
) != GELIC_DESCR_DMA_NOT_IN_USE
)
385 dev_info(ctodev(card
), "%s: ERROR status\n", __func__
);
387 descr
->hw_regs
.dmac_cmd_status
= 0;
388 descr
->hw_regs
.result_size
= 0;
389 descr
->hw_regs
.valid_size
= 0;
390 descr
->hw_regs
.data_error
= 0;
391 descr
->hw_regs
.payload
.dev_addr
= 0;
392 descr
->hw_regs
.payload
.size
= 0;
394 descr
->skb
= netdev_alloc_skb(*card
->netdev
, rx_skb_size
);
396 descr
->hw_regs
.payload
.dev_addr
= 0; /* tell DMAC don't touch memory */
400 offset
= ((unsigned long)descr
->skb
->data
) &
401 (GELIC_NET_RXBUF_ALIGN
- 1);
403 skb_reserve(descr
->skb
, GELIC_NET_RXBUF_ALIGN
- offset
);
404 /* io-mmu-map the skb */
405 cpu_addr
= dma_map_single(ctodev(card
), descr
->skb
->data
,
406 GELIC_NET_MAX_FRAME
, DMA_FROM_DEVICE
);
407 descr
->hw_regs
.payload
.dev_addr
= cpu_to_be32(cpu_addr
);
408 if (dma_mapping_error(ctodev(card
), cpu_addr
)) {
409 dev_kfree_skb_any(descr
->skb
);
411 dev_info(ctodev(card
),
412 "%s:Could not iommu-map rx buffer\n", __func__
);
413 gelic_descr_set_status(descr
, GELIC_DESCR_DMA_NOT_IN_USE
);
417 descr
->hw_regs
.payload
.size
= cpu_to_be32(GELIC_NET_MAX_FRAME
);
418 descr
->hw_regs
.payload
.dev_addr
= cpu_to_be32(cpu_addr
);
420 gelic_descr_set_status(descr
, GELIC_DESCR_DMA_CARDOWNED
);
426 * gelic_card_release_rx_chain - free all skb of rx descr
427 * @card: card structure
430 static void gelic_card_release_rx_chain(struct gelic_card
*card
)
432 struct gelic_descr
*descr
= card
->rx_chain
.head
;
436 dma_unmap_single(ctodev(card
),
437 be32_to_cpu(descr
->hw_regs
.payload
.dev_addr
),
440 descr
->hw_regs
.payload
.dev_addr
= 0;
441 descr
->hw_regs
.payload
.size
= 0;
442 dev_kfree_skb_any(descr
->skb
);
444 gelic_descr_set_status(descr
,
445 GELIC_DESCR_DMA_NOT_IN_USE
);
448 } while (descr
!= card
->rx_chain
.head
);
452 * gelic_card_fill_rx_chain - fills descriptors/skbs in the rx chains
453 * @card: card structure
455 * fills all descriptors in the rx chain: allocates skbs
456 * and iommu-maps them.
457 * returns 0 on success, < 0 on failure
459 static int gelic_card_fill_rx_chain(struct gelic_card
*card
)
461 struct gelic_descr
*descr
= card
->rx_chain
.head
;
466 ret
= gelic_descr_prepare_rx(card
, descr
);
471 } while (descr
!= card
->rx_chain
.head
);
475 gelic_card_release_rx_chain(card
);
480 * gelic_card_alloc_rx_skbs - allocates rx skbs in rx descriptor chains
481 * @card: card structure
483 * returns 0 on success, < 0 on failure
485 static int gelic_card_alloc_rx_skbs(struct gelic_card
*card
)
487 struct gelic_descr_chain
*chain
;
489 chain
= &card
->rx_chain
;
490 ret
= gelic_card_fill_rx_chain(card
);
491 chain
->tail
= card
->rx_top
->prev
; /* point to the last */
496 * gelic_descr_release_tx - processes a used tx descriptor
497 * @card: card structure
498 * @descr: descriptor to release
500 * releases a used tx descriptor (unmapping, freeing of skb)
502 static void gelic_descr_release_tx(struct gelic_card
*card
,
503 struct gelic_descr
*descr
)
505 struct sk_buff
*skb
= descr
->skb
;
507 BUG_ON(!(be32_to_cpu(descr
->hw_regs
.data_status
) & GELIC_DESCR_TX_TAIL
));
509 dma_unmap_single(ctodev(card
),
510 be32_to_cpu(descr
->hw_regs
.payload
.dev_addr
), skb
->len
,
512 dev_kfree_skb_any(skb
);
514 descr
->hw_regs
.payload
.dev_addr
= 0;
515 descr
->hw_regs
.payload
.size
= 0;
516 descr
->hw_regs
.next_descr_addr
= 0;
517 descr
->hw_regs
.result_size
= 0;
518 descr
->hw_regs
.valid_size
= 0;
519 descr
->hw_regs
.data_status
= 0;
520 descr
->hw_regs
.data_error
= 0;
523 /* set descr status */
524 gelic_descr_set_status(descr
, GELIC_DESCR_DMA_NOT_IN_USE
);
527 static void gelic_card_stop_queues(struct gelic_card
*card
)
529 netif_stop_queue(card
->netdev
[GELIC_PORT_ETHERNET_0
]);
531 if (card
->netdev
[GELIC_PORT_WIRELESS
])
532 netif_stop_queue(card
->netdev
[GELIC_PORT_WIRELESS
]);
534 static void gelic_card_wake_queues(struct gelic_card
*card
)
536 netif_wake_queue(card
->netdev
[GELIC_PORT_ETHERNET_0
]);
538 if (card
->netdev
[GELIC_PORT_WIRELESS
])
539 netif_wake_queue(card
->netdev
[GELIC_PORT_WIRELESS
]);
542 * gelic_card_release_tx_chain - processes sent tx descriptors
543 * @card: adapter structure
544 * @stop: net_stop sequence
546 * releases the tx descriptors that gelic has finished with
548 static void gelic_card_release_tx_chain(struct gelic_card
*card
, int stop
)
550 struct gelic_descr_chain
*tx_chain
;
551 enum gelic_descr_dma_status status
;
552 struct net_device
*netdev
;
555 for (tx_chain
= &card
->tx_chain
;
556 tx_chain
->head
!= tx_chain
->tail
&& tx_chain
->tail
;
557 tx_chain
->tail
= tx_chain
->tail
->next
) {
558 status
= gelic_descr_get_status(tx_chain
->tail
);
559 netdev
= tx_chain
->tail
->skb
->dev
;
561 case GELIC_DESCR_DMA_RESPONSE_ERROR
:
562 case GELIC_DESCR_DMA_PROTECTION_ERROR
:
563 case GELIC_DESCR_DMA_FORCE_END
:
564 if (printk_ratelimit())
565 dev_info(ctodev(card
),
566 "%s: forcing end of tx descriptor " \
569 netdev
->stats
.tx_dropped
++;
572 case GELIC_DESCR_DMA_COMPLETE
:
573 if (tx_chain
->tail
->skb
) {
574 netdev
->stats
.tx_packets
++;
575 netdev
->stats
.tx_bytes
+=
576 tx_chain
->tail
->skb
->len
;
580 case GELIC_DESCR_DMA_CARDOWNED
:
581 /* pending tx request */
583 /* any other value (== GELIC_DESCR_DMA_NOT_IN_USE) */
587 gelic_descr_release_tx(card
, tx_chain
->tail
);
591 if (!stop
&& release
)
592 gelic_card_wake_queues(card
);
596 * gelic_net_set_multi - sets multicast addresses and promisc flags
597 * @netdev: interface device structure
599 * gelic_net_set_multi configures multicast addresses as needed for the
600 * netdev interface. It also sets up multicast, allmulti and promisc
601 * flags appropriately
603 void gelic_net_set_multi(struct net_device
*netdev
)
605 struct gelic_card
*card
= netdev_card(netdev
);
606 struct netdev_hw_addr
*ha
;
612 /* clear all multicast address */
613 status
= lv1_net_remove_multicast_address(bus_id(card
), dev_id(card
),
616 dev_err(ctodev(card
),
617 "lv1_net_remove_multicast_address failed %d\n",
619 /* set broadcast address */
620 status
= lv1_net_add_multicast_address(bus_id(card
), dev_id(card
),
621 GELIC_NET_BROADCAST_ADDR
, 0);
623 dev_err(ctodev(card
),
624 "lv1_net_add_multicast_address failed, %d\n",
627 if ((netdev
->flags
& IFF_ALLMULTI
) ||
628 (netdev_mc_count(netdev
) > GELIC_NET_MC_COUNT_MAX
)) {
629 status
= lv1_net_add_multicast_address(bus_id(card
),
633 dev_err(ctodev(card
),
634 "lv1_net_add_multicast_address failed, %d\n",
639 /* set multicast addresses */
640 netdev_for_each_mc_addr(ha
, netdev
) {
643 for (i
= 0; i
< ETH_ALEN
; i
++) {
647 status
= lv1_net_add_multicast_address(bus_id(card
),
651 dev_err(ctodev(card
),
652 "lv1_net_add_multicast_address failed, %d\n",
658 * gelic_net_stop - called upon ifconfig down
659 * @netdev: interface device structure
663 int gelic_net_stop(struct net_device
*netdev
)
665 struct gelic_card
*card
;
667 pr_debug("%s: start\n", __func__
);
669 netif_stop_queue(netdev
);
670 netif_carrier_off(netdev
);
672 card
= netdev_card(netdev
);
673 gelic_card_down(card
);
675 pr_debug("%s: done\n", __func__
);
680 * gelic_card_get_next_tx_descr - returns the next available tx descriptor
681 * @card: device structure to get descriptor from
683 * returns the address of the next descriptor, or NULL if not available.
685 static struct gelic_descr
*
686 gelic_card_get_next_tx_descr(struct gelic_card
*card
)
688 if (!card
->tx_chain
.head
)
690 /* see if the next descriptor is free */
691 if (card
->tx_chain
.tail
!= card
->tx_chain
.head
->next
&&
692 gelic_descr_get_status(card
->tx_chain
.head
) ==
693 GELIC_DESCR_DMA_NOT_IN_USE
)
694 return card
->tx_chain
.head
;
701 * gelic_descr_set_tx_cmdstat - sets the tx descriptor command field
702 * @descr: descriptor structure to fill out
703 * @skb: packet to consider
705 * fills out the command and status field of the descriptor structure,
706 * depending on hardware checksum settings. This function assumes a wmb()
707 * has executed before.
709 static void gelic_descr_set_tx_cmdstat(struct gelic_descr
*descr
,
712 if (skb
->ip_summed
!= CHECKSUM_PARTIAL
)
713 descr
->hw_regs
.dmac_cmd_status
=
714 cpu_to_be32(GELIC_DESCR_DMA_CMD_NO_CHKSUM
|
715 GELIC_DESCR_TX_DMA_FRAME_TAIL
);
718 * if yes: tcp? udp? */
719 if (skb
->protocol
== htons(ETH_P_IP
)) {
720 if (ip_hdr(skb
)->protocol
== IPPROTO_TCP
)
721 descr
->hw_regs
.dmac_cmd_status
=
722 cpu_to_be32(GELIC_DESCR_DMA_CMD_TCP_CHKSUM
|
723 GELIC_DESCR_TX_DMA_FRAME_TAIL
);
725 else if (ip_hdr(skb
)->protocol
== IPPROTO_UDP
)
726 descr
->hw_regs
.dmac_cmd_status
=
727 cpu_to_be32(GELIC_DESCR_DMA_CMD_UDP_CHKSUM
|
728 GELIC_DESCR_TX_DMA_FRAME_TAIL
);
730 * the stack should checksum non-tcp and non-udp
731 * packets on his own: NETIF_F_IP_CSUM
733 descr
->hw_regs
.dmac_cmd_status
=
734 cpu_to_be32(GELIC_DESCR_DMA_CMD_NO_CHKSUM
|
735 GELIC_DESCR_TX_DMA_FRAME_TAIL
);
740 static struct sk_buff
*gelic_put_vlan_tag(struct sk_buff
*skb
,
743 struct vlan_ethhdr
*veth
;
744 static unsigned int c
;
746 if (skb_headroom(skb
) < VLAN_HLEN
) {
747 struct sk_buff
*sk_tmp
= skb
;
748 pr_debug("%s: hd=%d c=%ud\n", __func__
, skb_headroom(skb
), c
);
749 skb
= skb_realloc_headroom(sk_tmp
, VLAN_HLEN
);
752 dev_kfree_skb_any(sk_tmp
);
754 veth
= skb_push(skb
, VLAN_HLEN
);
756 /* Move the mac addresses to the top of buffer */
757 memmove(skb
->data
, skb
->data
+ VLAN_HLEN
, 2 * ETH_ALEN
);
759 veth
->h_vlan_proto
= cpu_to_be16(ETH_P_8021Q
);
760 veth
->h_vlan_TCI
= htons(tag
);
766 * gelic_descr_prepare_tx - setup a descriptor for sending packets
767 * @card: card structure
768 * @descr: descriptor structure
769 * @skb: packet to use
771 * returns 0 on success, <0 on failure.
774 static int gelic_descr_prepare_tx(struct gelic_card
*card
,
775 struct gelic_descr
*descr
,
780 if (card
->vlan_required
) {
781 struct sk_buff
*skb_tmp
;
782 enum gelic_port_type type
;
784 type
= netdev_port(skb
->dev
)->type
;
785 skb_tmp
= gelic_put_vlan_tag(skb
,
786 card
->vlan
[type
].tx
);
792 buf
= dma_map_single(ctodev(card
), skb
->data
, skb
->len
, DMA_TO_DEVICE
);
794 if (dma_mapping_error(ctodev(card
), buf
)) {
795 dev_err(ctodev(card
),
796 "dma map 2 failed (%p, %i). Dropping packet\n",
797 skb
->data
, skb
->len
);
801 descr
->hw_regs
.payload
.dev_addr
= cpu_to_be32(buf
);
802 descr
->hw_regs
.payload
.size
= cpu_to_be32(skb
->len
);
804 descr
->hw_regs
.data_status
= 0;
805 descr
->hw_regs
.next_descr_addr
= 0; /* terminate hw descr */
806 gelic_descr_set_tx_cmdstat(descr
, skb
);
808 /* bump free descriptor pointer */
809 card
->tx_chain
.head
= descr
->next
;
814 * gelic_card_kick_txdma - enables TX DMA processing
815 * @card: card structure
816 * @descr: descriptor address to enable TX processing at
819 static int gelic_card_kick_txdma(struct gelic_card
*card
,
820 struct gelic_descr
*descr
)
824 if (card
->tx_dma_progress
)
827 if (gelic_descr_get_status(descr
) == GELIC_DESCR_DMA_CARDOWNED
) {
828 card
->tx_dma_progress
= 1;
829 status
= lv1_net_start_tx_dma(bus_id(card
), dev_id(card
),
830 descr
->link
.cpu_addr
, 0);
832 card
->tx_dma_progress
= 0;
833 dev_info(ctodev(card
), "lv1_net_start_txdma failed," \
834 "status=%d\n", status
);
841 * gelic_net_xmit - transmits a frame over the device
842 * @skb: packet to send out
843 * @netdev: interface device structure
845 * returns NETDEV_TX_OK on success, NETDEV_TX_BUSY on failure
847 netdev_tx_t
gelic_net_xmit(struct sk_buff
*skb
, struct net_device
*netdev
)
849 struct gelic_card
*card
= netdev_card(netdev
);
850 struct gelic_descr
*descr
;
854 spin_lock_irqsave(&card
->tx_lock
, flags
);
856 gelic_card_release_tx_chain(card
, 0);
858 descr
= gelic_card_get_next_tx_descr(card
);
861 * no more descriptors free
863 gelic_card_stop_queues(card
);
864 spin_unlock_irqrestore(&card
->tx_lock
, flags
);
865 return NETDEV_TX_BUSY
;
868 result
= gelic_descr_prepare_tx(card
, descr
, skb
);
871 * DMA map failed. As chances are that failure
872 * would continue, just release skb and return
874 netdev
->stats
.tx_dropped
++;
875 dev_kfree_skb_any(skb
);
876 spin_unlock_irqrestore(&card
->tx_lock
, flags
);
880 * link this prepared descriptor to previous one
881 * to achieve high performance
883 descr
->prev
->hw_regs
.next_descr_addr
=
884 cpu_to_be32(descr
->link
.cpu_addr
);
886 * as hardware descriptor is modified in the above lines,
887 * ensure that the hardware sees it
890 if (gelic_card_kick_txdma(card
, descr
)) {
893 * release descriptor which was just prepared
895 netdev
->stats
.tx_dropped
++;
896 /* don't trigger BUG_ON() in gelic_descr_release_tx */
897 descr
->hw_regs
.data_status
= cpu_to_be32(GELIC_DESCR_TX_TAIL
);
898 gelic_descr_release_tx(card
, descr
);
900 card
->tx_chain
.head
= descr
;
901 /* reset hw termination */
902 descr
->prev
->hw_regs
.next_descr_addr
= 0;
903 dev_info(ctodev(card
), "%s: kick failure\n", __func__
);
906 spin_unlock_irqrestore(&card
->tx_lock
, flags
);
911 * gelic_net_pass_skb_up - takes an skb from a descriptor and passes it on
912 * @descr: descriptor to process
913 * @card: card structure
914 * @netdev: net_device structure to be passed packet
916 * iommu-unmaps the skb, fills out skb structure and passes the data to the
917 * stack. The descriptor state is not changed.
919 static void gelic_net_pass_skb_up(struct gelic_descr
*descr
,
920 struct gelic_card
*card
,
921 struct net_device
*netdev
)
924 struct sk_buff
*skb
= descr
->skb
;
925 u32 data_status
, data_error
;
927 data_status
= be32_to_cpu(descr
->hw_regs
.data_status
);
928 data_error
= be32_to_cpu(descr
->hw_regs
.data_error
);
929 /* unmap skb buffer */
930 dma_unmap_single(ctodev(card
),
931 be32_to_cpu(descr
->hw_regs
.payload
.dev_addr
),
932 be32_to_cpu(descr
->hw_regs
.payload
.size
), DMA_FROM_DEVICE
);
934 skb_put(skb
, be32_to_cpu(descr
->hw_regs
.valid_size
)?
935 be32_to_cpu(descr
->hw_regs
.valid_size
) :
936 be32_to_cpu(descr
->hw_regs
.result_size
));
937 if (!descr
->hw_regs
.valid_size
)
938 dev_info(ctodev(card
), "buffer full %x %x %x\n",
939 be32_to_cpu(descr
->hw_regs
.result_size
),
940 be32_to_cpu(descr
->hw_regs
.payload
.size
),
941 be32_to_cpu(descr
->hw_regs
.dmac_cmd_status
));
945 * the card put 2 bytes vlan tag in front
946 * of the ethernet frame
949 skb
->protocol
= eth_type_trans(skb
, netdev
);
951 /* checksum offload */
952 if (netdev
->features
& NETIF_F_RXCSUM
) {
953 if ((data_status
& GELIC_DESCR_DATA_STATUS_CHK_MASK
) &&
954 (!(data_error
& GELIC_DESCR_DATA_ERROR_CHK_MASK
)))
955 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
957 skb_checksum_none_assert(skb
);
959 skb_checksum_none_assert(skb
);
961 /* update netdevice statistics */
962 netdev
->stats
.rx_packets
++;
963 netdev
->stats
.rx_bytes
+= skb
->len
;
965 /* pass skb up to stack */
966 netif_receive_skb(skb
);
970 * gelic_card_decode_one_descr - processes an rx descriptor
971 * @card: card structure
973 * returns 1 if a packet has been sent to the stack, otherwise 0
975 * processes an rx descriptor by iommu-unmapping the data buffer and passing
976 * the packet up to the stack
978 static int gelic_card_decode_one_descr(struct gelic_card
*card
)
980 enum gelic_descr_dma_status status
;
981 struct gelic_descr_chain
*chain
= &card
->rx_chain
;
982 struct gelic_descr
*descr
= chain
->head
;
983 struct net_device
*netdev
= NULL
;
984 int dmac_chain_ended
;
986 status
= gelic_descr_get_status(descr
);
988 if (status
== GELIC_DESCR_DMA_CARDOWNED
)
991 if (status
== GELIC_DESCR_DMA_NOT_IN_USE
) {
992 dev_dbg(ctodev(card
), "dormant descr? %p\n", descr
);
996 /* netdevice select */
997 if (card
->vlan_required
) {
1000 vid
= *(u16
*)(descr
->skb
->data
) & VLAN_VID_MASK
;
1001 for (i
= 0; i
< GELIC_PORT_MAX
; i
++) {
1002 if (card
->vlan
[i
].rx
== vid
) {
1003 netdev
= card
->netdev
[i
];
1007 if (GELIC_PORT_MAX
<= i
) {
1008 pr_info("%s: unknown packet vid=%x\n", __func__
, vid
);
1012 netdev
= card
->netdev
[GELIC_PORT_ETHERNET_0
];
1014 if ((status
== GELIC_DESCR_DMA_RESPONSE_ERROR
) ||
1015 (status
== GELIC_DESCR_DMA_PROTECTION_ERROR
) ||
1016 (status
== GELIC_DESCR_DMA_FORCE_END
)) {
1017 dev_info(ctodev(card
), "dropping RX descriptor with state %x\n",
1019 netdev
->stats
.rx_dropped
++;
1023 if (status
== GELIC_DESCR_DMA_BUFFER_FULL
) {
1025 * Buffer full would occur if and only if
1026 * the frame length was longer than the size of this
1027 * descriptor's buffer. If the frame length was equal
1028 * to or shorter than buffer'size, FRAME_END condition
1030 * Anyway this frame was longer than the MTU,
1033 dev_info(ctodev(card
), "overlength frame\n");
1037 * descriptors any other than FRAME_END here should
1038 * be treated as error.
1040 if (status
!= GELIC_DESCR_DMA_FRAME_END
) {
1041 dev_dbg(ctodev(card
), "RX descriptor with state %x\n",
1046 /* ok, we've got a packet in descr */
1047 gelic_net_pass_skb_up(descr
, card
, netdev
);
1050 /* is the current descriptor terminated with next_descr == NULL? */
1052 be32_to_cpu(descr
->hw_regs
.dmac_cmd_status
) &
1053 GELIC_DESCR_RX_DMA_CHAIN_END
;
1055 * So that always DMAC can see the end
1056 * of the descriptor chain to avoid
1057 * from unwanted DMAC overrun.
1059 descr
->hw_regs
.next_descr_addr
= 0;
1061 /* change the descriptor state: */
1062 gelic_descr_set_status(descr
, GELIC_DESCR_DMA_NOT_IN_USE
);
1065 * this call can fail, but for now, just leave this
1066 * descriptor without skb
1068 gelic_descr_prepare_rx(card
, descr
);
1070 chain
->tail
= descr
;
1071 chain
->head
= descr
->next
;
1074 * Set this descriptor the end of the chain.
1076 descr
->prev
->hw_regs
.next_descr_addr
=
1077 cpu_to_be32(descr
->link
.cpu_addr
);
1080 * If dmac chain was met, DMAC stopped.
1084 if (dmac_chain_ended
)
1085 gelic_card_enable_rxdmac(card
);
1091 * gelic_net_poll - NAPI poll function called by the stack to return packets
1092 * @napi: napi structure
1093 * @budget: number of packets we can pass to the stack at most
1095 * returns the number of the processed packets
1098 static int gelic_net_poll(struct napi_struct
*napi
, int budget
)
1100 struct gelic_card
*card
= container_of(napi
, struct gelic_card
, napi
);
1101 int packets_done
= 0;
1103 while (packets_done
< budget
) {
1104 if (!gelic_card_decode_one_descr(card
))
1110 if (packets_done
< budget
) {
1111 napi_complete_done(napi
, packets_done
);
1112 gelic_card_rx_irq_on(card
);
1114 return packets_done
;
1118 * gelic_card_interrupt - event handler for gelic_net
1120 static irqreturn_t
gelic_card_interrupt(int irq
, void *ptr
)
1122 unsigned long flags
;
1123 struct gelic_card
*card
= ptr
;
1126 status
= card
->irq_status
;
1131 status
&= card
->irq_mask
;
1133 if (status
& GELIC_CARD_RXINT
) {
1134 gelic_card_rx_irq_off(card
);
1135 napi_schedule(&card
->napi
);
1138 if (status
& GELIC_CARD_TXINT
) {
1139 spin_lock_irqsave(&card
->tx_lock
, flags
);
1140 card
->tx_dma_progress
= 0;
1141 gelic_card_release_tx_chain(card
, 0);
1142 /* kick outstanding tx descriptor if any */
1143 gelic_card_kick_txdma(card
, card
->tx_chain
.tail
);
1144 spin_unlock_irqrestore(&card
->tx_lock
, flags
);
1147 /* ether port status changed */
1148 if (status
& GELIC_CARD_PORT_STATUS_CHANGED
)
1149 gelic_card_get_ether_port_status(card
, 1);
1151 #ifdef CONFIG_GELIC_WIRELESS
1152 if (status
& (GELIC_CARD_WLAN_EVENT_RECEIVED
|
1153 GELIC_CARD_WLAN_COMMAND_COMPLETED
))
1154 gelic_wl_interrupt(card
->netdev
[GELIC_PORT_WIRELESS
], status
);
1160 #ifdef CONFIG_NET_POLL_CONTROLLER
1162 * gelic_net_poll_controller - artificial interrupt for netconsole etc.
1163 * @netdev: interface device structure
1165 * see Documentation/networking/netconsole.rst
1167 void gelic_net_poll_controller(struct net_device
*netdev
)
1169 struct gelic_card
*card
= netdev_card(netdev
);
1171 gelic_card_set_irq_mask(card
, 0);
1172 gelic_card_interrupt(netdev
->irq
, netdev
);
1173 gelic_card_set_irq_mask(card
, card
->irq_mask
);
1175 #endif /* CONFIG_NET_POLL_CONTROLLER */
1178 * gelic_net_open - called upon ifconfig up
1179 * @netdev: interface device structure
1181 * returns 0 on success, <0 on failure
1183 * gelic_net_open allocates all the descriptors and memory needed for
1184 * operation, sets up multicast list and enables interrupts
1186 int gelic_net_open(struct net_device
*netdev
)
1188 struct gelic_card
*card
= netdev_card(netdev
);
1190 dev_dbg(ctodev(card
), " -> %s %p\n", __func__
, netdev
);
1192 gelic_card_up(card
);
1194 netif_start_queue(netdev
);
1195 gelic_card_get_ether_port_status(card
, 1);
1197 dev_dbg(ctodev(card
), " <- %s\n", __func__
);
1201 void gelic_net_get_drvinfo(struct net_device
*netdev
,
1202 struct ethtool_drvinfo
*info
)
1204 strscpy(info
->driver
, DRV_NAME
, sizeof(info
->driver
));
1205 strscpy(info
->version
, DRV_VERSION
, sizeof(info
->version
));
1208 static int gelic_ether_get_link_ksettings(struct net_device
*netdev
,
1209 struct ethtool_link_ksettings
*cmd
)
1211 struct gelic_card
*card
= netdev_card(netdev
);
1212 u32 supported
, advertising
;
1214 gelic_card_get_ether_port_status(card
, 0);
1216 if (card
->ether_port_status
& GELIC_LV1_ETHER_FULL_DUPLEX
)
1217 cmd
->base
.duplex
= DUPLEX_FULL
;
1219 cmd
->base
.duplex
= DUPLEX_HALF
;
1221 switch (card
->ether_port_status
& GELIC_LV1_ETHER_SPEED_MASK
) {
1222 case GELIC_LV1_ETHER_SPEED_10
:
1223 cmd
->base
.speed
= SPEED_10
;
1225 case GELIC_LV1_ETHER_SPEED_100
:
1226 cmd
->base
.speed
= SPEED_100
;
1228 case GELIC_LV1_ETHER_SPEED_1000
:
1229 cmd
->base
.speed
= SPEED_1000
;
1232 pr_info("%s: speed unknown\n", __func__
);
1233 cmd
->base
.speed
= SPEED_10
;
1237 supported
= SUPPORTED_TP
| SUPPORTED_Autoneg
|
1238 SUPPORTED_10baseT_Half
| SUPPORTED_10baseT_Full
|
1239 SUPPORTED_100baseT_Half
| SUPPORTED_100baseT_Full
|
1240 SUPPORTED_1000baseT_Full
;
1241 advertising
= supported
;
1242 if (card
->link_mode
& GELIC_LV1_ETHER_AUTO_NEG
) {
1243 cmd
->base
.autoneg
= AUTONEG_ENABLE
;
1245 cmd
->base
.autoneg
= AUTONEG_DISABLE
;
1246 advertising
&= ~ADVERTISED_Autoneg
;
1248 cmd
->base
.port
= PORT_TP
;
1250 ethtool_convert_legacy_u32_to_link_mode(cmd
->link_modes
.supported
,
1252 ethtool_convert_legacy_u32_to_link_mode(cmd
->link_modes
.advertising
,
1259 gelic_ether_set_link_ksettings(struct net_device
*netdev
,
1260 const struct ethtool_link_ksettings
*cmd
)
1262 struct gelic_card
*card
= netdev_card(netdev
);
1266 if (cmd
->base
.autoneg
== AUTONEG_ENABLE
) {
1267 mode
= GELIC_LV1_ETHER_AUTO_NEG
;
1269 switch (cmd
->base
.speed
) {
1271 mode
= GELIC_LV1_ETHER_SPEED_10
;
1274 mode
= GELIC_LV1_ETHER_SPEED_100
;
1277 mode
= GELIC_LV1_ETHER_SPEED_1000
;
1282 if (cmd
->base
.duplex
== DUPLEX_FULL
) {
1283 mode
|= GELIC_LV1_ETHER_FULL_DUPLEX
;
1284 } else if (cmd
->base
.speed
== SPEED_1000
) {
1285 pr_info("1000 half duplex is not supported.\n");
1290 ret
= gelic_card_set_link_mode(card
, mode
);
1298 static void gelic_net_get_wol(struct net_device
*netdev
,
1299 struct ethtool_wolinfo
*wol
)
1301 if (0 <= ps3_compare_firmware_version(2, 2, 0))
1302 wol
->supported
= WAKE_MAGIC
;
1306 wol
->wolopts
= ps3_sys_manager_get_wol() ? wol
->supported
: 0;
1307 memset(&wol
->sopass
, 0, sizeof(wol
->sopass
));
1309 static int gelic_net_set_wol(struct net_device
*netdev
,
1310 struct ethtool_wolinfo
*wol
)
1313 struct gelic_card
*card
;
1316 if (ps3_compare_firmware_version(2, 2, 0) < 0 ||
1317 !capable(CAP_NET_ADMIN
))
1320 if (wol
->wolopts
& ~WAKE_MAGIC
)
1323 card
= netdev_card(netdev
);
1324 if (wol
->wolopts
& WAKE_MAGIC
) {
1325 status
= lv1_net_control(bus_id(card
), dev_id(card
),
1327 GELIC_LV1_WOL_MAGIC_PACKET
,
1328 0, GELIC_LV1_WOL_MP_ENABLE
,
1331 pr_info("%s: enabling WOL failed %d\n", __func__
,
1336 status
= lv1_net_control(bus_id(card
), dev_id(card
),
1338 GELIC_LV1_WOL_ADD_MATCH_ADDR
,
1339 0, GELIC_LV1_WOL_MATCH_ALL
,
1342 ps3_sys_manager_set_wol(1);
1344 pr_info("%s: enabling WOL filter failed %d\n",
1349 status
= lv1_net_control(bus_id(card
), dev_id(card
),
1351 GELIC_LV1_WOL_MAGIC_PACKET
,
1352 0, GELIC_LV1_WOL_MP_DISABLE
,
1355 pr_info("%s: disabling WOL failed %d\n", __func__
,
1360 status
= lv1_net_control(bus_id(card
), dev_id(card
),
1362 GELIC_LV1_WOL_DELETE_MATCH_ADDR
,
1363 0, GELIC_LV1_WOL_MATCH_ALL
,
1366 ps3_sys_manager_set_wol(0);
1368 pr_info("%s: removing WOL filter failed %d\n",
1377 static const struct ethtool_ops gelic_ether_ethtool_ops
= {
1378 .get_drvinfo
= gelic_net_get_drvinfo
,
1379 .get_link
= ethtool_op_get_link
,
1380 .get_wol
= gelic_net_get_wol
,
1381 .set_wol
= gelic_net_set_wol
,
1382 .get_link_ksettings
= gelic_ether_get_link_ksettings
,
1383 .set_link_ksettings
= gelic_ether_set_link_ksettings
,
1387 * gelic_net_tx_timeout_task - task scheduled by the watchdog timeout
1388 * function (to be called not under interrupt status)
1389 * @work: work is context of tx timout task
1391 * called as task when tx hangs, resets interface (if interface is up)
1393 static void gelic_net_tx_timeout_task(struct work_struct
*work
)
1395 struct gelic_card
*card
=
1396 container_of(work
, struct gelic_card
, tx_timeout_task
);
1397 struct net_device
*netdev
= card
->netdev
[GELIC_PORT_ETHERNET_0
];
1399 dev_info(ctodev(card
), "%s:Timed out. Restarting...\n", __func__
);
1401 if (!(netdev
->flags
& IFF_UP
))
1404 netif_device_detach(netdev
);
1405 gelic_net_stop(netdev
);
1407 gelic_net_open(netdev
);
1408 netif_device_attach(netdev
);
1411 atomic_dec(&card
->tx_timeout_task_counter
);
1415 * gelic_net_tx_timeout - called when the tx timeout watchdog kicks in.
1416 * @netdev: interface device structure
1419 * called, if tx hangs. Schedules a task that resets the interface
1421 void gelic_net_tx_timeout(struct net_device
*netdev
, unsigned int txqueue
)
1423 struct gelic_card
*card
;
1425 card
= netdev_card(netdev
);
1426 atomic_inc(&card
->tx_timeout_task_counter
);
1427 if (netdev
->flags
& IFF_UP
)
1428 schedule_work(&card
->tx_timeout_task
);
1430 atomic_dec(&card
->tx_timeout_task_counter
);
1433 static const struct net_device_ops gelic_netdevice_ops
= {
1434 .ndo_open
= gelic_net_open
,
1435 .ndo_stop
= gelic_net_stop
,
1436 .ndo_start_xmit
= gelic_net_xmit
,
1437 .ndo_set_rx_mode
= gelic_net_set_multi
,
1438 .ndo_tx_timeout
= gelic_net_tx_timeout
,
1439 .ndo_set_mac_address
= eth_mac_addr
,
1440 .ndo_validate_addr
= eth_validate_addr
,
1441 #ifdef CONFIG_NET_POLL_CONTROLLER
1442 .ndo_poll_controller
= gelic_net_poll_controller
,
1447 * gelic_ether_setup_netdev_ops - initialization of net_device operations
1448 * @netdev: net_device structure
1449 * @napi: napi structure
1451 * fills out function pointers in the net_device structure
1453 static void gelic_ether_setup_netdev_ops(struct net_device
*netdev
,
1454 struct napi_struct
*napi
)
1456 netdev
->watchdog_timeo
= GELIC_NET_WATCHDOG_TIMEOUT
;
1458 netif_napi_add(netdev
, napi
, gelic_net_poll
);
1459 netdev
->ethtool_ops
= &gelic_ether_ethtool_ops
;
1460 netdev
->netdev_ops
= &gelic_netdevice_ops
;
1464 * gelic_net_setup_netdev - initialization of net_device
1465 * @netdev: net_device structure
1466 * @card: card structure
1468 * Returns 0 on success or <0 on failure
1470 * gelic_ether_setup_netdev initializes the net_device structure
1473 int gelic_net_setup_netdev(struct net_device
*netdev
, struct gelic_card
*card
)
1478 netdev
->hw_features
= NETIF_F_IP_CSUM
| NETIF_F_RXCSUM
;
1480 netdev
->features
= NETIF_F_IP_CSUM
;
1481 if (GELIC_CARD_RX_CSUM_DEFAULT
)
1482 netdev
->features
|= NETIF_F_RXCSUM
;
1484 status
= lv1_net_control(bus_id(card
), dev_id(card
),
1485 GELIC_LV1_GET_MAC_ADDRESS
,
1488 if (status
|| !is_valid_ether_addr((u8
*)&v1
)) {
1489 dev_info(ctodev(card
),
1490 "%s:lv1_net_control GET_MAC_ADDR failed %d\n",
1494 eth_hw_addr_set(netdev
, (u8
*)&v1
);
1496 if (card
->vlan_required
) {
1497 netdev
->hard_header_len
+= VLAN_HLEN
;
1499 * As vlan is internally used,
1500 * we can not receive vlan packets
1502 netdev
->features
|= NETIF_F_VLAN_CHALLENGED
;
1505 /* MTU range: 64 - 1518 */
1506 netdev
->min_mtu
= GELIC_NET_MIN_MTU
;
1507 netdev
->max_mtu
= GELIC_NET_MAX_MTU
;
1509 status
= register_netdev(netdev
);
1511 dev_err(ctodev(card
), "%s:Couldn't register %s %d\n",
1512 __func__
, netdev
->name
, status
);
1515 dev_info(ctodev(card
), "%s: MAC addr %pM\n",
1516 netdev
->name
, netdev
->dev_addr
);
1521 #define GELIC_ALIGN (32)
1524 * gelic_alloc_card_net - allocates net_device and card structure
1525 * @netdev: interface device structure
1527 * returns the card structure or NULL in case of errors
1529 * the card and net_device structures are linked to each other
1531 static struct gelic_card
*gelic_alloc_card_net(struct net_device
**netdev
)
1533 struct gelic_card
*card
;
1534 struct gelic_port
*port
;
1538 * gelic requires dma descriptor is 32 bytes aligned and
1539 * the hypervisor requires irq_status is 8 bytes aligned.
1541 BUILD_BUG_ON(offsetof(struct gelic_card
, irq_status
) % 8);
1542 BUILD_BUG_ON(offsetof(struct gelic_card
, descr
) % 32);
1544 sizeof(struct gelic_card
) +
1545 sizeof(struct gelic_descr
) * GELIC_NET_RX_DESCRIPTORS
+
1546 sizeof(struct gelic_descr
) * GELIC_NET_TX_DESCRIPTORS
+
1549 p
= kzalloc(alloc_size
, GFP_KERNEL
);
1552 card
= PTR_ALIGN(p
, GELIC_ALIGN
);
1558 *netdev
= alloc_etherdev(sizeof(struct gelic_port
));
1560 kfree(card
->unalign
);
1563 port
= netdev_priv(*netdev
);
1566 port
->netdev
= *netdev
;
1568 port
->type
= GELIC_PORT_ETHERNET_0
;
1571 card
->netdev
[GELIC_PORT_ETHERNET_0
] = *netdev
;
1573 INIT_WORK(&card
->tx_timeout_task
, gelic_net_tx_timeout_task
);
1574 init_waitqueue_head(&card
->waitq
);
1575 atomic_set(&card
->tx_timeout_task_counter
, 0);
1576 mutex_init(&card
->updown_lock
);
1577 atomic_set(&card
->users
, 0);
1582 static void gelic_card_get_vlan_info(struct gelic_card
*card
)
1591 [GELIC_PORT_ETHERNET_0
] = {
1592 .tx
= GELIC_LV1_VLAN_TX_ETHERNET_0
,
1593 .rx
= GELIC_LV1_VLAN_RX_ETHERNET_0
1595 [GELIC_PORT_WIRELESS
] = {
1596 .tx
= GELIC_LV1_VLAN_TX_WIRELESS
,
1597 .rx
= GELIC_LV1_VLAN_RX_WIRELESS
1601 for (i
= 0; i
< ARRAY_SIZE(vlan_id_ix
); i
++) {
1603 status
= lv1_net_control(bus_id(card
), dev_id(card
),
1604 GELIC_LV1_GET_VLAN_ID
,
1607 if (status
|| !v1
) {
1608 if (status
!= LV1_NO_ENTRY
)
1609 dev_dbg(ctodev(card
),
1610 "get vlan id for tx(%d) failed(%d)\n",
1611 vlan_id_ix
[i
].tx
, status
);
1612 card
->vlan
[i
].tx
= 0;
1613 card
->vlan
[i
].rx
= 0;
1616 card
->vlan
[i
].tx
= (u16
)v1
;
1619 status
= lv1_net_control(bus_id(card
), dev_id(card
),
1620 GELIC_LV1_GET_VLAN_ID
,
1623 if (status
|| !v1
) {
1624 if (status
!= LV1_NO_ENTRY
)
1625 dev_info(ctodev(card
),
1626 "get vlan id for rx(%d) failed(%d)\n",
1627 vlan_id_ix
[i
].rx
, status
);
1628 card
->vlan
[i
].tx
= 0;
1629 card
->vlan
[i
].rx
= 0;
1632 card
->vlan
[i
].rx
= (u16
)v1
;
1634 dev_dbg(ctodev(card
), "vlan_id[%d] tx=%02x rx=%02x\n",
1635 i
, card
->vlan
[i
].tx
, card
->vlan
[i
].rx
);
1638 if (card
->vlan
[GELIC_PORT_ETHERNET_0
].tx
) {
1639 BUG_ON(!card
->vlan
[GELIC_PORT_WIRELESS
].tx
);
1640 card
->vlan_required
= 1;
1642 card
->vlan_required
= 0;
1644 /* check wirelss capable firmware */
1645 if (ps3_compare_firmware_version(1, 6, 0) < 0) {
1646 card
->vlan
[GELIC_PORT_WIRELESS
].tx
= 0;
1647 card
->vlan
[GELIC_PORT_WIRELESS
].rx
= 0;
1650 dev_info(ctodev(card
), "internal vlan %s\n",
1651 card
->vlan_required
? "enabled" : "disabled");
1654 * ps3_gelic_driver_probe - add a device to the control of this driver
1656 static int ps3_gelic_driver_probe(struct ps3_system_bus_device
*dev
)
1658 struct gelic_card
*card
;
1659 struct net_device
*netdev
;
1662 pr_debug("%s: called\n", __func__
);
1664 udbg_shutdown_ps3gelic();
1666 result
= ps3_open_hv_device(dev
);
1669 dev_dbg(&dev
->core
, "%s:ps3_open_hv_device failed\n",
1674 result
= ps3_dma_region_create(dev
->d_region
);
1677 dev_dbg(&dev
->core
, "%s:ps3_dma_region_create failed(%d)\n",
1679 BUG_ON("check region type");
1680 goto fail_dma_region
;
1683 /* alloc card/netdevice */
1684 card
= gelic_alloc_card_net(&netdev
);
1686 dev_info(&dev
->core
, "%s:gelic_net_alloc_card failed\n",
1689 goto fail_alloc_card
;
1691 ps3_system_bus_set_drvdata(dev
, card
);
1694 /* get internal vlan info */
1695 gelic_card_get_vlan_info(card
);
1697 card
->link_mode
= GELIC_LV1_ETHER_AUTO_NEG
;
1699 /* setup interrupt */
1700 result
= lv1_net_set_interrupt_status_indicator(bus_id(card
),
1702 ps3_mm_phys_to_lpar(__pa(&card
->irq_status
)),
1707 "%s:set_interrupt_status_indicator failed: %s\n",
1708 __func__
, ps3_result(result
));
1710 goto fail_status_indicator
;
1713 result
= ps3_sb_event_receive_port_setup(dev
, PS3_BINDING_CPU_ANY
,
1717 dev_info(ctodev(card
),
1718 "%s:gelic_net_open_device failed (%d)\n",
1721 goto fail_alloc_irq
;
1723 result
= request_irq(card
->irq
, gelic_card_interrupt
,
1724 0, netdev
->name
, card
);
1727 dev_info(ctodev(card
), "%s:request_irq failed (%d)\n",
1729 goto fail_request_irq
;
1732 /* setup card structure */
1733 card
->irq_mask
= GELIC_CARD_RXINT
| GELIC_CARD_TXINT
|
1734 GELIC_CARD_PORT_STATUS_CHANGED
;
1737 result
= gelic_card_init_chain(card
, &card
->tx_chain
,
1738 card
->descr
, GELIC_NET_TX_DESCRIPTORS
);
1741 result
= gelic_card_init_chain(card
, &card
->rx_chain
,
1742 card
->descr
+ GELIC_NET_TX_DESCRIPTORS
,
1743 GELIC_NET_RX_DESCRIPTORS
);
1748 card
->tx_top
= card
->tx_chain
.head
;
1749 card
->rx_top
= card
->rx_chain
.head
;
1750 dev_dbg(ctodev(card
), "descr rx %p, tx %p, size %#lx, num %#x\n",
1751 card
->rx_top
, card
->tx_top
, sizeof(struct gelic_descr
),
1752 GELIC_NET_RX_DESCRIPTORS
);
1753 /* allocate rx skbs */
1754 result
= gelic_card_alloc_rx_skbs(card
);
1756 goto fail_alloc_skbs
;
1758 spin_lock_init(&card
->tx_lock
);
1759 card
->tx_dma_progress
= 0;
1761 /* setup net_device structure */
1762 netdev
->irq
= card
->irq
;
1763 SET_NETDEV_DEV(netdev
, &card
->dev
->core
);
1764 gelic_ether_setup_netdev_ops(netdev
, &card
->napi
);
1765 result
= gelic_net_setup_netdev(netdev
, card
);
1767 dev_dbg(&dev
->core
, "%s: setup_netdev failed %d\n",
1769 goto fail_setup_netdev
;
1772 #ifdef CONFIG_GELIC_WIRELESS
1773 result
= gelic_wl_driver_probe(card
);
1775 dev_dbg(&dev
->core
, "%s: WL init failed\n", __func__
);
1776 goto fail_setup_netdev
;
1779 pr_debug("%s: done\n", __func__
);
1784 gelic_card_free_chain(card
, card
->rx_chain
.head
);
1786 gelic_card_free_chain(card
, card
->tx_chain
.head
);
1788 free_irq(card
->irq
, card
);
1791 ps3_sb_event_receive_port_destroy(dev
, card
->irq
);
1793 lv1_net_set_interrupt_status_indicator(bus_id(card
),
1796 fail_status_indicator
:
1797 ps3_system_bus_set_drvdata(dev
, NULL
);
1798 kfree(netdev_card(netdev
)->unalign
);
1799 free_netdev(netdev
);
1801 ps3_dma_region_free(dev
->d_region
);
1803 ps3_close_hv_device(dev
);
1809 * ps3_gelic_driver_remove - remove a device from the control of this driver
1812 static void ps3_gelic_driver_remove(struct ps3_system_bus_device
*dev
)
1814 struct gelic_card
*card
= ps3_system_bus_get_drvdata(dev
);
1815 struct net_device
*netdev0
;
1816 pr_debug("%s: called\n", __func__
);
1818 /* set auto-negotiation */
1819 gelic_card_set_link_mode(card
, GELIC_LV1_ETHER_AUTO_NEG
);
1821 #ifdef CONFIG_GELIC_WIRELESS
1822 gelic_wl_driver_remove(card
);
1824 /* stop interrupt */
1825 gelic_card_set_irq_mask(card
, 0);
1827 /* turn off DMA, force end */
1828 gelic_card_disable_rxdmac(card
);
1829 gelic_card_disable_txdmac(card
);
1831 /* release chains */
1832 gelic_card_release_tx_chain(card
, 1);
1833 gelic_card_release_rx_chain(card
);
1835 gelic_card_free_chain(card
, card
->tx_top
);
1836 gelic_card_free_chain(card
, card
->rx_top
);
1838 netdev0
= card
->netdev
[GELIC_PORT_ETHERNET_0
];
1839 /* disconnect event port */
1840 free_irq(card
->irq
, card
);
1842 ps3_sb_event_receive_port_destroy(card
->dev
, card
->irq
);
1844 wait_event(card
->waitq
,
1845 atomic_read(&card
->tx_timeout_task_counter
) == 0);
1847 lv1_net_set_interrupt_status_indicator(bus_id(card
), dev_id(card
),
1850 unregister_netdev(netdev0
);
1851 kfree(netdev_card(netdev0
)->unalign
);
1852 free_netdev(netdev0
);
1854 ps3_system_bus_set_drvdata(dev
, NULL
);
1856 ps3_dma_region_free(dev
->d_region
);
1858 ps3_close_hv_device(dev
);
1860 pr_debug("%s: done\n", __func__
);
1863 static struct ps3_system_bus_driver ps3_gelic_driver
= {
1864 .match_id
= PS3_MATCH_ID_GELIC
,
1865 .probe
= ps3_gelic_driver_probe
,
1866 .remove
= ps3_gelic_driver_remove
,
1867 .shutdown
= ps3_gelic_driver_remove
,
1868 .core
.name
= "ps3_gelic_driver",
1869 .core
.owner
= THIS_MODULE
,
1872 static int __init
ps3_gelic_driver_init (void)
1874 return firmware_has_feature(FW_FEATURE_PS3_LV1
)
1875 ? ps3_system_bus_driver_register(&ps3_gelic_driver
)
1879 static void __exit
ps3_gelic_driver_exit (void)
1881 ps3_system_bus_driver_unregister(&ps3_gelic_driver
);
1884 module_init(ps3_gelic_driver_init
);
1885 module_exit(ps3_gelic_driver_exit
);
1887 MODULE_ALIAS(PS3_MODULE_ALIAS_GELIC
);