blob: 5bf229bb34c26e55879ddc62e13be82183e028c7 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Masakazu Mokuno02c18892007-07-05 20:11:16 +090034
35#include <linux/etherdevice.h>
36#include <linux/ethtool.h>
37#include <linux/if_vlan.h>
38
39#include <linux/in.h>
40#include <linux/ip.h>
41#include <linux/tcp.h>
42
43#include <linux/dma-mapping.h>
44#include <net/checksum.h>
45#include <asm/firmware.h>
46#include <asm/ps3.h>
47#include <asm/lv1call.h>
48
49#include "ps3_gelic_net.h"
Masakazu Mokuno09dde542008-02-07 19:58:57 +090050#include "ps3_gelic_wireless.h"
Masakazu Mokuno02c18892007-07-05 20:11:16 +090051
52#define DRV_NAME "Gelic Network Driver"
Masakazu Mokuno09dde542008-02-07 19:58:57 +090053#define DRV_VERSION "2.0"
Masakazu Mokuno02c18892007-07-05 20:11:16 +090054
55MODULE_AUTHOR("SCE Inc.");
56MODULE_DESCRIPTION("Gelic Network driver");
57MODULE_LICENSE("GPL");
58
Masakazu Mokuno589866f2008-02-07 19:58:42 +090059
60static inline void gelic_card_enable_rxdmac(struct gelic_card *card);
61static inline void gelic_card_disable_rxdmac(struct gelic_card *card);
62static inline void gelic_card_disable_txdmac(struct gelic_card *card);
63static inline void gelic_card_reset_chain(struct gelic_card *card,
64 struct gelic_descr_chain *chain,
65 struct gelic_descr *start_descr);
Masakazu Mokuno02c18892007-07-05 20:11:16 +090066
67/* set irq_mask */
Masakazu Mokuno589866f2008-02-07 19:58:42 +090068int gelic_card_set_irq_mask(struct gelic_card *card, u64 mask)
Masakazu Mokuno02c18892007-07-05 20:11:16 +090069{
70 int status;
71
72 status = lv1_net_set_interrupt_mask(bus_id(card), dev_id(card),
73 mask, 0);
74 if (status)
75 dev_info(ctodev(card),
Masakazu Mokuno589866f2008-02-07 19:58:42 +090076 "%s failed %d\n", __func__, status);
Masakazu Mokuno02c18892007-07-05 20:11:16 +090077 return status;
78}
Masakazu Mokuno589866f2008-02-07 19:58:42 +090079
Masakazu Mokuno59e97322008-02-07 19:58:08 +090080static inline void gelic_card_rx_irq_on(struct gelic_card *card)
Masakazu Mokuno02c18892007-07-05 20:11:16 +090081{
Masakazu Mokuno589866f2008-02-07 19:58:42 +090082 card->irq_mask |= GELIC_CARD_RXINT;
83 gelic_card_set_irq_mask(card, card->irq_mask);
Masakazu Mokuno02c18892007-07-05 20:11:16 +090084}
Masakazu Mokuno59e97322008-02-07 19:58:08 +090085static inline void gelic_card_rx_irq_off(struct gelic_card *card)
Masakazu Mokuno02c18892007-07-05 20:11:16 +090086{
Masakazu Mokuno589866f2008-02-07 19:58:42 +090087 card->irq_mask &= ~GELIC_CARD_RXINT;
88 gelic_card_set_irq_mask(card, card->irq_mask);
Masakazu Mokuno02c18892007-07-05 20:11:16 +090089}
Masakazu Mokuno01fed4c2008-02-07 19:58:32 +090090
Masakazu Mokuno589866f2008-02-07 19:58:42 +090091static void gelic_card_get_ether_port_status(struct gelic_card *card,
92 int inform)
Masakazu Mokuno01fed4c2008-02-07 19:58:32 +090093{
94 u64 v2;
95 struct net_device *ether_netdev;
96
97 lv1_net_control(bus_id(card), dev_id(card),
98 GELIC_LV1_GET_ETH_PORT_STATUS,
Geoff Levandd4d7f1f2009-12-01 12:15:53 +000099 GELIC_LV1_VLAN_TX_ETHERNET_0, 0, 0,
Masakazu Mokuno01fed4c2008-02-07 19:58:32 +0900100 &card->ether_port_status, &v2);
101
102 if (inform) {
Geoff Levandd4d7f1f2009-12-01 12:15:53 +0000103 ether_netdev = card->netdev[GELIC_PORT_ETHERNET_0];
Masakazu Mokuno01fed4c2008-02-07 19:58:32 +0900104 if (card->ether_port_status & GELIC_LV1_ETHER_LINK_UP)
105 netif_carrier_on(ether_netdev);
106 else
107 netif_carrier_off(ether_netdev);
108 }
109}
110
Hideyuki Sasaki55873ed2009-12-01 12:15:58 +0000111static int gelic_card_set_link_mode(struct gelic_card *card, int mode)
112{
113 int status;
114 u64 v1, v2;
115
116 status = lv1_net_control(bus_id(card), dev_id(card),
117 GELIC_LV1_SET_NEGOTIATION_MODE,
118 GELIC_LV1_PHY_ETHERNET_0, mode, 0, &v1, &v2);
119 if (status) {
120 pr_info("%s: failed setting negotiation mode %d\n", __func__,
121 status);
122 return -EBUSY;
123 }
124
125 card->link_mode = mode;
126 return 0;
127}
128
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900129void gelic_card_up(struct gelic_card *card)
130{
131 pr_debug("%s: called\n", __func__);
Daniel Walker2914f3e2008-05-22 00:00:03 -0700132 mutex_lock(&card->updown_lock);
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900133 if (atomic_inc_return(&card->users) == 1) {
134 pr_debug("%s: real do\n", __func__);
135 /* enable irq */
136 gelic_card_set_irq_mask(card, card->irq_mask);
137 /* start rx */
138 gelic_card_enable_rxdmac(card);
139
140 napi_enable(&card->napi);
141 }
Daniel Walker2914f3e2008-05-22 00:00:03 -0700142 mutex_unlock(&card->updown_lock);
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900143 pr_debug("%s: done\n", __func__);
144}
145
146void gelic_card_down(struct gelic_card *card)
147{
148 u64 mask;
149 pr_debug("%s: called\n", __func__);
Daniel Walker2914f3e2008-05-22 00:00:03 -0700150 mutex_lock(&card->updown_lock);
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900151 if (atomic_dec_if_positive(&card->users) == 0) {
152 pr_debug("%s: real do\n", __func__);
153 napi_disable(&card->napi);
154 /*
155 * Disable irq. Wireless interrupts will
156 * be disabled later if any
157 */
158 mask = card->irq_mask & (GELIC_CARD_WLAN_EVENT_RECEIVED |
159 GELIC_CARD_WLAN_COMMAND_COMPLETED);
160 gelic_card_set_irq_mask(card, mask);
161 /* stop rx */
162 gelic_card_disable_rxdmac(card);
163 gelic_card_reset_chain(card, &card->rx_chain,
164 card->descr + GELIC_NET_TX_DESCRIPTORS);
165 /* stop tx */
166 gelic_card_disable_txdmac(card);
167 }
Daniel Walker2914f3e2008-05-22 00:00:03 -0700168 mutex_unlock(&card->updown_lock);
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900169 pr_debug("%s: done\n", __func__);
170}
Masakazu Mokuno01fed4c2008-02-07 19:58:32 +0900171
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900172/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900173 * gelic_descr_get_status -- returns the status of a descriptor
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900174 * @descr: descriptor to look at
175 *
176 * returns the status as in the dmac_cmd_status field of the descriptor
177 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900178static enum gelic_descr_dma_status
179gelic_descr_get_status(struct gelic_descr *descr)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900180{
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900181 return be32_to_cpu(descr->dmac_cmd_status) & GELIC_DESCR_DMA_STAT_MASK;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900182}
183
184/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900185 * gelic_descr_set_status -- sets the status of a descriptor
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900186 * @descr: descriptor to change
187 * @status: status to set in the descriptor
188 *
189 * changes the status to the specified value. Doesn't change other bits
190 * in the status
191 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900192static void gelic_descr_set_status(struct gelic_descr *descr,
193 enum gelic_descr_dma_status status)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900194{
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900195 descr->dmac_cmd_status = cpu_to_be32(status |
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900196 (be32_to_cpu(descr->dmac_cmd_status) &
197 ~GELIC_DESCR_DMA_STAT_MASK));
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900198 /*
199 * dma_cmd_status field is used to indicate whether the descriptor
200 * is valid or not.
201 * Usually caller of this function wants to inform that to the
202 * hardware, so we assure here the hardware sees the change.
203 */
204 wmb();
205}
206
207/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900208 * gelic_card_free_chain - free descriptor chain
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900209 * @card: card structure
210 * @descr_in: address of desc
211 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900212static void gelic_card_free_chain(struct gelic_card *card,
213 struct gelic_descr *descr_in)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900214{
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900215 struct gelic_descr *descr;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900216
217 for (descr = descr_in; descr && descr->bus_addr; descr = descr->next) {
218 dma_unmap_single(ctodev(card), descr->bus_addr,
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900219 GELIC_DESCR_SIZE, DMA_BIDIRECTIONAL);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900220 descr->bus_addr = 0;
221 }
222}
223
224/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900225 * gelic_card_init_chain - links descriptor chain
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900226 * @card: card structure
227 * @chain: address of chain
228 * @start_descr: address of descriptor array
229 * @no: number of descriptors
230 *
231 * we manage a circular list that mirrors the hardware structure,
232 * except that the hardware uses bus addresses.
233 *
234 * returns 0 on success, <0 on failure
235 */
Geert Uytterhoeven48dce822009-06-10 04:38:58 +0000236static int __devinit gelic_card_init_chain(struct gelic_card *card,
237 struct gelic_descr_chain *chain,
238 struct gelic_descr *start_descr,
239 int no)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900240{
241 int i;
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900242 struct gelic_descr *descr;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900243
244 descr = start_descr;
245 memset(descr, 0, sizeof(*descr) * no);
246
247 /* set up the hardware pointers in each descriptor */
248 for (i = 0; i < no; i++, descr++) {
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900249 gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900250 descr->bus_addr =
251 dma_map_single(ctodev(card), descr,
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900252 GELIC_DESCR_SIZE,
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900253 DMA_BIDIRECTIONAL);
254
255 if (!descr->bus_addr)
256 goto iommu_error;
257
258 descr->next = descr + 1;
259 descr->prev = descr - 1;
260 }
261 /* make them as ring */
262 (descr - 1)->next = start_descr;
263 start_descr->prev = (descr - 1);
264
265 /* chain bus addr of hw descriptor */
266 descr = start_descr;
267 for (i = 0; i < no; i++, descr++) {
Masakazu Mokuno100e1d82008-02-07 19:57:54 +0900268 descr->next_descr_addr = cpu_to_be32(descr->next->bus_addr);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900269 }
270
271 chain->head = start_descr;
272 chain->tail = start_descr;
273
274 /* do not chain last hw descriptor */
275 (descr - 1)->next_descr_addr = 0;
276
277 return 0;
278
279iommu_error:
280 for (i--, descr--; 0 <= i; i--, descr--)
281 if (descr->bus_addr)
282 dma_unmap_single(ctodev(card), descr->bus_addr,
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900283 GELIC_DESCR_SIZE,
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900284 DMA_BIDIRECTIONAL);
285 return -ENOMEM;
286}
287
288/**
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900289 * gelic_card_reset_chain - reset status of a descriptor chain
290 * @card: card structure
291 * @chain: address of chain
292 * @start_descr: address of descriptor array
293 *
294 * Reset the status of dma descriptors to ready state
295 * and re-initialize the hardware chain for later use
296 */
297static void gelic_card_reset_chain(struct gelic_card *card,
298 struct gelic_descr_chain *chain,
299 struct gelic_descr *start_descr)
300{
301 struct gelic_descr *descr;
302
303 for (descr = start_descr; start_descr != descr->next; descr++) {
304 gelic_descr_set_status(descr, GELIC_DESCR_DMA_CARDOWNED);
305 descr->next_descr_addr = cpu_to_be32(descr->next->bus_addr);
306 }
307
308 chain->head = start_descr;
309 chain->tail = (descr - 1);
310
311 (descr - 1)->next_descr_addr = 0;
312}
313/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900314 * gelic_descr_prepare_rx - reinitializes a rx descriptor
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900315 * @card: card structure
316 * @descr: descriptor to re-init
317 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200318 * return 0 on success, <0 on failure
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900319 *
320 * allocates a new rx skb, iommu-maps it and attaches it to the descriptor.
321 * Activate the descriptor state-wise
322 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900323static int gelic_descr_prepare_rx(struct gelic_card *card,
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900324 struct gelic_descr *descr)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900325{
326 int offset;
327 unsigned int bufsize;
328
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900329 if (gelic_descr_get_status(descr) != GELIC_DESCR_DMA_NOT_IN_USE)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900330 dev_info(ctodev(card), "%s: ERROR status \n", __func__);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900331 /* we need to round up the buffer size to a multiple of 128 */
332 bufsize = ALIGN(GELIC_NET_MAX_MTU, GELIC_NET_RXBUF_ALIGN);
333
334 /* and we need to have it 128 byte aligned, therefore we allocate a
335 * bit more */
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900336 descr->skb = dev_alloc_skb(bufsize + GELIC_NET_RXBUF_ALIGN - 1);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900337 if (!descr->skb) {
338 descr->buf_addr = 0; /* tell DMAC don't touch memory */
339 dev_info(ctodev(card),
340 "%s:allocate skb failed !!\n", __func__);
341 return -ENOMEM;
342 }
Masakazu Mokuno100e1d82008-02-07 19:57:54 +0900343 descr->buf_size = cpu_to_be32(bufsize);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900344 descr->dmac_cmd_status = 0;
345 descr->result_size = 0;
346 descr->valid_size = 0;
347 descr->data_error = 0;
348
349 offset = ((unsigned long)descr->skb->data) &
350 (GELIC_NET_RXBUF_ALIGN - 1);
351 if (offset)
352 skb_reserve(descr->skb, GELIC_NET_RXBUF_ALIGN - offset);
353 /* io-mmu-map the skb */
Masakazu Mokuno100e1d82008-02-07 19:57:54 +0900354 descr->buf_addr = cpu_to_be32(dma_map_single(ctodev(card),
355 descr->skb->data,
356 GELIC_NET_MAX_MTU,
357 DMA_FROM_DEVICE));
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900358 if (!descr->buf_addr) {
359 dev_kfree_skb_any(descr->skb);
360 descr->skb = NULL;
361 dev_info(ctodev(card),
362 "%s:Could not iommu-map rx buffer\n", __func__);
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900363 gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900364 return -ENOMEM;
365 } else {
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900366 gelic_descr_set_status(descr, GELIC_DESCR_DMA_CARDOWNED);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900367 return 0;
368 }
369}
370
371/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900372 * gelic_card_release_rx_chain - free all skb of rx descr
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900373 * @card: card structure
374 *
375 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900376static void gelic_card_release_rx_chain(struct gelic_card *card)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900377{
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900378 struct gelic_descr *descr = card->rx_chain.head;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900379
380 do {
381 if (descr->skb) {
382 dma_unmap_single(ctodev(card),
Masakazu Mokuno100e1d82008-02-07 19:57:54 +0900383 be32_to_cpu(descr->buf_addr),
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900384 descr->skb->len,
385 DMA_FROM_DEVICE);
386 descr->buf_addr = 0;
387 dev_kfree_skb_any(descr->skb);
388 descr->skb = NULL;
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900389 gelic_descr_set_status(descr,
390 GELIC_DESCR_DMA_NOT_IN_USE);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900391 }
392 descr = descr->next;
393 } while (descr != card->rx_chain.head);
394}
395
396/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900397 * gelic_card_fill_rx_chain - fills descriptors/skbs in the rx chains
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900398 * @card: card structure
399 *
400 * fills all descriptors in the rx chain: allocates skbs
401 * and iommu-maps them.
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900402 * returns 0 on success, < 0 on failure
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900403 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900404static int gelic_card_fill_rx_chain(struct gelic_card *card)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900405{
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900406 struct gelic_descr *descr = card->rx_chain.head;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900407 int ret;
408
409 do {
410 if (!descr->skb) {
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900411 ret = gelic_descr_prepare_rx(card, descr);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900412 if (ret)
413 goto rewind;
414 }
415 descr = descr->next;
416 } while (descr != card->rx_chain.head);
417
418 return 0;
419rewind:
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900420 gelic_card_release_rx_chain(card);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900421 return ret;
422}
423
424/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900425 * gelic_card_alloc_rx_skbs - allocates rx skbs in rx descriptor chains
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900426 * @card: card structure
427 *
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900428 * returns 0 on success, < 0 on failure
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900429 */
Geert Uytterhoeven48dce822009-06-10 04:38:58 +0000430static int __devinit gelic_card_alloc_rx_skbs(struct gelic_card *card)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900431{
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900432 struct gelic_descr_chain *chain;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900433 int ret;
434 chain = &card->rx_chain;
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900435 ret = gelic_card_fill_rx_chain(card);
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900436 chain->tail = card->rx_top->prev; /* point to the last */
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900437 return ret;
438}
439
440/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900441 * gelic_descr_release_tx - processes a used tx descriptor
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900442 * @card: card structure
443 * @descr: descriptor to release
444 *
445 * releases a used tx descriptor (unmapping, freeing of skb)
446 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900447static void gelic_descr_release_tx(struct gelic_card *card,
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900448 struct gelic_descr *descr)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900449{
Masakazu Mokuno173261e2007-08-31 22:22:32 +0900450 struct sk_buff *skb = descr->skb;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900451
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900452 BUG_ON(!(be32_to_cpu(descr->data_status) & GELIC_DESCR_TX_TAIL));
453
454 dma_unmap_single(ctodev(card), be32_to_cpu(descr->buf_addr), skb->len,
455 DMA_TO_DEVICE);
Masakazu Mokuno173261e2007-08-31 22:22:32 +0900456 dev_kfree_skb_any(skb);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900457
458 descr->buf_addr = 0;
459 descr->buf_size = 0;
460 descr->next_descr_addr = 0;
461 descr->result_size = 0;
462 descr->valid_size = 0;
463 descr->data_status = 0;
464 descr->data_error = 0;
465 descr->skb = NULL;
466
467 /* set descr status */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900468 gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900469}
470
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900471static void gelic_card_stop_queues(struct gelic_card *card)
472{
Geoff Levandd4d7f1f2009-12-01 12:15:53 +0000473 netif_stop_queue(card->netdev[GELIC_PORT_ETHERNET_0]);
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900474
475 if (card->netdev[GELIC_PORT_WIRELESS])
476 netif_stop_queue(card->netdev[GELIC_PORT_WIRELESS]);
477}
478static void gelic_card_wake_queues(struct gelic_card *card)
479{
Geoff Levandd4d7f1f2009-12-01 12:15:53 +0000480 netif_wake_queue(card->netdev[GELIC_PORT_ETHERNET_0]);
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900481
482 if (card->netdev[GELIC_PORT_WIRELESS])
483 netif_wake_queue(card->netdev[GELIC_PORT_WIRELESS]);
484}
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900485/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900486 * gelic_card_release_tx_chain - processes sent tx descriptors
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900487 * @card: adapter structure
488 * @stop: net_stop sequence
489 *
490 * releases the tx descriptors that gelic has finished with
491 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900492static void gelic_card_release_tx_chain(struct gelic_card *card, int stop)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900493{
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900494 struct gelic_descr_chain *tx_chain;
495 enum gelic_descr_dma_status status;
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900496 struct net_device *netdev;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900497 int release = 0;
498
499 for (tx_chain = &card->tx_chain;
500 tx_chain->head != tx_chain->tail && tx_chain->tail;
501 tx_chain->tail = tx_chain->tail->next) {
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900502 status = gelic_descr_get_status(tx_chain->tail);
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900503 netdev = tx_chain->tail->skb->dev;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900504 switch (status) {
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900505 case GELIC_DESCR_DMA_RESPONSE_ERROR:
506 case GELIC_DESCR_DMA_PROTECTION_ERROR:
507 case GELIC_DESCR_DMA_FORCE_END:
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900508 if (printk_ratelimit())
509 dev_info(ctodev(card),
510 "%s: forcing end of tx descriptor " \
511 "with status %x\n",
512 __func__, status);
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900513 netdev->stats.tx_dropped++;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900514 break;
515
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900516 case GELIC_DESCR_DMA_COMPLETE:
Masakazu Mokuno48544cc262007-07-20 17:24:56 +0900517 if (tx_chain->tail->skb) {
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900518 netdev->stats.tx_packets++;
519 netdev->stats.tx_bytes +=
Masakazu Mokuno48544cc262007-07-20 17:24:56 +0900520 tx_chain->tail->skb->len;
521 }
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900522 break;
523
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900524 case GELIC_DESCR_DMA_CARDOWNED:
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900525 /* pending tx request */
526 default:
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900527 /* any other value (== GELIC_DESCR_DMA_NOT_IN_USE) */
Masakazu Mokuno48544cc262007-07-20 17:24:56 +0900528 if (!stop)
529 goto out;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900530 }
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900531 gelic_descr_release_tx(card, tx_chain->tail);
Masakazu Mokuno48544cc262007-07-20 17:24:56 +0900532 release ++;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900533 }
534out:
Masakazu Mokuno173261e2007-08-31 22:22:32 +0900535 if (!stop && release)
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900536 gelic_card_wake_queues(card);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900537}
538
539/**
540 * gelic_net_set_multi - sets multicast addresses and promisc flags
541 * @netdev: interface device structure
542 *
543 * gelic_net_set_multi configures multicast addresses as needed for the
544 * netdev interface. It also sets up multicast, allmulti and promisc
545 * flags appropriately
546 */
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900547void gelic_net_set_multi(struct net_device *netdev)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900548{
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900549 struct gelic_card *card = netdev_card(netdev);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900550 struct dev_mc_list *mc;
551 unsigned int i;
552 uint8_t *p;
553 u64 addr;
554 int status;
555
556 /* clear all multicast address */
557 status = lv1_net_remove_multicast_address(bus_id(card), dev_id(card),
558 0, 1);
559 if (status)
560 dev_err(ctodev(card),
561 "lv1_net_remove_multicast_address failed %d\n",
562 status);
563 /* set broadcast address */
564 status = lv1_net_add_multicast_address(bus_id(card), dev_id(card),
565 GELIC_NET_BROADCAST_ADDR, 0);
566 if (status)
567 dev_err(ctodev(card),
568 "lv1_net_add_multicast_address failed, %d\n",
569 status);
570
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900571 if ((netdev->flags & IFF_ALLMULTI) ||
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000572 (netdev_mc_count(netdev) > GELIC_NET_MC_COUNT_MAX)) {
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900573 status = lv1_net_add_multicast_address(bus_id(card),
574 dev_id(card),
575 0, 1);
576 if (status)
577 dev_err(ctodev(card),
578 "lv1_net_add_multicast_address failed, %d\n",
579 status);
580 return;
581 }
582
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900583 /* set multicast addresses */
Jiri Pirkof9dcbcc2010-02-23 09:19:49 +0000584 netdev_for_each_mc_addr(mc, netdev) {
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900585 addr = 0;
586 p = mc->dmi_addr;
587 for (i = 0; i < ETH_ALEN; i++) {
588 addr <<= 8;
589 addr |= *p++;
590 }
591 status = lv1_net_add_multicast_address(bus_id(card),
592 dev_id(card),
593 addr, 0);
594 if (status)
595 dev_err(ctodev(card),
596 "lv1_net_add_multicast_address failed, %d\n",
597 status);
598 }
599}
600
601/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900602 * gelic_card_enable_rxdmac - enables the receive DMA controller
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900603 * @card: card structure
604 *
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900605 * gelic_card_enable_rxdmac enables the DMA controller by setting RX_DMA_EN
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900606 * in the GDADMACCNTR register
607 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900608static inline void gelic_card_enable_rxdmac(struct gelic_card *card)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900609{
610 int status;
611
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900612#ifdef DEBUG
613 if (gelic_descr_get_status(card->rx_chain.head) !=
614 GELIC_DESCR_DMA_CARDOWNED) {
615 printk(KERN_ERR "%s: status=%x\n", __func__,
616 be32_to_cpu(card->rx_chain.head->dmac_cmd_status));
617 printk(KERN_ERR "%s: nextphy=%x\n", __func__,
618 be32_to_cpu(card->rx_chain.head->next_descr_addr));
619 printk(KERN_ERR "%s: head=%p\n", __func__,
620 card->rx_chain.head);
621 }
622#endif
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900623 status = lv1_net_start_rx_dma(bus_id(card), dev_id(card),
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900624 card->rx_chain.head->bus_addr, 0);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900625 if (status)
626 dev_info(ctodev(card),
627 "lv1_net_start_rx_dma failed, status=%d\n", status);
628}
629
630/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900631 * gelic_card_disable_rxdmac - disables the receive DMA controller
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900632 * @card: card structure
633 *
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900634 * gelic_card_disable_rxdmac terminates processing on the DMA controller by
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900635 * turing off DMA and issueing a force end
636 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900637static inline void gelic_card_disable_rxdmac(struct gelic_card *card)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900638{
639 int status;
640
641 /* this hvc blocks until the DMA in progress really stopped */
642 status = lv1_net_stop_rx_dma(bus_id(card), dev_id(card), 0);
643 if (status)
644 dev_err(ctodev(card),
645 "lv1_net_stop_rx_dma faild, %d\n", status);
646}
647
648/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900649 * gelic_card_disable_txdmac - disables the transmit DMA controller
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900650 * @card: card structure
651 *
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900652 * gelic_card_disable_txdmac terminates processing on the DMA controller by
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900653 * turing off DMA and issueing a force end
654 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900655static inline void gelic_card_disable_txdmac(struct gelic_card *card)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900656{
657 int status;
658
659 /* this hvc blocks until the DMA in progress really stopped */
660 status = lv1_net_stop_tx_dma(bus_id(card), dev_id(card), 0);
661 if (status)
662 dev_err(ctodev(card),
663 "lv1_net_stop_tx_dma faild, status=%d\n", status);
664}
665
666/**
667 * gelic_net_stop - called upon ifconfig down
668 * @netdev: interface device structure
669 *
670 * always returns 0
671 */
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900672int gelic_net_stop(struct net_device *netdev)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900673{
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900674 struct gelic_card *card;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900675
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900676 pr_debug("%s: start\n", __func__);
677
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900678 netif_stop_queue(netdev);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900679 netif_carrier_off(netdev);
680
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900681 card = netdev_card(netdev);
682 gelic_card_down(card);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900683
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900684 pr_debug("%s: done\n", __func__);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900685 return 0;
686}
687
688/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900689 * gelic_card_get_next_tx_descr - returns the next available tx descriptor
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900690 * @card: device structure to get descriptor from
691 *
692 * returns the address of the next descriptor, or NULL if not available.
693 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900694static struct gelic_descr *
695gelic_card_get_next_tx_descr(struct gelic_card *card)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900696{
697 if (!card->tx_chain.head)
698 return NULL;
Masakazu Mokuno173261e2007-08-31 22:22:32 +0900699 /* see if the next descriptor is free */
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900700 if (card->tx_chain.tail != card->tx_chain.head->next &&
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900701 gelic_descr_get_status(card->tx_chain.head) ==
702 GELIC_DESCR_DMA_NOT_IN_USE)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900703 return card->tx_chain.head;
704 else
705 return NULL;
706
707}
708
709/**
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900710 * gelic_net_set_txdescr_cmdstat - sets the tx descriptor command field
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900711 * @descr: descriptor structure to fill out
712 * @skb: packet to consider
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900713 *
714 * fills out the command and status field of the descriptor structure,
715 * depending on hardware checksum settings. This function assumes a wmb()
716 * has executed before.
717 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900718static void gelic_descr_set_tx_cmdstat(struct gelic_descr *descr,
719 struct sk_buff *skb)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900720{
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900721 if (skb->ip_summed != CHECKSUM_PARTIAL)
Masakazu Mokuno100e1d82008-02-07 19:57:54 +0900722 descr->dmac_cmd_status =
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900723 cpu_to_be32(GELIC_DESCR_DMA_CMD_NO_CHKSUM |
724 GELIC_DESCR_TX_DMA_FRAME_TAIL);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900725 else {
726 /* is packet ip?
727 * if yes: tcp? udp? */
728 if (skb->protocol == htons(ETH_P_IP)) {
729 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
730 descr->dmac_cmd_status =
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900731 cpu_to_be32(GELIC_DESCR_DMA_CMD_TCP_CHKSUM |
732 GELIC_DESCR_TX_DMA_FRAME_TAIL);
Masakazu Mokuno173261e2007-08-31 22:22:32 +0900733
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900734 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
735 descr->dmac_cmd_status =
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900736 cpu_to_be32(GELIC_DESCR_DMA_CMD_UDP_CHKSUM |
737 GELIC_DESCR_TX_DMA_FRAME_TAIL);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900738 else /*
739 * the stack should checksum non-tcp and non-udp
740 * packets on his own: NETIF_F_IP_CSUM
741 */
742 descr->dmac_cmd_status =
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900743 cpu_to_be32(GELIC_DESCR_DMA_CMD_NO_CHKSUM |
744 GELIC_DESCR_TX_DMA_FRAME_TAIL);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900745 }
746 }
747}
748
Masakazu Mokuno173261e2007-08-31 22:22:32 +0900749static inline struct sk_buff *gelic_put_vlan_tag(struct sk_buff *skb,
750 unsigned short tag)
751{
752 struct vlan_ethhdr *veth;
753 static unsigned int c;
754
755 if (skb_headroom(skb) < VLAN_HLEN) {
756 struct sk_buff *sk_tmp = skb;
757 pr_debug("%s: hd=%d c=%ud\n", __func__, skb_headroom(skb), c);
758 skb = skb_realloc_headroom(sk_tmp, VLAN_HLEN);
759 if (!skb)
760 return NULL;
761 dev_kfree_skb_any(sk_tmp);
762 }
763 veth = (struct vlan_ethhdr *)skb_push(skb, VLAN_HLEN);
764
765 /* Move the mac addresses to the top of buffer */
766 memmove(skb->data, skb->data + VLAN_HLEN, 2 * ETH_ALEN);
767
Harvey Harrison09640e62009-02-01 00:45:17 -0800768 veth->h_vlan_proto = cpu_to_be16(ETH_P_8021Q);
Masakazu Mokuno173261e2007-08-31 22:22:32 +0900769 veth->h_vlan_TCI = htons(tag);
770
771 return skb;
772}
773
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900774/**
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900775 * gelic_descr_prepare_tx - setup a descriptor for sending packets
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900776 * @card: card structure
777 * @descr: descriptor structure
778 * @skb: packet to use
779 *
780 * returns 0 on success, <0 on failure.
781 *
782 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900783static int gelic_descr_prepare_tx(struct gelic_card *card,
784 struct gelic_descr *descr,
785 struct sk_buff *skb)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900786{
Masakazu Mokuno173261e2007-08-31 22:22:32 +0900787 dma_addr_t buf;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900788
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900789 if (card->vlan_required) {
Masakazu Mokuno173261e2007-08-31 22:22:32 +0900790 struct sk_buff *skb_tmp;
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900791 enum gelic_port_type type;
792
793 type = netdev_port(skb->dev)->type;
Masakazu Mokuno173261e2007-08-31 22:22:32 +0900794 skb_tmp = gelic_put_vlan_tag(skb,
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900795 card->vlan[type].tx);
Masakazu Mokuno173261e2007-08-31 22:22:32 +0900796 if (!skb_tmp)
797 return -ENOMEM;
798 skb = skb_tmp;
Masakazu Mokuno48544cc262007-07-20 17:24:56 +0900799 }
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900800
Masakazu Mokuno173261e2007-08-31 22:22:32 +0900801 buf = dma_map_single(ctodev(card), skb->data, skb->len, DMA_TO_DEVICE);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900802
Masakazu Mokuno173261e2007-08-31 22:22:32 +0900803 if (!buf) {
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900804 dev_err(ctodev(card),
805 "dma map 2 failed (%p, %i). Dropping packet\n",
Masakazu Mokuno173261e2007-08-31 22:22:32 +0900806 skb->data, skb->len);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900807 return -ENOMEM;
808 }
809
Masakazu Mokuno100e1d82008-02-07 19:57:54 +0900810 descr->buf_addr = cpu_to_be32(buf);
811 descr->buf_size = cpu_to_be32(skb->len);
Masakazu Mokuno173261e2007-08-31 22:22:32 +0900812 descr->skb = skb;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900813 descr->data_status = 0;
Masakazu Mokuno173261e2007-08-31 22:22:32 +0900814 descr->next_descr_addr = 0; /* terminate hw descr */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900815 gelic_descr_set_tx_cmdstat(descr, skb);
Masakazu Mokuno48544cc262007-07-20 17:24:56 +0900816
817 /* bump free descriptor pointer */
Masakazu Mokuno173261e2007-08-31 22:22:32 +0900818 card->tx_chain.head = descr->next;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900819 return 0;
820}
821
822/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900823 * gelic_card_kick_txdma - enables TX DMA processing
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900824 * @card: card structure
825 * @descr: descriptor address to enable TX processing at
826 *
827 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900828static int gelic_card_kick_txdma(struct gelic_card *card,
829 struct gelic_descr *descr)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900830{
Masakazu Mokuno48544cc262007-07-20 17:24:56 +0900831 int status = 0;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900832
833 if (card->tx_dma_progress)
834 return 0;
835
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900836 if (gelic_descr_get_status(descr) == GELIC_DESCR_DMA_CARDOWNED) {
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900837 card->tx_dma_progress = 1;
Masakazu Mokunodc029ad2007-08-31 22:25:09 +0900838 status = lv1_net_start_tx_dma(bus_id(card), dev_id(card),
839 descr->bus_addr, 0);
840 if (status)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900841 dev_info(ctodev(card), "lv1_net_start_txdma failed," \
Masakazu Mokunodc029ad2007-08-31 22:25:09 +0900842 "status=%d\n", status);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900843 }
844 return status;
845}
846
847/**
848 * gelic_net_xmit - transmits a frame over the device
849 * @skb: packet to send out
850 * @netdev: interface device structure
851 *
852 * returns 0 on success, <0 on failure
853 */
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900854int gelic_net_xmit(struct sk_buff *skb, struct net_device *netdev)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900855{
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900856 struct gelic_card *card = netdev_card(netdev);
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900857 struct gelic_descr *descr;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900858 int result;
859 unsigned long flags;
860
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900861 spin_lock_irqsave(&card->tx_lock, flags);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900862
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900863 gelic_card_release_tx_chain(card, 0);
Masakazu Mokuno48544cc262007-07-20 17:24:56 +0900864
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900865 descr = gelic_card_get_next_tx_descr(card);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900866 if (!descr) {
Masakazu Mokuno48544cc262007-07-20 17:24:56 +0900867 /*
868 * no more descriptors free
869 */
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900870 gelic_card_stop_queues(card);
871 spin_unlock_irqrestore(&card->tx_lock, flags);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900872 return NETDEV_TX_BUSY;
873 }
Masakazu Mokuno48544cc262007-07-20 17:24:56 +0900874
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900875 result = gelic_descr_prepare_tx(card, descr, skb);
Masakazu Mokuno48544cc262007-07-20 17:24:56 +0900876 if (result) {
877 /*
878 * DMA map failed. As chanses are that failure
879 * would continue, just release skb and return
880 */
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900881 netdev->stats.tx_dropped++;
Masakazu Mokuno48544cc262007-07-20 17:24:56 +0900882 dev_kfree_skb_any(skb);
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900883 spin_unlock_irqrestore(&card->tx_lock, flags);
Masakazu Mokuno48544cc262007-07-20 17:24:56 +0900884 return NETDEV_TX_OK;
885 }
886 /*
887 * link this prepared descriptor to previous one
888 * to achieve high performance
889 */
Masakazu Mokuno100e1d82008-02-07 19:57:54 +0900890 descr->prev->next_descr_addr = cpu_to_be32(descr->bus_addr);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900891 /*
892 * as hardware descriptor is modified in the above lines,
893 * ensure that the hardware sees it
894 */
895 wmb();
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900896 if (gelic_card_kick_txdma(card, descr)) {
Masakazu Mokuno48544cc262007-07-20 17:24:56 +0900897 /*
898 * kick failed.
899 * release descriptors which were just prepared
900 */
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900901 netdev->stats.tx_dropped++;
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900902 gelic_descr_release_tx(card, descr);
903 gelic_descr_release_tx(card, descr->next);
Masakazu Mokuno48544cc262007-07-20 17:24:56 +0900904 card->tx_chain.tail = descr->next->next;
905 dev_info(ctodev(card), "%s: kick failure\n", __func__);
906 } else {
907 /* OK, DMA started/reserved */
908 netdev->trans_start = jiffies;
909 }
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900910
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900911 spin_unlock_irqrestore(&card->tx_lock, flags);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900912 return NETDEV_TX_OK;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900913}
914
915/**
916 * gelic_net_pass_skb_up - takes an skb from a descriptor and passes it on
917 * @descr: descriptor to process
918 * @card: card structure
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900919 * @netdev: net_device structure to be passed packet
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900920 *
921 * iommu-unmaps the skb, fills out skb structure and passes the data to the
922 * stack. The descriptor state is not changed.
923 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900924static void gelic_net_pass_skb_up(struct gelic_descr *descr,
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900925 struct gelic_card *card,
926 struct net_device *netdev)
927
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900928{
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900929 struct sk_buff *skb = descr->skb;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900930 u32 data_status, data_error;
931
Masakazu Mokuno100e1d82008-02-07 19:57:54 +0900932 data_status = be32_to_cpu(descr->data_status);
933 data_error = be32_to_cpu(descr->data_error);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900934 /* unmap skb buffer */
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900935 dma_unmap_single(ctodev(card), be32_to_cpu(descr->buf_addr),
936 GELIC_NET_MAX_MTU,
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900937 DMA_FROM_DEVICE);
938
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900939 skb_put(skb, be32_to_cpu(descr->valid_size)?
Masakazu Mokuno100e1d82008-02-07 19:57:54 +0900940 be32_to_cpu(descr->valid_size) :
941 be32_to_cpu(descr->result_size));
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900942 if (!descr->valid_size)
943 dev_info(ctodev(card), "buffer full %x %x %x\n",
Masakazu Mokuno100e1d82008-02-07 19:57:54 +0900944 be32_to_cpu(descr->result_size),
945 be32_to_cpu(descr->buf_size),
946 be32_to_cpu(descr->dmac_cmd_status));
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900947
948 descr->skb = NULL;
949 /*
950 * the card put 2 bytes vlan tag in front
951 * of the ethernet frame
952 */
953 skb_pull(skb, 2);
954 skb->protocol = eth_type_trans(skb, netdev);
955
956 /* checksum offload */
957 if (card->rx_csum) {
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900958 if ((data_status & GELIC_DESCR_DATA_STATUS_CHK_MASK) &&
959 (!(data_error & GELIC_DESCR_DATA_ERROR_CHK_MASK)))
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900960 skb->ip_summed = CHECKSUM_UNNECESSARY;
961 else
962 skb->ip_summed = CHECKSUM_NONE;
963 } else
964 skb->ip_summed = CHECKSUM_NONE;
965
966 /* update netdevice statistics */
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900967 netdev->stats.rx_packets++;
968 netdev->stats.rx_bytes += skb->len;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900969
970 /* pass skb up to stack */
971 netif_receive_skb(skb);
972}
973
974/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900975 * gelic_card_decode_one_descr - processes an rx descriptor
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900976 * @card: card structure
977 *
978 * returns 1 if a packet has been sent to the stack, otherwise 0
979 *
980 * processes an rx descriptor by iommu-unmapping the data buffer and passing
981 * the packet up to the stack
982 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900983static int gelic_card_decode_one_descr(struct gelic_card *card)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900984{
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900985 enum gelic_descr_dma_status status;
986 struct gelic_descr_chain *chain = &card->rx_chain;
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900987 struct gelic_descr *descr = chain->head;
988 struct net_device *netdev = NULL;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900989 int dmac_chain_ended;
990
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900991 status = gelic_descr_get_status(descr);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900992 /* is this descriptor terminated with next_descr == NULL? */
993 dmac_chain_ended =
Masakazu Mokuno100e1d82008-02-07 19:57:54 +0900994 be32_to_cpu(descr->dmac_cmd_status) &
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900995 GELIC_DESCR_RX_DMA_CHAIN_END;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900996
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900997 if (status == GELIC_DESCR_DMA_CARDOWNED)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900998 return 0;
999
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001000 if (status == GELIC_DESCR_DMA_NOT_IN_USE) {
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001001 dev_dbg(ctodev(card), "dormant descr? %p\n", descr);
1002 return 0;
1003 }
1004
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001005 /* netdevice select */
1006 if (card->vlan_required) {
1007 unsigned int i;
1008 u16 vid;
1009 vid = *(u16 *)(descr->skb->data) & VLAN_VID_MASK;
1010 for (i = 0; i < GELIC_PORT_MAX; i++) {
1011 if (card->vlan[i].rx == vid) {
1012 netdev = card->netdev[i];
1013 break;
1014 }
1015 };
1016 if (GELIC_PORT_MAX <= i) {
1017 pr_info("%s: unknown packet vid=%x\n", __func__, vid);
1018 goto refill;
1019 }
1020 } else
Geoff Levandd4d7f1f2009-12-01 12:15:53 +00001021 netdev = card->netdev[GELIC_PORT_ETHERNET_0];
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001022
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001023 if ((status == GELIC_DESCR_DMA_RESPONSE_ERROR) ||
1024 (status == GELIC_DESCR_DMA_PROTECTION_ERROR) ||
1025 (status == GELIC_DESCR_DMA_FORCE_END)) {
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001026 dev_info(ctodev(card), "dropping RX descriptor with state %x\n",
1027 status);
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001028 netdev->stats.rx_dropped++;
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001029 goto refill;
1030 }
1031
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001032 if (status == GELIC_DESCR_DMA_BUFFER_FULL) {
Masakazu Mokunofe6d3a42007-07-20 17:39:25 +09001033 /*
1034 * Buffer full would occur if and only if
1035 * the frame length was longer than the size of this
1036 * descriptor's buffer. If the frame length was equal
1037 * to or shorter than buffer'size, FRAME_END condition
1038 * would occur.
1039 * Anyway this frame was longer than the MTU,
1040 * just drop it.
1041 */
1042 dev_info(ctodev(card), "overlength frame\n");
1043 goto refill;
1044 }
1045 /*
1046 * descriptoers any other than FRAME_END here should
1047 * be treated as error.
1048 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001049 if (status != GELIC_DESCR_DMA_FRAME_END) {
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001050 dev_dbg(ctodev(card), "RX descriptor with state %x\n",
1051 status);
1052 goto refill;
1053 }
1054
1055 /* ok, we've got a packet in descr */
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001056 gelic_net_pass_skb_up(descr, card, netdev);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001057refill:
Masakazu Mokunofe6d3a42007-07-20 17:39:25 +09001058 /*
1059 * So that always DMAC can see the end
1060 * of the descriptor chain to avoid
1061 * from unwanted DMAC overrun.
1062 */
1063 descr->next_descr_addr = 0;
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001064
1065 /* change the descriptor state: */
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001066 gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001067
Masakazu Mokunofe6d3a42007-07-20 17:39:25 +09001068 /*
1069 * this call can fail, but for now, just leave this
1070 * decriptor without skb
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001071 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001072 gelic_descr_prepare_rx(card, descr);
Masakazu Mokunofe6d3a42007-07-20 17:39:25 +09001073
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001074 chain->tail = descr;
1075 chain->head = descr->next;
Masakazu Mokunofe6d3a42007-07-20 17:39:25 +09001076
1077 /*
1078 * Set this descriptor the end of the chain.
1079 */
Masakazu Mokuno100e1d82008-02-07 19:57:54 +09001080 descr->prev->next_descr_addr = cpu_to_be32(descr->bus_addr);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001081
Masakazu Mokunofe6d3a42007-07-20 17:39:25 +09001082 /*
1083 * If dmac chain was met, DMAC stopped.
1084 * thus re-enable it
1085 */
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001086 if (dmac_chain_ended) {
Masakazu Mokuno583aae12007-07-20 17:35:54 +09001087 card->rx_dma_restart_required = 1;
1088 dev_dbg(ctodev(card), "reenable rx dma scheduled\n");
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001089 }
1090
1091 return 1;
1092}
1093
1094/**
1095 * gelic_net_poll - NAPI poll function called by the stack to return packets
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001096 * @napi: napi structure
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001097 * @budget: number of packets we can pass to the stack at most
1098 *
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001099 * returns the number of the processed packets
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001100 *
1101 */
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001102static int gelic_net_poll(struct napi_struct *napi, int budget)
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001103{
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001104 struct gelic_card *card = container_of(napi, struct gelic_card, napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001105 int packets_done = 0;
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001106
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001107 while (packets_done < budget) {
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001108 if (!gelic_card_decode_one_descr(card))
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001109 break;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001110
1111 packets_done++;
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001112 }
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001113
1114 if (packets_done < budget) {
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001115 napi_complete(napi);
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001116 gelic_card_rx_irq_on(card);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001117 }
1118 return packets_done;
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001119}
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001120/**
1121 * gelic_net_change_mtu - changes the MTU of an interface
1122 * @netdev: interface device structure
1123 * @new_mtu: new MTU value
1124 *
1125 * returns 0 on success, <0 on failure
1126 */
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001127int gelic_net_change_mtu(struct net_device *netdev, int new_mtu)
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001128{
1129 /* no need to re-alloc skbs or so -- the max mtu is about 2.3k
1130 * and mtu is outbound only anyway */
1131 if ((new_mtu < GELIC_NET_MIN_MTU) ||
1132 (new_mtu > GELIC_NET_MAX_MTU)) {
1133 return -EINVAL;
1134 }
1135 netdev->mtu = new_mtu;
1136 return 0;
1137}
1138
1139/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001140 * gelic_card_interrupt - event handler for gelic_net
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001141 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001142static irqreturn_t gelic_card_interrupt(int irq, void *ptr)
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001143{
1144 unsigned long flags;
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001145 struct gelic_card *card = ptr;
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001146 u64 status;
1147
1148 status = card->irq_status;
1149
1150 if (!status)
1151 return IRQ_NONE;
1152
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001153 status &= card->irq_mask;
1154
Masakazu Mokuno583aae12007-07-20 17:35:54 +09001155 if (card->rx_dma_restart_required) {
1156 card->rx_dma_restart_required = 0;
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001157 gelic_card_enable_rxdmac(card);
Masakazu Mokuno583aae12007-07-20 17:35:54 +09001158 }
1159
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001160 if (status & GELIC_CARD_RXINT) {
1161 gelic_card_rx_irq_off(card);
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001162 napi_schedule(&card->napi);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001163 }
1164
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001165 if (status & GELIC_CARD_TXINT) {
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001166 spin_lock_irqsave(&card->tx_lock, flags);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001167 card->tx_dma_progress = 0;
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001168 gelic_card_release_tx_chain(card, 0);
Masakazu Mokuno48544cc262007-07-20 17:24:56 +09001169 /* kick outstanding tx descriptor if any */
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001170 gelic_card_kick_txdma(card, card->tx_chain.tail);
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001171 spin_unlock_irqrestore(&card->tx_lock, flags);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001172 }
Masakazu Mokuno01fed4c2008-02-07 19:58:32 +09001173
1174 /* ether port status changed */
1175 if (status & GELIC_CARD_PORT_STATUS_CHANGED)
1176 gelic_card_get_ether_port_status(card, 1);
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001177
Masakazu Mokuno09dde542008-02-07 19:58:57 +09001178#ifdef CONFIG_GELIC_WIRELESS
1179 if (status & (GELIC_CARD_WLAN_EVENT_RECEIVED |
1180 GELIC_CARD_WLAN_COMMAND_COMPLETED))
1181 gelic_wl_interrupt(card->netdev[GELIC_PORT_WIRELESS], status);
1182#endif
1183
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001184 return IRQ_HANDLED;
1185}
1186
1187#ifdef CONFIG_NET_POLL_CONTROLLER
1188/**
1189 * gelic_net_poll_controller - artificial interrupt for netconsole etc.
1190 * @netdev: interface device structure
1191 *
1192 * see Documentation/networking/netconsole.txt
1193 */
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001194void gelic_net_poll_controller(struct net_device *netdev)
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001195{
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001196 struct gelic_card *card = netdev_card(netdev);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001197
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001198 gelic_card_set_irq_mask(card, 0);
1199 gelic_card_interrupt(netdev->irq, netdev);
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001200 gelic_card_set_irq_mask(card, card->irq_mask);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001201}
1202#endif /* CONFIG_NET_POLL_CONTROLLER */
1203
1204/**
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001205 * gelic_net_open - called upon ifonfig up
1206 * @netdev: interface device structure
1207 *
1208 * returns 0 on success, <0 on failure
1209 *
1210 * gelic_net_open allocates all the descriptors and memory needed for
1211 * operation, sets up multicast list and enables interrupts
1212 */
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001213int gelic_net_open(struct net_device *netdev)
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001214{
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001215 struct gelic_card *card = netdev_card(netdev);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001216
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001217 dev_dbg(ctodev(card), " -> %s %p\n", __func__, netdev);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001218
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001219 gelic_card_up(card);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001220
1221 netif_start_queue(netdev);
Masakazu Mokuno01fed4c2008-02-07 19:58:32 +09001222 gelic_card_get_ether_port_status(card, 1);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001223
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001224 dev_dbg(ctodev(card), " <- %s\n", __func__);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001225 return 0;
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001226}
1227
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001228void gelic_net_get_drvinfo(struct net_device *netdev,
1229 struct ethtool_drvinfo *info)
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001230{
1231 strncpy(info->driver, DRV_NAME, sizeof(info->driver) - 1);
1232 strncpy(info->version, DRV_VERSION, sizeof(info->version) - 1);
1233}
1234
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001235static int gelic_ether_get_settings(struct net_device *netdev,
1236 struct ethtool_cmd *cmd)
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001237{
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001238 struct gelic_card *card = netdev_card(netdev);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001239
Masakazu Mokuno01fed4c2008-02-07 19:58:32 +09001240 gelic_card_get_ether_port_status(card, 0);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001241
Masakazu Mokuno01fed4c2008-02-07 19:58:32 +09001242 if (card->ether_port_status & GELIC_LV1_ETHER_FULL_DUPLEX)
1243 cmd->duplex = DUPLEX_FULL;
1244 else
1245 cmd->duplex = DUPLEX_HALF;
1246
1247 switch (card->ether_port_status & GELIC_LV1_ETHER_SPEED_MASK) {
1248 case GELIC_LV1_ETHER_SPEED_10:
1249 cmd->speed = SPEED_10;
1250 break;
1251 case GELIC_LV1_ETHER_SPEED_100:
1252 cmd->speed = SPEED_100;
1253 break;
1254 case GELIC_LV1_ETHER_SPEED_1000:
1255 cmd->speed = SPEED_1000;
1256 break;
1257 default:
1258 pr_info("%s: speed unknown\n", __func__);
1259 cmd->speed = SPEED_10;
1260 break;
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001261 }
Masakazu Mokuno01fed4c2008-02-07 19:58:32 +09001262
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001263 cmd->supported = SUPPORTED_TP | SUPPORTED_Autoneg |
1264 SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full |
1265 SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full |
Hideyuki Sasaki55873ed2009-12-01 12:15:58 +00001266 SUPPORTED_1000baseT_Full;
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001267 cmd->advertising = cmd->supported;
Hideyuki Sasaki55873ed2009-12-01 12:15:58 +00001268 if (card->link_mode & GELIC_LV1_ETHER_AUTO_NEG) {
1269 cmd->autoneg = AUTONEG_ENABLE;
1270 } else {
1271 cmd->autoneg = AUTONEG_DISABLE;
1272 cmd->advertising &= ~ADVERTISED_Autoneg;
1273 }
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001274 cmd->port = PORT_TP;
1275
1276 return 0;
1277}
1278
Hideyuki Sasaki55873ed2009-12-01 12:15:58 +00001279static int gelic_ether_set_settings(struct net_device *netdev,
1280 struct ethtool_cmd *cmd)
1281{
1282 struct gelic_card *card = netdev_card(netdev);
1283 u64 mode;
1284 int ret;
1285
1286 if (cmd->autoneg == AUTONEG_ENABLE) {
1287 mode = GELIC_LV1_ETHER_AUTO_NEG;
1288 } else {
1289 switch (cmd->speed) {
1290 case SPEED_10:
1291 mode = GELIC_LV1_ETHER_SPEED_10;
1292 break;
1293 case SPEED_100:
1294 mode = GELIC_LV1_ETHER_SPEED_100;
1295 break;
1296 case SPEED_1000:
1297 mode = GELIC_LV1_ETHER_SPEED_1000;
1298 break;
1299 default:
1300 return -EINVAL;
1301 }
1302 if (cmd->duplex == DUPLEX_FULL)
1303 mode |= GELIC_LV1_ETHER_FULL_DUPLEX;
1304 else if (cmd->speed == SPEED_1000) {
1305 pr_info("1000 half duplex is not supported.\n");
1306 return -EINVAL;
1307 }
1308 }
1309
1310 ret = gelic_card_set_link_mode(card, mode);
1311
1312 if (ret)
1313 return ret;
1314
1315 return 0;
1316}
1317
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001318u32 gelic_net_get_rx_csum(struct net_device *netdev)
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001319{
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001320 struct gelic_card *card = netdev_card(netdev);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001321
1322 return card->rx_csum;
1323}
1324
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001325int gelic_net_set_rx_csum(struct net_device *netdev, u32 data)
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001326{
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001327 struct gelic_card *card = netdev_card(netdev);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001328
1329 card->rx_csum = data;
1330 return 0;
1331}
1332
Masakazu Mokuno3faac212008-03-27 11:39:31 +11001333static void gelic_net_get_wol(struct net_device *netdev,
1334 struct ethtool_wolinfo *wol)
1335{
1336 if (0 <= ps3_compare_firmware_version(2, 2, 0))
1337 wol->supported = WAKE_MAGIC;
1338 else
1339 wol->supported = 0;
1340
1341 wol->wolopts = ps3_sys_manager_get_wol() ? wol->supported : 0;
1342 memset(&wol->sopass, 0, sizeof(wol->sopass));
1343}
1344static int gelic_net_set_wol(struct net_device *netdev,
1345 struct ethtool_wolinfo *wol)
1346{
1347 int status;
1348 struct gelic_card *card;
1349 u64 v1, v2;
1350
1351 if (ps3_compare_firmware_version(2, 2, 0) < 0 ||
1352 !capable(CAP_NET_ADMIN))
1353 return -EPERM;
1354
1355 if (wol->wolopts & ~WAKE_MAGIC)
1356 return -EINVAL;
1357
1358 card = netdev_card(netdev);
1359 if (wol->wolopts & WAKE_MAGIC) {
1360 status = lv1_net_control(bus_id(card), dev_id(card),
1361 GELIC_LV1_SET_WOL,
1362 GELIC_LV1_WOL_MAGIC_PACKET,
1363 0, GELIC_LV1_WOL_MP_ENABLE,
1364 &v1, &v2);
1365 if (status) {
1366 pr_info("%s: enabling WOL failed %d\n", __func__,
1367 status);
1368 status = -EIO;
1369 goto done;
1370 }
1371 status = lv1_net_control(bus_id(card), dev_id(card),
1372 GELIC_LV1_SET_WOL,
1373 GELIC_LV1_WOL_ADD_MATCH_ADDR,
1374 0, GELIC_LV1_WOL_MATCH_ALL,
1375 &v1, &v2);
1376 if (!status)
1377 ps3_sys_manager_set_wol(1);
1378 else {
1379 pr_info("%s: enabling WOL filter failed %d\n",
1380 __func__, status);
1381 status = -EIO;
1382 }
1383 } else {
1384 status = lv1_net_control(bus_id(card), dev_id(card),
1385 GELIC_LV1_SET_WOL,
1386 GELIC_LV1_WOL_MAGIC_PACKET,
1387 0, GELIC_LV1_WOL_MP_DISABLE,
1388 &v1, &v2);
1389 if (status) {
1390 pr_info("%s: disabling WOL failed %d\n", __func__,
1391 status);
1392 status = -EIO;
1393 goto done;
1394 }
1395 status = lv1_net_control(bus_id(card), dev_id(card),
1396 GELIC_LV1_SET_WOL,
1397 GELIC_LV1_WOL_DELETE_MATCH_ADDR,
1398 0, GELIC_LV1_WOL_MATCH_ALL,
1399 &v1, &v2);
1400 if (!status)
1401 ps3_sys_manager_set_wol(0);
1402 else {
1403 pr_info("%s: removing WOL filter failed %d\n",
1404 __func__, status);
1405 status = -EIO;
1406 }
1407 }
1408done:
1409 return status;
1410}
1411
Stephen Hemminger0fc0b732009-09-02 01:03:33 -07001412static const struct ethtool_ops gelic_ether_ethtool_ops = {
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001413 .get_drvinfo = gelic_net_get_drvinfo,
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001414 .get_settings = gelic_ether_get_settings,
Hideyuki Sasaki55873ed2009-12-01 12:15:58 +00001415 .set_settings = gelic_ether_set_settings,
Masakazu Mokuno7bc56b92008-02-07 19:58:20 +09001416 .get_link = ethtool_op_get_link,
Masakazu Mokuno7bc56b92008-02-07 19:58:20 +09001417 .get_tx_csum = ethtool_op_get_tx_csum,
1418 .set_tx_csum = ethtool_op_set_tx_csum,
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001419 .get_rx_csum = gelic_net_get_rx_csum,
1420 .set_rx_csum = gelic_net_set_rx_csum,
Masakazu Mokuno3faac212008-03-27 11:39:31 +11001421 .get_wol = gelic_net_get_wol,
1422 .set_wol = gelic_net_set_wol,
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001423};
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001424
1425/**
1426 * gelic_net_tx_timeout_task - task scheduled by the watchdog timeout
1427 * function (to be called not under interrupt status)
1428 * @work: work is context of tx timout task
1429 *
1430 * called as task when tx hangs, resets interface (if interface is up)
1431 */
1432static void gelic_net_tx_timeout_task(struct work_struct *work)
1433{
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001434 struct gelic_card *card =
1435 container_of(work, struct gelic_card, tx_timeout_task);
Geoff Levandd4d7f1f2009-12-01 12:15:53 +00001436 struct net_device *netdev = card->netdev[GELIC_PORT_ETHERNET_0];
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001437
1438 dev_info(ctodev(card), "%s:Timed out. Restarting... \n", __func__);
1439
1440 if (!(netdev->flags & IFF_UP))
1441 goto out;
1442
1443 netif_device_detach(netdev);
1444 gelic_net_stop(netdev);
1445
1446 gelic_net_open(netdev);
1447 netif_device_attach(netdev);
1448
1449out:
1450 atomic_dec(&card->tx_timeout_task_counter);
1451}
1452
1453/**
1454 * gelic_net_tx_timeout - called when the tx timeout watchdog kicks in.
1455 * @netdev: interface device structure
1456 *
1457 * called, if tx hangs. Schedules a task that resets the interface
1458 */
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001459void gelic_net_tx_timeout(struct net_device *netdev)
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001460{
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001461 struct gelic_card *card;
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001462
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001463 card = netdev_card(netdev);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001464 atomic_inc(&card->tx_timeout_task_counter);
1465 if (netdev->flags & IFF_UP)
1466 schedule_work(&card->tx_timeout_task);
1467 else
1468 atomic_dec(&card->tx_timeout_task_counter);
1469}
1470
Masakazu Mokuno5384e832009-01-15 22:47:29 +00001471static const struct net_device_ops gelic_netdevice_ops = {
1472 .ndo_open = gelic_net_open,
1473 .ndo_stop = gelic_net_stop,
1474 .ndo_start_xmit = gelic_net_xmit,
1475 .ndo_set_multicast_list = gelic_net_set_multi,
1476 .ndo_change_mtu = gelic_net_change_mtu,
1477 .ndo_tx_timeout = gelic_net_tx_timeout,
Ben Hutchings240c1022009-07-09 17:54:35 +00001478 .ndo_set_mac_address = eth_mac_addr,
Masakazu Mokuno5384e832009-01-15 22:47:29 +00001479 .ndo_validate_addr = eth_validate_addr,
1480#ifdef CONFIG_NET_POLL_CONTROLLER
1481 .ndo_poll_controller = gelic_net_poll_controller,
1482#endif
1483};
1484
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001485/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001486 * gelic_ether_setup_netdev_ops - initialization of net_device operations
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001487 * @netdev: net_device structure
1488 *
1489 * fills out function pointers in the net_device structure
1490 */
Geert Uytterhoeven48dce822009-06-10 04:38:58 +00001491static void __devinit gelic_ether_setup_netdev_ops(struct net_device *netdev,
1492 struct napi_struct *napi)
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001493{
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001494 netdev->watchdog_timeo = GELIC_NET_WATCHDOG_TIMEOUT;
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001495 /* NAPI */
1496 netif_napi_add(netdev, napi,
1497 gelic_net_poll, GELIC_NET_NAPI_WEIGHT);
1498 netdev->ethtool_ops = &gelic_ether_ethtool_ops;
Masakazu Mokuno5384e832009-01-15 22:47:29 +00001499 netdev->netdev_ops = &gelic_netdevice_ops;
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001500}
1501
1502/**
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001503 * gelic_ether_setup_netdev - initialization of net_device
1504 * @netdev: net_device structure
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001505 * @card: card structure
1506 *
1507 * Returns 0 on success or <0 on failure
1508 *
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001509 * gelic_ether_setup_netdev initializes the net_device structure
1510 * and register it.
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001511 **/
Geert Uytterhoeven48dce822009-06-10 04:38:58 +00001512int __devinit gelic_net_setup_netdev(struct net_device *netdev,
1513 struct gelic_card *card)
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001514{
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001515 int status;
1516 u64 v1, v2;
1517
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001518 netdev->features = NETIF_F_IP_CSUM;
1519
1520 status = lv1_net_control(bus_id(card), dev_id(card),
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001521 GELIC_LV1_GET_MAC_ADDRESS,
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001522 0, 0, 0, &v1, &v2);
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001523 v1 <<= 16;
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001524 if (status || !is_valid_ether_addr((u8 *)&v1)) {
1525 dev_info(ctodev(card),
1526 "%s:lv1_net_control GET_MAC_ADDR failed %d\n",
1527 __func__, status);
1528 return -EINVAL;
1529 }
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001530 memcpy(netdev->dev_addr, &v1, ETH_ALEN);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001531
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001532 if (card->vlan_required) {
Masakazu Mokuno173261e2007-08-31 22:22:32 +09001533 netdev->hard_header_len += VLAN_HLEN;
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001534 /*
1535 * As vlan is internally used,
1536 * we can not receive vlan packets
1537 */
1538 netdev->features |= NETIF_F_VLAN_CHALLENGED;
Masakazu Mokuno173261e2007-08-31 22:22:32 +09001539 }
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001540
1541 status = register_netdev(netdev);
1542 if (status) {
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001543 dev_err(ctodev(card), "%s:Couldn't register %s %d\n",
1544 __func__, netdev->name, status);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001545 return status;
1546 }
Johannes Berge1749612008-10-27 15:59:26 -07001547 dev_info(ctodev(card), "%s: MAC addr %pM\n",
1548 netdev->name, netdev->dev_addr);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001549
1550 return 0;
1551}
1552
1553/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001554 * gelic_alloc_card_net - allocates net_device and card structure
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001555 *
1556 * returns the card structure or NULL in case of errors
1557 *
1558 * the card and net_device structures are linked to each other
1559 */
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001560#define GELIC_ALIGN (32)
Geert Uytterhoeven48dce822009-06-10 04:38:58 +00001561static struct gelic_card * __devinit gelic_alloc_card_net(struct net_device **netdev)
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001562{
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001563 struct gelic_card *card;
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001564 struct gelic_port *port;
1565 void *p;
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001566 size_t alloc_size;
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001567 /*
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001568 * gelic requires dma descriptor is 32 bytes aligned and
1569 * the hypervisor requires irq_status is 8 bytes aligned.
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001570 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001571 BUILD_BUG_ON(offsetof(struct gelic_card, irq_status) % 8);
1572 BUILD_BUG_ON(offsetof(struct gelic_card, descr) % 32);
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001573 alloc_size =
1574 sizeof(struct gelic_card) +
1575 sizeof(struct gelic_descr) * GELIC_NET_RX_DESCRIPTORS +
1576 sizeof(struct gelic_descr) * GELIC_NET_TX_DESCRIPTORS +
1577 GELIC_ALIGN - 1;
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001578
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001579 p = kzalloc(alloc_size, GFP_KERNEL);
1580 if (!p)
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001581 return NULL;
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001582 card = PTR_ALIGN(p, GELIC_ALIGN);
1583 card->unalign = p;
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001584
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001585 /*
1586 * alloc netdev
1587 */
1588 *netdev = alloc_etherdev(sizeof(struct gelic_port));
1589 if (!netdev) {
1590 kfree(card->unalign);
1591 return NULL;
1592 }
1593 port = netdev_priv(*netdev);
1594
1595 /* gelic_port */
1596 port->netdev = *netdev;
1597 port->card = card;
Geoff Levandd4d7f1f2009-12-01 12:15:53 +00001598 port->type = GELIC_PORT_ETHERNET_0;
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001599
1600 /* gelic_card */
Geoff Levandd4d7f1f2009-12-01 12:15:53 +00001601 card->netdev[GELIC_PORT_ETHERNET_0] = *netdev;
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001602
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001603 INIT_WORK(&card->tx_timeout_task, gelic_net_tx_timeout_task);
1604 init_waitqueue_head(&card->waitq);
1605 atomic_set(&card->tx_timeout_task_counter, 0);
Daniel Walker2914f3e2008-05-22 00:00:03 -07001606 mutex_init(&card->updown_lock);
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001607 atomic_set(&card->users, 0);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001608
1609 return card;
1610}
1611
Geert Uytterhoeven48dce822009-06-10 04:38:58 +00001612static void __devinit gelic_card_get_vlan_info(struct gelic_card *card)
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001613{
1614 u64 v1, v2;
1615 int status;
1616 unsigned int i;
1617 struct {
1618 int tx;
1619 int rx;
1620 } vlan_id_ix[2] = {
Geoff Levandd4d7f1f2009-12-01 12:15:53 +00001621 [GELIC_PORT_ETHERNET_0] = {
1622 .tx = GELIC_LV1_VLAN_TX_ETHERNET_0,
1623 .rx = GELIC_LV1_VLAN_RX_ETHERNET_0
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001624 },
1625 [GELIC_PORT_WIRELESS] = {
1626 .tx = GELIC_LV1_VLAN_TX_WIRELESS,
1627 .rx = GELIC_LV1_VLAN_RX_WIRELESS
1628 }
1629 };
1630
1631 for (i = 0; i < ARRAY_SIZE(vlan_id_ix); i++) {
1632 /* tx tag */
1633 status = lv1_net_control(bus_id(card), dev_id(card),
1634 GELIC_LV1_GET_VLAN_ID,
1635 vlan_id_ix[i].tx,
1636 0, 0, &v1, &v2);
1637 if (status || !v1) {
1638 if (status != LV1_NO_ENTRY)
1639 dev_dbg(ctodev(card),
1640 "get vlan id for tx(%d) failed(%d)\n",
1641 vlan_id_ix[i].tx, status);
1642 card->vlan[i].tx = 0;
1643 card->vlan[i].rx = 0;
1644 continue;
1645 }
1646 card->vlan[i].tx = (u16)v1;
1647
1648 /* rx tag */
1649 status = lv1_net_control(bus_id(card), dev_id(card),
1650 GELIC_LV1_GET_VLAN_ID,
1651 vlan_id_ix[i].rx,
1652 0, 0, &v1, &v2);
1653 if (status || !v1) {
1654 if (status != LV1_NO_ENTRY)
1655 dev_info(ctodev(card),
1656 "get vlan id for rx(%d) failed(%d)\n",
1657 vlan_id_ix[i].rx, status);
1658 card->vlan[i].tx = 0;
1659 card->vlan[i].rx = 0;
1660 continue;
1661 }
1662 card->vlan[i].rx = (u16)v1;
1663
1664 dev_dbg(ctodev(card), "vlan_id[%d] tx=%02x rx=%02x\n",
1665 i, card->vlan[i].tx, card->vlan[i].rx);
1666 }
1667
Geoff Levandd4d7f1f2009-12-01 12:15:53 +00001668 if (card->vlan[GELIC_PORT_ETHERNET_0].tx) {
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001669 BUG_ON(!card->vlan[GELIC_PORT_WIRELESS].tx);
1670 card->vlan_required = 1;
1671 } else
1672 card->vlan_required = 0;
1673
1674 /* check wirelss capable firmware */
1675 if (ps3_compare_firmware_version(1, 6, 0) < 0) {
1676 card->vlan[GELIC_PORT_WIRELESS].tx = 0;
1677 card->vlan[GELIC_PORT_WIRELESS].rx = 0;
1678 }
1679
1680 dev_info(ctodev(card), "internal vlan %s\n",
1681 card->vlan_required? "enabled" : "disabled");
1682}
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001683/**
1684 * ps3_gelic_driver_probe - add a device to the control of this driver
1685 */
Geert Uytterhoeven48dce822009-06-10 04:38:58 +00001686static int __devinit ps3_gelic_driver_probe(struct ps3_system_bus_device *dev)
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001687{
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001688 struct gelic_card *card;
1689 struct net_device *netdev;
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001690 int result;
1691
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001692 pr_debug("%s: called\n", __func__);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001693 result = ps3_open_hv_device(dev);
1694
1695 if (result) {
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001696 dev_dbg(&dev->core, "%s:ps3_open_hv_device failed\n",
1697 __func__);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001698 goto fail_open;
1699 }
1700
1701 result = ps3_dma_region_create(dev->d_region);
1702
1703 if (result) {
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001704 dev_dbg(&dev->core, "%s:ps3_dma_region_create failed(%d)\n",
1705 __func__, result);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001706 BUG_ON("check region type");
1707 goto fail_dma_region;
1708 }
1709
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001710 /* alloc card/netdevice */
1711 card = gelic_alloc_card_net(&netdev);
1712 if (!card) {
1713 dev_info(&dev->core, "%s:gelic_net_alloc_card failed\n",
1714 __func__);
1715 result = -ENOMEM;
1716 goto fail_alloc_card;
1717 }
Geert Uytterhoeven03fa68c2009-06-10 04:38:54 +00001718 ps3_system_bus_set_drvdata(dev, card);
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001719 card->dev = dev;
1720
1721 /* get internal vlan info */
1722 gelic_card_get_vlan_info(card);
1723
Hideyuki Sasaki55873ed2009-12-01 12:15:58 +00001724 card->link_mode = GELIC_LV1_ETHER_AUTO_NEG;
1725
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001726 /* setup interrupt */
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001727 result = lv1_net_set_interrupt_status_indicator(bus_id(card),
1728 dev_id(card),
1729 ps3_mm_phys_to_lpar(__pa(&card->irq_status)),
1730 0);
1731
1732 if (result) {
1733 dev_dbg(&dev->core,
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001734 "%s:set_interrupt_status_indicator failed: %s\n",
1735 __func__, ps3_result(result));
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001736 result = -EIO;
1737 goto fail_status_indicator;
1738 }
1739
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001740 result = ps3_sb_event_receive_port_setup(dev, PS3_BINDING_CPU_ANY,
1741 &card->irq);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001742
1743 if (result) {
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001744 dev_info(ctodev(card),
1745 "%s:gelic_net_open_device failed (%d)\n",
1746 __func__, result);
1747 result = -EPERM;
1748 goto fail_alloc_irq;
1749 }
1750 result = request_irq(card->irq, gelic_card_interrupt,
1751 IRQF_DISABLED, netdev->name, card);
1752
1753 if (result) {
1754 dev_info(ctodev(card), "%s:request_irq failed (%d)\n",
1755 __func__, result);
1756 goto fail_request_irq;
1757 }
1758
1759 /* setup card structure */
1760 card->irq_mask = GELIC_CARD_RXINT | GELIC_CARD_TXINT |
1761 GELIC_CARD_PORT_STATUS_CHANGED;
1762 card->rx_csum = GELIC_CARD_RX_CSUM_DEFAULT;
1763
1764
1765 if (gelic_card_init_chain(card, &card->tx_chain,
1766 card->descr, GELIC_NET_TX_DESCRIPTORS))
1767 goto fail_alloc_tx;
1768 if (gelic_card_init_chain(card, &card->rx_chain,
1769 card->descr + GELIC_NET_TX_DESCRIPTORS,
1770 GELIC_NET_RX_DESCRIPTORS))
1771 goto fail_alloc_rx;
1772
1773 /* head of chain */
1774 card->tx_top = card->tx_chain.head;
1775 card->rx_top = card->rx_chain.head;
1776 dev_dbg(ctodev(card), "descr rx %p, tx %p, size %#lx, num %#x\n",
1777 card->rx_top, card->tx_top, sizeof(struct gelic_descr),
1778 GELIC_NET_RX_DESCRIPTORS);
1779 /* allocate rx skbs */
1780 if (gelic_card_alloc_rx_skbs(card))
1781 goto fail_alloc_skbs;
1782
1783 spin_lock_init(&card->tx_lock);
1784 card->tx_dma_progress = 0;
1785
1786 /* setup net_device structure */
1787 netdev->irq = card->irq;
1788 SET_NETDEV_DEV(netdev, &card->dev->core);
1789 gelic_ether_setup_netdev_ops(netdev, &card->napi);
1790 result = gelic_net_setup_netdev(netdev, card);
1791 if (result) {
1792 dev_dbg(&dev->core, "%s: setup_netdev failed %d",
1793 __func__, result);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001794 goto fail_setup_netdev;
1795 }
1796
Masakazu Mokuno09dde542008-02-07 19:58:57 +09001797#ifdef CONFIG_GELIC_WIRELESS
1798 if (gelic_wl_driver_probe(card)) {
1799 dev_dbg(&dev->core, "%s: WL init failed\n", __func__);
1800 goto fail_setup_netdev;
1801 }
1802#endif
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001803 pr_debug("%s: done\n", __func__);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001804 return 0;
1805
1806fail_setup_netdev:
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001807fail_alloc_skbs:
1808 gelic_card_free_chain(card, card->rx_chain.head);
1809fail_alloc_rx:
1810 gelic_card_free_chain(card, card->tx_chain.head);
1811fail_alloc_tx:
1812 free_irq(card->irq, card);
1813 netdev->irq = NO_IRQ;
1814fail_request_irq:
1815 ps3_sb_event_receive_port_destroy(dev, card->irq);
1816fail_alloc_irq:
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001817 lv1_net_set_interrupt_status_indicator(bus_id(card),
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001818 bus_id(card),
1819 0, 0);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001820fail_status_indicator:
Geert Uytterhoeven03fa68c2009-06-10 04:38:54 +00001821 ps3_system_bus_set_drvdata(dev, NULL);
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001822 kfree(netdev_card(netdev)->unalign);
1823 free_netdev(netdev);
1824fail_alloc_card:
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001825 ps3_dma_region_free(dev->d_region);
1826fail_dma_region:
1827 ps3_close_hv_device(dev);
1828fail_open:
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001829 return result;
1830}
1831
1832/**
1833 * ps3_gelic_driver_remove - remove a device from the control of this driver
1834 */
1835
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001836static int ps3_gelic_driver_remove(struct ps3_system_bus_device *dev)
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001837{
Geert Uytterhoeven03fa68c2009-06-10 04:38:54 +00001838 struct gelic_card *card = ps3_system_bus_get_drvdata(dev);
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001839 struct net_device *netdev0;
1840 pr_debug("%s: called\n", __func__);
1841
Hideyuki Sasaki55873ed2009-12-01 12:15:58 +00001842 /* set auto-negotiation */
1843 gelic_card_set_link_mode(card, GELIC_LV1_ETHER_AUTO_NEG);
1844
Masakazu Mokuno09dde542008-02-07 19:58:57 +09001845#ifdef CONFIG_GELIC_WIRELESS
1846 gelic_wl_driver_remove(card);
1847#endif
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001848 /* stop interrupt */
1849 gelic_card_set_irq_mask(card, 0);
1850
1851 /* turn off DMA, force end */
1852 gelic_card_disable_rxdmac(card);
1853 gelic_card_disable_txdmac(card);
1854
1855 /* release chains */
1856 gelic_card_release_tx_chain(card, 1);
1857 gelic_card_release_rx_chain(card);
1858
1859 gelic_card_free_chain(card, card->tx_top);
1860 gelic_card_free_chain(card, card->rx_top);
1861
Geoff Levandd4d7f1f2009-12-01 12:15:53 +00001862 netdev0 = card->netdev[GELIC_PORT_ETHERNET_0];
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001863 /* disconnect event port */
1864 free_irq(card->irq, card);
1865 netdev0->irq = NO_IRQ;
1866 ps3_sb_event_receive_port_destroy(card->dev, card->irq);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001867
1868 wait_event(card->waitq,
1869 atomic_read(&card->tx_timeout_task_counter) == 0);
1870
1871 lv1_net_set_interrupt_status_indicator(bus_id(card), dev_id(card),
1872 0 , 0);
1873
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001874 unregister_netdev(netdev0);
1875 kfree(netdev_card(netdev0)->unalign);
1876 free_netdev(netdev0);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001877
Geert Uytterhoeven03fa68c2009-06-10 04:38:54 +00001878 ps3_system_bus_set_drvdata(dev, NULL);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001879
1880 ps3_dma_region_free(dev->d_region);
1881
1882 ps3_close_hv_device(dev);
1883
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001884 pr_debug("%s: done\n", __func__);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001885 return 0;
1886}
1887
1888static struct ps3_system_bus_driver ps3_gelic_driver = {
1889 .match_id = PS3_MATCH_ID_GELIC,
1890 .probe = ps3_gelic_driver_probe,
1891 .remove = ps3_gelic_driver_remove,
1892 .shutdown = ps3_gelic_driver_remove,
1893 .core.name = "ps3_gelic_driver",
1894 .core.owner = THIS_MODULE,
1895};
1896
1897static int __init ps3_gelic_driver_init (void)
1898{
1899 return firmware_has_feature(FW_FEATURE_PS3_LV1)
1900 ? ps3_system_bus_driver_register(&ps3_gelic_driver)
1901 : -ENODEV;
1902}
1903
1904static void __exit ps3_gelic_driver_exit (void)
1905{
1906 ps3_system_bus_driver_unregister(&ps3_gelic_driver);
1907}
1908
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001909module_init(ps3_gelic_driver_init);
1910module_exit(ps3_gelic_driver_exit);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001911
1912MODULE_ALIAS(PS3_MODULE_ALIAS_GELIC);
1913