blob: 0c768593aad00aa24708f949ab16cceecdce40ac [file] [log] [blame]
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001/*
2 * PS3 gelic network driver.
3 *
4 * Copyright (C) 2007 Sony Computer Entertainment Inc.
5 * Copyright 2006, 2007 Sony Corporation
6 *
7 * This file is based on: spider_net.c
8 *
9 * (C) Copyright IBM Corp. 2005
10 *
11 * Authors : Utz Bacher <utz.bacher@de.ibm.com>
12 * Jens Osterkamp <Jens.Osterkamp@de.ibm.com>
13 *
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)
17 * any later version.
18 *
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.
23 *
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.
27 */
28
29#undef DEBUG
30
31#include <linux/kernel.h>
32#include <linux/module.h>
33
34#include <linux/etherdevice.h>
35#include <linux/ethtool.h>
36#include <linux/if_vlan.h>
37
38#include <linux/in.h>
39#include <linux/ip.h>
40#include <linux/tcp.h>
41
42#include <linux/dma-mapping.h>
43#include <net/checksum.h>
44#include <asm/firmware.h>
45#include <asm/ps3.h>
46#include <asm/lv1call.h>
47
48#include "ps3_gelic_net.h"
Masakazu Mokuno09dde542008-02-07 19:58:57 +090049#include "ps3_gelic_wireless.h"
Masakazu Mokuno02c18892007-07-05 20:11:16 +090050
51#define DRV_NAME "Gelic Network Driver"
Masakazu Mokuno09dde542008-02-07 19:58:57 +090052#define DRV_VERSION "2.0"
Masakazu Mokuno02c18892007-07-05 20:11:16 +090053
54MODULE_AUTHOR("SCE Inc.");
55MODULE_DESCRIPTION("Gelic Network driver");
56MODULE_LICENSE("GPL");
57
Masakazu Mokuno589866f2008-02-07 19:58:42 +090058
59static inline void gelic_card_enable_rxdmac(struct gelic_card *card);
60static inline void gelic_card_disable_rxdmac(struct gelic_card *card);
61static inline void gelic_card_disable_txdmac(struct gelic_card *card);
62static inline void gelic_card_reset_chain(struct gelic_card *card,
63 struct gelic_descr_chain *chain,
64 struct gelic_descr *start_descr);
Masakazu Mokuno02c18892007-07-05 20:11:16 +090065
66/* set irq_mask */
Masakazu Mokuno589866f2008-02-07 19:58:42 +090067int gelic_card_set_irq_mask(struct gelic_card *card, u64 mask)
Masakazu Mokuno02c18892007-07-05 20:11:16 +090068{
69 int status;
70
71 status = lv1_net_set_interrupt_mask(bus_id(card), dev_id(card),
72 mask, 0);
73 if (status)
74 dev_info(ctodev(card),
Masakazu Mokuno589866f2008-02-07 19:58:42 +090075 "%s failed %d\n", __func__, status);
Masakazu Mokuno02c18892007-07-05 20:11:16 +090076 return status;
77}
Masakazu Mokuno589866f2008-02-07 19:58:42 +090078
Masakazu Mokuno59e97322008-02-07 19:58:08 +090079static inline void gelic_card_rx_irq_on(struct gelic_card *card)
Masakazu Mokuno02c18892007-07-05 20:11:16 +090080{
Masakazu Mokuno589866f2008-02-07 19:58:42 +090081 card->irq_mask |= GELIC_CARD_RXINT;
82 gelic_card_set_irq_mask(card, card->irq_mask);
Masakazu Mokuno02c18892007-07-05 20:11:16 +090083}
Masakazu Mokuno59e97322008-02-07 19:58:08 +090084static inline void gelic_card_rx_irq_off(struct gelic_card *card)
Masakazu Mokuno02c18892007-07-05 20:11:16 +090085{
Masakazu Mokuno589866f2008-02-07 19:58:42 +090086 card->irq_mask &= ~GELIC_CARD_RXINT;
87 gelic_card_set_irq_mask(card, card->irq_mask);
Masakazu Mokuno02c18892007-07-05 20:11:16 +090088}
Masakazu Mokuno01fed4c2008-02-07 19:58:32 +090089
Masakazu Mokuno589866f2008-02-07 19:58:42 +090090static void gelic_card_get_ether_port_status(struct gelic_card *card,
91 int inform)
Masakazu Mokuno01fed4c2008-02-07 19:58:32 +090092{
93 u64 v2;
94 struct net_device *ether_netdev;
95
96 lv1_net_control(bus_id(card), dev_id(card),
97 GELIC_LV1_GET_ETH_PORT_STATUS,
Geoff Levandd4d7f1f2009-12-01 12:15:53 +000098 GELIC_LV1_VLAN_TX_ETHERNET_0, 0, 0,
Masakazu Mokuno01fed4c2008-02-07 19:58:32 +090099 &card->ether_port_status, &v2);
100
101 if (inform) {
Geoff Levandd4d7f1f2009-12-01 12:15:53 +0000102 ether_netdev = card->netdev[GELIC_PORT_ETHERNET_0];
Masakazu Mokuno01fed4c2008-02-07 19:58:32 +0900103 if (card->ether_port_status & GELIC_LV1_ETHER_LINK_UP)
104 netif_carrier_on(ether_netdev);
105 else
106 netif_carrier_off(ether_netdev);
107 }
108}
109
Hideyuki Sasaki55873ed2009-12-01 12:15:58 +0000110static int gelic_card_set_link_mode(struct gelic_card *card, int mode)
111{
112 int status;
113 u64 v1, v2;
114
115 status = lv1_net_control(bus_id(card), dev_id(card),
116 GELIC_LV1_SET_NEGOTIATION_MODE,
117 GELIC_LV1_PHY_ETHERNET_0, mode, 0, &v1, &v2);
118 if (status) {
119 pr_info("%s: failed setting negotiation mode %d\n", __func__,
120 status);
121 return -EBUSY;
122 }
123
124 card->link_mode = mode;
125 return 0;
126}
127
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900128void gelic_card_up(struct gelic_card *card)
129{
130 pr_debug("%s: called\n", __func__);
Daniel Walker2914f3e2008-05-22 00:00:03 -0700131 mutex_lock(&card->updown_lock);
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900132 if (atomic_inc_return(&card->users) == 1) {
133 pr_debug("%s: real do\n", __func__);
134 /* enable irq */
135 gelic_card_set_irq_mask(card, card->irq_mask);
136 /* start rx */
137 gelic_card_enable_rxdmac(card);
138
139 napi_enable(&card->napi);
140 }
Daniel Walker2914f3e2008-05-22 00:00:03 -0700141 mutex_unlock(&card->updown_lock);
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900142 pr_debug("%s: done\n", __func__);
143}
144
145void gelic_card_down(struct gelic_card *card)
146{
147 u64 mask;
148 pr_debug("%s: called\n", __func__);
Daniel Walker2914f3e2008-05-22 00:00:03 -0700149 mutex_lock(&card->updown_lock);
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900150 if (atomic_dec_if_positive(&card->users) == 0) {
151 pr_debug("%s: real do\n", __func__);
152 napi_disable(&card->napi);
153 /*
154 * Disable irq. Wireless interrupts will
155 * be disabled later if any
156 */
157 mask = card->irq_mask & (GELIC_CARD_WLAN_EVENT_RECEIVED |
158 GELIC_CARD_WLAN_COMMAND_COMPLETED);
159 gelic_card_set_irq_mask(card, mask);
160 /* stop rx */
161 gelic_card_disable_rxdmac(card);
162 gelic_card_reset_chain(card, &card->rx_chain,
163 card->descr + GELIC_NET_TX_DESCRIPTORS);
164 /* stop tx */
165 gelic_card_disable_txdmac(card);
166 }
Daniel Walker2914f3e2008-05-22 00:00:03 -0700167 mutex_unlock(&card->updown_lock);
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900168 pr_debug("%s: done\n", __func__);
169}
Masakazu Mokuno01fed4c2008-02-07 19:58:32 +0900170
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900171/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900172 * gelic_descr_get_status -- returns the status of a descriptor
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900173 * @descr: descriptor to look at
174 *
175 * returns the status as in the dmac_cmd_status field of the descriptor
176 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900177static enum gelic_descr_dma_status
178gelic_descr_get_status(struct gelic_descr *descr)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900179{
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900180 return be32_to_cpu(descr->dmac_cmd_status) & GELIC_DESCR_DMA_STAT_MASK;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900181}
182
183/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900184 * gelic_descr_set_status -- sets the status of a descriptor
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900185 * @descr: descriptor to change
186 * @status: status to set in the descriptor
187 *
188 * changes the status to the specified value. Doesn't change other bits
189 * in the status
190 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900191static void gelic_descr_set_status(struct gelic_descr *descr,
192 enum gelic_descr_dma_status status)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900193{
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900194 descr->dmac_cmd_status = cpu_to_be32(status |
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900195 (be32_to_cpu(descr->dmac_cmd_status) &
196 ~GELIC_DESCR_DMA_STAT_MASK));
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900197 /*
198 * dma_cmd_status field is used to indicate whether the descriptor
199 * is valid or not.
200 * Usually caller of this function wants to inform that to the
201 * hardware, so we assure here the hardware sees the change.
202 */
203 wmb();
204}
205
206/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900207 * gelic_card_free_chain - free descriptor chain
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900208 * @card: card structure
209 * @descr_in: address of desc
210 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900211static void gelic_card_free_chain(struct gelic_card *card,
212 struct gelic_descr *descr_in)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900213{
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900214 struct gelic_descr *descr;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900215
216 for (descr = descr_in; descr && descr->bus_addr; descr = descr->next) {
217 dma_unmap_single(ctodev(card), descr->bus_addr,
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900218 GELIC_DESCR_SIZE, DMA_BIDIRECTIONAL);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900219 descr->bus_addr = 0;
220 }
221}
222
223/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900224 * gelic_card_init_chain - links descriptor chain
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900225 * @card: card structure
226 * @chain: address of chain
227 * @start_descr: address of descriptor array
228 * @no: number of descriptors
229 *
230 * we manage a circular list that mirrors the hardware structure,
231 * except that the hardware uses bus addresses.
232 *
233 * returns 0 on success, <0 on failure
234 */
Geert Uytterhoeven48dce822009-06-10 04:38:58 +0000235static int __devinit gelic_card_init_chain(struct gelic_card *card,
236 struct gelic_descr_chain *chain,
237 struct gelic_descr *start_descr,
238 int no)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900239{
240 int i;
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900241 struct gelic_descr *descr;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900242
243 descr = start_descr;
244 memset(descr, 0, sizeof(*descr) * no);
245
246 /* set up the hardware pointers in each descriptor */
247 for (i = 0; i < no; i++, descr++) {
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900248 gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900249 descr->bus_addr =
250 dma_map_single(ctodev(card), descr,
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900251 GELIC_DESCR_SIZE,
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900252 DMA_BIDIRECTIONAL);
253
254 if (!descr->bus_addr)
255 goto iommu_error;
256
257 descr->next = descr + 1;
258 descr->prev = descr - 1;
259 }
260 /* make them as ring */
261 (descr - 1)->next = start_descr;
262 start_descr->prev = (descr - 1);
263
264 /* chain bus addr of hw descriptor */
265 descr = start_descr;
266 for (i = 0; i < no; i++, descr++) {
Masakazu Mokuno100e1d82008-02-07 19:57:54 +0900267 descr->next_descr_addr = cpu_to_be32(descr->next->bus_addr);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900268 }
269
270 chain->head = start_descr;
271 chain->tail = start_descr;
272
273 /* do not chain last hw descriptor */
274 (descr - 1)->next_descr_addr = 0;
275
276 return 0;
277
278iommu_error:
279 for (i--, descr--; 0 <= i; i--, descr--)
280 if (descr->bus_addr)
281 dma_unmap_single(ctodev(card), descr->bus_addr,
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900282 GELIC_DESCR_SIZE,
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900283 DMA_BIDIRECTIONAL);
284 return -ENOMEM;
285}
286
287/**
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900288 * gelic_card_reset_chain - reset status of a descriptor chain
289 * @card: card structure
290 * @chain: address of chain
291 * @start_descr: address of descriptor array
292 *
293 * Reset the status of dma descriptors to ready state
294 * and re-initialize the hardware chain for later use
295 */
296static void gelic_card_reset_chain(struct gelic_card *card,
297 struct gelic_descr_chain *chain,
298 struct gelic_descr *start_descr)
299{
300 struct gelic_descr *descr;
301
302 for (descr = start_descr; start_descr != descr->next; descr++) {
303 gelic_descr_set_status(descr, GELIC_DESCR_DMA_CARDOWNED);
304 descr->next_descr_addr = cpu_to_be32(descr->next->bus_addr);
305 }
306
307 chain->head = start_descr;
308 chain->tail = (descr - 1);
309
310 (descr - 1)->next_descr_addr = 0;
311}
312/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900313 * gelic_descr_prepare_rx - reinitializes a rx descriptor
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900314 * @card: card structure
315 * @descr: descriptor to re-init
316 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200317 * return 0 on success, <0 on failure
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900318 *
319 * allocates a new rx skb, iommu-maps it and attaches it to the descriptor.
320 * Activate the descriptor state-wise
321 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900322static int gelic_descr_prepare_rx(struct gelic_card *card,
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900323 struct gelic_descr *descr)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900324{
325 int offset;
326 unsigned int bufsize;
327
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900328 if (gelic_descr_get_status(descr) != GELIC_DESCR_DMA_NOT_IN_USE)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900329 dev_info(ctodev(card), "%s: ERROR status \n", __func__);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900330 /* we need to round up the buffer size to a multiple of 128 */
331 bufsize = ALIGN(GELIC_NET_MAX_MTU, GELIC_NET_RXBUF_ALIGN);
332
333 /* and we need to have it 128 byte aligned, therefore we allocate a
334 * bit more */
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900335 descr->skb = dev_alloc_skb(bufsize + GELIC_NET_RXBUF_ALIGN - 1);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900336 if (!descr->skb) {
337 descr->buf_addr = 0; /* tell DMAC don't touch memory */
338 dev_info(ctodev(card),
339 "%s:allocate skb failed !!\n", __func__);
340 return -ENOMEM;
341 }
Masakazu Mokuno100e1d82008-02-07 19:57:54 +0900342 descr->buf_size = cpu_to_be32(bufsize);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900343 descr->dmac_cmd_status = 0;
344 descr->result_size = 0;
345 descr->valid_size = 0;
346 descr->data_error = 0;
347
348 offset = ((unsigned long)descr->skb->data) &
349 (GELIC_NET_RXBUF_ALIGN - 1);
350 if (offset)
351 skb_reserve(descr->skb, GELIC_NET_RXBUF_ALIGN - offset);
352 /* io-mmu-map the skb */
Masakazu Mokuno100e1d82008-02-07 19:57:54 +0900353 descr->buf_addr = cpu_to_be32(dma_map_single(ctodev(card),
354 descr->skb->data,
355 GELIC_NET_MAX_MTU,
356 DMA_FROM_DEVICE));
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900357 if (!descr->buf_addr) {
358 dev_kfree_skb_any(descr->skb);
359 descr->skb = NULL;
360 dev_info(ctodev(card),
361 "%s:Could not iommu-map rx buffer\n", __func__);
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900362 gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900363 return -ENOMEM;
364 } else {
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900365 gelic_descr_set_status(descr, GELIC_DESCR_DMA_CARDOWNED);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900366 return 0;
367 }
368}
369
370/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900371 * gelic_card_release_rx_chain - free all skb of rx descr
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900372 * @card: card structure
373 *
374 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900375static void gelic_card_release_rx_chain(struct gelic_card *card)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900376{
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900377 struct gelic_descr *descr = card->rx_chain.head;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900378
379 do {
380 if (descr->skb) {
381 dma_unmap_single(ctodev(card),
Masakazu Mokuno100e1d82008-02-07 19:57:54 +0900382 be32_to_cpu(descr->buf_addr),
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900383 descr->skb->len,
384 DMA_FROM_DEVICE);
385 descr->buf_addr = 0;
386 dev_kfree_skb_any(descr->skb);
387 descr->skb = NULL;
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900388 gelic_descr_set_status(descr,
389 GELIC_DESCR_DMA_NOT_IN_USE);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900390 }
391 descr = descr->next;
392 } while (descr != card->rx_chain.head);
393}
394
395/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900396 * gelic_card_fill_rx_chain - fills descriptors/skbs in the rx chains
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900397 * @card: card structure
398 *
399 * fills all descriptors in the rx chain: allocates skbs
400 * and iommu-maps them.
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900401 * returns 0 on success, < 0 on failure
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900402 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900403static int gelic_card_fill_rx_chain(struct gelic_card *card)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900404{
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900405 struct gelic_descr *descr = card->rx_chain.head;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900406 int ret;
407
408 do {
409 if (!descr->skb) {
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900410 ret = gelic_descr_prepare_rx(card, descr);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900411 if (ret)
412 goto rewind;
413 }
414 descr = descr->next;
415 } while (descr != card->rx_chain.head);
416
417 return 0;
418rewind:
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900419 gelic_card_release_rx_chain(card);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900420 return ret;
421}
422
423/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900424 * gelic_card_alloc_rx_skbs - allocates rx skbs in rx descriptor chains
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900425 * @card: card structure
426 *
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900427 * returns 0 on success, < 0 on failure
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900428 */
Geert Uytterhoeven48dce822009-06-10 04:38:58 +0000429static int __devinit gelic_card_alloc_rx_skbs(struct gelic_card *card)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900430{
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900431 struct gelic_descr_chain *chain;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900432 int ret;
433 chain = &card->rx_chain;
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900434 ret = gelic_card_fill_rx_chain(card);
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900435 chain->tail = card->rx_top->prev; /* point to the last */
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900436 return ret;
437}
438
439/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900440 * gelic_descr_release_tx - processes a used tx descriptor
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900441 * @card: card structure
442 * @descr: descriptor to release
443 *
444 * releases a used tx descriptor (unmapping, freeing of skb)
445 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900446static void gelic_descr_release_tx(struct gelic_card *card,
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900447 struct gelic_descr *descr)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900448{
Masakazu Mokuno173261e2007-08-31 22:22:32 +0900449 struct sk_buff *skb = descr->skb;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900450
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900451 BUG_ON(!(be32_to_cpu(descr->data_status) & GELIC_DESCR_TX_TAIL));
452
453 dma_unmap_single(ctodev(card), be32_to_cpu(descr->buf_addr), skb->len,
454 DMA_TO_DEVICE);
Masakazu Mokuno173261e2007-08-31 22:22:32 +0900455 dev_kfree_skb_any(skb);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900456
457 descr->buf_addr = 0;
458 descr->buf_size = 0;
459 descr->next_descr_addr = 0;
460 descr->result_size = 0;
461 descr->valid_size = 0;
462 descr->data_status = 0;
463 descr->data_error = 0;
464 descr->skb = NULL;
465
466 /* set descr status */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900467 gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900468}
469
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900470static void gelic_card_stop_queues(struct gelic_card *card)
471{
Geoff Levandd4d7f1f2009-12-01 12:15:53 +0000472 netif_stop_queue(card->netdev[GELIC_PORT_ETHERNET_0]);
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900473
474 if (card->netdev[GELIC_PORT_WIRELESS])
475 netif_stop_queue(card->netdev[GELIC_PORT_WIRELESS]);
476}
477static void gelic_card_wake_queues(struct gelic_card *card)
478{
Geoff Levandd4d7f1f2009-12-01 12:15:53 +0000479 netif_wake_queue(card->netdev[GELIC_PORT_ETHERNET_0]);
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900480
481 if (card->netdev[GELIC_PORT_WIRELESS])
482 netif_wake_queue(card->netdev[GELIC_PORT_WIRELESS]);
483}
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900484/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900485 * gelic_card_release_tx_chain - processes sent tx descriptors
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900486 * @card: adapter structure
487 * @stop: net_stop sequence
488 *
489 * releases the tx descriptors that gelic has finished with
490 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900491static void gelic_card_release_tx_chain(struct gelic_card *card, int stop)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900492{
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900493 struct gelic_descr_chain *tx_chain;
494 enum gelic_descr_dma_status status;
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900495 struct net_device *netdev;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900496 int release = 0;
497
498 for (tx_chain = &card->tx_chain;
499 tx_chain->head != tx_chain->tail && tx_chain->tail;
500 tx_chain->tail = tx_chain->tail->next) {
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900501 status = gelic_descr_get_status(tx_chain->tail);
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900502 netdev = tx_chain->tail->skb->dev;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900503 switch (status) {
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900504 case GELIC_DESCR_DMA_RESPONSE_ERROR:
505 case GELIC_DESCR_DMA_PROTECTION_ERROR:
506 case GELIC_DESCR_DMA_FORCE_END:
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900507 if (printk_ratelimit())
508 dev_info(ctodev(card),
509 "%s: forcing end of tx descriptor " \
510 "with status %x\n",
511 __func__, status);
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900512 netdev->stats.tx_dropped++;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900513 break;
514
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900515 case GELIC_DESCR_DMA_COMPLETE:
Masakazu Mokuno48544cc262007-07-20 17:24:56 +0900516 if (tx_chain->tail->skb) {
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900517 netdev->stats.tx_packets++;
518 netdev->stats.tx_bytes +=
Masakazu Mokuno48544cc262007-07-20 17:24:56 +0900519 tx_chain->tail->skb->len;
520 }
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900521 break;
522
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900523 case GELIC_DESCR_DMA_CARDOWNED:
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900524 /* pending tx request */
525 default:
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900526 /* any other value (== GELIC_DESCR_DMA_NOT_IN_USE) */
Masakazu Mokuno48544cc262007-07-20 17:24:56 +0900527 if (!stop)
528 goto out;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900529 }
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900530 gelic_descr_release_tx(card, tx_chain->tail);
Masakazu Mokuno48544cc262007-07-20 17:24:56 +0900531 release ++;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900532 }
533out:
Masakazu Mokuno173261e2007-08-31 22:22:32 +0900534 if (!stop && release)
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900535 gelic_card_wake_queues(card);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900536}
537
538/**
539 * gelic_net_set_multi - sets multicast addresses and promisc flags
540 * @netdev: interface device structure
541 *
542 * gelic_net_set_multi configures multicast addresses as needed for the
543 * netdev interface. It also sets up multicast, allmulti and promisc
544 * flags appropriately
545 */
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900546void gelic_net_set_multi(struct net_device *netdev)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900547{
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900548 struct gelic_card *card = netdev_card(netdev);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900549 struct dev_mc_list *mc;
550 unsigned int i;
551 uint8_t *p;
552 u64 addr;
553 int status;
554
555 /* clear all multicast address */
556 status = lv1_net_remove_multicast_address(bus_id(card), dev_id(card),
557 0, 1);
558 if (status)
559 dev_err(ctodev(card),
560 "lv1_net_remove_multicast_address failed %d\n",
561 status);
562 /* set broadcast address */
563 status = lv1_net_add_multicast_address(bus_id(card), dev_id(card),
564 GELIC_NET_BROADCAST_ADDR, 0);
565 if (status)
566 dev_err(ctodev(card),
567 "lv1_net_add_multicast_address failed, %d\n",
568 status);
569
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900570 if ((netdev->flags & IFF_ALLMULTI) ||
571 (netdev->mc_count > GELIC_NET_MC_COUNT_MAX)) {
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900572 status = lv1_net_add_multicast_address(bus_id(card),
573 dev_id(card),
574 0, 1);
575 if (status)
576 dev_err(ctodev(card),
577 "lv1_net_add_multicast_address failed, %d\n",
578 status);
579 return;
580 }
581
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900582 /* set multicast addresses */
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900583 for (mc = netdev->mc_list; mc; mc = mc->next) {
584 addr = 0;
585 p = mc->dmi_addr;
586 for (i = 0; i < ETH_ALEN; i++) {
587 addr <<= 8;
588 addr |= *p++;
589 }
590 status = lv1_net_add_multicast_address(bus_id(card),
591 dev_id(card),
592 addr, 0);
593 if (status)
594 dev_err(ctodev(card),
595 "lv1_net_add_multicast_address failed, %d\n",
596 status);
597 }
598}
599
600/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900601 * gelic_card_enable_rxdmac - enables the receive DMA controller
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900602 * @card: card structure
603 *
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900604 * gelic_card_enable_rxdmac enables the DMA controller by setting RX_DMA_EN
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900605 * in the GDADMACCNTR register
606 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900607static inline void gelic_card_enable_rxdmac(struct gelic_card *card)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900608{
609 int status;
610
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900611#ifdef DEBUG
612 if (gelic_descr_get_status(card->rx_chain.head) !=
613 GELIC_DESCR_DMA_CARDOWNED) {
614 printk(KERN_ERR "%s: status=%x\n", __func__,
615 be32_to_cpu(card->rx_chain.head->dmac_cmd_status));
616 printk(KERN_ERR "%s: nextphy=%x\n", __func__,
617 be32_to_cpu(card->rx_chain.head->next_descr_addr));
618 printk(KERN_ERR "%s: head=%p\n", __func__,
619 card->rx_chain.head);
620 }
621#endif
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900622 status = lv1_net_start_rx_dma(bus_id(card), dev_id(card),
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900623 card->rx_chain.head->bus_addr, 0);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900624 if (status)
625 dev_info(ctodev(card),
626 "lv1_net_start_rx_dma failed, status=%d\n", status);
627}
628
629/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900630 * gelic_card_disable_rxdmac - disables the receive DMA controller
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900631 * @card: card structure
632 *
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900633 * gelic_card_disable_rxdmac terminates processing on the DMA controller by
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900634 * turing off DMA and issueing a force end
635 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900636static inline void gelic_card_disable_rxdmac(struct gelic_card *card)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900637{
638 int status;
639
640 /* this hvc blocks until the DMA in progress really stopped */
641 status = lv1_net_stop_rx_dma(bus_id(card), dev_id(card), 0);
642 if (status)
643 dev_err(ctodev(card),
644 "lv1_net_stop_rx_dma faild, %d\n", status);
645}
646
647/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900648 * gelic_card_disable_txdmac - disables the transmit DMA controller
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900649 * @card: card structure
650 *
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900651 * gelic_card_disable_txdmac terminates processing on the DMA controller by
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900652 * turing off DMA and issueing a force end
653 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900654static inline void gelic_card_disable_txdmac(struct gelic_card *card)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900655{
656 int status;
657
658 /* this hvc blocks until the DMA in progress really stopped */
659 status = lv1_net_stop_tx_dma(bus_id(card), dev_id(card), 0);
660 if (status)
661 dev_err(ctodev(card),
662 "lv1_net_stop_tx_dma faild, status=%d\n", status);
663}
664
665/**
666 * gelic_net_stop - called upon ifconfig down
667 * @netdev: interface device structure
668 *
669 * always returns 0
670 */
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900671int gelic_net_stop(struct net_device *netdev)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900672{
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900673 struct gelic_card *card;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900674
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900675 pr_debug("%s: start\n", __func__);
676
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900677 netif_stop_queue(netdev);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900678 netif_carrier_off(netdev);
679
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900680 card = netdev_card(netdev);
681 gelic_card_down(card);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900682
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900683 pr_debug("%s: done\n", __func__);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900684 return 0;
685}
686
687/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900688 * gelic_card_get_next_tx_descr - returns the next available tx descriptor
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900689 * @card: device structure to get descriptor from
690 *
691 * returns the address of the next descriptor, or NULL if not available.
692 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900693static struct gelic_descr *
694gelic_card_get_next_tx_descr(struct gelic_card *card)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900695{
696 if (!card->tx_chain.head)
697 return NULL;
Masakazu Mokuno173261e2007-08-31 22:22:32 +0900698 /* see if the next descriptor is free */
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900699 if (card->tx_chain.tail != card->tx_chain.head->next &&
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900700 gelic_descr_get_status(card->tx_chain.head) ==
701 GELIC_DESCR_DMA_NOT_IN_USE)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900702 return card->tx_chain.head;
703 else
704 return NULL;
705
706}
707
708/**
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900709 * gelic_net_set_txdescr_cmdstat - sets the tx descriptor command field
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900710 * @descr: descriptor structure to fill out
711 * @skb: packet to consider
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900712 *
713 * fills out the command and status field of the descriptor structure,
714 * depending on hardware checksum settings. This function assumes a wmb()
715 * has executed before.
716 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900717static void gelic_descr_set_tx_cmdstat(struct gelic_descr *descr,
718 struct sk_buff *skb)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900719{
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900720 if (skb->ip_summed != CHECKSUM_PARTIAL)
Masakazu Mokuno100e1d82008-02-07 19:57:54 +0900721 descr->dmac_cmd_status =
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900722 cpu_to_be32(GELIC_DESCR_DMA_CMD_NO_CHKSUM |
723 GELIC_DESCR_TX_DMA_FRAME_TAIL);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900724 else {
725 /* is packet ip?
726 * if yes: tcp? udp? */
727 if (skb->protocol == htons(ETH_P_IP)) {
728 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
729 descr->dmac_cmd_status =
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900730 cpu_to_be32(GELIC_DESCR_DMA_CMD_TCP_CHKSUM |
731 GELIC_DESCR_TX_DMA_FRAME_TAIL);
Masakazu Mokuno173261e2007-08-31 22:22:32 +0900732
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900733 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
734 descr->dmac_cmd_status =
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900735 cpu_to_be32(GELIC_DESCR_DMA_CMD_UDP_CHKSUM |
736 GELIC_DESCR_TX_DMA_FRAME_TAIL);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900737 else /*
738 * the stack should checksum non-tcp and non-udp
739 * packets on his own: NETIF_F_IP_CSUM
740 */
741 descr->dmac_cmd_status =
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900742 cpu_to_be32(GELIC_DESCR_DMA_CMD_NO_CHKSUM |
743 GELIC_DESCR_TX_DMA_FRAME_TAIL);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900744 }
745 }
746}
747
Masakazu Mokuno173261e2007-08-31 22:22:32 +0900748static inline struct sk_buff *gelic_put_vlan_tag(struct sk_buff *skb,
749 unsigned short tag)
750{
751 struct vlan_ethhdr *veth;
752 static unsigned int c;
753
754 if (skb_headroom(skb) < VLAN_HLEN) {
755 struct sk_buff *sk_tmp = skb;
756 pr_debug("%s: hd=%d c=%ud\n", __func__, skb_headroom(skb), c);
757 skb = skb_realloc_headroom(sk_tmp, VLAN_HLEN);
758 if (!skb)
759 return NULL;
760 dev_kfree_skb_any(sk_tmp);
761 }
762 veth = (struct vlan_ethhdr *)skb_push(skb, VLAN_HLEN);
763
764 /* Move the mac addresses to the top of buffer */
765 memmove(skb->data, skb->data + VLAN_HLEN, 2 * ETH_ALEN);
766
Harvey Harrison09640e62009-02-01 00:45:17 -0800767 veth->h_vlan_proto = cpu_to_be16(ETH_P_8021Q);
Masakazu Mokuno173261e2007-08-31 22:22:32 +0900768 veth->h_vlan_TCI = htons(tag);
769
770 return skb;
771}
772
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900773/**
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900774 * gelic_descr_prepare_tx - setup a descriptor for sending packets
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900775 * @card: card structure
776 * @descr: descriptor structure
777 * @skb: packet to use
778 *
779 * returns 0 on success, <0 on failure.
780 *
781 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900782static int gelic_descr_prepare_tx(struct gelic_card *card,
783 struct gelic_descr *descr,
784 struct sk_buff *skb)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900785{
Masakazu Mokuno173261e2007-08-31 22:22:32 +0900786 dma_addr_t buf;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900787
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900788 if (card->vlan_required) {
Masakazu Mokuno173261e2007-08-31 22:22:32 +0900789 struct sk_buff *skb_tmp;
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900790 enum gelic_port_type type;
791
792 type = netdev_port(skb->dev)->type;
Masakazu Mokuno173261e2007-08-31 22:22:32 +0900793 skb_tmp = gelic_put_vlan_tag(skb,
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900794 card->vlan[type].tx);
Masakazu Mokuno173261e2007-08-31 22:22:32 +0900795 if (!skb_tmp)
796 return -ENOMEM;
797 skb = skb_tmp;
Masakazu Mokuno48544cc262007-07-20 17:24:56 +0900798 }
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900799
Masakazu Mokuno173261e2007-08-31 22:22:32 +0900800 buf = dma_map_single(ctodev(card), skb->data, skb->len, DMA_TO_DEVICE);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900801
Masakazu Mokuno173261e2007-08-31 22:22:32 +0900802 if (!buf) {
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900803 dev_err(ctodev(card),
804 "dma map 2 failed (%p, %i). Dropping packet\n",
Masakazu Mokuno173261e2007-08-31 22:22:32 +0900805 skb->data, skb->len);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900806 return -ENOMEM;
807 }
808
Masakazu Mokuno100e1d82008-02-07 19:57:54 +0900809 descr->buf_addr = cpu_to_be32(buf);
810 descr->buf_size = cpu_to_be32(skb->len);
Masakazu Mokuno173261e2007-08-31 22:22:32 +0900811 descr->skb = skb;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900812 descr->data_status = 0;
Masakazu Mokuno173261e2007-08-31 22:22:32 +0900813 descr->next_descr_addr = 0; /* terminate hw descr */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900814 gelic_descr_set_tx_cmdstat(descr, skb);
Masakazu Mokuno48544cc262007-07-20 17:24:56 +0900815
816 /* bump free descriptor pointer */
Masakazu Mokuno173261e2007-08-31 22:22:32 +0900817 card->tx_chain.head = descr->next;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900818 return 0;
819}
820
821/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900822 * gelic_card_kick_txdma - enables TX DMA processing
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900823 * @card: card structure
824 * @descr: descriptor address to enable TX processing at
825 *
826 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900827static int gelic_card_kick_txdma(struct gelic_card *card,
828 struct gelic_descr *descr)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900829{
Masakazu Mokuno48544cc262007-07-20 17:24:56 +0900830 int status = 0;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900831
832 if (card->tx_dma_progress)
833 return 0;
834
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900835 if (gelic_descr_get_status(descr) == GELIC_DESCR_DMA_CARDOWNED) {
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900836 card->tx_dma_progress = 1;
Masakazu Mokunodc029ad2007-08-31 22:25:09 +0900837 status = lv1_net_start_tx_dma(bus_id(card), dev_id(card),
838 descr->bus_addr, 0);
839 if (status)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900840 dev_info(ctodev(card), "lv1_net_start_txdma failed," \
Masakazu Mokunodc029ad2007-08-31 22:25:09 +0900841 "status=%d\n", status);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900842 }
843 return status;
844}
845
846/**
847 * gelic_net_xmit - transmits a frame over the device
848 * @skb: packet to send out
849 * @netdev: interface device structure
850 *
851 * returns 0 on success, <0 on failure
852 */
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900853int gelic_net_xmit(struct sk_buff *skb, struct net_device *netdev)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900854{
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900855 struct gelic_card *card = netdev_card(netdev);
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900856 struct gelic_descr *descr;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900857 int result;
858 unsigned long flags;
859
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900860 spin_lock_irqsave(&card->tx_lock, flags);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900861
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900862 gelic_card_release_tx_chain(card, 0);
Masakazu Mokuno48544cc262007-07-20 17:24:56 +0900863
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900864 descr = gelic_card_get_next_tx_descr(card);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900865 if (!descr) {
Masakazu Mokuno48544cc262007-07-20 17:24:56 +0900866 /*
867 * no more descriptors free
868 */
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900869 gelic_card_stop_queues(card);
870 spin_unlock_irqrestore(&card->tx_lock, flags);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900871 return NETDEV_TX_BUSY;
872 }
Masakazu Mokuno48544cc262007-07-20 17:24:56 +0900873
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900874 result = gelic_descr_prepare_tx(card, descr, skb);
Masakazu Mokuno48544cc262007-07-20 17:24:56 +0900875 if (result) {
876 /*
877 * DMA map failed. As chanses are that failure
878 * would continue, just release skb and return
879 */
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900880 netdev->stats.tx_dropped++;
Masakazu Mokuno48544cc262007-07-20 17:24:56 +0900881 dev_kfree_skb_any(skb);
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900882 spin_unlock_irqrestore(&card->tx_lock, flags);
Masakazu Mokuno48544cc262007-07-20 17:24:56 +0900883 return NETDEV_TX_OK;
884 }
885 /*
886 * link this prepared descriptor to previous one
887 * to achieve high performance
888 */
Masakazu Mokuno100e1d82008-02-07 19:57:54 +0900889 descr->prev->next_descr_addr = cpu_to_be32(descr->bus_addr);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900890 /*
891 * as hardware descriptor is modified in the above lines,
892 * ensure that the hardware sees it
893 */
894 wmb();
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900895 if (gelic_card_kick_txdma(card, descr)) {
Masakazu Mokuno48544cc262007-07-20 17:24:56 +0900896 /*
897 * kick failed.
898 * release descriptors which were just prepared
899 */
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900900 netdev->stats.tx_dropped++;
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900901 gelic_descr_release_tx(card, descr);
902 gelic_descr_release_tx(card, descr->next);
Masakazu Mokuno48544cc262007-07-20 17:24:56 +0900903 card->tx_chain.tail = descr->next->next;
904 dev_info(ctodev(card), "%s: kick failure\n", __func__);
905 } else {
906 /* OK, DMA started/reserved */
907 netdev->trans_start = jiffies;
908 }
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900909
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900910 spin_unlock_irqrestore(&card->tx_lock, flags);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900911 return NETDEV_TX_OK;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900912}
913
914/**
915 * gelic_net_pass_skb_up - takes an skb from a descriptor and passes it on
916 * @descr: descriptor to process
917 * @card: card structure
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900918 * @netdev: net_device structure to be passed packet
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900919 *
920 * iommu-unmaps the skb, fills out skb structure and passes the data to the
921 * stack. The descriptor state is not changed.
922 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900923static void gelic_net_pass_skb_up(struct gelic_descr *descr,
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900924 struct gelic_card *card,
925 struct net_device *netdev)
926
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900927{
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900928 struct sk_buff *skb = descr->skb;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900929 u32 data_status, data_error;
930
Masakazu Mokuno100e1d82008-02-07 19:57:54 +0900931 data_status = be32_to_cpu(descr->data_status);
932 data_error = be32_to_cpu(descr->data_error);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900933 /* unmap skb buffer */
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900934 dma_unmap_single(ctodev(card), be32_to_cpu(descr->buf_addr),
935 GELIC_NET_MAX_MTU,
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900936 DMA_FROM_DEVICE);
937
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900938 skb_put(skb, be32_to_cpu(descr->valid_size)?
Masakazu Mokuno100e1d82008-02-07 19:57:54 +0900939 be32_to_cpu(descr->valid_size) :
940 be32_to_cpu(descr->result_size));
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900941 if (!descr->valid_size)
942 dev_info(ctodev(card), "buffer full %x %x %x\n",
Masakazu Mokuno100e1d82008-02-07 19:57:54 +0900943 be32_to_cpu(descr->result_size),
944 be32_to_cpu(descr->buf_size),
945 be32_to_cpu(descr->dmac_cmd_status));
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900946
947 descr->skb = NULL;
948 /*
949 * the card put 2 bytes vlan tag in front
950 * of the ethernet frame
951 */
952 skb_pull(skb, 2);
953 skb->protocol = eth_type_trans(skb, netdev);
954
955 /* checksum offload */
956 if (card->rx_csum) {
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900957 if ((data_status & GELIC_DESCR_DATA_STATUS_CHK_MASK) &&
958 (!(data_error & GELIC_DESCR_DATA_ERROR_CHK_MASK)))
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900959 skb->ip_summed = CHECKSUM_UNNECESSARY;
960 else
961 skb->ip_summed = CHECKSUM_NONE;
962 } else
963 skb->ip_summed = CHECKSUM_NONE;
964
965 /* update netdevice statistics */
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900966 netdev->stats.rx_packets++;
967 netdev->stats.rx_bytes += skb->len;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900968
969 /* pass skb up to stack */
970 netif_receive_skb(skb);
971}
972
973/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900974 * gelic_card_decode_one_descr - processes an rx descriptor
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900975 * @card: card structure
976 *
977 * returns 1 if a packet has been sent to the stack, otherwise 0
978 *
979 * processes an rx descriptor by iommu-unmapping the data buffer and passing
980 * the packet up to the stack
981 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900982static int gelic_card_decode_one_descr(struct gelic_card *card)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900983{
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900984 enum gelic_descr_dma_status status;
985 struct gelic_descr_chain *chain = &card->rx_chain;
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900986 struct gelic_descr *descr = chain->head;
987 struct net_device *netdev = NULL;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900988 int dmac_chain_ended;
989
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900990 status = gelic_descr_get_status(descr);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900991 /* is this descriptor terminated with next_descr == NULL? */
992 dmac_chain_ended =
Masakazu Mokuno100e1d82008-02-07 19:57:54 +0900993 be32_to_cpu(descr->dmac_cmd_status) &
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900994 GELIC_DESCR_RX_DMA_CHAIN_END;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900995
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900996 if (status == GELIC_DESCR_DMA_CARDOWNED)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900997 return 0;
998
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900999 if (status == GELIC_DESCR_DMA_NOT_IN_USE) {
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001000 dev_dbg(ctodev(card), "dormant descr? %p\n", descr);
1001 return 0;
1002 }
1003
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001004 /* netdevice select */
1005 if (card->vlan_required) {
1006 unsigned int i;
1007 u16 vid;
1008 vid = *(u16 *)(descr->skb->data) & VLAN_VID_MASK;
1009 for (i = 0; i < GELIC_PORT_MAX; i++) {
1010 if (card->vlan[i].rx == vid) {
1011 netdev = card->netdev[i];
1012 break;
1013 }
1014 };
1015 if (GELIC_PORT_MAX <= i) {
1016 pr_info("%s: unknown packet vid=%x\n", __func__, vid);
1017 goto refill;
1018 }
1019 } else
Geoff Levandd4d7f1f2009-12-01 12:15:53 +00001020 netdev = card->netdev[GELIC_PORT_ETHERNET_0];
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001021
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001022 if ((status == GELIC_DESCR_DMA_RESPONSE_ERROR) ||
1023 (status == GELIC_DESCR_DMA_PROTECTION_ERROR) ||
1024 (status == GELIC_DESCR_DMA_FORCE_END)) {
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001025 dev_info(ctodev(card), "dropping RX descriptor with state %x\n",
1026 status);
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001027 netdev->stats.rx_dropped++;
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001028 goto refill;
1029 }
1030
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001031 if (status == GELIC_DESCR_DMA_BUFFER_FULL) {
Masakazu Mokunofe6d3a42007-07-20 17:39:25 +09001032 /*
1033 * Buffer full would occur if and only if
1034 * the frame length was longer than the size of this
1035 * descriptor's buffer. If the frame length was equal
1036 * to or shorter than buffer'size, FRAME_END condition
1037 * would occur.
1038 * Anyway this frame was longer than the MTU,
1039 * just drop it.
1040 */
1041 dev_info(ctodev(card), "overlength frame\n");
1042 goto refill;
1043 }
1044 /*
1045 * descriptoers any other than FRAME_END here should
1046 * be treated as error.
1047 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001048 if (status != GELIC_DESCR_DMA_FRAME_END) {
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001049 dev_dbg(ctodev(card), "RX descriptor with state %x\n",
1050 status);
1051 goto refill;
1052 }
1053
1054 /* ok, we've got a packet in descr */
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001055 gelic_net_pass_skb_up(descr, card, netdev);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001056refill:
Masakazu Mokunofe6d3a42007-07-20 17:39:25 +09001057 /*
1058 * So that always DMAC can see the end
1059 * of the descriptor chain to avoid
1060 * from unwanted DMAC overrun.
1061 */
1062 descr->next_descr_addr = 0;
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001063
1064 /* change the descriptor state: */
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001065 gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001066
Masakazu Mokunofe6d3a42007-07-20 17:39:25 +09001067 /*
1068 * this call can fail, but for now, just leave this
1069 * decriptor without skb
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001070 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001071 gelic_descr_prepare_rx(card, descr);
Masakazu Mokunofe6d3a42007-07-20 17:39:25 +09001072
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001073 chain->tail = descr;
1074 chain->head = descr->next;
Masakazu Mokunofe6d3a42007-07-20 17:39:25 +09001075
1076 /*
1077 * Set this descriptor the end of the chain.
1078 */
Masakazu Mokuno100e1d82008-02-07 19:57:54 +09001079 descr->prev->next_descr_addr = cpu_to_be32(descr->bus_addr);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001080
Masakazu Mokunofe6d3a42007-07-20 17:39:25 +09001081 /*
1082 * If dmac chain was met, DMAC stopped.
1083 * thus re-enable it
1084 */
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001085 if (dmac_chain_ended) {
Masakazu Mokuno583aae12007-07-20 17:35:54 +09001086 card->rx_dma_restart_required = 1;
1087 dev_dbg(ctodev(card), "reenable rx dma scheduled\n");
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001088 }
1089
1090 return 1;
1091}
1092
1093/**
1094 * gelic_net_poll - NAPI poll function called by the stack to return packets
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001095 * @napi: napi structure
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001096 * @budget: number of packets we can pass to the stack at most
1097 *
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001098 * returns the number of the processed packets
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001099 *
1100 */
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001101static int gelic_net_poll(struct napi_struct *napi, int budget)
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001102{
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001103 struct gelic_card *card = container_of(napi, struct gelic_card, napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001104 int packets_done = 0;
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001105
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001106 while (packets_done < budget) {
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001107 if (!gelic_card_decode_one_descr(card))
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001108 break;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001109
1110 packets_done++;
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001111 }
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001112
1113 if (packets_done < budget) {
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001114 napi_complete(napi);
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001115 gelic_card_rx_irq_on(card);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001116 }
1117 return packets_done;
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001118}
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001119/**
1120 * gelic_net_change_mtu - changes the MTU of an interface
1121 * @netdev: interface device structure
1122 * @new_mtu: new MTU value
1123 *
1124 * returns 0 on success, <0 on failure
1125 */
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001126int gelic_net_change_mtu(struct net_device *netdev, int new_mtu)
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001127{
1128 /* no need to re-alloc skbs or so -- the max mtu is about 2.3k
1129 * and mtu is outbound only anyway */
1130 if ((new_mtu < GELIC_NET_MIN_MTU) ||
1131 (new_mtu > GELIC_NET_MAX_MTU)) {
1132 return -EINVAL;
1133 }
1134 netdev->mtu = new_mtu;
1135 return 0;
1136}
1137
1138/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001139 * gelic_card_interrupt - event handler for gelic_net
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001140 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001141static irqreturn_t gelic_card_interrupt(int irq, void *ptr)
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001142{
1143 unsigned long flags;
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001144 struct gelic_card *card = ptr;
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001145 u64 status;
1146
1147 status = card->irq_status;
1148
1149 if (!status)
1150 return IRQ_NONE;
1151
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001152 status &= card->irq_mask;
1153
Masakazu Mokuno583aae12007-07-20 17:35:54 +09001154 if (card->rx_dma_restart_required) {
1155 card->rx_dma_restart_required = 0;
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001156 gelic_card_enable_rxdmac(card);
Masakazu Mokuno583aae12007-07-20 17:35:54 +09001157 }
1158
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001159 if (status & GELIC_CARD_RXINT) {
1160 gelic_card_rx_irq_off(card);
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001161 napi_schedule(&card->napi);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001162 }
1163
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001164 if (status & GELIC_CARD_TXINT) {
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001165 spin_lock_irqsave(&card->tx_lock, flags);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001166 card->tx_dma_progress = 0;
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001167 gelic_card_release_tx_chain(card, 0);
Masakazu Mokuno48544cc262007-07-20 17:24:56 +09001168 /* kick outstanding tx descriptor if any */
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001169 gelic_card_kick_txdma(card, card->tx_chain.tail);
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001170 spin_unlock_irqrestore(&card->tx_lock, flags);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001171 }
Masakazu Mokuno01fed4c2008-02-07 19:58:32 +09001172
1173 /* ether port status changed */
1174 if (status & GELIC_CARD_PORT_STATUS_CHANGED)
1175 gelic_card_get_ether_port_status(card, 1);
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001176
Masakazu Mokuno09dde542008-02-07 19:58:57 +09001177#ifdef CONFIG_GELIC_WIRELESS
1178 if (status & (GELIC_CARD_WLAN_EVENT_RECEIVED |
1179 GELIC_CARD_WLAN_COMMAND_COMPLETED))
1180 gelic_wl_interrupt(card->netdev[GELIC_PORT_WIRELESS], status);
1181#endif
1182
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001183 return IRQ_HANDLED;
1184}
1185
1186#ifdef CONFIG_NET_POLL_CONTROLLER
1187/**
1188 * gelic_net_poll_controller - artificial interrupt for netconsole etc.
1189 * @netdev: interface device structure
1190 *
1191 * see Documentation/networking/netconsole.txt
1192 */
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001193void gelic_net_poll_controller(struct net_device *netdev)
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001194{
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001195 struct gelic_card *card = netdev_card(netdev);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001196
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001197 gelic_card_set_irq_mask(card, 0);
1198 gelic_card_interrupt(netdev->irq, netdev);
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001199 gelic_card_set_irq_mask(card, card->irq_mask);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001200}
1201#endif /* CONFIG_NET_POLL_CONTROLLER */
1202
1203/**
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001204 * gelic_net_open - called upon ifonfig up
1205 * @netdev: interface device structure
1206 *
1207 * returns 0 on success, <0 on failure
1208 *
1209 * gelic_net_open allocates all the descriptors and memory needed for
1210 * operation, sets up multicast list and enables interrupts
1211 */
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001212int gelic_net_open(struct net_device *netdev)
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001213{
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001214 struct gelic_card *card = netdev_card(netdev);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001215
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001216 dev_dbg(ctodev(card), " -> %s %p\n", __func__, netdev);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001217
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001218 gelic_card_up(card);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001219
1220 netif_start_queue(netdev);
Masakazu Mokuno01fed4c2008-02-07 19:58:32 +09001221 gelic_card_get_ether_port_status(card, 1);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001222
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001223 dev_dbg(ctodev(card), " <- %s\n", __func__);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001224 return 0;
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001225}
1226
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001227void gelic_net_get_drvinfo(struct net_device *netdev,
1228 struct ethtool_drvinfo *info)
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001229{
1230 strncpy(info->driver, DRV_NAME, sizeof(info->driver) - 1);
1231 strncpy(info->version, DRV_VERSION, sizeof(info->version) - 1);
1232}
1233
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001234static int gelic_ether_get_settings(struct net_device *netdev,
1235 struct ethtool_cmd *cmd)
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001236{
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001237 struct gelic_card *card = netdev_card(netdev);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001238
Masakazu Mokuno01fed4c2008-02-07 19:58:32 +09001239 gelic_card_get_ether_port_status(card, 0);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001240
Masakazu Mokuno01fed4c2008-02-07 19:58:32 +09001241 if (card->ether_port_status & GELIC_LV1_ETHER_FULL_DUPLEX)
1242 cmd->duplex = DUPLEX_FULL;
1243 else
1244 cmd->duplex = DUPLEX_HALF;
1245
1246 switch (card->ether_port_status & GELIC_LV1_ETHER_SPEED_MASK) {
1247 case GELIC_LV1_ETHER_SPEED_10:
1248 cmd->speed = SPEED_10;
1249 break;
1250 case GELIC_LV1_ETHER_SPEED_100:
1251 cmd->speed = SPEED_100;
1252 break;
1253 case GELIC_LV1_ETHER_SPEED_1000:
1254 cmd->speed = SPEED_1000;
1255 break;
1256 default:
1257 pr_info("%s: speed unknown\n", __func__);
1258 cmd->speed = SPEED_10;
1259 break;
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001260 }
Masakazu Mokuno01fed4c2008-02-07 19:58:32 +09001261
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001262 cmd->supported = SUPPORTED_TP | SUPPORTED_Autoneg |
1263 SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full |
1264 SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full |
Hideyuki Sasaki55873ed2009-12-01 12:15:58 +00001265 SUPPORTED_1000baseT_Full;
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001266 cmd->advertising = cmd->supported;
Hideyuki Sasaki55873ed2009-12-01 12:15:58 +00001267 if (card->link_mode & GELIC_LV1_ETHER_AUTO_NEG) {
1268 cmd->autoneg = AUTONEG_ENABLE;
1269 } else {
1270 cmd->autoneg = AUTONEG_DISABLE;
1271 cmd->advertising &= ~ADVERTISED_Autoneg;
1272 }
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001273 cmd->port = PORT_TP;
1274
1275 return 0;
1276}
1277
Hideyuki Sasaki55873ed2009-12-01 12:15:58 +00001278static int gelic_ether_set_settings(struct net_device *netdev,
1279 struct ethtool_cmd *cmd)
1280{
1281 struct gelic_card *card = netdev_card(netdev);
1282 u64 mode;
1283 int ret;
1284
1285 if (cmd->autoneg == AUTONEG_ENABLE) {
1286 mode = GELIC_LV1_ETHER_AUTO_NEG;
1287 } else {
1288 switch (cmd->speed) {
1289 case SPEED_10:
1290 mode = GELIC_LV1_ETHER_SPEED_10;
1291 break;
1292 case SPEED_100:
1293 mode = GELIC_LV1_ETHER_SPEED_100;
1294 break;
1295 case SPEED_1000:
1296 mode = GELIC_LV1_ETHER_SPEED_1000;
1297 break;
1298 default:
1299 return -EINVAL;
1300 }
1301 if (cmd->duplex == DUPLEX_FULL)
1302 mode |= GELIC_LV1_ETHER_FULL_DUPLEX;
1303 else if (cmd->speed == SPEED_1000) {
1304 pr_info("1000 half duplex is not supported.\n");
1305 return -EINVAL;
1306 }
1307 }
1308
1309 ret = gelic_card_set_link_mode(card, mode);
1310
1311 if (ret)
1312 return ret;
1313
1314 return 0;
1315}
1316
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001317u32 gelic_net_get_rx_csum(struct net_device *netdev)
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001318{
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001319 struct gelic_card *card = netdev_card(netdev);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001320
1321 return card->rx_csum;
1322}
1323
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001324int gelic_net_set_rx_csum(struct net_device *netdev, u32 data)
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001325{
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001326 struct gelic_card *card = netdev_card(netdev);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001327
1328 card->rx_csum = data;
1329 return 0;
1330}
1331
Masakazu Mokuno3faac212008-03-27 11:39:31 +11001332static void gelic_net_get_wol(struct net_device *netdev,
1333 struct ethtool_wolinfo *wol)
1334{
1335 if (0 <= ps3_compare_firmware_version(2, 2, 0))
1336 wol->supported = WAKE_MAGIC;
1337 else
1338 wol->supported = 0;
1339
1340 wol->wolopts = ps3_sys_manager_get_wol() ? wol->supported : 0;
1341 memset(&wol->sopass, 0, sizeof(wol->sopass));
1342}
1343static int gelic_net_set_wol(struct net_device *netdev,
1344 struct ethtool_wolinfo *wol)
1345{
1346 int status;
1347 struct gelic_card *card;
1348 u64 v1, v2;
1349
1350 if (ps3_compare_firmware_version(2, 2, 0) < 0 ||
1351 !capable(CAP_NET_ADMIN))
1352 return -EPERM;
1353
1354 if (wol->wolopts & ~WAKE_MAGIC)
1355 return -EINVAL;
1356
1357 card = netdev_card(netdev);
1358 if (wol->wolopts & WAKE_MAGIC) {
1359 status = lv1_net_control(bus_id(card), dev_id(card),
1360 GELIC_LV1_SET_WOL,
1361 GELIC_LV1_WOL_MAGIC_PACKET,
1362 0, GELIC_LV1_WOL_MP_ENABLE,
1363 &v1, &v2);
1364 if (status) {
1365 pr_info("%s: enabling WOL failed %d\n", __func__,
1366 status);
1367 status = -EIO;
1368 goto done;
1369 }
1370 status = lv1_net_control(bus_id(card), dev_id(card),
1371 GELIC_LV1_SET_WOL,
1372 GELIC_LV1_WOL_ADD_MATCH_ADDR,
1373 0, GELIC_LV1_WOL_MATCH_ALL,
1374 &v1, &v2);
1375 if (!status)
1376 ps3_sys_manager_set_wol(1);
1377 else {
1378 pr_info("%s: enabling WOL filter failed %d\n",
1379 __func__, status);
1380 status = -EIO;
1381 }
1382 } else {
1383 status = lv1_net_control(bus_id(card), dev_id(card),
1384 GELIC_LV1_SET_WOL,
1385 GELIC_LV1_WOL_MAGIC_PACKET,
1386 0, GELIC_LV1_WOL_MP_DISABLE,
1387 &v1, &v2);
1388 if (status) {
1389 pr_info("%s: disabling WOL failed %d\n", __func__,
1390 status);
1391 status = -EIO;
1392 goto done;
1393 }
1394 status = lv1_net_control(bus_id(card), dev_id(card),
1395 GELIC_LV1_SET_WOL,
1396 GELIC_LV1_WOL_DELETE_MATCH_ADDR,
1397 0, GELIC_LV1_WOL_MATCH_ALL,
1398 &v1, &v2);
1399 if (!status)
1400 ps3_sys_manager_set_wol(0);
1401 else {
1402 pr_info("%s: removing WOL filter failed %d\n",
1403 __func__, status);
1404 status = -EIO;
1405 }
1406 }
1407done:
1408 return status;
1409}
1410
Stephen Hemminger0fc0b732009-09-02 01:03:33 -07001411static const struct ethtool_ops gelic_ether_ethtool_ops = {
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001412 .get_drvinfo = gelic_net_get_drvinfo,
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001413 .get_settings = gelic_ether_get_settings,
Hideyuki Sasaki55873ed2009-12-01 12:15:58 +00001414 .set_settings = gelic_ether_set_settings,
Masakazu Mokuno7bc56b92008-02-07 19:58:20 +09001415 .get_link = ethtool_op_get_link,
Masakazu Mokuno7bc56b92008-02-07 19:58:20 +09001416 .get_tx_csum = ethtool_op_get_tx_csum,
1417 .set_tx_csum = ethtool_op_set_tx_csum,
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001418 .get_rx_csum = gelic_net_get_rx_csum,
1419 .set_rx_csum = gelic_net_set_rx_csum,
Masakazu Mokuno3faac212008-03-27 11:39:31 +11001420 .get_wol = gelic_net_get_wol,
1421 .set_wol = gelic_net_set_wol,
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001422};
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001423
1424/**
1425 * gelic_net_tx_timeout_task - task scheduled by the watchdog timeout
1426 * function (to be called not under interrupt status)
1427 * @work: work is context of tx timout task
1428 *
1429 * called as task when tx hangs, resets interface (if interface is up)
1430 */
1431static void gelic_net_tx_timeout_task(struct work_struct *work)
1432{
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001433 struct gelic_card *card =
1434 container_of(work, struct gelic_card, tx_timeout_task);
Geoff Levandd4d7f1f2009-12-01 12:15:53 +00001435 struct net_device *netdev = card->netdev[GELIC_PORT_ETHERNET_0];
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001436
1437 dev_info(ctodev(card), "%s:Timed out. Restarting... \n", __func__);
1438
1439 if (!(netdev->flags & IFF_UP))
1440 goto out;
1441
1442 netif_device_detach(netdev);
1443 gelic_net_stop(netdev);
1444
1445 gelic_net_open(netdev);
1446 netif_device_attach(netdev);
1447
1448out:
1449 atomic_dec(&card->tx_timeout_task_counter);
1450}
1451
1452/**
1453 * gelic_net_tx_timeout - called when the tx timeout watchdog kicks in.
1454 * @netdev: interface device structure
1455 *
1456 * called, if tx hangs. Schedules a task that resets the interface
1457 */
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001458void gelic_net_tx_timeout(struct net_device *netdev)
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001459{
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001460 struct gelic_card *card;
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001461
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001462 card = netdev_card(netdev);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001463 atomic_inc(&card->tx_timeout_task_counter);
1464 if (netdev->flags & IFF_UP)
1465 schedule_work(&card->tx_timeout_task);
1466 else
1467 atomic_dec(&card->tx_timeout_task_counter);
1468}
1469
Masakazu Mokuno5384e832009-01-15 22:47:29 +00001470static const struct net_device_ops gelic_netdevice_ops = {
1471 .ndo_open = gelic_net_open,
1472 .ndo_stop = gelic_net_stop,
1473 .ndo_start_xmit = gelic_net_xmit,
1474 .ndo_set_multicast_list = gelic_net_set_multi,
1475 .ndo_change_mtu = gelic_net_change_mtu,
1476 .ndo_tx_timeout = gelic_net_tx_timeout,
Ben Hutchings240c1022009-07-09 17:54:35 +00001477 .ndo_set_mac_address = eth_mac_addr,
Masakazu Mokuno5384e832009-01-15 22:47:29 +00001478 .ndo_validate_addr = eth_validate_addr,
1479#ifdef CONFIG_NET_POLL_CONTROLLER
1480 .ndo_poll_controller = gelic_net_poll_controller,
1481#endif
1482};
1483
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001484/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001485 * gelic_ether_setup_netdev_ops - initialization of net_device operations
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001486 * @netdev: net_device structure
1487 *
1488 * fills out function pointers in the net_device structure
1489 */
Geert Uytterhoeven48dce822009-06-10 04:38:58 +00001490static void __devinit gelic_ether_setup_netdev_ops(struct net_device *netdev,
1491 struct napi_struct *napi)
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001492{
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001493 netdev->watchdog_timeo = GELIC_NET_WATCHDOG_TIMEOUT;
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001494 /* NAPI */
1495 netif_napi_add(netdev, napi,
1496 gelic_net_poll, GELIC_NET_NAPI_WEIGHT);
1497 netdev->ethtool_ops = &gelic_ether_ethtool_ops;
Masakazu Mokuno5384e832009-01-15 22:47:29 +00001498 netdev->netdev_ops = &gelic_netdevice_ops;
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001499}
1500
1501/**
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001502 * gelic_ether_setup_netdev - initialization of net_device
1503 * @netdev: net_device structure
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001504 * @card: card structure
1505 *
1506 * Returns 0 on success or <0 on failure
1507 *
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001508 * gelic_ether_setup_netdev initializes the net_device structure
1509 * and register it.
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001510 **/
Geert Uytterhoeven48dce822009-06-10 04:38:58 +00001511int __devinit gelic_net_setup_netdev(struct net_device *netdev,
1512 struct gelic_card *card)
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001513{
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001514 int status;
1515 u64 v1, v2;
1516
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001517 netdev->features = NETIF_F_IP_CSUM;
1518
1519 status = lv1_net_control(bus_id(card), dev_id(card),
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001520 GELIC_LV1_GET_MAC_ADDRESS,
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001521 0, 0, 0, &v1, &v2);
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001522 v1 <<= 16;
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001523 if (status || !is_valid_ether_addr((u8 *)&v1)) {
1524 dev_info(ctodev(card),
1525 "%s:lv1_net_control GET_MAC_ADDR failed %d\n",
1526 __func__, status);
1527 return -EINVAL;
1528 }
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001529 memcpy(netdev->dev_addr, &v1, ETH_ALEN);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001530
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001531 if (card->vlan_required) {
Masakazu Mokuno173261e2007-08-31 22:22:32 +09001532 netdev->hard_header_len += VLAN_HLEN;
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001533 /*
1534 * As vlan is internally used,
1535 * we can not receive vlan packets
1536 */
1537 netdev->features |= NETIF_F_VLAN_CHALLENGED;
Masakazu Mokuno173261e2007-08-31 22:22:32 +09001538 }
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001539
1540 status = register_netdev(netdev);
1541 if (status) {
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001542 dev_err(ctodev(card), "%s:Couldn't register %s %d\n",
1543 __func__, netdev->name, status);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001544 return status;
1545 }
Johannes Berge1749612008-10-27 15:59:26 -07001546 dev_info(ctodev(card), "%s: MAC addr %pM\n",
1547 netdev->name, netdev->dev_addr);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001548
1549 return 0;
1550}
1551
1552/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001553 * gelic_alloc_card_net - allocates net_device and card structure
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001554 *
1555 * returns the card structure or NULL in case of errors
1556 *
1557 * the card and net_device structures are linked to each other
1558 */
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001559#define GELIC_ALIGN (32)
Geert Uytterhoeven48dce822009-06-10 04:38:58 +00001560static struct gelic_card * __devinit gelic_alloc_card_net(struct net_device **netdev)
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001561{
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001562 struct gelic_card *card;
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001563 struct gelic_port *port;
1564 void *p;
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001565 size_t alloc_size;
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001566 /*
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001567 * gelic requires dma descriptor is 32 bytes aligned and
1568 * the hypervisor requires irq_status is 8 bytes aligned.
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001569 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001570 BUILD_BUG_ON(offsetof(struct gelic_card, irq_status) % 8);
1571 BUILD_BUG_ON(offsetof(struct gelic_card, descr) % 32);
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001572 alloc_size =
1573 sizeof(struct gelic_card) +
1574 sizeof(struct gelic_descr) * GELIC_NET_RX_DESCRIPTORS +
1575 sizeof(struct gelic_descr) * GELIC_NET_TX_DESCRIPTORS +
1576 GELIC_ALIGN - 1;
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001577
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001578 p = kzalloc(alloc_size, GFP_KERNEL);
1579 if (!p)
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001580 return NULL;
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001581 card = PTR_ALIGN(p, GELIC_ALIGN);
1582 card->unalign = p;
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001583
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001584 /*
1585 * alloc netdev
1586 */
1587 *netdev = alloc_etherdev(sizeof(struct gelic_port));
1588 if (!netdev) {
1589 kfree(card->unalign);
1590 return NULL;
1591 }
1592 port = netdev_priv(*netdev);
1593
1594 /* gelic_port */
1595 port->netdev = *netdev;
1596 port->card = card;
Geoff Levandd4d7f1f2009-12-01 12:15:53 +00001597 port->type = GELIC_PORT_ETHERNET_0;
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001598
1599 /* gelic_card */
Geoff Levandd4d7f1f2009-12-01 12:15:53 +00001600 card->netdev[GELIC_PORT_ETHERNET_0] = *netdev;
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001601
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001602 INIT_WORK(&card->tx_timeout_task, gelic_net_tx_timeout_task);
1603 init_waitqueue_head(&card->waitq);
1604 atomic_set(&card->tx_timeout_task_counter, 0);
Daniel Walker2914f3e2008-05-22 00:00:03 -07001605 mutex_init(&card->updown_lock);
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001606 atomic_set(&card->users, 0);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001607
1608 return card;
1609}
1610
Geert Uytterhoeven48dce822009-06-10 04:38:58 +00001611static void __devinit gelic_card_get_vlan_info(struct gelic_card *card)
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001612{
1613 u64 v1, v2;
1614 int status;
1615 unsigned int i;
1616 struct {
1617 int tx;
1618 int rx;
1619 } vlan_id_ix[2] = {
Geoff Levandd4d7f1f2009-12-01 12:15:53 +00001620 [GELIC_PORT_ETHERNET_0] = {
1621 .tx = GELIC_LV1_VLAN_TX_ETHERNET_0,
1622 .rx = GELIC_LV1_VLAN_RX_ETHERNET_0
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001623 },
1624 [GELIC_PORT_WIRELESS] = {
1625 .tx = GELIC_LV1_VLAN_TX_WIRELESS,
1626 .rx = GELIC_LV1_VLAN_RX_WIRELESS
1627 }
1628 };
1629
1630 for (i = 0; i < ARRAY_SIZE(vlan_id_ix); i++) {
1631 /* tx tag */
1632 status = lv1_net_control(bus_id(card), dev_id(card),
1633 GELIC_LV1_GET_VLAN_ID,
1634 vlan_id_ix[i].tx,
1635 0, 0, &v1, &v2);
1636 if (status || !v1) {
1637 if (status != LV1_NO_ENTRY)
1638 dev_dbg(ctodev(card),
1639 "get vlan id for tx(%d) failed(%d)\n",
1640 vlan_id_ix[i].tx, status);
1641 card->vlan[i].tx = 0;
1642 card->vlan[i].rx = 0;
1643 continue;
1644 }
1645 card->vlan[i].tx = (u16)v1;
1646
1647 /* rx tag */
1648 status = lv1_net_control(bus_id(card), dev_id(card),
1649 GELIC_LV1_GET_VLAN_ID,
1650 vlan_id_ix[i].rx,
1651 0, 0, &v1, &v2);
1652 if (status || !v1) {
1653 if (status != LV1_NO_ENTRY)
1654 dev_info(ctodev(card),
1655 "get vlan id for rx(%d) failed(%d)\n",
1656 vlan_id_ix[i].rx, status);
1657 card->vlan[i].tx = 0;
1658 card->vlan[i].rx = 0;
1659 continue;
1660 }
1661 card->vlan[i].rx = (u16)v1;
1662
1663 dev_dbg(ctodev(card), "vlan_id[%d] tx=%02x rx=%02x\n",
1664 i, card->vlan[i].tx, card->vlan[i].rx);
1665 }
1666
Geoff Levandd4d7f1f2009-12-01 12:15:53 +00001667 if (card->vlan[GELIC_PORT_ETHERNET_0].tx) {
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001668 BUG_ON(!card->vlan[GELIC_PORT_WIRELESS].tx);
1669 card->vlan_required = 1;
1670 } else
1671 card->vlan_required = 0;
1672
1673 /* check wirelss capable firmware */
1674 if (ps3_compare_firmware_version(1, 6, 0) < 0) {
1675 card->vlan[GELIC_PORT_WIRELESS].tx = 0;
1676 card->vlan[GELIC_PORT_WIRELESS].rx = 0;
1677 }
1678
1679 dev_info(ctodev(card), "internal vlan %s\n",
1680 card->vlan_required? "enabled" : "disabled");
1681}
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001682/**
1683 * ps3_gelic_driver_probe - add a device to the control of this driver
1684 */
Geert Uytterhoeven48dce822009-06-10 04:38:58 +00001685static int __devinit ps3_gelic_driver_probe(struct ps3_system_bus_device *dev)
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001686{
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001687 struct gelic_card *card;
1688 struct net_device *netdev;
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001689 int result;
1690
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001691 pr_debug("%s: called\n", __func__);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001692 result = ps3_open_hv_device(dev);
1693
1694 if (result) {
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001695 dev_dbg(&dev->core, "%s:ps3_open_hv_device failed\n",
1696 __func__);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001697 goto fail_open;
1698 }
1699
1700 result = ps3_dma_region_create(dev->d_region);
1701
1702 if (result) {
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001703 dev_dbg(&dev->core, "%s:ps3_dma_region_create failed(%d)\n",
1704 __func__, result);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001705 BUG_ON("check region type");
1706 goto fail_dma_region;
1707 }
1708
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001709 /* alloc card/netdevice */
1710 card = gelic_alloc_card_net(&netdev);
1711 if (!card) {
1712 dev_info(&dev->core, "%s:gelic_net_alloc_card failed\n",
1713 __func__);
1714 result = -ENOMEM;
1715 goto fail_alloc_card;
1716 }
Geert Uytterhoeven03fa68c2009-06-10 04:38:54 +00001717 ps3_system_bus_set_drvdata(dev, card);
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001718 card->dev = dev;
1719
1720 /* get internal vlan info */
1721 gelic_card_get_vlan_info(card);
1722
Hideyuki Sasaki55873ed2009-12-01 12:15:58 +00001723 card->link_mode = GELIC_LV1_ETHER_AUTO_NEG;
1724
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001725 /* setup interrupt */
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001726 result = lv1_net_set_interrupt_status_indicator(bus_id(card),
1727 dev_id(card),
1728 ps3_mm_phys_to_lpar(__pa(&card->irq_status)),
1729 0);
1730
1731 if (result) {
1732 dev_dbg(&dev->core,
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001733 "%s:set_interrupt_status_indicator failed: %s\n",
1734 __func__, ps3_result(result));
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001735 result = -EIO;
1736 goto fail_status_indicator;
1737 }
1738
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001739 result = ps3_sb_event_receive_port_setup(dev, PS3_BINDING_CPU_ANY,
1740 &card->irq);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001741
1742 if (result) {
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001743 dev_info(ctodev(card),
1744 "%s:gelic_net_open_device failed (%d)\n",
1745 __func__, result);
1746 result = -EPERM;
1747 goto fail_alloc_irq;
1748 }
1749 result = request_irq(card->irq, gelic_card_interrupt,
1750 IRQF_DISABLED, netdev->name, card);
1751
1752 if (result) {
1753 dev_info(ctodev(card), "%s:request_irq failed (%d)\n",
1754 __func__, result);
1755 goto fail_request_irq;
1756 }
1757
1758 /* setup card structure */
1759 card->irq_mask = GELIC_CARD_RXINT | GELIC_CARD_TXINT |
1760 GELIC_CARD_PORT_STATUS_CHANGED;
1761 card->rx_csum = GELIC_CARD_RX_CSUM_DEFAULT;
1762
1763
1764 if (gelic_card_init_chain(card, &card->tx_chain,
1765 card->descr, GELIC_NET_TX_DESCRIPTORS))
1766 goto fail_alloc_tx;
1767 if (gelic_card_init_chain(card, &card->rx_chain,
1768 card->descr + GELIC_NET_TX_DESCRIPTORS,
1769 GELIC_NET_RX_DESCRIPTORS))
1770 goto fail_alloc_rx;
1771
1772 /* head of chain */
1773 card->tx_top = card->tx_chain.head;
1774 card->rx_top = card->rx_chain.head;
1775 dev_dbg(ctodev(card), "descr rx %p, tx %p, size %#lx, num %#x\n",
1776 card->rx_top, card->tx_top, sizeof(struct gelic_descr),
1777 GELIC_NET_RX_DESCRIPTORS);
1778 /* allocate rx skbs */
1779 if (gelic_card_alloc_rx_skbs(card))
1780 goto fail_alloc_skbs;
1781
1782 spin_lock_init(&card->tx_lock);
1783 card->tx_dma_progress = 0;
1784
1785 /* setup net_device structure */
1786 netdev->irq = card->irq;
1787 SET_NETDEV_DEV(netdev, &card->dev->core);
1788 gelic_ether_setup_netdev_ops(netdev, &card->napi);
1789 result = gelic_net_setup_netdev(netdev, card);
1790 if (result) {
1791 dev_dbg(&dev->core, "%s: setup_netdev failed %d",
1792 __func__, result);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001793 goto fail_setup_netdev;
1794 }
1795
Masakazu Mokuno09dde542008-02-07 19:58:57 +09001796#ifdef CONFIG_GELIC_WIRELESS
1797 if (gelic_wl_driver_probe(card)) {
1798 dev_dbg(&dev->core, "%s: WL init failed\n", __func__);
1799 goto fail_setup_netdev;
1800 }
1801#endif
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001802 pr_debug("%s: done\n", __func__);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001803 return 0;
1804
1805fail_setup_netdev:
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001806fail_alloc_skbs:
1807 gelic_card_free_chain(card, card->rx_chain.head);
1808fail_alloc_rx:
1809 gelic_card_free_chain(card, card->tx_chain.head);
1810fail_alloc_tx:
1811 free_irq(card->irq, card);
1812 netdev->irq = NO_IRQ;
1813fail_request_irq:
1814 ps3_sb_event_receive_port_destroy(dev, card->irq);
1815fail_alloc_irq:
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001816 lv1_net_set_interrupt_status_indicator(bus_id(card),
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001817 bus_id(card),
1818 0, 0);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001819fail_status_indicator:
Geert Uytterhoeven03fa68c2009-06-10 04:38:54 +00001820 ps3_system_bus_set_drvdata(dev, NULL);
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001821 kfree(netdev_card(netdev)->unalign);
1822 free_netdev(netdev);
1823fail_alloc_card:
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001824 ps3_dma_region_free(dev->d_region);
1825fail_dma_region:
1826 ps3_close_hv_device(dev);
1827fail_open:
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001828 return result;
1829}
1830
1831/**
1832 * ps3_gelic_driver_remove - remove a device from the control of this driver
1833 */
1834
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001835static int ps3_gelic_driver_remove(struct ps3_system_bus_device *dev)
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001836{
Geert Uytterhoeven03fa68c2009-06-10 04:38:54 +00001837 struct gelic_card *card = ps3_system_bus_get_drvdata(dev);
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001838 struct net_device *netdev0;
1839 pr_debug("%s: called\n", __func__);
1840
Hideyuki Sasaki55873ed2009-12-01 12:15:58 +00001841 /* set auto-negotiation */
1842 gelic_card_set_link_mode(card, GELIC_LV1_ETHER_AUTO_NEG);
1843
Masakazu Mokuno09dde542008-02-07 19:58:57 +09001844#ifdef CONFIG_GELIC_WIRELESS
1845 gelic_wl_driver_remove(card);
1846#endif
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001847 /* stop interrupt */
1848 gelic_card_set_irq_mask(card, 0);
1849
1850 /* turn off DMA, force end */
1851 gelic_card_disable_rxdmac(card);
1852 gelic_card_disable_txdmac(card);
1853
1854 /* release chains */
1855 gelic_card_release_tx_chain(card, 1);
1856 gelic_card_release_rx_chain(card);
1857
1858 gelic_card_free_chain(card, card->tx_top);
1859 gelic_card_free_chain(card, card->rx_top);
1860
Geoff Levandd4d7f1f2009-12-01 12:15:53 +00001861 netdev0 = card->netdev[GELIC_PORT_ETHERNET_0];
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001862 /* disconnect event port */
1863 free_irq(card->irq, card);
1864 netdev0->irq = NO_IRQ;
1865 ps3_sb_event_receive_port_destroy(card->dev, card->irq);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001866
1867 wait_event(card->waitq,
1868 atomic_read(&card->tx_timeout_task_counter) == 0);
1869
1870 lv1_net_set_interrupt_status_indicator(bus_id(card), dev_id(card),
1871 0 , 0);
1872
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001873 unregister_netdev(netdev0);
1874 kfree(netdev_card(netdev0)->unalign);
1875 free_netdev(netdev0);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001876
Geert Uytterhoeven03fa68c2009-06-10 04:38:54 +00001877 ps3_system_bus_set_drvdata(dev, NULL);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001878
1879 ps3_dma_region_free(dev->d_region);
1880
1881 ps3_close_hv_device(dev);
1882
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001883 pr_debug("%s: done\n", __func__);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001884 return 0;
1885}
1886
1887static struct ps3_system_bus_driver ps3_gelic_driver = {
1888 .match_id = PS3_MATCH_ID_GELIC,
1889 .probe = ps3_gelic_driver_probe,
1890 .remove = ps3_gelic_driver_remove,
1891 .shutdown = ps3_gelic_driver_remove,
1892 .core.name = "ps3_gelic_driver",
1893 .core.owner = THIS_MODULE,
1894};
1895
1896static int __init ps3_gelic_driver_init (void)
1897{
1898 return firmware_has_feature(FW_FEATURE_PS3_LV1)
1899 ? ps3_system_bus_driver_register(&ps3_gelic_driver)
1900 : -ENODEV;
1901}
1902
1903static void __exit ps3_gelic_driver_exit (void)
1904{
1905 ps3_system_bus_driver_unregister(&ps3_gelic_driver);
1906}
1907
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001908module_init(ps3_gelic_driver_init);
1909module_exit(ps3_gelic_driver_exit);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001910
1911MODULE_ALIAS(PS3_MODULE_ALIAS_GELIC);
1912