blob: 4acc17bffc422038777ff019fe10dc5d2e53d07b [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
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000031#include <linux/interrupt.h>
Masakazu Mokuno02c18892007-07-05 20:11:16 +090032#include <linux/kernel.h>
33#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Masakazu Mokuno02c18892007-07-05 20:11:16 +090035
36#include <linux/etherdevice.h>
37#include <linux/ethtool.h>
38#include <linux/if_vlan.h>
39
40#include <linux/in.h>
41#include <linux/ip.h>
42#include <linux/tcp.h>
43
44#include <linux/dma-mapping.h>
45#include <net/checksum.h>
46#include <asm/firmware.h>
47#include <asm/ps3.h>
48#include <asm/lv1call.h>
49
50#include "ps3_gelic_net.h"
Masakazu Mokuno09dde542008-02-07 19:58:57 +090051#include "ps3_gelic_wireless.h"
Masakazu Mokuno02c18892007-07-05 20:11:16 +090052
53#define DRV_NAME "Gelic Network Driver"
Masakazu Mokuno09dde542008-02-07 19:58:57 +090054#define DRV_VERSION "2.0"
Masakazu Mokuno02c18892007-07-05 20:11:16 +090055
56MODULE_AUTHOR("SCE Inc.");
57MODULE_DESCRIPTION("Gelic Network driver");
58MODULE_LICENSE("GPL");
59
Masakazu Mokuno589866f2008-02-07 19:58:42 +090060
61static inline void gelic_card_enable_rxdmac(struct gelic_card *card);
62static inline void gelic_card_disable_rxdmac(struct gelic_card *card);
63static inline void gelic_card_disable_txdmac(struct gelic_card *card);
64static inline void gelic_card_reset_chain(struct gelic_card *card,
65 struct gelic_descr_chain *chain,
66 struct gelic_descr *start_descr);
Masakazu Mokuno02c18892007-07-05 20:11:16 +090067
68/* set irq_mask */
Masakazu Mokuno589866f2008-02-07 19:58:42 +090069int gelic_card_set_irq_mask(struct gelic_card *card, u64 mask)
Masakazu Mokuno02c18892007-07-05 20:11:16 +090070{
71 int status;
72
73 status = lv1_net_set_interrupt_mask(bus_id(card), dev_id(card),
74 mask, 0);
75 if (status)
76 dev_info(ctodev(card),
Masakazu Mokuno589866f2008-02-07 19:58:42 +090077 "%s failed %d\n", __func__, status);
Masakazu Mokuno02c18892007-07-05 20:11:16 +090078 return status;
79}
Masakazu Mokuno589866f2008-02-07 19:58:42 +090080
Masakazu Mokuno59e97322008-02-07 19:58:08 +090081static inline void gelic_card_rx_irq_on(struct gelic_card *card)
Masakazu Mokuno02c18892007-07-05 20:11:16 +090082{
Masakazu Mokuno589866f2008-02-07 19:58:42 +090083 card->irq_mask |= GELIC_CARD_RXINT;
84 gelic_card_set_irq_mask(card, card->irq_mask);
Masakazu Mokuno02c18892007-07-05 20:11:16 +090085}
Masakazu Mokuno59e97322008-02-07 19:58:08 +090086static inline void gelic_card_rx_irq_off(struct gelic_card *card)
Masakazu Mokuno02c18892007-07-05 20:11:16 +090087{
Masakazu Mokuno589866f2008-02-07 19:58:42 +090088 card->irq_mask &= ~GELIC_CARD_RXINT;
89 gelic_card_set_irq_mask(card, card->irq_mask);
Masakazu Mokuno02c18892007-07-05 20:11:16 +090090}
Masakazu Mokuno01fed4c2008-02-07 19:58:32 +090091
Masakazu Mokuno589866f2008-02-07 19:58:42 +090092static void gelic_card_get_ether_port_status(struct gelic_card *card,
93 int inform)
Masakazu Mokuno01fed4c2008-02-07 19:58:32 +090094{
95 u64 v2;
96 struct net_device *ether_netdev;
97
98 lv1_net_control(bus_id(card), dev_id(card),
99 GELIC_LV1_GET_ETH_PORT_STATUS,
Geoff Levandd4d7f1f2009-12-01 12:15:53 +0000100 GELIC_LV1_VLAN_TX_ETHERNET_0, 0, 0,
Masakazu Mokuno01fed4c2008-02-07 19:58:32 +0900101 &card->ether_port_status, &v2);
102
103 if (inform) {
Geoff Levandd4d7f1f2009-12-01 12:15:53 +0000104 ether_netdev = card->netdev[GELIC_PORT_ETHERNET_0];
Masakazu Mokuno01fed4c2008-02-07 19:58:32 +0900105 if (card->ether_port_status & GELIC_LV1_ETHER_LINK_UP)
106 netif_carrier_on(ether_netdev);
107 else
108 netif_carrier_off(ether_netdev);
109 }
110}
111
Hideyuki Sasaki55873ed2009-12-01 12:15:58 +0000112static int gelic_card_set_link_mode(struct gelic_card *card, int mode)
113{
114 int status;
115 u64 v1, v2;
116
117 status = lv1_net_control(bus_id(card), dev_id(card),
118 GELIC_LV1_SET_NEGOTIATION_MODE,
119 GELIC_LV1_PHY_ETHERNET_0, mode, 0, &v1, &v2);
120 if (status) {
121 pr_info("%s: failed setting negotiation mode %d\n", __func__,
122 status);
123 return -EBUSY;
124 }
125
126 card->link_mode = mode;
127 return 0;
128}
129
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900130void gelic_card_up(struct gelic_card *card)
131{
132 pr_debug("%s: called\n", __func__);
Daniel Walker2914f3e2008-05-22 00:00:03 -0700133 mutex_lock(&card->updown_lock);
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900134 if (atomic_inc_return(&card->users) == 1) {
135 pr_debug("%s: real do\n", __func__);
136 /* enable irq */
137 gelic_card_set_irq_mask(card, card->irq_mask);
138 /* start rx */
139 gelic_card_enable_rxdmac(card);
140
141 napi_enable(&card->napi);
142 }
Daniel Walker2914f3e2008-05-22 00:00:03 -0700143 mutex_unlock(&card->updown_lock);
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900144 pr_debug("%s: done\n", __func__);
145}
146
147void gelic_card_down(struct gelic_card *card)
148{
149 u64 mask;
150 pr_debug("%s: called\n", __func__);
Daniel Walker2914f3e2008-05-22 00:00:03 -0700151 mutex_lock(&card->updown_lock);
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900152 if (atomic_dec_if_positive(&card->users) == 0) {
153 pr_debug("%s: real do\n", __func__);
154 napi_disable(&card->napi);
155 /*
156 * Disable irq. Wireless interrupts will
157 * be disabled later if any
158 */
159 mask = card->irq_mask & (GELIC_CARD_WLAN_EVENT_RECEIVED |
160 GELIC_CARD_WLAN_COMMAND_COMPLETED);
161 gelic_card_set_irq_mask(card, mask);
162 /* stop rx */
163 gelic_card_disable_rxdmac(card);
164 gelic_card_reset_chain(card, &card->rx_chain,
165 card->descr + GELIC_NET_TX_DESCRIPTORS);
166 /* stop tx */
167 gelic_card_disable_txdmac(card);
168 }
Daniel Walker2914f3e2008-05-22 00:00:03 -0700169 mutex_unlock(&card->updown_lock);
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900170 pr_debug("%s: done\n", __func__);
171}
Masakazu Mokuno01fed4c2008-02-07 19:58:32 +0900172
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900173/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900174 * gelic_descr_get_status -- returns the status of a descriptor
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900175 * @descr: descriptor to look at
176 *
177 * returns the status as in the dmac_cmd_status field of the descriptor
178 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900179static enum gelic_descr_dma_status
180gelic_descr_get_status(struct gelic_descr *descr)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900181{
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900182 return be32_to_cpu(descr->dmac_cmd_status) & GELIC_DESCR_DMA_STAT_MASK;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900183}
184
185/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900186 * gelic_descr_set_status -- sets the status of a descriptor
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900187 * @descr: descriptor to change
188 * @status: status to set in the descriptor
189 *
190 * changes the status to the specified value. Doesn't change other bits
191 * in the status
192 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900193static void gelic_descr_set_status(struct gelic_descr *descr,
194 enum gelic_descr_dma_status status)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900195{
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900196 descr->dmac_cmd_status = cpu_to_be32(status |
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900197 (be32_to_cpu(descr->dmac_cmd_status) &
198 ~GELIC_DESCR_DMA_STAT_MASK));
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900199 /*
200 * dma_cmd_status field is used to indicate whether the descriptor
201 * is valid or not.
202 * Usually caller of this function wants to inform that to the
203 * hardware, so we assure here the hardware sees the change.
204 */
205 wmb();
206}
207
208/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900209 * gelic_card_free_chain - free descriptor chain
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900210 * @card: card structure
211 * @descr_in: address of desc
212 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900213static void gelic_card_free_chain(struct gelic_card *card,
214 struct gelic_descr *descr_in)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900215{
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900216 struct gelic_descr *descr;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900217
218 for (descr = descr_in; descr && descr->bus_addr; descr = descr->next) {
219 dma_unmap_single(ctodev(card), descr->bus_addr,
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900220 GELIC_DESCR_SIZE, DMA_BIDIRECTIONAL);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900221 descr->bus_addr = 0;
222 }
223}
224
225/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900226 * gelic_card_init_chain - links descriptor chain
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900227 * @card: card structure
228 * @chain: address of chain
229 * @start_descr: address of descriptor array
230 * @no: number of descriptors
231 *
232 * we manage a circular list that mirrors the hardware structure,
233 * except that the hardware uses bus addresses.
234 *
235 * returns 0 on success, <0 on failure
236 */
Geert Uytterhoeven48dce822009-06-10 04:38:58 +0000237static int __devinit gelic_card_init_chain(struct gelic_card *card,
238 struct gelic_descr_chain *chain,
239 struct gelic_descr *start_descr,
240 int no)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900241{
242 int i;
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900243 struct gelic_descr *descr;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900244
245 descr = start_descr;
246 memset(descr, 0, sizeof(*descr) * no);
247
248 /* set up the hardware pointers in each descriptor */
249 for (i = 0; i < no; i++, descr++) {
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900250 gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900251 descr->bus_addr =
252 dma_map_single(ctodev(card), descr,
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900253 GELIC_DESCR_SIZE,
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900254 DMA_BIDIRECTIONAL);
255
256 if (!descr->bus_addr)
257 goto iommu_error;
258
259 descr->next = descr + 1;
260 descr->prev = descr - 1;
261 }
262 /* make them as ring */
263 (descr - 1)->next = start_descr;
264 start_descr->prev = (descr - 1);
265
266 /* chain bus addr of hw descriptor */
267 descr = start_descr;
268 for (i = 0; i < no; i++, descr++) {
Masakazu Mokuno100e1d82008-02-07 19:57:54 +0900269 descr->next_descr_addr = cpu_to_be32(descr->next->bus_addr);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900270 }
271
272 chain->head = start_descr;
273 chain->tail = start_descr;
274
275 /* do not chain last hw descriptor */
276 (descr - 1)->next_descr_addr = 0;
277
278 return 0;
279
280iommu_error:
281 for (i--, descr--; 0 <= i; i--, descr--)
282 if (descr->bus_addr)
283 dma_unmap_single(ctodev(card), descr->bus_addr,
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900284 GELIC_DESCR_SIZE,
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900285 DMA_BIDIRECTIONAL);
286 return -ENOMEM;
287}
288
289/**
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900290 * gelic_card_reset_chain - reset status of a descriptor chain
291 * @card: card structure
292 * @chain: address of chain
293 * @start_descr: address of descriptor array
294 *
295 * Reset the status of dma descriptors to ready state
296 * and re-initialize the hardware chain for later use
297 */
298static void gelic_card_reset_chain(struct gelic_card *card,
299 struct gelic_descr_chain *chain,
300 struct gelic_descr *start_descr)
301{
302 struct gelic_descr *descr;
303
304 for (descr = start_descr; start_descr != descr->next; descr++) {
305 gelic_descr_set_status(descr, GELIC_DESCR_DMA_CARDOWNED);
306 descr->next_descr_addr = cpu_to_be32(descr->next->bus_addr);
307 }
308
309 chain->head = start_descr;
310 chain->tail = (descr - 1);
311
312 (descr - 1)->next_descr_addr = 0;
313}
314/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900315 * gelic_descr_prepare_rx - reinitializes a rx descriptor
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900316 * @card: card structure
317 * @descr: descriptor to re-init
318 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200319 * return 0 on success, <0 on failure
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900320 *
321 * allocates a new rx skb, iommu-maps it and attaches it to the descriptor.
322 * Activate the descriptor state-wise
323 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900324static int gelic_descr_prepare_rx(struct gelic_card *card,
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900325 struct gelic_descr *descr)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900326{
327 int offset;
328 unsigned int bufsize;
329
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900330 if (gelic_descr_get_status(descr) != GELIC_DESCR_DMA_NOT_IN_USE)
Frans Pop583d0772010-03-24 07:57:31 +0000331 dev_info(ctodev(card), "%s: ERROR status\n", __func__);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900332 /* we need to round up the buffer size to a multiple of 128 */
333 bufsize = ALIGN(GELIC_NET_MAX_MTU, GELIC_NET_RXBUF_ALIGN);
334
335 /* and we need to have it 128 byte aligned, therefore we allocate a
336 * bit more */
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900337 descr->skb = dev_alloc_skb(bufsize + GELIC_NET_RXBUF_ALIGN - 1);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900338 if (!descr->skb) {
339 descr->buf_addr = 0; /* tell DMAC don't touch memory */
340 dev_info(ctodev(card),
341 "%s:allocate skb failed !!\n", __func__);
342 return -ENOMEM;
343 }
Masakazu Mokuno100e1d82008-02-07 19:57:54 +0900344 descr->buf_size = cpu_to_be32(bufsize);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900345 descr->dmac_cmd_status = 0;
346 descr->result_size = 0;
347 descr->valid_size = 0;
348 descr->data_error = 0;
349
350 offset = ((unsigned long)descr->skb->data) &
351 (GELIC_NET_RXBUF_ALIGN - 1);
352 if (offset)
353 skb_reserve(descr->skb, GELIC_NET_RXBUF_ALIGN - offset);
354 /* io-mmu-map the skb */
Masakazu Mokuno100e1d82008-02-07 19:57:54 +0900355 descr->buf_addr = cpu_to_be32(dma_map_single(ctodev(card),
356 descr->skb->data,
357 GELIC_NET_MAX_MTU,
358 DMA_FROM_DEVICE));
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900359 if (!descr->buf_addr) {
360 dev_kfree_skb_any(descr->skb);
361 descr->skb = NULL;
362 dev_info(ctodev(card),
363 "%s:Could not iommu-map rx buffer\n", __func__);
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900364 gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900365 return -ENOMEM;
366 } else {
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900367 gelic_descr_set_status(descr, GELIC_DESCR_DMA_CARDOWNED);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900368 return 0;
369 }
370}
371
372/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900373 * gelic_card_release_rx_chain - free all skb of rx descr
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900374 * @card: card structure
375 *
376 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900377static void gelic_card_release_rx_chain(struct gelic_card *card)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900378{
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900379 struct gelic_descr *descr = card->rx_chain.head;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900380
381 do {
382 if (descr->skb) {
383 dma_unmap_single(ctodev(card),
Masakazu Mokuno100e1d82008-02-07 19:57:54 +0900384 be32_to_cpu(descr->buf_addr),
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900385 descr->skb->len,
386 DMA_FROM_DEVICE);
387 descr->buf_addr = 0;
388 dev_kfree_skb_any(descr->skb);
389 descr->skb = NULL;
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900390 gelic_descr_set_status(descr,
391 GELIC_DESCR_DMA_NOT_IN_USE);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900392 }
393 descr = descr->next;
394 } while (descr != card->rx_chain.head);
395}
396
397/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900398 * gelic_card_fill_rx_chain - fills descriptors/skbs in the rx chains
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900399 * @card: card structure
400 *
401 * fills all descriptors in the rx chain: allocates skbs
402 * and iommu-maps them.
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900403 * returns 0 on success, < 0 on failure
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900404 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900405static int gelic_card_fill_rx_chain(struct gelic_card *card)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900406{
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900407 struct gelic_descr *descr = card->rx_chain.head;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900408 int ret;
409
410 do {
411 if (!descr->skb) {
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900412 ret = gelic_descr_prepare_rx(card, descr);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900413 if (ret)
414 goto rewind;
415 }
416 descr = descr->next;
417 } while (descr != card->rx_chain.head);
418
419 return 0;
420rewind:
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900421 gelic_card_release_rx_chain(card);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900422 return ret;
423}
424
425/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900426 * gelic_card_alloc_rx_skbs - allocates rx skbs in rx descriptor chains
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900427 * @card: card structure
428 *
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900429 * returns 0 on success, < 0 on failure
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900430 */
Geert Uytterhoeven48dce822009-06-10 04:38:58 +0000431static int __devinit gelic_card_alloc_rx_skbs(struct gelic_card *card)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900432{
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900433 struct gelic_descr_chain *chain;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900434 int ret;
435 chain = &card->rx_chain;
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900436 ret = gelic_card_fill_rx_chain(card);
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900437 chain->tail = card->rx_top->prev; /* point to the last */
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900438 return ret;
439}
440
441/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900442 * gelic_descr_release_tx - processes a used tx descriptor
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900443 * @card: card structure
444 * @descr: descriptor to release
445 *
446 * releases a used tx descriptor (unmapping, freeing of skb)
447 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900448static void gelic_descr_release_tx(struct gelic_card *card,
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900449 struct gelic_descr *descr)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900450{
Masakazu Mokuno173261e2007-08-31 22:22:32 +0900451 struct sk_buff *skb = descr->skb;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900452
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900453 BUG_ON(!(be32_to_cpu(descr->data_status) & GELIC_DESCR_TX_TAIL));
454
455 dma_unmap_single(ctodev(card), be32_to_cpu(descr->buf_addr), skb->len,
456 DMA_TO_DEVICE);
Masakazu Mokuno173261e2007-08-31 22:22:32 +0900457 dev_kfree_skb_any(skb);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900458
459 descr->buf_addr = 0;
460 descr->buf_size = 0;
461 descr->next_descr_addr = 0;
462 descr->result_size = 0;
463 descr->valid_size = 0;
464 descr->data_status = 0;
465 descr->data_error = 0;
466 descr->skb = NULL;
467
468 /* set descr status */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900469 gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900470}
471
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900472static void gelic_card_stop_queues(struct gelic_card *card)
473{
Geoff Levandd4d7f1f2009-12-01 12:15:53 +0000474 netif_stop_queue(card->netdev[GELIC_PORT_ETHERNET_0]);
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900475
476 if (card->netdev[GELIC_PORT_WIRELESS])
477 netif_stop_queue(card->netdev[GELIC_PORT_WIRELESS]);
478}
479static void gelic_card_wake_queues(struct gelic_card *card)
480{
Geoff Levandd4d7f1f2009-12-01 12:15:53 +0000481 netif_wake_queue(card->netdev[GELIC_PORT_ETHERNET_0]);
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900482
483 if (card->netdev[GELIC_PORT_WIRELESS])
484 netif_wake_queue(card->netdev[GELIC_PORT_WIRELESS]);
485}
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900486/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900487 * gelic_card_release_tx_chain - processes sent tx descriptors
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900488 * @card: adapter structure
489 * @stop: net_stop sequence
490 *
491 * releases the tx descriptors that gelic has finished with
492 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900493static void gelic_card_release_tx_chain(struct gelic_card *card, int stop)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900494{
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900495 struct gelic_descr_chain *tx_chain;
496 enum gelic_descr_dma_status status;
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900497 struct net_device *netdev;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900498 int release = 0;
499
500 for (tx_chain = &card->tx_chain;
501 tx_chain->head != tx_chain->tail && tx_chain->tail;
502 tx_chain->tail = tx_chain->tail->next) {
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900503 status = gelic_descr_get_status(tx_chain->tail);
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900504 netdev = tx_chain->tail->skb->dev;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900505 switch (status) {
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900506 case GELIC_DESCR_DMA_RESPONSE_ERROR:
507 case GELIC_DESCR_DMA_PROTECTION_ERROR:
508 case GELIC_DESCR_DMA_FORCE_END:
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900509 if (printk_ratelimit())
510 dev_info(ctodev(card),
511 "%s: forcing end of tx descriptor " \
512 "with status %x\n",
513 __func__, status);
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900514 netdev->stats.tx_dropped++;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900515 break;
516
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900517 case GELIC_DESCR_DMA_COMPLETE:
Masakazu Mokuno48544cc2007-07-20 17:24:56 +0900518 if (tx_chain->tail->skb) {
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900519 netdev->stats.tx_packets++;
520 netdev->stats.tx_bytes +=
Masakazu Mokuno48544cc2007-07-20 17:24:56 +0900521 tx_chain->tail->skb->len;
522 }
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900523 break;
524
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900525 case GELIC_DESCR_DMA_CARDOWNED:
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900526 /* pending tx request */
527 default:
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900528 /* any other value (== GELIC_DESCR_DMA_NOT_IN_USE) */
Masakazu Mokuno48544cc2007-07-20 17:24:56 +0900529 if (!stop)
530 goto out;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900531 }
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900532 gelic_descr_release_tx(card, tx_chain->tail);
Masakazu Mokuno48544cc2007-07-20 17:24:56 +0900533 release ++;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900534 }
535out:
Masakazu Mokuno173261e2007-08-31 22:22:32 +0900536 if (!stop && release)
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900537 gelic_card_wake_queues(card);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900538}
539
540/**
541 * gelic_net_set_multi - sets multicast addresses and promisc flags
542 * @netdev: interface device structure
543 *
544 * gelic_net_set_multi configures multicast addresses as needed for the
545 * netdev interface. It also sets up multicast, allmulti and promisc
546 * flags appropriately
547 */
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900548void gelic_net_set_multi(struct net_device *netdev)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900549{
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900550 struct gelic_card *card = netdev_card(netdev);
Jiri Pirko22bedad2010-04-01 21:22:57 +0000551 struct netdev_hw_addr *ha;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900552 unsigned int i;
553 uint8_t *p;
554 u64 addr;
555 int status;
556
557 /* clear all multicast address */
558 status = lv1_net_remove_multicast_address(bus_id(card), dev_id(card),
559 0, 1);
560 if (status)
561 dev_err(ctodev(card),
562 "lv1_net_remove_multicast_address failed %d\n",
563 status);
564 /* set broadcast address */
565 status = lv1_net_add_multicast_address(bus_id(card), dev_id(card),
566 GELIC_NET_BROADCAST_ADDR, 0);
567 if (status)
568 dev_err(ctodev(card),
569 "lv1_net_add_multicast_address failed, %d\n",
570 status);
571
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900572 if ((netdev->flags & IFF_ALLMULTI) ||
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000573 (netdev_mc_count(netdev) > GELIC_NET_MC_COUNT_MAX)) {
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900574 status = lv1_net_add_multicast_address(bus_id(card),
575 dev_id(card),
576 0, 1);
577 if (status)
578 dev_err(ctodev(card),
579 "lv1_net_add_multicast_address failed, %d\n",
580 status);
581 return;
582 }
583
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900584 /* set multicast addresses */
Jiri Pirko22bedad2010-04-01 21:22:57 +0000585 netdev_for_each_mc_addr(ha, netdev) {
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900586 addr = 0;
Jiri Pirko22bedad2010-04-01 21:22:57 +0000587 p = ha->addr;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900588 for (i = 0; i < ETH_ALEN; i++) {
589 addr <<= 8;
590 addr |= *p++;
591 }
592 status = lv1_net_add_multicast_address(bus_id(card),
593 dev_id(card),
594 addr, 0);
595 if (status)
596 dev_err(ctodev(card),
597 "lv1_net_add_multicast_address failed, %d\n",
598 status);
599 }
600}
601
602/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900603 * gelic_card_enable_rxdmac - enables the receive DMA controller
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900604 * @card: card structure
605 *
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900606 * gelic_card_enable_rxdmac enables the DMA controller by setting RX_DMA_EN
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900607 * in the GDADMACCNTR register
608 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900609static inline void gelic_card_enable_rxdmac(struct gelic_card *card)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900610{
611 int status;
612
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900613#ifdef DEBUG
614 if (gelic_descr_get_status(card->rx_chain.head) !=
615 GELIC_DESCR_DMA_CARDOWNED) {
616 printk(KERN_ERR "%s: status=%x\n", __func__,
617 be32_to_cpu(card->rx_chain.head->dmac_cmd_status));
618 printk(KERN_ERR "%s: nextphy=%x\n", __func__,
619 be32_to_cpu(card->rx_chain.head->next_descr_addr));
620 printk(KERN_ERR "%s: head=%p\n", __func__,
621 card->rx_chain.head);
622 }
623#endif
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900624 status = lv1_net_start_rx_dma(bus_id(card), dev_id(card),
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900625 card->rx_chain.head->bus_addr, 0);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900626 if (status)
627 dev_info(ctodev(card),
628 "lv1_net_start_rx_dma failed, status=%d\n", status);
629}
630
631/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900632 * gelic_card_disable_rxdmac - disables the receive DMA controller
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900633 * @card: card structure
634 *
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900635 * gelic_card_disable_rxdmac terminates processing on the DMA controller by
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300636 * turing off DMA and issuing a force end
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900637 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900638static inline void gelic_card_disable_rxdmac(struct gelic_card *card)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900639{
640 int status;
641
642 /* this hvc blocks until the DMA in progress really stopped */
643 status = lv1_net_stop_rx_dma(bus_id(card), dev_id(card), 0);
644 if (status)
645 dev_err(ctodev(card),
Paul Bolle426d3102010-08-07 12:30:03 +0200646 "lv1_net_stop_rx_dma failed, %d\n", status);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900647}
648
649/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900650 * gelic_card_disable_txdmac - disables the transmit DMA controller
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900651 * @card: card structure
652 *
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900653 * gelic_card_disable_txdmac terminates processing on the DMA controller by
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300654 * turing off DMA and issuing a force end
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900655 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900656static inline void gelic_card_disable_txdmac(struct gelic_card *card)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900657{
658 int status;
659
660 /* this hvc blocks until the DMA in progress really stopped */
661 status = lv1_net_stop_tx_dma(bus_id(card), dev_id(card), 0);
662 if (status)
663 dev_err(ctodev(card),
Paul Bolle426d3102010-08-07 12:30:03 +0200664 "lv1_net_stop_tx_dma failed, status=%d\n", status);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900665}
666
667/**
668 * gelic_net_stop - called upon ifconfig down
669 * @netdev: interface device structure
670 *
671 * always returns 0
672 */
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900673int gelic_net_stop(struct net_device *netdev)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900674{
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900675 struct gelic_card *card;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900676
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900677 pr_debug("%s: start\n", __func__);
678
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900679 netif_stop_queue(netdev);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900680 netif_carrier_off(netdev);
681
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900682 card = netdev_card(netdev);
683 gelic_card_down(card);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900684
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900685 pr_debug("%s: done\n", __func__);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900686 return 0;
687}
688
689/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900690 * gelic_card_get_next_tx_descr - returns the next available tx descriptor
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900691 * @card: device structure to get descriptor from
692 *
693 * returns the address of the next descriptor, or NULL if not available.
694 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900695static struct gelic_descr *
696gelic_card_get_next_tx_descr(struct gelic_card *card)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900697{
698 if (!card->tx_chain.head)
699 return NULL;
Masakazu Mokuno173261e2007-08-31 22:22:32 +0900700 /* see if the next descriptor is free */
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900701 if (card->tx_chain.tail != card->tx_chain.head->next &&
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900702 gelic_descr_get_status(card->tx_chain.head) ==
703 GELIC_DESCR_DMA_NOT_IN_USE)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900704 return card->tx_chain.head;
705 else
706 return NULL;
707
708}
709
710/**
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900711 * gelic_net_set_txdescr_cmdstat - sets the tx descriptor command field
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900712 * @descr: descriptor structure to fill out
713 * @skb: packet to consider
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900714 *
715 * fills out the command and status field of the descriptor structure,
716 * depending on hardware checksum settings. This function assumes a wmb()
717 * has executed before.
718 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900719static void gelic_descr_set_tx_cmdstat(struct gelic_descr *descr,
720 struct sk_buff *skb)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900721{
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900722 if (skb->ip_summed != CHECKSUM_PARTIAL)
Masakazu Mokuno100e1d82008-02-07 19:57:54 +0900723 descr->dmac_cmd_status =
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900724 cpu_to_be32(GELIC_DESCR_DMA_CMD_NO_CHKSUM |
725 GELIC_DESCR_TX_DMA_FRAME_TAIL);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900726 else {
727 /* is packet ip?
728 * if yes: tcp? udp? */
729 if (skb->protocol == htons(ETH_P_IP)) {
730 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
731 descr->dmac_cmd_status =
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900732 cpu_to_be32(GELIC_DESCR_DMA_CMD_TCP_CHKSUM |
733 GELIC_DESCR_TX_DMA_FRAME_TAIL);
Masakazu Mokuno173261e2007-08-31 22:22:32 +0900734
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900735 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
736 descr->dmac_cmd_status =
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900737 cpu_to_be32(GELIC_DESCR_DMA_CMD_UDP_CHKSUM |
738 GELIC_DESCR_TX_DMA_FRAME_TAIL);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900739 else /*
740 * the stack should checksum non-tcp and non-udp
741 * packets on his own: NETIF_F_IP_CSUM
742 */
743 descr->dmac_cmd_status =
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900744 cpu_to_be32(GELIC_DESCR_DMA_CMD_NO_CHKSUM |
745 GELIC_DESCR_TX_DMA_FRAME_TAIL);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900746 }
747 }
748}
749
Masakazu Mokuno173261e2007-08-31 22:22:32 +0900750static inline struct sk_buff *gelic_put_vlan_tag(struct sk_buff *skb,
751 unsigned short tag)
752{
753 struct vlan_ethhdr *veth;
754 static unsigned int c;
755
756 if (skb_headroom(skb) < VLAN_HLEN) {
757 struct sk_buff *sk_tmp = skb;
758 pr_debug("%s: hd=%d c=%ud\n", __func__, skb_headroom(skb), c);
759 skb = skb_realloc_headroom(sk_tmp, VLAN_HLEN);
760 if (!skb)
761 return NULL;
762 dev_kfree_skb_any(sk_tmp);
763 }
764 veth = (struct vlan_ethhdr *)skb_push(skb, VLAN_HLEN);
765
766 /* Move the mac addresses to the top of buffer */
767 memmove(skb->data, skb->data + VLAN_HLEN, 2 * ETH_ALEN);
768
Harvey Harrison09640e62009-02-01 00:45:17 -0800769 veth->h_vlan_proto = cpu_to_be16(ETH_P_8021Q);
Masakazu Mokuno173261e2007-08-31 22:22:32 +0900770 veth->h_vlan_TCI = htons(tag);
771
772 return skb;
773}
774
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900775/**
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900776 * gelic_descr_prepare_tx - setup a descriptor for sending packets
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900777 * @card: card structure
778 * @descr: descriptor structure
779 * @skb: packet to use
780 *
781 * returns 0 on success, <0 on failure.
782 *
783 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900784static int gelic_descr_prepare_tx(struct gelic_card *card,
785 struct gelic_descr *descr,
786 struct sk_buff *skb)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900787{
Masakazu Mokuno173261e2007-08-31 22:22:32 +0900788 dma_addr_t buf;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900789
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900790 if (card->vlan_required) {
Masakazu Mokuno173261e2007-08-31 22:22:32 +0900791 struct sk_buff *skb_tmp;
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900792 enum gelic_port_type type;
793
794 type = netdev_port(skb->dev)->type;
Masakazu Mokuno173261e2007-08-31 22:22:32 +0900795 skb_tmp = gelic_put_vlan_tag(skb,
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900796 card->vlan[type].tx);
Masakazu Mokuno173261e2007-08-31 22:22:32 +0900797 if (!skb_tmp)
798 return -ENOMEM;
799 skb = skb_tmp;
Masakazu Mokuno48544cc2007-07-20 17:24:56 +0900800 }
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900801
Masakazu Mokuno173261e2007-08-31 22:22:32 +0900802 buf = dma_map_single(ctodev(card), skb->data, skb->len, DMA_TO_DEVICE);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900803
Masakazu Mokuno173261e2007-08-31 22:22:32 +0900804 if (!buf) {
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900805 dev_err(ctodev(card),
806 "dma map 2 failed (%p, %i). Dropping packet\n",
Masakazu Mokuno173261e2007-08-31 22:22:32 +0900807 skb->data, skb->len);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900808 return -ENOMEM;
809 }
810
Masakazu Mokuno100e1d82008-02-07 19:57:54 +0900811 descr->buf_addr = cpu_to_be32(buf);
812 descr->buf_size = cpu_to_be32(skb->len);
Masakazu Mokuno173261e2007-08-31 22:22:32 +0900813 descr->skb = skb;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900814 descr->data_status = 0;
Masakazu Mokuno173261e2007-08-31 22:22:32 +0900815 descr->next_descr_addr = 0; /* terminate hw descr */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900816 gelic_descr_set_tx_cmdstat(descr, skb);
Masakazu Mokuno48544cc2007-07-20 17:24:56 +0900817
818 /* bump free descriptor pointer */
Masakazu Mokuno173261e2007-08-31 22:22:32 +0900819 card->tx_chain.head = descr->next;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900820 return 0;
821}
822
823/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900824 * gelic_card_kick_txdma - enables TX DMA processing
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900825 * @card: card structure
826 * @descr: descriptor address to enable TX processing at
827 *
828 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900829static int gelic_card_kick_txdma(struct gelic_card *card,
830 struct gelic_descr *descr)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900831{
Masakazu Mokuno48544cc2007-07-20 17:24:56 +0900832 int status = 0;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900833
834 if (card->tx_dma_progress)
835 return 0;
836
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900837 if (gelic_descr_get_status(descr) == GELIC_DESCR_DMA_CARDOWNED) {
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900838 card->tx_dma_progress = 1;
Masakazu Mokunodc029ad2007-08-31 22:25:09 +0900839 status = lv1_net_start_tx_dma(bus_id(card), dev_id(card),
840 descr->bus_addr, 0);
Andre Heidereff896c2011-07-12 10:13:26 +0000841 if (status) {
842 card->tx_dma_progress = 0;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900843 dev_info(ctodev(card), "lv1_net_start_txdma failed," \
Masakazu Mokunodc029ad2007-08-31 22:25:09 +0900844 "status=%d\n", status);
Andre Heidereff896c2011-07-12 10:13:26 +0000845 }
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900846 }
847 return status;
848}
849
850/**
851 * gelic_net_xmit - transmits a frame over the device
852 * @skb: packet to send out
853 * @netdev: interface device structure
854 *
855 * returns 0 on success, <0 on failure
856 */
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900857int gelic_net_xmit(struct sk_buff *skb, struct net_device *netdev)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900858{
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900859 struct gelic_card *card = netdev_card(netdev);
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900860 struct gelic_descr *descr;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900861 int result;
862 unsigned long flags;
863
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900864 spin_lock_irqsave(&card->tx_lock, flags);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900865
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900866 gelic_card_release_tx_chain(card, 0);
Masakazu Mokuno48544cc2007-07-20 17:24:56 +0900867
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900868 descr = gelic_card_get_next_tx_descr(card);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900869 if (!descr) {
Masakazu Mokuno48544cc2007-07-20 17:24:56 +0900870 /*
871 * no more descriptors free
872 */
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900873 gelic_card_stop_queues(card);
874 spin_unlock_irqrestore(&card->tx_lock, flags);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900875 return NETDEV_TX_BUSY;
876 }
Masakazu Mokuno48544cc2007-07-20 17:24:56 +0900877
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900878 result = gelic_descr_prepare_tx(card, descr, skb);
Masakazu Mokuno48544cc2007-07-20 17:24:56 +0900879 if (result) {
880 /*
Andre Heidere3001c92011-07-12 10:13:24 +0000881 * DMA map failed. As chances are that failure
Masakazu Mokuno48544cc2007-07-20 17:24:56 +0900882 * would continue, just release skb and return
883 */
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900884 netdev->stats.tx_dropped++;
Masakazu Mokuno48544cc2007-07-20 17:24:56 +0900885 dev_kfree_skb_any(skb);
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900886 spin_unlock_irqrestore(&card->tx_lock, flags);
Masakazu Mokuno48544cc2007-07-20 17:24:56 +0900887 return NETDEV_TX_OK;
888 }
889 /*
890 * link this prepared descriptor to previous one
891 * to achieve high performance
892 */
Masakazu Mokuno100e1d82008-02-07 19:57:54 +0900893 descr->prev->next_descr_addr = cpu_to_be32(descr->bus_addr);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900894 /*
895 * as hardware descriptor is modified in the above lines,
896 * ensure that the hardware sees it
897 */
898 wmb();
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900899 if (gelic_card_kick_txdma(card, descr)) {
Masakazu Mokuno48544cc2007-07-20 17:24:56 +0900900 /*
901 * kick failed.
Andre Heiderbb4f85c2011-07-12 10:13:25 +0000902 * release descriptor which was just prepared
Masakazu Mokuno48544cc2007-07-20 17:24:56 +0900903 */
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900904 netdev->stats.tx_dropped++;
Andre Heiderbb4f85c2011-07-12 10:13:25 +0000905 /* don't trigger BUG_ON() in gelic_descr_release_tx */
906 descr->data_status = cpu_to_be32(GELIC_DESCR_TX_TAIL);
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900907 gelic_descr_release_tx(card, descr);
Andre Heiderbb4f85c2011-07-12 10:13:25 +0000908 /* reset head */
909 card->tx_chain.head = descr;
910 /* reset hw termination */
911 descr->prev->next_descr_addr = 0;
Masakazu Mokuno48544cc2007-07-20 17:24:56 +0900912 dev_info(ctodev(card), "%s: kick failure\n", __func__);
Masakazu Mokuno48544cc2007-07-20 17:24:56 +0900913 }
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900914
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900915 spin_unlock_irqrestore(&card->tx_lock, flags);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900916 return NETDEV_TX_OK;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900917}
918
919/**
920 * gelic_net_pass_skb_up - takes an skb from a descriptor and passes it on
921 * @descr: descriptor to process
922 * @card: card structure
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900923 * @netdev: net_device structure to be passed packet
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900924 *
925 * iommu-unmaps the skb, fills out skb structure and passes the data to the
926 * stack. The descriptor state is not changed.
927 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900928static void gelic_net_pass_skb_up(struct gelic_descr *descr,
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900929 struct gelic_card *card,
930 struct net_device *netdev)
931
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900932{
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900933 struct sk_buff *skb = descr->skb;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900934 u32 data_status, data_error;
935
Masakazu Mokuno100e1d82008-02-07 19:57:54 +0900936 data_status = be32_to_cpu(descr->data_status);
937 data_error = be32_to_cpu(descr->data_error);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900938 /* unmap skb buffer */
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900939 dma_unmap_single(ctodev(card), be32_to_cpu(descr->buf_addr),
940 GELIC_NET_MAX_MTU,
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900941 DMA_FROM_DEVICE);
942
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900943 skb_put(skb, be32_to_cpu(descr->valid_size)?
Masakazu Mokuno100e1d82008-02-07 19:57:54 +0900944 be32_to_cpu(descr->valid_size) :
945 be32_to_cpu(descr->result_size));
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900946 if (!descr->valid_size)
947 dev_info(ctodev(card), "buffer full %x %x %x\n",
Masakazu Mokuno100e1d82008-02-07 19:57:54 +0900948 be32_to_cpu(descr->result_size),
949 be32_to_cpu(descr->buf_size),
950 be32_to_cpu(descr->dmac_cmd_status));
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900951
952 descr->skb = NULL;
953 /*
954 * the card put 2 bytes vlan tag in front
955 * of the ethernet frame
956 */
957 skb_pull(skb, 2);
958 skb->protocol = eth_type_trans(skb, netdev);
959
960 /* checksum offload */
Michał Mirosławd1423c72011-04-10 04:49:55 +0000961 if (netdev->features & NETIF_F_RXCSUM) {
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900962 if ((data_status & GELIC_DESCR_DATA_STATUS_CHK_MASK) &&
963 (!(data_error & GELIC_DESCR_DATA_ERROR_CHK_MASK)))
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900964 skb->ip_summed = CHECKSUM_UNNECESSARY;
965 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -0700966 skb_checksum_none_assert(skb);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900967 } else
Eric Dumazetbc8acf22010-09-02 13:07:41 -0700968 skb_checksum_none_assert(skb);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900969
970 /* update netdevice statistics */
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900971 netdev->stats.rx_packets++;
972 netdev->stats.rx_bytes += skb->len;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900973
974 /* pass skb up to stack */
975 netif_receive_skb(skb);
976}
977
978/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900979 * gelic_card_decode_one_descr - processes an rx descriptor
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900980 * @card: card structure
981 *
982 * returns 1 if a packet has been sent to the stack, otherwise 0
983 *
984 * processes an rx descriptor by iommu-unmapping the data buffer and passing
985 * the packet up to the stack
986 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900987static int gelic_card_decode_one_descr(struct gelic_card *card)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900988{
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900989 enum gelic_descr_dma_status status;
990 struct gelic_descr_chain *chain = &card->rx_chain;
Masakazu Mokuno589866f2008-02-07 19:58:42 +0900991 struct gelic_descr *descr = chain->head;
992 struct net_device *netdev = NULL;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900993 int dmac_chain_ended;
994
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900995 status = gelic_descr_get_status(descr);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900996 /* is this descriptor terminated with next_descr == NULL? */
997 dmac_chain_ended =
Masakazu Mokuno100e1d82008-02-07 19:57:54 +0900998 be32_to_cpu(descr->dmac_cmd_status) &
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900999 GELIC_DESCR_RX_DMA_CHAIN_END;
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001000
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001001 if (status == GELIC_DESCR_DMA_CARDOWNED)
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001002 return 0;
1003
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001004 if (status == GELIC_DESCR_DMA_NOT_IN_USE) {
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001005 dev_dbg(ctodev(card), "dormant descr? %p\n", descr);
1006 return 0;
1007 }
1008
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001009 /* netdevice select */
1010 if (card->vlan_required) {
1011 unsigned int i;
1012 u16 vid;
1013 vid = *(u16 *)(descr->skb->data) & VLAN_VID_MASK;
1014 for (i = 0; i < GELIC_PORT_MAX; i++) {
1015 if (card->vlan[i].rx == vid) {
1016 netdev = card->netdev[i];
1017 break;
1018 }
Joe Perches6403eab2011-06-03 11:51:20 +00001019 }
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001020 if (GELIC_PORT_MAX <= i) {
1021 pr_info("%s: unknown packet vid=%x\n", __func__, vid);
1022 goto refill;
1023 }
1024 } else
Geoff Levandd4d7f1f2009-12-01 12:15:53 +00001025 netdev = card->netdev[GELIC_PORT_ETHERNET_0];
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001026
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001027 if ((status == GELIC_DESCR_DMA_RESPONSE_ERROR) ||
1028 (status == GELIC_DESCR_DMA_PROTECTION_ERROR) ||
1029 (status == GELIC_DESCR_DMA_FORCE_END)) {
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001030 dev_info(ctodev(card), "dropping RX descriptor with state %x\n",
1031 status);
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001032 netdev->stats.rx_dropped++;
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001033 goto refill;
1034 }
1035
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001036 if (status == GELIC_DESCR_DMA_BUFFER_FULL) {
Masakazu Mokunofe6d3a42007-07-20 17:39:25 +09001037 /*
1038 * Buffer full would occur if and only if
1039 * the frame length was longer than the size of this
1040 * descriptor's buffer. If the frame length was equal
1041 * to or shorter than buffer'size, FRAME_END condition
1042 * would occur.
1043 * Anyway this frame was longer than the MTU,
1044 * just drop it.
1045 */
1046 dev_info(ctodev(card), "overlength frame\n");
1047 goto refill;
1048 }
1049 /*
Andre Heidere3001c92011-07-12 10:13:24 +00001050 * descriptors any other than FRAME_END here should
Masakazu Mokunofe6d3a42007-07-20 17:39:25 +09001051 * be treated as error.
1052 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001053 if (status != GELIC_DESCR_DMA_FRAME_END) {
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001054 dev_dbg(ctodev(card), "RX descriptor with state %x\n",
1055 status);
1056 goto refill;
1057 }
1058
1059 /* ok, we've got a packet in descr */
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001060 gelic_net_pass_skb_up(descr, card, netdev);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001061refill:
Masakazu Mokunofe6d3a42007-07-20 17:39:25 +09001062 /*
1063 * So that always DMAC can see the end
1064 * of the descriptor chain to avoid
1065 * from unwanted DMAC overrun.
1066 */
1067 descr->next_descr_addr = 0;
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001068
1069 /* change the descriptor state: */
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001070 gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001071
Masakazu Mokunofe6d3a42007-07-20 17:39:25 +09001072 /*
1073 * this call can fail, but for now, just leave this
1074 * decriptor without skb
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001075 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001076 gelic_descr_prepare_rx(card, descr);
Masakazu Mokunofe6d3a42007-07-20 17:39:25 +09001077
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001078 chain->tail = descr;
1079 chain->head = descr->next;
Masakazu Mokunofe6d3a42007-07-20 17:39:25 +09001080
1081 /*
1082 * Set this descriptor the end of the chain.
1083 */
Masakazu Mokuno100e1d82008-02-07 19:57:54 +09001084 descr->prev->next_descr_addr = cpu_to_be32(descr->bus_addr);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001085
Masakazu Mokunofe6d3a42007-07-20 17:39:25 +09001086 /*
1087 * If dmac chain was met, DMAC stopped.
1088 * thus re-enable it
1089 */
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001090 if (dmac_chain_ended) {
Masakazu Mokuno583aae12007-07-20 17:35:54 +09001091 card->rx_dma_restart_required = 1;
1092 dev_dbg(ctodev(card), "reenable rx dma scheduled\n");
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001093 }
1094
1095 return 1;
1096}
1097
1098/**
1099 * gelic_net_poll - NAPI poll function called by the stack to return packets
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001100 * @napi: napi structure
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001101 * @budget: number of packets we can pass to the stack at most
1102 *
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001103 * returns the number of the processed packets
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001104 *
1105 */
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001106static int gelic_net_poll(struct napi_struct *napi, int budget)
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001107{
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001108 struct gelic_card *card = container_of(napi, struct gelic_card, napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001109 int packets_done = 0;
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001110
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001111 while (packets_done < budget) {
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001112 if (!gelic_card_decode_one_descr(card))
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001113 break;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001114
1115 packets_done++;
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001116 }
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001117
1118 if (packets_done < budget) {
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001119 napi_complete(napi);
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001120 gelic_card_rx_irq_on(card);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001121 }
1122 return packets_done;
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001123}
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001124/**
1125 * gelic_net_change_mtu - changes the MTU of an interface
1126 * @netdev: interface device structure
1127 * @new_mtu: new MTU value
1128 *
1129 * returns 0 on success, <0 on failure
1130 */
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001131int gelic_net_change_mtu(struct net_device *netdev, int new_mtu)
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001132{
1133 /* no need to re-alloc skbs or so -- the max mtu is about 2.3k
1134 * and mtu is outbound only anyway */
1135 if ((new_mtu < GELIC_NET_MIN_MTU) ||
1136 (new_mtu > GELIC_NET_MAX_MTU)) {
1137 return -EINVAL;
1138 }
1139 netdev->mtu = new_mtu;
1140 return 0;
1141}
1142
1143/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001144 * gelic_card_interrupt - event handler for gelic_net
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001145 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001146static irqreturn_t gelic_card_interrupt(int irq, void *ptr)
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001147{
1148 unsigned long flags;
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001149 struct gelic_card *card = ptr;
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001150 u64 status;
1151
1152 status = card->irq_status;
1153
1154 if (!status)
1155 return IRQ_NONE;
1156
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001157 status &= card->irq_mask;
1158
Masakazu Mokuno583aae12007-07-20 17:35:54 +09001159 if (card->rx_dma_restart_required) {
1160 card->rx_dma_restart_required = 0;
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001161 gelic_card_enable_rxdmac(card);
Masakazu Mokuno583aae12007-07-20 17:35:54 +09001162 }
1163
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001164 if (status & GELIC_CARD_RXINT) {
1165 gelic_card_rx_irq_off(card);
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001166 napi_schedule(&card->napi);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001167 }
1168
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001169 if (status & GELIC_CARD_TXINT) {
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001170 spin_lock_irqsave(&card->tx_lock, flags);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001171 card->tx_dma_progress = 0;
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001172 gelic_card_release_tx_chain(card, 0);
Masakazu Mokuno48544cc2007-07-20 17:24:56 +09001173 /* kick outstanding tx descriptor if any */
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001174 gelic_card_kick_txdma(card, card->tx_chain.tail);
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001175 spin_unlock_irqrestore(&card->tx_lock, flags);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001176 }
Masakazu Mokuno01fed4c2008-02-07 19:58:32 +09001177
1178 /* ether port status changed */
1179 if (status & GELIC_CARD_PORT_STATUS_CHANGED)
1180 gelic_card_get_ether_port_status(card, 1);
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001181
Masakazu Mokuno09dde542008-02-07 19:58:57 +09001182#ifdef CONFIG_GELIC_WIRELESS
1183 if (status & (GELIC_CARD_WLAN_EVENT_RECEIVED |
1184 GELIC_CARD_WLAN_COMMAND_COMPLETED))
1185 gelic_wl_interrupt(card->netdev[GELIC_PORT_WIRELESS], status);
1186#endif
1187
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001188 return IRQ_HANDLED;
1189}
1190
1191#ifdef CONFIG_NET_POLL_CONTROLLER
1192/**
1193 * gelic_net_poll_controller - artificial interrupt for netconsole etc.
1194 * @netdev: interface device structure
1195 *
1196 * see Documentation/networking/netconsole.txt
1197 */
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001198void gelic_net_poll_controller(struct net_device *netdev)
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001199{
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001200 struct gelic_card *card = netdev_card(netdev);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001201
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001202 gelic_card_set_irq_mask(card, 0);
1203 gelic_card_interrupt(netdev->irq, netdev);
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001204 gelic_card_set_irq_mask(card, card->irq_mask);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001205}
1206#endif /* CONFIG_NET_POLL_CONTROLLER */
1207
1208/**
Andre Heidere3001c92011-07-12 10:13:24 +00001209 * gelic_net_open - called upon ifconfig up
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001210 * @netdev: interface device structure
1211 *
1212 * returns 0 on success, <0 on failure
1213 *
1214 * gelic_net_open allocates all the descriptors and memory needed for
1215 * operation, sets up multicast list and enables interrupts
1216 */
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001217int gelic_net_open(struct net_device *netdev)
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001218{
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001219 struct gelic_card *card = netdev_card(netdev);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001220
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001221 dev_dbg(ctodev(card), " -> %s %p\n", __func__, netdev);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001222
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001223 gelic_card_up(card);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001224
1225 netif_start_queue(netdev);
Masakazu Mokuno01fed4c2008-02-07 19:58:32 +09001226 gelic_card_get_ether_port_status(card, 1);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001227
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001228 dev_dbg(ctodev(card), " <- %s\n", __func__);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001229 return 0;
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001230}
1231
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001232void gelic_net_get_drvinfo(struct net_device *netdev,
1233 struct ethtool_drvinfo *info)
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001234{
1235 strncpy(info->driver, DRV_NAME, sizeof(info->driver) - 1);
1236 strncpy(info->version, DRV_VERSION, sizeof(info->version) - 1);
1237}
1238
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001239static int gelic_ether_get_settings(struct net_device *netdev,
1240 struct ethtool_cmd *cmd)
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001241{
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001242 struct gelic_card *card = netdev_card(netdev);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001243
Masakazu Mokuno01fed4c2008-02-07 19:58:32 +09001244 gelic_card_get_ether_port_status(card, 0);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001245
Masakazu Mokuno01fed4c2008-02-07 19:58:32 +09001246 if (card->ether_port_status & GELIC_LV1_ETHER_FULL_DUPLEX)
1247 cmd->duplex = DUPLEX_FULL;
1248 else
1249 cmd->duplex = DUPLEX_HALF;
1250
1251 switch (card->ether_port_status & GELIC_LV1_ETHER_SPEED_MASK) {
1252 case GELIC_LV1_ETHER_SPEED_10:
David Decotigny70739492011-04-27 18:32:40 +00001253 ethtool_cmd_speed_set(cmd, SPEED_10);
Masakazu Mokuno01fed4c2008-02-07 19:58:32 +09001254 break;
1255 case GELIC_LV1_ETHER_SPEED_100:
David Decotigny70739492011-04-27 18:32:40 +00001256 ethtool_cmd_speed_set(cmd, SPEED_100);
Masakazu Mokuno01fed4c2008-02-07 19:58:32 +09001257 break;
1258 case GELIC_LV1_ETHER_SPEED_1000:
David Decotigny70739492011-04-27 18:32:40 +00001259 ethtool_cmd_speed_set(cmd, SPEED_1000);
Masakazu Mokuno01fed4c2008-02-07 19:58:32 +09001260 break;
1261 default:
1262 pr_info("%s: speed unknown\n", __func__);
David Decotigny70739492011-04-27 18:32:40 +00001263 ethtool_cmd_speed_set(cmd, SPEED_10);
Masakazu Mokuno01fed4c2008-02-07 19:58:32 +09001264 break;
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001265 }
Masakazu Mokuno01fed4c2008-02-07 19:58:32 +09001266
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001267 cmd->supported = SUPPORTED_TP | SUPPORTED_Autoneg |
1268 SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full |
1269 SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full |
Hideyuki Sasaki55873ed2009-12-01 12:15:58 +00001270 SUPPORTED_1000baseT_Full;
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001271 cmd->advertising = cmd->supported;
Hideyuki Sasaki55873ed2009-12-01 12:15:58 +00001272 if (card->link_mode & GELIC_LV1_ETHER_AUTO_NEG) {
1273 cmd->autoneg = AUTONEG_ENABLE;
1274 } else {
1275 cmd->autoneg = AUTONEG_DISABLE;
1276 cmd->advertising &= ~ADVERTISED_Autoneg;
1277 }
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001278 cmd->port = PORT_TP;
1279
1280 return 0;
1281}
1282
Hideyuki Sasaki55873ed2009-12-01 12:15:58 +00001283static int gelic_ether_set_settings(struct net_device *netdev,
1284 struct ethtool_cmd *cmd)
1285{
1286 struct gelic_card *card = netdev_card(netdev);
1287 u64 mode;
1288 int ret;
1289
1290 if (cmd->autoneg == AUTONEG_ENABLE) {
1291 mode = GELIC_LV1_ETHER_AUTO_NEG;
1292 } else {
1293 switch (cmd->speed) {
1294 case SPEED_10:
1295 mode = GELIC_LV1_ETHER_SPEED_10;
1296 break;
1297 case SPEED_100:
1298 mode = GELIC_LV1_ETHER_SPEED_100;
1299 break;
1300 case SPEED_1000:
1301 mode = GELIC_LV1_ETHER_SPEED_1000;
1302 break;
1303 default:
1304 return -EINVAL;
1305 }
1306 if (cmd->duplex == DUPLEX_FULL)
1307 mode |= GELIC_LV1_ETHER_FULL_DUPLEX;
1308 else if (cmd->speed == SPEED_1000) {
1309 pr_info("1000 half duplex is not supported.\n");
1310 return -EINVAL;
1311 }
1312 }
1313
1314 ret = gelic_card_set_link_mode(card, mode);
1315
1316 if (ret)
1317 return ret;
1318
1319 return 0;
1320}
1321
Masakazu Mokuno3faac212008-03-27 11:39:31 +11001322static void gelic_net_get_wol(struct net_device *netdev,
1323 struct ethtool_wolinfo *wol)
1324{
1325 if (0 <= ps3_compare_firmware_version(2, 2, 0))
1326 wol->supported = WAKE_MAGIC;
1327 else
1328 wol->supported = 0;
1329
1330 wol->wolopts = ps3_sys_manager_get_wol() ? wol->supported : 0;
1331 memset(&wol->sopass, 0, sizeof(wol->sopass));
1332}
1333static int gelic_net_set_wol(struct net_device *netdev,
1334 struct ethtool_wolinfo *wol)
1335{
1336 int status;
1337 struct gelic_card *card;
1338 u64 v1, v2;
1339
1340 if (ps3_compare_firmware_version(2, 2, 0) < 0 ||
1341 !capable(CAP_NET_ADMIN))
1342 return -EPERM;
1343
1344 if (wol->wolopts & ~WAKE_MAGIC)
1345 return -EINVAL;
1346
1347 card = netdev_card(netdev);
1348 if (wol->wolopts & WAKE_MAGIC) {
1349 status = lv1_net_control(bus_id(card), dev_id(card),
1350 GELIC_LV1_SET_WOL,
1351 GELIC_LV1_WOL_MAGIC_PACKET,
1352 0, GELIC_LV1_WOL_MP_ENABLE,
1353 &v1, &v2);
1354 if (status) {
1355 pr_info("%s: enabling WOL failed %d\n", __func__,
1356 status);
1357 status = -EIO;
1358 goto done;
1359 }
1360 status = lv1_net_control(bus_id(card), dev_id(card),
1361 GELIC_LV1_SET_WOL,
1362 GELIC_LV1_WOL_ADD_MATCH_ADDR,
1363 0, GELIC_LV1_WOL_MATCH_ALL,
1364 &v1, &v2);
1365 if (!status)
1366 ps3_sys_manager_set_wol(1);
1367 else {
1368 pr_info("%s: enabling WOL filter failed %d\n",
1369 __func__, status);
1370 status = -EIO;
1371 }
1372 } else {
1373 status = lv1_net_control(bus_id(card), dev_id(card),
1374 GELIC_LV1_SET_WOL,
1375 GELIC_LV1_WOL_MAGIC_PACKET,
1376 0, GELIC_LV1_WOL_MP_DISABLE,
1377 &v1, &v2);
1378 if (status) {
1379 pr_info("%s: disabling WOL failed %d\n", __func__,
1380 status);
1381 status = -EIO;
1382 goto done;
1383 }
1384 status = lv1_net_control(bus_id(card), dev_id(card),
1385 GELIC_LV1_SET_WOL,
1386 GELIC_LV1_WOL_DELETE_MATCH_ADDR,
1387 0, GELIC_LV1_WOL_MATCH_ALL,
1388 &v1, &v2);
1389 if (!status)
1390 ps3_sys_manager_set_wol(0);
1391 else {
1392 pr_info("%s: removing WOL filter failed %d\n",
1393 __func__, status);
1394 status = -EIO;
1395 }
1396 }
1397done:
1398 return status;
1399}
1400
Stephen Hemminger0fc0b732009-09-02 01:03:33 -07001401static const struct ethtool_ops gelic_ether_ethtool_ops = {
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001402 .get_drvinfo = gelic_net_get_drvinfo,
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001403 .get_settings = gelic_ether_get_settings,
Hideyuki Sasaki55873ed2009-12-01 12:15:58 +00001404 .set_settings = gelic_ether_set_settings,
Masakazu Mokuno7bc56b92008-02-07 19:58:20 +09001405 .get_link = ethtool_op_get_link,
Masakazu Mokuno3faac212008-03-27 11:39:31 +11001406 .get_wol = gelic_net_get_wol,
1407 .set_wol = gelic_net_set_wol,
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001408};
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001409
1410/**
1411 * gelic_net_tx_timeout_task - task scheduled by the watchdog timeout
1412 * function (to be called not under interrupt status)
1413 * @work: work is context of tx timout task
1414 *
1415 * called as task when tx hangs, resets interface (if interface is up)
1416 */
1417static void gelic_net_tx_timeout_task(struct work_struct *work)
1418{
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001419 struct gelic_card *card =
1420 container_of(work, struct gelic_card, tx_timeout_task);
Geoff Levandd4d7f1f2009-12-01 12:15:53 +00001421 struct net_device *netdev = card->netdev[GELIC_PORT_ETHERNET_0];
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001422
Frans Pop583d0772010-03-24 07:57:31 +00001423 dev_info(ctodev(card), "%s:Timed out. Restarting...\n", __func__);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001424
1425 if (!(netdev->flags & IFF_UP))
1426 goto out;
1427
1428 netif_device_detach(netdev);
1429 gelic_net_stop(netdev);
1430
1431 gelic_net_open(netdev);
1432 netif_device_attach(netdev);
1433
1434out:
1435 atomic_dec(&card->tx_timeout_task_counter);
1436}
1437
1438/**
1439 * gelic_net_tx_timeout - called when the tx timeout watchdog kicks in.
1440 * @netdev: interface device structure
1441 *
1442 * called, if tx hangs. Schedules a task that resets the interface
1443 */
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001444void gelic_net_tx_timeout(struct net_device *netdev)
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001445{
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001446 struct gelic_card *card;
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001447
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001448 card = netdev_card(netdev);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001449 atomic_inc(&card->tx_timeout_task_counter);
1450 if (netdev->flags & IFF_UP)
1451 schedule_work(&card->tx_timeout_task);
1452 else
1453 atomic_dec(&card->tx_timeout_task_counter);
1454}
1455
Masakazu Mokuno5384e832009-01-15 22:47:29 +00001456static const struct net_device_ops gelic_netdevice_ops = {
1457 .ndo_open = gelic_net_open,
1458 .ndo_stop = gelic_net_stop,
1459 .ndo_start_xmit = gelic_net_xmit,
1460 .ndo_set_multicast_list = gelic_net_set_multi,
1461 .ndo_change_mtu = gelic_net_change_mtu,
1462 .ndo_tx_timeout = gelic_net_tx_timeout,
Ben Hutchings240c1022009-07-09 17:54:35 +00001463 .ndo_set_mac_address = eth_mac_addr,
Masakazu Mokuno5384e832009-01-15 22:47:29 +00001464 .ndo_validate_addr = eth_validate_addr,
1465#ifdef CONFIG_NET_POLL_CONTROLLER
1466 .ndo_poll_controller = gelic_net_poll_controller,
1467#endif
1468};
1469
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001470/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001471 * gelic_ether_setup_netdev_ops - initialization of net_device operations
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001472 * @netdev: net_device structure
1473 *
1474 * fills out function pointers in the net_device structure
1475 */
Geert Uytterhoeven48dce822009-06-10 04:38:58 +00001476static void __devinit gelic_ether_setup_netdev_ops(struct net_device *netdev,
1477 struct napi_struct *napi)
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001478{
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001479 netdev->watchdog_timeo = GELIC_NET_WATCHDOG_TIMEOUT;
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001480 /* NAPI */
1481 netif_napi_add(netdev, napi,
1482 gelic_net_poll, GELIC_NET_NAPI_WEIGHT);
1483 netdev->ethtool_ops = &gelic_ether_ethtool_ops;
Masakazu Mokuno5384e832009-01-15 22:47:29 +00001484 netdev->netdev_ops = &gelic_netdevice_ops;
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001485}
1486
1487/**
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001488 * gelic_ether_setup_netdev - initialization of net_device
1489 * @netdev: net_device structure
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001490 * @card: card structure
1491 *
1492 * Returns 0 on success or <0 on failure
1493 *
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001494 * gelic_ether_setup_netdev initializes the net_device structure
1495 * and register it.
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001496 **/
Geert Uytterhoeven48dce822009-06-10 04:38:58 +00001497int __devinit gelic_net_setup_netdev(struct net_device *netdev,
1498 struct gelic_card *card)
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001499{
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001500 int status;
1501 u64 v1, v2;
1502
Michał Mirosławd1423c72011-04-10 04:49:55 +00001503 netdev->hw_features = NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
1504
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001505 netdev->features = NETIF_F_IP_CSUM;
Michał Mirosławd1423c72011-04-10 04:49:55 +00001506 if (GELIC_CARD_RX_CSUM_DEFAULT)
1507 netdev->features |= NETIF_F_RXCSUM;
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001508
1509 status = lv1_net_control(bus_id(card), dev_id(card),
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001510 GELIC_LV1_GET_MAC_ADDRESS,
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001511 0, 0, 0, &v1, &v2);
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001512 v1 <<= 16;
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001513 if (status || !is_valid_ether_addr((u8 *)&v1)) {
1514 dev_info(ctodev(card),
1515 "%s:lv1_net_control GET_MAC_ADDR failed %d\n",
1516 __func__, status);
1517 return -EINVAL;
1518 }
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001519 memcpy(netdev->dev_addr, &v1, ETH_ALEN);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001520
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001521 if (card->vlan_required) {
Masakazu Mokuno173261e2007-08-31 22:22:32 +09001522 netdev->hard_header_len += VLAN_HLEN;
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001523 /*
1524 * As vlan is internally used,
1525 * we can not receive vlan packets
1526 */
1527 netdev->features |= NETIF_F_VLAN_CHALLENGED;
Masakazu Mokuno173261e2007-08-31 22:22:32 +09001528 }
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001529
1530 status = register_netdev(netdev);
1531 if (status) {
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001532 dev_err(ctodev(card), "%s:Couldn't register %s %d\n",
1533 __func__, netdev->name, status);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001534 return status;
1535 }
Johannes Berge1749612008-10-27 15:59:26 -07001536 dev_info(ctodev(card), "%s: MAC addr %pM\n",
1537 netdev->name, netdev->dev_addr);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001538
1539 return 0;
1540}
1541
1542/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001543 * gelic_alloc_card_net - allocates net_device and card structure
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001544 *
1545 * returns the card structure or NULL in case of errors
1546 *
1547 * the card and net_device structures are linked to each other
1548 */
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001549#define GELIC_ALIGN (32)
Geert Uytterhoeven48dce822009-06-10 04:38:58 +00001550static struct gelic_card * __devinit gelic_alloc_card_net(struct net_device **netdev)
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001551{
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001552 struct gelic_card *card;
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001553 struct gelic_port *port;
1554 void *p;
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001555 size_t alloc_size;
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001556 /*
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001557 * gelic requires dma descriptor is 32 bytes aligned and
1558 * the hypervisor requires irq_status is 8 bytes aligned.
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001559 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001560 BUILD_BUG_ON(offsetof(struct gelic_card, irq_status) % 8);
1561 BUILD_BUG_ON(offsetof(struct gelic_card, descr) % 32);
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001562 alloc_size =
1563 sizeof(struct gelic_card) +
1564 sizeof(struct gelic_descr) * GELIC_NET_RX_DESCRIPTORS +
1565 sizeof(struct gelic_descr) * GELIC_NET_TX_DESCRIPTORS +
1566 GELIC_ALIGN - 1;
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001567
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001568 p = kzalloc(alloc_size, GFP_KERNEL);
1569 if (!p)
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001570 return NULL;
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001571 card = PTR_ALIGN(p, GELIC_ALIGN);
1572 card->unalign = p;
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001573
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001574 /*
1575 * alloc netdev
1576 */
1577 *netdev = alloc_etherdev(sizeof(struct gelic_port));
1578 if (!netdev) {
1579 kfree(card->unalign);
1580 return NULL;
1581 }
1582 port = netdev_priv(*netdev);
1583
1584 /* gelic_port */
1585 port->netdev = *netdev;
1586 port->card = card;
Geoff Levandd4d7f1f2009-12-01 12:15:53 +00001587 port->type = GELIC_PORT_ETHERNET_0;
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001588
1589 /* gelic_card */
Geoff Levandd4d7f1f2009-12-01 12:15:53 +00001590 card->netdev[GELIC_PORT_ETHERNET_0] = *netdev;
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001591
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001592 INIT_WORK(&card->tx_timeout_task, gelic_net_tx_timeout_task);
1593 init_waitqueue_head(&card->waitq);
1594 atomic_set(&card->tx_timeout_task_counter, 0);
Daniel Walker2914f3e2008-05-22 00:00:03 -07001595 mutex_init(&card->updown_lock);
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001596 atomic_set(&card->users, 0);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001597
1598 return card;
1599}
1600
Geert Uytterhoeven48dce822009-06-10 04:38:58 +00001601static void __devinit gelic_card_get_vlan_info(struct gelic_card *card)
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001602{
1603 u64 v1, v2;
1604 int status;
1605 unsigned int i;
1606 struct {
1607 int tx;
1608 int rx;
1609 } vlan_id_ix[2] = {
Geoff Levandd4d7f1f2009-12-01 12:15:53 +00001610 [GELIC_PORT_ETHERNET_0] = {
1611 .tx = GELIC_LV1_VLAN_TX_ETHERNET_0,
1612 .rx = GELIC_LV1_VLAN_RX_ETHERNET_0
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001613 },
1614 [GELIC_PORT_WIRELESS] = {
1615 .tx = GELIC_LV1_VLAN_TX_WIRELESS,
1616 .rx = GELIC_LV1_VLAN_RX_WIRELESS
1617 }
1618 };
1619
1620 for (i = 0; i < ARRAY_SIZE(vlan_id_ix); i++) {
1621 /* tx tag */
1622 status = lv1_net_control(bus_id(card), dev_id(card),
1623 GELIC_LV1_GET_VLAN_ID,
1624 vlan_id_ix[i].tx,
1625 0, 0, &v1, &v2);
1626 if (status || !v1) {
1627 if (status != LV1_NO_ENTRY)
1628 dev_dbg(ctodev(card),
1629 "get vlan id for tx(%d) failed(%d)\n",
1630 vlan_id_ix[i].tx, status);
1631 card->vlan[i].tx = 0;
1632 card->vlan[i].rx = 0;
1633 continue;
1634 }
1635 card->vlan[i].tx = (u16)v1;
1636
1637 /* rx tag */
1638 status = lv1_net_control(bus_id(card), dev_id(card),
1639 GELIC_LV1_GET_VLAN_ID,
1640 vlan_id_ix[i].rx,
1641 0, 0, &v1, &v2);
1642 if (status || !v1) {
1643 if (status != LV1_NO_ENTRY)
1644 dev_info(ctodev(card),
1645 "get vlan id for rx(%d) failed(%d)\n",
1646 vlan_id_ix[i].rx, status);
1647 card->vlan[i].tx = 0;
1648 card->vlan[i].rx = 0;
1649 continue;
1650 }
1651 card->vlan[i].rx = (u16)v1;
1652
1653 dev_dbg(ctodev(card), "vlan_id[%d] tx=%02x rx=%02x\n",
1654 i, card->vlan[i].tx, card->vlan[i].rx);
1655 }
1656
Geoff Levandd4d7f1f2009-12-01 12:15:53 +00001657 if (card->vlan[GELIC_PORT_ETHERNET_0].tx) {
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001658 BUG_ON(!card->vlan[GELIC_PORT_WIRELESS].tx);
1659 card->vlan_required = 1;
1660 } else
1661 card->vlan_required = 0;
1662
1663 /* check wirelss capable firmware */
1664 if (ps3_compare_firmware_version(1, 6, 0) < 0) {
1665 card->vlan[GELIC_PORT_WIRELESS].tx = 0;
1666 card->vlan[GELIC_PORT_WIRELESS].rx = 0;
1667 }
1668
1669 dev_info(ctodev(card), "internal vlan %s\n",
1670 card->vlan_required? "enabled" : "disabled");
1671}
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001672/**
1673 * ps3_gelic_driver_probe - add a device to the control of this driver
1674 */
Geert Uytterhoeven48dce822009-06-10 04:38:58 +00001675static int __devinit ps3_gelic_driver_probe(struct ps3_system_bus_device *dev)
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001676{
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001677 struct gelic_card *card;
1678 struct net_device *netdev;
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001679 int result;
1680
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001681 pr_debug("%s: called\n", __func__);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001682 result = ps3_open_hv_device(dev);
1683
1684 if (result) {
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001685 dev_dbg(&dev->core, "%s:ps3_open_hv_device failed\n",
1686 __func__);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001687 goto fail_open;
1688 }
1689
1690 result = ps3_dma_region_create(dev->d_region);
1691
1692 if (result) {
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001693 dev_dbg(&dev->core, "%s:ps3_dma_region_create failed(%d)\n",
1694 __func__, result);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001695 BUG_ON("check region type");
1696 goto fail_dma_region;
1697 }
1698
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001699 /* alloc card/netdevice */
1700 card = gelic_alloc_card_net(&netdev);
1701 if (!card) {
1702 dev_info(&dev->core, "%s:gelic_net_alloc_card failed\n",
1703 __func__);
1704 result = -ENOMEM;
1705 goto fail_alloc_card;
1706 }
Geert Uytterhoeven03fa68c2009-06-10 04:38:54 +00001707 ps3_system_bus_set_drvdata(dev, card);
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001708 card->dev = dev;
1709
1710 /* get internal vlan info */
1711 gelic_card_get_vlan_info(card);
1712
Hideyuki Sasaki55873ed2009-12-01 12:15:58 +00001713 card->link_mode = GELIC_LV1_ETHER_AUTO_NEG;
1714
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001715 /* setup interrupt */
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001716 result = lv1_net_set_interrupt_status_indicator(bus_id(card),
1717 dev_id(card),
1718 ps3_mm_phys_to_lpar(__pa(&card->irq_status)),
1719 0);
1720
1721 if (result) {
1722 dev_dbg(&dev->core,
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001723 "%s:set_interrupt_status_indicator failed: %s\n",
1724 __func__, ps3_result(result));
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001725 result = -EIO;
1726 goto fail_status_indicator;
1727 }
1728
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001729 result = ps3_sb_event_receive_port_setup(dev, PS3_BINDING_CPU_ANY,
1730 &card->irq);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001731
1732 if (result) {
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001733 dev_info(ctodev(card),
1734 "%s:gelic_net_open_device failed (%d)\n",
1735 __func__, result);
1736 result = -EPERM;
1737 goto fail_alloc_irq;
1738 }
1739 result = request_irq(card->irq, gelic_card_interrupt,
1740 IRQF_DISABLED, netdev->name, card);
1741
1742 if (result) {
1743 dev_info(ctodev(card), "%s:request_irq failed (%d)\n",
1744 __func__, result);
1745 goto fail_request_irq;
1746 }
1747
1748 /* setup card structure */
1749 card->irq_mask = GELIC_CARD_RXINT | GELIC_CARD_TXINT |
1750 GELIC_CARD_PORT_STATUS_CHANGED;
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001751
1752
1753 if (gelic_card_init_chain(card, &card->tx_chain,
1754 card->descr, GELIC_NET_TX_DESCRIPTORS))
1755 goto fail_alloc_tx;
1756 if (gelic_card_init_chain(card, &card->rx_chain,
1757 card->descr + GELIC_NET_TX_DESCRIPTORS,
1758 GELIC_NET_RX_DESCRIPTORS))
1759 goto fail_alloc_rx;
1760
1761 /* head of chain */
1762 card->tx_top = card->tx_chain.head;
1763 card->rx_top = card->rx_chain.head;
1764 dev_dbg(ctodev(card), "descr rx %p, tx %p, size %#lx, num %#x\n",
1765 card->rx_top, card->tx_top, sizeof(struct gelic_descr),
1766 GELIC_NET_RX_DESCRIPTORS);
1767 /* allocate rx skbs */
1768 if (gelic_card_alloc_rx_skbs(card))
1769 goto fail_alloc_skbs;
1770
1771 spin_lock_init(&card->tx_lock);
1772 card->tx_dma_progress = 0;
1773
1774 /* setup net_device structure */
1775 netdev->irq = card->irq;
1776 SET_NETDEV_DEV(netdev, &card->dev->core);
1777 gelic_ether_setup_netdev_ops(netdev, &card->napi);
1778 result = gelic_net_setup_netdev(netdev, card);
1779 if (result) {
1780 dev_dbg(&dev->core, "%s: setup_netdev failed %d",
1781 __func__, result);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001782 goto fail_setup_netdev;
1783 }
1784
Masakazu Mokuno09dde542008-02-07 19:58:57 +09001785#ifdef CONFIG_GELIC_WIRELESS
1786 if (gelic_wl_driver_probe(card)) {
1787 dev_dbg(&dev->core, "%s: WL init failed\n", __func__);
1788 goto fail_setup_netdev;
1789 }
1790#endif
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001791 pr_debug("%s: done\n", __func__);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001792 return 0;
1793
1794fail_setup_netdev:
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001795fail_alloc_skbs:
1796 gelic_card_free_chain(card, card->rx_chain.head);
1797fail_alloc_rx:
1798 gelic_card_free_chain(card, card->tx_chain.head);
1799fail_alloc_tx:
1800 free_irq(card->irq, card);
1801 netdev->irq = NO_IRQ;
1802fail_request_irq:
1803 ps3_sb_event_receive_port_destroy(dev, card->irq);
1804fail_alloc_irq:
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001805 lv1_net_set_interrupt_status_indicator(bus_id(card),
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001806 bus_id(card),
1807 0, 0);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001808fail_status_indicator:
Geert Uytterhoeven03fa68c2009-06-10 04:38:54 +00001809 ps3_system_bus_set_drvdata(dev, NULL);
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001810 kfree(netdev_card(netdev)->unalign);
1811 free_netdev(netdev);
1812fail_alloc_card:
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001813 ps3_dma_region_free(dev->d_region);
1814fail_dma_region:
1815 ps3_close_hv_device(dev);
1816fail_open:
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001817 return result;
1818}
1819
1820/**
1821 * ps3_gelic_driver_remove - remove a device from the control of this driver
1822 */
1823
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001824static int ps3_gelic_driver_remove(struct ps3_system_bus_device *dev)
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001825{
Geert Uytterhoeven03fa68c2009-06-10 04:38:54 +00001826 struct gelic_card *card = ps3_system_bus_get_drvdata(dev);
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001827 struct net_device *netdev0;
1828 pr_debug("%s: called\n", __func__);
1829
Hideyuki Sasaki55873ed2009-12-01 12:15:58 +00001830 /* set auto-negotiation */
1831 gelic_card_set_link_mode(card, GELIC_LV1_ETHER_AUTO_NEG);
1832
Masakazu Mokuno09dde542008-02-07 19:58:57 +09001833#ifdef CONFIG_GELIC_WIRELESS
1834 gelic_wl_driver_remove(card);
1835#endif
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001836 /* stop interrupt */
1837 gelic_card_set_irq_mask(card, 0);
1838
1839 /* turn off DMA, force end */
1840 gelic_card_disable_rxdmac(card);
1841 gelic_card_disable_txdmac(card);
1842
1843 /* release chains */
1844 gelic_card_release_tx_chain(card, 1);
1845 gelic_card_release_rx_chain(card);
1846
1847 gelic_card_free_chain(card, card->tx_top);
1848 gelic_card_free_chain(card, card->rx_top);
1849
Geoff Levandd4d7f1f2009-12-01 12:15:53 +00001850 netdev0 = card->netdev[GELIC_PORT_ETHERNET_0];
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001851 /* disconnect event port */
1852 free_irq(card->irq, card);
1853 netdev0->irq = NO_IRQ;
1854 ps3_sb_event_receive_port_destroy(card->dev, card->irq);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001855
1856 wait_event(card->waitq,
1857 atomic_read(&card->tx_timeout_task_counter) == 0);
1858
1859 lv1_net_set_interrupt_status_indicator(bus_id(card), dev_id(card),
1860 0 , 0);
1861
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001862 unregister_netdev(netdev0);
1863 kfree(netdev_card(netdev0)->unalign);
1864 free_netdev(netdev0);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001865
Geert Uytterhoeven03fa68c2009-06-10 04:38:54 +00001866 ps3_system_bus_set_drvdata(dev, NULL);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001867
1868 ps3_dma_region_free(dev->d_region);
1869
1870 ps3_close_hv_device(dev);
1871
Masakazu Mokuno589866f2008-02-07 19:58:42 +09001872 pr_debug("%s: done\n", __func__);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001873 return 0;
1874}
1875
1876static struct ps3_system_bus_driver ps3_gelic_driver = {
1877 .match_id = PS3_MATCH_ID_GELIC,
1878 .probe = ps3_gelic_driver_probe,
1879 .remove = ps3_gelic_driver_remove,
1880 .shutdown = ps3_gelic_driver_remove,
1881 .core.name = "ps3_gelic_driver",
1882 .core.owner = THIS_MODULE,
1883};
1884
1885static int __init ps3_gelic_driver_init (void)
1886{
1887 return firmware_has_feature(FW_FEATURE_PS3_LV1)
1888 ? ps3_system_bus_driver_register(&ps3_gelic_driver)
1889 : -ENODEV;
1890}
1891
1892static void __exit ps3_gelic_driver_exit (void)
1893{
1894 ps3_system_bus_driver_unregister(&ps3_gelic_driver);
1895}
1896
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001897module_init(ps3_gelic_driver_init);
1898module_exit(ps3_gelic_driver_exit);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001899
1900MODULE_ALIAS(PS3_MODULE_ALIAS_GELIC);
1901