blob: 87fc3b765e8e16ea460a51cde8ecf64612fe9471 [file] [log] [blame]
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001/*
2 * PS3 gelic network driver.
3 *
4 * Copyright (C) 2007 Sony Computer Entertainment Inc.
5 * Copyright 2006, 2007 Sony Corporation
6 *
7 * This file is based on: spider_net.c
8 *
9 * (C) Copyright IBM Corp. 2005
10 *
11 * Authors : Utz Bacher <utz.bacher@de.ibm.com>
12 * Jens Osterkamp <Jens.Osterkamp@de.ibm.com>
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2, or (at your option)
17 * any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 */
28
29#undef DEBUG
30
31#include <linux/kernel.h>
32#include <linux/module.h>
33
34#include <linux/etherdevice.h>
35#include <linux/ethtool.h>
36#include <linux/if_vlan.h>
37
38#include <linux/in.h>
39#include <linux/ip.h>
40#include <linux/tcp.h>
41
42#include <linux/dma-mapping.h>
43#include <net/checksum.h>
44#include <asm/firmware.h>
45#include <asm/ps3.h>
46#include <asm/lv1call.h>
47
48#include "ps3_gelic_net.h"
49
50#define DRV_NAME "Gelic Network Driver"
51#define DRV_VERSION "1.0"
52
53MODULE_AUTHOR("SCE Inc.");
54MODULE_DESCRIPTION("Gelic Network driver");
55MODULE_LICENSE("GPL");
56
Masakazu Mokuno59e97322008-02-07 19:58:08 +090057static inline struct device *ctodev(struct gelic_card *card)
Masakazu Mokuno02c18892007-07-05 20:11:16 +090058{
59 return &card->dev->core;
60}
Masakazu Mokuno59e97322008-02-07 19:58:08 +090061static inline u64 bus_id(struct gelic_card *card)
Masakazu Mokuno02c18892007-07-05 20:11:16 +090062{
63 return card->dev->bus_id;
64}
Masakazu Mokuno59e97322008-02-07 19:58:08 +090065static inline u64 dev_id(struct gelic_card *card)
Masakazu Mokuno02c18892007-07-05 20:11:16 +090066{
67 return card->dev->dev_id;
68}
69
70/* set irq_mask */
Masakazu Mokuno59e97322008-02-07 19:58:08 +090071static int gelic_card_set_irq_mask(struct gelic_card *card, u64 mask)
Masakazu Mokuno02c18892007-07-05 20:11:16 +090072{
73 int status;
74
75 status = lv1_net_set_interrupt_mask(bus_id(card), dev_id(card),
76 mask, 0);
77 if (status)
78 dev_info(ctodev(card),
79 "lv1_net_set_interrupt_mask failed %d\n", status);
80 return status;
81}
Masakazu Mokuno59e97322008-02-07 19:58:08 +090082static inline void gelic_card_rx_irq_on(struct gelic_card *card)
Masakazu Mokuno02c18892007-07-05 20:11:16 +090083{
Masakazu Mokuno59e97322008-02-07 19:58:08 +090084 gelic_card_set_irq_mask(card, card->ghiintmask | GELIC_CARD_RXINT);
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 Mokuno59e97322008-02-07 19:58:08 +090088 gelic_card_set_irq_mask(card, card->ghiintmask & ~GELIC_CARD_RXINT);
Masakazu Mokuno02c18892007-07-05 20:11:16 +090089}
90/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +090091 * gelic_descr_get_status -- returns the status of a descriptor
Masakazu Mokuno02c18892007-07-05 20:11:16 +090092 * @descr: descriptor to look at
93 *
94 * returns the status as in the dmac_cmd_status field of the descriptor
95 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +090096static enum gelic_descr_dma_status
97gelic_descr_get_status(struct gelic_descr *descr)
Masakazu Mokuno02c18892007-07-05 20:11:16 +090098{
Masakazu Mokuno59e97322008-02-07 19:58:08 +090099 return be32_to_cpu(descr->dmac_cmd_status) & GELIC_DESCR_DMA_STAT_MASK;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900100}
101
102/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900103 * gelic_descr_set_status -- sets the status of a descriptor
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900104 * @descr: descriptor to change
105 * @status: status to set in the descriptor
106 *
107 * changes the status to the specified value. Doesn't change other bits
108 * in the status
109 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900110static void gelic_descr_set_status(struct gelic_descr *descr,
111 enum gelic_descr_dma_status status)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900112{
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900113 descr->dmac_cmd_status = cpu_to_be32(status |
114 (be32_to_cpu(descr->dmac_cmd_status) &
115 ~GELIC_DESCR_DMA_STAT_MASK));
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900116 /*
117 * dma_cmd_status field is used to indicate whether the descriptor
118 * is valid or not.
119 * Usually caller of this function wants to inform that to the
120 * hardware, so we assure here the hardware sees the change.
121 */
122 wmb();
123}
124
125/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900126 * gelic_card_free_chain - free descriptor chain
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900127 * @card: card structure
128 * @descr_in: address of desc
129 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900130static void gelic_card_free_chain(struct gelic_card *card,
131 struct gelic_descr *descr_in)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900132{
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900133 struct gelic_descr *descr;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900134
135 for (descr = descr_in; descr && descr->bus_addr; descr = descr->next) {
136 dma_unmap_single(ctodev(card), descr->bus_addr,
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900137 GELIC_DESCR_SIZE, DMA_BIDIRECTIONAL);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900138 descr->bus_addr = 0;
139 }
140}
141
142/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900143 * gelic_card_init_chain - links descriptor chain
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900144 * @card: card structure
145 * @chain: address of chain
146 * @start_descr: address of descriptor array
147 * @no: number of descriptors
148 *
149 * we manage a circular list that mirrors the hardware structure,
150 * except that the hardware uses bus addresses.
151 *
152 * returns 0 on success, <0 on failure
153 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900154static int gelic_card_init_chain(struct gelic_card *card,
155 struct gelic_descr_chain *chain,
156 struct gelic_descr *start_descr, int no)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900157{
158 int i;
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900159 struct gelic_descr *descr;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900160
161 descr = start_descr;
162 memset(descr, 0, sizeof(*descr) * no);
163
164 /* set up the hardware pointers in each descriptor */
165 for (i = 0; i < no; i++, descr++) {
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900166 gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900167 descr->bus_addr =
168 dma_map_single(ctodev(card), descr,
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900169 GELIC_DESCR_SIZE,
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900170 DMA_BIDIRECTIONAL);
171
172 if (!descr->bus_addr)
173 goto iommu_error;
174
175 descr->next = descr + 1;
176 descr->prev = descr - 1;
177 }
178 /* make them as ring */
179 (descr - 1)->next = start_descr;
180 start_descr->prev = (descr - 1);
181
182 /* chain bus addr of hw descriptor */
183 descr = start_descr;
184 for (i = 0; i < no; i++, descr++) {
Masakazu Mokuno100e1d82008-02-07 19:57:54 +0900185 descr->next_descr_addr = cpu_to_be32(descr->next->bus_addr);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900186 }
187
188 chain->head = start_descr;
189 chain->tail = start_descr;
190
191 /* do not chain last hw descriptor */
192 (descr - 1)->next_descr_addr = 0;
193
194 return 0;
195
196iommu_error:
197 for (i--, descr--; 0 <= i; i--, descr--)
198 if (descr->bus_addr)
199 dma_unmap_single(ctodev(card), descr->bus_addr,
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900200 GELIC_DESCR_SIZE,
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900201 DMA_BIDIRECTIONAL);
202 return -ENOMEM;
203}
204
205/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900206 * gelic_descr_prepare_rx - reinitializes a rx descriptor
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900207 * @card: card structure
208 * @descr: descriptor to re-init
209 *
210 * return 0 on succes, <0 on failure
211 *
212 * allocates a new rx skb, iommu-maps it and attaches it to the descriptor.
213 * Activate the descriptor state-wise
214 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900215static int gelic_descr_prepare_rx(struct gelic_card *card,
216 struct gelic_descr *descr)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900217{
218 int offset;
219 unsigned int bufsize;
220
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900221 if (gelic_descr_get_status(descr) != GELIC_DESCR_DMA_NOT_IN_USE)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900222 dev_info(ctodev(card), "%s: ERROR status \n", __func__);
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900223
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900224 /* we need to round up the buffer size to a multiple of 128 */
225 bufsize = ALIGN(GELIC_NET_MAX_MTU, GELIC_NET_RXBUF_ALIGN);
226
227 /* and we need to have it 128 byte aligned, therefore we allocate a
228 * bit more */
229 descr->skb = netdev_alloc_skb(card->netdev,
230 bufsize + GELIC_NET_RXBUF_ALIGN - 1);
231 if (!descr->skb) {
232 descr->buf_addr = 0; /* tell DMAC don't touch memory */
233 dev_info(ctodev(card),
234 "%s:allocate skb failed !!\n", __func__);
235 return -ENOMEM;
236 }
Masakazu Mokuno100e1d82008-02-07 19:57:54 +0900237 descr->buf_size = cpu_to_be32(bufsize);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900238 descr->dmac_cmd_status = 0;
239 descr->result_size = 0;
240 descr->valid_size = 0;
241 descr->data_error = 0;
242
243 offset = ((unsigned long)descr->skb->data) &
244 (GELIC_NET_RXBUF_ALIGN - 1);
245 if (offset)
246 skb_reserve(descr->skb, GELIC_NET_RXBUF_ALIGN - offset);
247 /* io-mmu-map the skb */
Masakazu Mokuno100e1d82008-02-07 19:57:54 +0900248 descr->buf_addr = cpu_to_be32(dma_map_single(ctodev(card),
249 descr->skb->data,
250 GELIC_NET_MAX_MTU,
251 DMA_FROM_DEVICE));
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900252 if (!descr->buf_addr) {
253 dev_kfree_skb_any(descr->skb);
254 descr->skb = NULL;
255 dev_info(ctodev(card),
256 "%s:Could not iommu-map rx buffer\n", __func__);
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900257 gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900258 return -ENOMEM;
259 } else {
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900260 gelic_descr_set_status(descr, GELIC_DESCR_DMA_CARDOWNED);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900261 return 0;
262 }
263}
264
265/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900266 * gelic_card_release_rx_chain - free all skb of rx descr
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900267 * @card: card structure
268 *
269 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900270static void gelic_card_release_rx_chain(struct gelic_card *card)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900271{
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900272 struct gelic_descr *descr = card->rx_chain.head;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900273
274 do {
275 if (descr->skb) {
276 dma_unmap_single(ctodev(card),
Masakazu Mokuno100e1d82008-02-07 19:57:54 +0900277 be32_to_cpu(descr->buf_addr),
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900278 descr->skb->len,
279 DMA_FROM_DEVICE);
280 descr->buf_addr = 0;
281 dev_kfree_skb_any(descr->skb);
282 descr->skb = NULL;
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900283 gelic_descr_set_status(descr,
284 GELIC_DESCR_DMA_NOT_IN_USE);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900285 }
286 descr = descr->next;
287 } while (descr != card->rx_chain.head);
288}
289
290/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900291 * gelic_card_fill_rx_chain - fills descriptors/skbs in the rx chains
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900292 * @card: card structure
293 *
294 * fills all descriptors in the rx chain: allocates skbs
295 * and iommu-maps them.
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900296 * returns 0 on success, < 0 on failure
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900297 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900298static int gelic_card_fill_rx_chain(struct gelic_card *card)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900299{
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900300 struct gelic_descr *descr = card->rx_chain.head;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900301 int ret;
302
303 do {
304 if (!descr->skb) {
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900305 ret = gelic_descr_prepare_rx(card, descr);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900306 if (ret)
307 goto rewind;
308 }
309 descr = descr->next;
310 } while (descr != card->rx_chain.head);
311
312 return 0;
313rewind:
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900314 gelic_card_release_rx_chain(card);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900315 return ret;
316}
317
318/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900319 * gelic_card_alloc_rx_skbs - allocates rx skbs in rx descriptor chains
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900320 * @card: card structure
321 *
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900322 * returns 0 on success, < 0 on failure
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900323 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900324static int gelic_card_alloc_rx_skbs(struct gelic_card *card)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900325{
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900326 struct gelic_descr_chain *chain;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900327 int ret;
328 chain = &card->rx_chain;
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900329 ret = gelic_card_fill_rx_chain(card);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900330 chain->head = card->rx_top->prev; /* point to the last */
331 return ret;
332}
333
334/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900335 * gelic_descr_release_tx - processes a used tx descriptor
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900336 * @card: card structure
337 * @descr: descriptor to release
338 *
339 * releases a used tx descriptor (unmapping, freeing of skb)
340 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900341static void gelic_descr_release_tx(struct gelic_card *card,
342 struct gelic_descr *descr)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900343{
Masakazu Mokuno173261e2007-08-31 22:22:32 +0900344 struct sk_buff *skb = descr->skb;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900345
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900346#ifdef DEBUG
Masakazu Mokuno100e1d82008-02-07 19:57:54 +0900347 BUG_ON(!(be32_to_cpu(descr->data_status) &
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900348 (1 << GELIC_DESCR_TX_DMA_FRAME_TAIL)));
349#endif
Masakazu Mokuno100e1d82008-02-07 19:57:54 +0900350 dma_unmap_single(ctodev(card),
351 be32_to_cpu(descr->buf_addr), skb->len, DMA_TO_DEVICE);
Masakazu Mokuno173261e2007-08-31 22:22:32 +0900352 dev_kfree_skb_any(skb);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900353
354 descr->buf_addr = 0;
355 descr->buf_size = 0;
356 descr->next_descr_addr = 0;
357 descr->result_size = 0;
358 descr->valid_size = 0;
359 descr->data_status = 0;
360 descr->data_error = 0;
361 descr->skb = NULL;
362
363 /* set descr status */
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}
366
367/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900368 * gelic_card_release_tx_chain - processes sent tx descriptors
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900369 * @card: adapter structure
370 * @stop: net_stop sequence
371 *
372 * releases the tx descriptors that gelic has finished with
373 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900374static void gelic_card_release_tx_chain(struct gelic_card *card, int stop)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900375{
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900376 struct gelic_descr_chain *tx_chain;
377 enum gelic_descr_dma_status status;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900378 int release = 0;
379
380 for (tx_chain = &card->tx_chain;
381 tx_chain->head != tx_chain->tail && tx_chain->tail;
382 tx_chain->tail = tx_chain->tail->next) {
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900383 status = gelic_descr_get_status(tx_chain->tail);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900384 switch (status) {
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900385 case GELIC_DESCR_DMA_RESPONSE_ERROR:
386 case GELIC_DESCR_DMA_PROTECTION_ERROR:
387 case GELIC_DESCR_DMA_FORCE_END:
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900388 if (printk_ratelimit())
389 dev_info(ctodev(card),
390 "%s: forcing end of tx descriptor " \
391 "with status %x\n",
392 __func__, status);
Masakazu Mokuno92548d62007-07-20 17:31:07 +0900393 card->netdev->stats.tx_dropped++;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900394 break;
395
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900396 case GELIC_DESCR_DMA_COMPLETE:
Masakazu Mokuno48544cc262007-07-20 17:24:56 +0900397 if (tx_chain->tail->skb) {
Masakazu Mokuno92548d62007-07-20 17:31:07 +0900398 card->netdev->stats.tx_packets++;
399 card->netdev->stats.tx_bytes +=
Masakazu Mokuno48544cc262007-07-20 17:24:56 +0900400 tx_chain->tail->skb->len;
401 }
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900402 break;
403
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900404 case GELIC_DESCR_DMA_CARDOWNED:
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900405 /* pending tx request */
406 default:
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900407 /* any other value (== GELIC_DESCR_DMA_NOT_IN_USE) */
Masakazu Mokuno48544cc262007-07-20 17:24:56 +0900408 if (!stop)
409 goto out;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900410 }
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900411 gelic_descr_release_tx(card, tx_chain->tail);
Masakazu Mokuno48544cc262007-07-20 17:24:56 +0900412 release ++;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900413 }
414out:
Masakazu Mokuno173261e2007-08-31 22:22:32 +0900415 if (!stop && release)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900416 netif_wake_queue(card->netdev);
417}
418
419/**
420 * gelic_net_set_multi - sets multicast addresses and promisc flags
421 * @netdev: interface device structure
422 *
423 * gelic_net_set_multi configures multicast addresses as needed for the
424 * netdev interface. It also sets up multicast, allmulti and promisc
425 * flags appropriately
426 */
427static void gelic_net_set_multi(struct net_device *netdev)
428{
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900429 struct gelic_card *card = netdev_priv(netdev);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900430 struct dev_mc_list *mc;
431 unsigned int i;
432 uint8_t *p;
433 u64 addr;
434 int status;
435
436 /* clear all multicast address */
437 status = lv1_net_remove_multicast_address(bus_id(card), dev_id(card),
438 0, 1);
439 if (status)
440 dev_err(ctodev(card),
441 "lv1_net_remove_multicast_address failed %d\n",
442 status);
443 /* set broadcast address */
444 status = lv1_net_add_multicast_address(bus_id(card), dev_id(card),
445 GELIC_NET_BROADCAST_ADDR, 0);
446 if (status)
447 dev_err(ctodev(card),
448 "lv1_net_add_multicast_address failed, %d\n",
449 status);
450
451 if (netdev->flags & IFF_ALLMULTI
452 || netdev->mc_count > GELIC_NET_MC_COUNT_MAX) { /* list max */
453 status = lv1_net_add_multicast_address(bus_id(card),
454 dev_id(card),
455 0, 1);
456 if (status)
457 dev_err(ctodev(card),
458 "lv1_net_add_multicast_address failed, %d\n",
459 status);
460 return;
461 }
462
463 /* set multicast address */
464 for (mc = netdev->mc_list; mc; mc = mc->next) {
465 addr = 0;
466 p = mc->dmi_addr;
467 for (i = 0; i < ETH_ALEN; i++) {
468 addr <<= 8;
469 addr |= *p++;
470 }
471 status = lv1_net_add_multicast_address(bus_id(card),
472 dev_id(card),
473 addr, 0);
474 if (status)
475 dev_err(ctodev(card),
476 "lv1_net_add_multicast_address failed, %d\n",
477 status);
478 }
479}
480
481/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900482 * gelic_card_enable_rxdmac - enables the receive DMA controller
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900483 * @card: card structure
484 *
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900485 * gelic_card_enable_rxdmac enables the DMA controller by setting RX_DMA_EN
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900486 * in the GDADMACCNTR register
487 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900488static inline void gelic_card_enable_rxdmac(struct gelic_card *card)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900489{
490 int status;
491
492 status = lv1_net_start_rx_dma(bus_id(card), dev_id(card),
493 card->rx_chain.tail->bus_addr, 0);
494 if (status)
495 dev_info(ctodev(card),
496 "lv1_net_start_rx_dma failed, status=%d\n", status);
497}
498
499/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900500 * gelic_card_disable_rxdmac - disables the receive DMA controller
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900501 * @card: card structure
502 *
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900503 * gelic_card_disable_rxdmac terminates processing on the DMA controller by
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900504 * turing off DMA and issueing a force end
505 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900506static inline void gelic_card_disable_rxdmac(struct gelic_card *card)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900507{
508 int status;
509
510 /* this hvc blocks until the DMA in progress really stopped */
511 status = lv1_net_stop_rx_dma(bus_id(card), dev_id(card), 0);
512 if (status)
513 dev_err(ctodev(card),
514 "lv1_net_stop_rx_dma faild, %d\n", status);
515}
516
517/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900518 * gelic_card_disable_txdmac - disables the transmit DMA controller
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900519 * @card: card structure
520 *
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900521 * gelic_card_disable_txdmac terminates processing on the DMA controller by
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900522 * turing off DMA and issueing a force end
523 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900524static inline void gelic_card_disable_txdmac(struct gelic_card *card)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900525{
526 int status;
527
528 /* this hvc blocks until the DMA in progress really stopped */
529 status = lv1_net_stop_tx_dma(bus_id(card), dev_id(card), 0);
530 if (status)
531 dev_err(ctodev(card),
532 "lv1_net_stop_tx_dma faild, status=%d\n", status);
533}
534
535/**
536 * gelic_net_stop - called upon ifconfig down
537 * @netdev: interface device structure
538 *
539 * always returns 0
540 */
541static int gelic_net_stop(struct net_device *netdev)
542{
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900543 struct gelic_card *card = netdev_priv(netdev);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900544
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700545 napi_disable(&card->napi);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900546 netif_stop_queue(netdev);
547
548 /* turn off DMA, force end */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900549 gelic_card_disable_rxdmac(card);
550 gelic_card_disable_txdmac(card);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900551
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900552 gelic_card_set_irq_mask(card, 0);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900553
554 /* disconnect event port */
555 free_irq(card->netdev->irq, card->netdev);
556 ps3_sb_event_receive_port_destroy(card->dev, card->netdev->irq);
557 card->netdev->irq = NO_IRQ;
558
559 netif_carrier_off(netdev);
560
561 /* release chains */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900562 gelic_card_release_tx_chain(card, 1);
563 gelic_card_release_rx_chain(card);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900564
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900565 gelic_card_free_chain(card, card->tx_top);
566 gelic_card_free_chain(card, card->rx_top);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900567
568 return 0;
569}
570
571/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900572 * gelic_card_get_next_tx_descr - returns the next available tx descriptor
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900573 * @card: device structure to get descriptor from
574 *
575 * returns the address of the next descriptor, or NULL if not available.
576 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900577static struct gelic_descr *
578gelic_card_get_next_tx_descr(struct gelic_card *card)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900579{
580 if (!card->tx_chain.head)
581 return NULL;
Masakazu Mokuno173261e2007-08-31 22:22:32 +0900582 /* see if the next descriptor is free */
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900583 if (card->tx_chain.tail != card->tx_chain.head->next &&
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900584 gelic_descr_get_status(card->tx_chain.head) ==
585 GELIC_DESCR_DMA_NOT_IN_USE)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900586 return card->tx_chain.head;
587 else
588 return NULL;
589
590}
591
592/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900593 * gelic_descr_set_tx_cmdstat - sets the tx descriptor command field
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900594 * @descr: descriptor structure to fill out
595 * @skb: packet to consider
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900596 *
597 * fills out the command and status field of the descriptor structure,
598 * depending on hardware checksum settings. This function assumes a wmb()
599 * has executed before.
600 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900601static void gelic_descr_set_tx_cmdstat(struct gelic_descr *descr,
602 struct sk_buff *skb)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900603{
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900604 if (skb->ip_summed != CHECKSUM_PARTIAL)
Masakazu Mokuno100e1d82008-02-07 19:57:54 +0900605 descr->dmac_cmd_status =
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900606 cpu_to_be32(GELIC_DESCR_DMA_CMD_NO_CHKSUM |
607 GELIC_DESCR_TX_DMA_FRAME_TAIL);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900608 else {
609 /* is packet ip?
610 * if yes: tcp? udp? */
611 if (skb->protocol == htons(ETH_P_IP)) {
612 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
613 descr->dmac_cmd_status =
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900614 cpu_to_be32(GELIC_DESCR_DMA_CMD_TCP_CHKSUM |
615 GELIC_DESCR_TX_DMA_FRAME_TAIL);
Masakazu Mokuno173261e2007-08-31 22:22:32 +0900616
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900617 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
618 descr->dmac_cmd_status =
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900619 cpu_to_be32(GELIC_DESCR_DMA_CMD_UDP_CHKSUM |
620 GELIC_DESCR_TX_DMA_FRAME_TAIL);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900621 else /*
622 * the stack should checksum non-tcp and non-udp
623 * packets on his own: NETIF_F_IP_CSUM
624 */
625 descr->dmac_cmd_status =
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900626 cpu_to_be32(GELIC_DESCR_DMA_CMD_NO_CHKSUM |
627 GELIC_DESCR_TX_DMA_FRAME_TAIL);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900628 }
629 }
630}
631
Masakazu Mokuno173261e2007-08-31 22:22:32 +0900632static inline struct sk_buff *gelic_put_vlan_tag(struct sk_buff *skb,
633 unsigned short tag)
634{
635 struct vlan_ethhdr *veth;
636 static unsigned int c;
637
638 if (skb_headroom(skb) < VLAN_HLEN) {
639 struct sk_buff *sk_tmp = skb;
640 pr_debug("%s: hd=%d c=%ud\n", __func__, skb_headroom(skb), c);
641 skb = skb_realloc_headroom(sk_tmp, VLAN_HLEN);
642 if (!skb)
643 return NULL;
644 dev_kfree_skb_any(sk_tmp);
645 }
646 veth = (struct vlan_ethhdr *)skb_push(skb, VLAN_HLEN);
647
648 /* Move the mac addresses to the top of buffer */
649 memmove(skb->data, skb->data + VLAN_HLEN, 2 * ETH_ALEN);
650
651 veth->h_vlan_proto = __constant_htons(ETH_P_8021Q);
652 veth->h_vlan_TCI = htons(tag);
653
654 return skb;
655}
656
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900657/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900658 * gelic_descr_prepare_tx - get dma address of skb_data
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900659 * @card: card structure
660 * @descr: descriptor structure
661 * @skb: packet to use
662 *
663 * returns 0 on success, <0 on failure.
664 *
665 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900666static int gelic_descr_prepare_tx(struct gelic_card *card,
667 struct gelic_descr *descr,
668 struct sk_buff *skb)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900669{
Masakazu Mokuno173261e2007-08-31 22:22:32 +0900670 dma_addr_t buf;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900671
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900672 if (card->vlan_index != -1) {
Masakazu Mokuno173261e2007-08-31 22:22:32 +0900673 struct sk_buff *skb_tmp;
674 skb_tmp = gelic_put_vlan_tag(skb,
675 card->vlan_id[card->vlan_index]);
676 if (!skb_tmp)
677 return -ENOMEM;
678 skb = skb_tmp;
Masakazu Mokuno48544cc262007-07-20 17:24:56 +0900679 }
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900680
Masakazu Mokuno173261e2007-08-31 22:22:32 +0900681 buf = dma_map_single(ctodev(card), skb->data, skb->len, DMA_TO_DEVICE);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900682
Masakazu Mokuno173261e2007-08-31 22:22:32 +0900683 if (!buf) {
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900684 dev_err(ctodev(card),
685 "dma map 2 failed (%p, %i). Dropping packet\n",
Masakazu Mokuno173261e2007-08-31 22:22:32 +0900686 skb->data, skb->len);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900687 return -ENOMEM;
688 }
689
Masakazu Mokuno100e1d82008-02-07 19:57:54 +0900690 descr->buf_addr = cpu_to_be32(buf);
691 descr->buf_size = cpu_to_be32(skb->len);
Masakazu Mokuno173261e2007-08-31 22:22:32 +0900692 descr->skb = skb;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900693 descr->data_status = 0;
Masakazu Mokuno173261e2007-08-31 22:22:32 +0900694 descr->next_descr_addr = 0; /* terminate hw descr */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900695 gelic_descr_set_tx_cmdstat(descr, skb);
Masakazu Mokuno48544cc262007-07-20 17:24:56 +0900696
697 /* bump free descriptor pointer */
Masakazu Mokuno173261e2007-08-31 22:22:32 +0900698 card->tx_chain.head = descr->next;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900699 return 0;
700}
701
702/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900703 * gelic_card_kick_txdma - enables TX DMA processing
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900704 * @card: card structure
705 * @descr: descriptor address to enable TX processing at
706 *
707 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900708static int gelic_card_kick_txdma(struct gelic_card *card,
709 struct gelic_descr *descr)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900710{
Masakazu Mokuno48544cc262007-07-20 17:24:56 +0900711 int status = 0;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900712
713 if (card->tx_dma_progress)
714 return 0;
715
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900716 if (gelic_descr_get_status(descr) == GELIC_DESCR_DMA_CARDOWNED) {
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900717 card->tx_dma_progress = 1;
Masakazu Mokunodc029ad2007-08-31 22:25:09 +0900718 status = lv1_net_start_tx_dma(bus_id(card), dev_id(card),
719 descr->bus_addr, 0);
720 if (status)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900721 dev_info(ctodev(card), "lv1_net_start_txdma failed," \
Masakazu Mokunodc029ad2007-08-31 22:25:09 +0900722 "status=%d\n", status);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900723 }
724 return status;
725}
726
727/**
728 * gelic_net_xmit - transmits a frame over the device
729 * @skb: packet to send out
730 * @netdev: interface device structure
731 *
732 * returns 0 on success, <0 on failure
733 */
734static int gelic_net_xmit(struct sk_buff *skb, struct net_device *netdev)
735{
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900736 struct gelic_card *card = netdev_priv(netdev);
737 struct gelic_descr *descr;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900738 int result;
739 unsigned long flags;
740
741 spin_lock_irqsave(&card->tx_dma_lock, flags);
742
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900743 gelic_card_release_tx_chain(card, 0);
Masakazu Mokuno48544cc262007-07-20 17:24:56 +0900744
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900745 descr = gelic_card_get_next_tx_descr(card);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900746 if (!descr) {
Masakazu Mokuno48544cc262007-07-20 17:24:56 +0900747 /*
748 * no more descriptors free
749 */
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900750 netif_stop_queue(netdev);
751 spin_unlock_irqrestore(&card->tx_dma_lock, flags);
752 return NETDEV_TX_BUSY;
753 }
Masakazu Mokuno48544cc262007-07-20 17:24:56 +0900754
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900755 result = gelic_descr_prepare_tx(card, descr, skb);
Masakazu Mokuno48544cc262007-07-20 17:24:56 +0900756 if (result) {
757 /*
758 * DMA map failed. As chanses are that failure
759 * would continue, just release skb and return
760 */
Masakazu Mokuno92548d62007-07-20 17:31:07 +0900761 card->netdev->stats.tx_dropped++;
Masakazu Mokuno48544cc262007-07-20 17:24:56 +0900762 dev_kfree_skb_any(skb);
763 spin_unlock_irqrestore(&card->tx_dma_lock, flags);
764 return NETDEV_TX_OK;
765 }
766 /*
767 * link this prepared descriptor to previous one
768 * to achieve high performance
769 */
Masakazu Mokuno100e1d82008-02-07 19:57:54 +0900770 descr->prev->next_descr_addr = cpu_to_be32(descr->bus_addr);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900771 /*
772 * as hardware descriptor is modified in the above lines,
773 * ensure that the hardware sees it
774 */
775 wmb();
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900776 if (gelic_card_kick_txdma(card, descr)) {
Masakazu Mokuno48544cc262007-07-20 17:24:56 +0900777 /*
778 * kick failed.
779 * release descriptors which were just prepared
780 */
Masakazu Mokuno92548d62007-07-20 17:31:07 +0900781 card->netdev->stats.tx_dropped++;
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900782 gelic_descr_release_tx(card, descr);
783 gelic_descr_release_tx(card, descr->next);
Masakazu Mokuno48544cc262007-07-20 17:24:56 +0900784 card->tx_chain.tail = descr->next->next;
785 dev_info(ctodev(card), "%s: kick failure\n", __func__);
786 } else {
787 /* OK, DMA started/reserved */
788 netdev->trans_start = jiffies;
789 }
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900790
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900791 spin_unlock_irqrestore(&card->tx_dma_lock, flags);
792 return NETDEV_TX_OK;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900793}
794
795/**
796 * gelic_net_pass_skb_up - takes an skb from a descriptor and passes it on
797 * @descr: descriptor to process
798 * @card: card structure
799 *
800 * iommu-unmaps the skb, fills out skb structure and passes the data to the
801 * stack. The descriptor state is not changed.
802 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900803static void gelic_net_pass_skb_up(struct gelic_descr *descr,
804 struct gelic_card *card)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900805{
806 struct sk_buff *skb;
807 struct net_device *netdev;
808 u32 data_status, data_error;
809
Masakazu Mokuno100e1d82008-02-07 19:57:54 +0900810 data_status = be32_to_cpu(descr->data_status);
811 data_error = be32_to_cpu(descr->data_error);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900812 netdev = card->netdev;
813 /* unmap skb buffer */
814 skb = descr->skb;
Masakazu Mokuno100e1d82008-02-07 19:57:54 +0900815 dma_unmap_single(ctodev(card),
816 be32_to_cpu(descr->buf_addr), GELIC_NET_MAX_MTU,
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900817 DMA_FROM_DEVICE);
818
Masakazu Mokuno100e1d82008-02-07 19:57:54 +0900819 skb_put(skb, descr->valid_size ?
820 be32_to_cpu(descr->valid_size) :
821 be32_to_cpu(descr->result_size));
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900822 if (!descr->valid_size)
823 dev_info(ctodev(card), "buffer full %x %x %x\n",
Masakazu Mokuno100e1d82008-02-07 19:57:54 +0900824 be32_to_cpu(descr->result_size),
825 be32_to_cpu(descr->buf_size),
826 be32_to_cpu(descr->dmac_cmd_status));
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900827
828 descr->skb = NULL;
829 /*
830 * the card put 2 bytes vlan tag in front
831 * of the ethernet frame
832 */
833 skb_pull(skb, 2);
834 skb->protocol = eth_type_trans(skb, netdev);
835
836 /* checksum offload */
837 if (card->rx_csum) {
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900838 if ((data_status & GELIC_DESCR_DATA_STATUS_CHK_MASK) &&
839 (!(data_error & GELIC_DESCR_DATA_ERROR_CHK_MASK)))
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900840 skb->ip_summed = CHECKSUM_UNNECESSARY;
841 else
842 skb->ip_summed = CHECKSUM_NONE;
843 } else
844 skb->ip_summed = CHECKSUM_NONE;
845
846 /* update netdevice statistics */
Masakazu Mokuno92548d62007-07-20 17:31:07 +0900847 card->netdev->stats.rx_packets++;
848 card->netdev->stats.rx_bytes += skb->len;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900849
850 /* pass skb up to stack */
851 netif_receive_skb(skb);
852}
853
854/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900855 * gelic_card_decode_one_descr - processes an rx descriptor
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900856 * @card: card structure
857 *
858 * returns 1 if a packet has been sent to the stack, otherwise 0
859 *
860 * processes an rx descriptor by iommu-unmapping the data buffer and passing
861 * the packet up to the stack
862 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900863static int gelic_card_decode_one_descr(struct gelic_card *card)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900864{
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900865 enum gelic_descr_dma_status status;
866 struct gelic_descr_chain *chain = &card->rx_chain;
867 struct gelic_descr *descr = chain->tail;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900868 int dmac_chain_ended;
869
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900870 status = gelic_descr_get_status(descr);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900871 /* is this descriptor terminated with next_descr == NULL? */
872 dmac_chain_ended =
Masakazu Mokuno100e1d82008-02-07 19:57:54 +0900873 be32_to_cpu(descr->dmac_cmd_status) &
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900874 GELIC_DESCR_RX_DMA_CHAIN_END;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900875
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900876 if (status == GELIC_DESCR_DMA_CARDOWNED)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900877 return 0;
878
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900879 if (status == GELIC_DESCR_DMA_NOT_IN_USE) {
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900880 dev_dbg(ctodev(card), "dormant descr? %p\n", descr);
881 return 0;
882 }
883
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900884 if ((status == GELIC_DESCR_DMA_RESPONSE_ERROR) ||
885 (status == GELIC_DESCR_DMA_PROTECTION_ERROR) ||
886 (status == GELIC_DESCR_DMA_FORCE_END)) {
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900887 dev_info(ctodev(card), "dropping RX descriptor with state %x\n",
888 status);
Masakazu Mokuno92548d62007-07-20 17:31:07 +0900889 card->netdev->stats.rx_dropped++;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900890 goto refill;
891 }
892
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900893 if (status == GELIC_DESCR_DMA_BUFFER_FULL) {
Masakazu Mokunofe6d3a42007-07-20 17:39:25 +0900894 /*
895 * Buffer full would occur if and only if
896 * the frame length was longer than the size of this
897 * descriptor's buffer. If the frame length was equal
898 * to or shorter than buffer'size, FRAME_END condition
899 * would occur.
900 * Anyway this frame was longer than the MTU,
901 * just drop it.
902 */
903 dev_info(ctodev(card), "overlength frame\n");
904 goto refill;
905 }
906 /*
907 * descriptoers any other than FRAME_END here should
908 * be treated as error.
909 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900910 if (status != GELIC_DESCR_DMA_FRAME_END) {
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900911 dev_dbg(ctodev(card), "RX descriptor with state %x\n",
912 status);
913 goto refill;
914 }
915
916 /* ok, we've got a packet in descr */
Masakazu Mokunofe6d3a42007-07-20 17:39:25 +0900917 gelic_net_pass_skb_up(descr, card);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900918refill:
Masakazu Mokunofe6d3a42007-07-20 17:39:25 +0900919 /*
920 * So that always DMAC can see the end
921 * of the descriptor chain to avoid
922 * from unwanted DMAC overrun.
923 */
924 descr->next_descr_addr = 0;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900925
926 /* change the descriptor state: */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900927 gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900928
Masakazu Mokunofe6d3a42007-07-20 17:39:25 +0900929 /*
930 * this call can fail, but for now, just leave this
931 * decriptor without skb
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900932 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900933 gelic_descr_prepare_rx(card, descr);
Masakazu Mokunofe6d3a42007-07-20 17:39:25 +0900934
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900935 chain->head = descr;
936 chain->tail = descr->next;
Masakazu Mokunofe6d3a42007-07-20 17:39:25 +0900937
938 /*
939 * Set this descriptor the end of the chain.
940 */
Masakazu Mokuno100e1d82008-02-07 19:57:54 +0900941 descr->prev->next_descr_addr = cpu_to_be32(descr->bus_addr);
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900942
Masakazu Mokunofe6d3a42007-07-20 17:39:25 +0900943 /*
944 * If dmac chain was met, DMAC stopped.
945 * thus re-enable it
946 */
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900947 if (dmac_chain_ended) {
Masakazu Mokuno583aae12007-07-20 17:35:54 +0900948 card->rx_dma_restart_required = 1;
949 dev_dbg(ctodev(card), "reenable rx dma scheduled\n");
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900950 }
951
952 return 1;
953}
954
955/**
956 * gelic_net_poll - NAPI poll function called by the stack to return packets
957 * @netdev: interface device structure
958 * @budget: number of packets we can pass to the stack at most
959 *
960 * returns 0 if no more packets available to the driver/stack. Returns 1,
961 * if the quota is exceeded, but the driver has still packets.
962 *
963 */
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700964static int gelic_net_poll(struct napi_struct *napi, int budget)
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900965{
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900966 struct gelic_card *card = container_of(napi, struct gelic_card, napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700967 struct net_device *netdev = card->netdev;
968 int packets_done = 0;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900969
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700970 while (packets_done < budget) {
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900971 if (!gelic_card_decode_one_descr(card))
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900972 break;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700973
974 packets_done++;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900975 }
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700976
977 if (packets_done < budget) {
978 netif_rx_complete(netdev, napi);
Masakazu Mokuno59e97322008-02-07 19:58:08 +0900979 gelic_card_rx_irq_on(card);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700980 }
981 return packets_done;
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900982}
Masakazu Mokuno02c18892007-07-05 20:11:16 +0900983/**
984 * gelic_net_change_mtu - changes the MTU of an interface
985 * @netdev: interface device structure
986 * @new_mtu: new MTU value
987 *
988 * returns 0 on success, <0 on failure
989 */
990static int gelic_net_change_mtu(struct net_device *netdev, int new_mtu)
991{
992 /* no need to re-alloc skbs or so -- the max mtu is about 2.3k
993 * and mtu is outbound only anyway */
994 if ((new_mtu < GELIC_NET_MIN_MTU) ||
995 (new_mtu > GELIC_NET_MAX_MTU)) {
996 return -EINVAL;
997 }
998 netdev->mtu = new_mtu;
999 return 0;
1000}
1001
1002/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001003 * gelic_card_interrupt - event handler for gelic_net
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001004 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001005static irqreturn_t gelic_card_interrupt(int irq, void *ptr)
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001006{
1007 unsigned long flags;
1008 struct net_device *netdev = ptr;
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001009 struct gelic_card *card = netdev_priv(netdev);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001010 u64 status;
1011
1012 status = card->irq_status;
1013
1014 if (!status)
1015 return IRQ_NONE;
1016
Masakazu Mokuno583aae12007-07-20 17:35:54 +09001017 if (card->rx_dma_restart_required) {
1018 card->rx_dma_restart_required = 0;
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001019 gelic_card_enable_rxdmac(card);
Masakazu Mokuno583aae12007-07-20 17:35:54 +09001020 }
1021
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001022 if (status & GELIC_CARD_RXINT) {
1023 gelic_card_rx_irq_off(card);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001024 netif_rx_schedule(netdev, &card->napi);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001025 }
1026
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001027 if (status & GELIC_CARD_TXINT) {
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001028 spin_lock_irqsave(&card->tx_dma_lock, flags);
1029 card->tx_dma_progress = 0;
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001030 gelic_card_release_tx_chain(card, 0);
Masakazu Mokuno48544cc262007-07-20 17:24:56 +09001031 /* kick outstanding tx descriptor if any */
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001032 gelic_card_kick_txdma(card, card->tx_chain.tail);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001033 spin_unlock_irqrestore(&card->tx_dma_lock, flags);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001034 }
1035 return IRQ_HANDLED;
1036}
1037
1038#ifdef CONFIG_NET_POLL_CONTROLLER
1039/**
1040 * gelic_net_poll_controller - artificial interrupt for netconsole etc.
1041 * @netdev: interface device structure
1042 *
1043 * see Documentation/networking/netconsole.txt
1044 */
1045static void gelic_net_poll_controller(struct net_device *netdev)
1046{
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001047 struct gelic_card *card = netdev_priv(netdev);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001048
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001049 gelic_card_set_irq_mask(card, 0);
1050 gelic_card_interrupt(netdev->irq, netdev);
1051 gelic_card_set_irq_mask(card, card->ghiintmask);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001052}
1053#endif /* CONFIG_NET_POLL_CONTROLLER */
1054
1055/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001056 * gelic_card_open - open device and map dma region
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001057 * @card: card structure
1058 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001059static int gelic_card_open(struct gelic_card *card)
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001060{
1061 int result;
1062
1063 result = ps3_sb_event_receive_port_setup(card->dev, PS3_BINDING_CPU_ANY,
1064 &card->netdev->irq);
1065
1066 if (result) {
1067 dev_info(ctodev(card),
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001068 "%s:%d: recieve_port_setup failed (%d)\n",
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001069 __func__, __LINE__, result);
1070 result = -EPERM;
1071 goto fail_alloc_irq;
1072 }
1073
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001074 result = request_irq(card->netdev->irq, gelic_card_interrupt,
Masakazu Mokunof0861f82007-07-20 17:33:02 +09001075 IRQF_DISABLED, card->netdev->name, card->netdev);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001076
1077 if (result) {
1078 dev_info(ctodev(card), "%s:%d: request_irq failed (%d)\n",
1079 __func__, __LINE__, result);
1080 goto fail_request_irq;
1081 }
1082
1083 return 0;
1084
1085fail_request_irq:
1086 ps3_sb_event_receive_port_destroy(card->dev, card->netdev->irq);
1087 card->netdev->irq = NO_IRQ;
1088fail_alloc_irq:
1089 return result;
1090}
1091
1092
1093/**
1094 * gelic_net_open - called upon ifonfig up
1095 * @netdev: interface device structure
1096 *
1097 * returns 0 on success, <0 on failure
1098 *
1099 * gelic_net_open allocates all the descriptors and memory needed for
1100 * operation, sets up multicast list and enables interrupts
1101 */
1102static int gelic_net_open(struct net_device *netdev)
1103{
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001104 struct gelic_card *card = netdev_priv(netdev);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001105
1106 dev_dbg(ctodev(card), " -> %s:%d\n", __func__, __LINE__);
1107
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001108 gelic_card_open(card);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001109
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001110 if (gelic_card_init_chain(card, &card->tx_chain,
1111 card->descr, GELIC_NET_TX_DESCRIPTORS))
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001112 goto alloc_tx_failed;
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001113 if (gelic_card_init_chain(card, &card->rx_chain,
1114 card->descr + GELIC_NET_TX_DESCRIPTORS,
1115 GELIC_NET_RX_DESCRIPTORS))
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001116 goto alloc_rx_failed;
1117
1118 /* head of chain */
1119 card->tx_top = card->tx_chain.head;
1120 card->rx_top = card->rx_chain.head;
1121 dev_dbg(ctodev(card), "descr rx %p, tx %p, size %#lx, num %#x\n",
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001122 card->rx_top, card->tx_top, sizeof(struct gelic_descr),
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001123 GELIC_NET_RX_DESCRIPTORS);
1124 /* allocate rx skbs */
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001125 if (gelic_card_alloc_rx_skbs(card))
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001126 goto alloc_skbs_failed;
1127
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001128 napi_enable(&card->napi);
1129
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001130 card->tx_dma_progress = 0;
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001131 card->ghiintmask = GELIC_CARD_RXINT | GELIC_CARD_TXINT;
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001132
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001133 gelic_card_set_irq_mask(card, card->ghiintmask);
1134 gelic_card_enable_rxdmac(card);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001135
1136 netif_start_queue(netdev);
1137 netif_carrier_on(netdev);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001138
1139 return 0;
1140
1141alloc_skbs_failed:
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001142 gelic_card_free_chain(card, card->rx_top);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001143alloc_rx_failed:
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001144 gelic_card_free_chain(card, card->tx_top);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001145alloc_tx_failed:
1146 return -ENOMEM;
1147}
1148
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001149static void gelic_net_get_drvinfo(struct net_device *netdev,
1150 struct ethtool_drvinfo *info)
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001151{
1152 strncpy(info->driver, DRV_NAME, sizeof(info->driver) - 1);
1153 strncpy(info->version, DRV_VERSION, sizeof(info->version) - 1);
1154}
1155
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001156static int gelic_ether_get_settings(struct net_device *netdev,
1157 struct ethtool_cmd *cmd)
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001158{
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001159 struct gelic_card *card = netdev_priv(netdev);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001160 int status;
1161 u64 v1, v2;
1162 int speed, duplex;
1163
1164 speed = duplex = -1;
1165 status = lv1_net_control(bus_id(card), dev_id(card),
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001166 GELIC_LV1_GET_ETH_PORT_STATUS,
1167 GELIC_LV1_VLAN_TX_ETHERNET, 0, 0,
1168 &v1, &v2);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001169 if (status) {
1170 /* link down */
1171 } else {
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001172 if (v1 & GELIC_LV1_ETHER_FULL_DUPLEX) {
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001173 duplex = DUPLEX_FULL;
1174 } else {
1175 duplex = DUPLEX_HALF;
1176 }
1177
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001178 if (v1 & GELIC_LV1_ETHER_SPEED_10) {
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001179 speed = SPEED_10;
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001180 } else if (v1 & GELIC_LV1_ETHER_SPEED_100) {
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001181 speed = SPEED_100;
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001182 } else if (v1 & GELIC_LV1_ETHER_SPEED_1000) {
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001183 speed = SPEED_1000;
1184 }
1185 }
1186 cmd->supported = SUPPORTED_TP | SUPPORTED_Autoneg |
1187 SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full |
1188 SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full |
1189 SUPPORTED_1000baseT_Half | SUPPORTED_1000baseT_Full;
1190 cmd->advertising = cmd->supported;
1191 cmd->speed = speed;
1192 cmd->duplex = duplex;
1193 cmd->autoneg = AUTONEG_ENABLE; /* always enabled */
1194 cmd->port = PORT_TP;
1195
1196 return 0;
1197}
1198
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001199static int gelic_net_nway_reset(struct net_device *netdev)
1200{
1201 if (netif_running(netdev)) {
1202 gelic_net_stop(netdev);
1203 gelic_net_open(netdev);
1204 }
1205 return 0;
1206}
1207
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001208static u32 gelic_net_get_rx_csum(struct net_device *netdev)
1209{
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001210 struct gelic_card *card = netdev_priv(netdev);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001211
1212 return card->rx_csum;
1213}
1214
1215static int gelic_net_set_rx_csum(struct net_device *netdev, u32 data)
1216{
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001217 struct gelic_card *card = netdev_priv(netdev);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001218
1219 card->rx_csum = data;
1220 return 0;
1221}
1222
1223static struct ethtool_ops gelic_net_ethtool_ops = {
1224 .get_drvinfo = gelic_net_get_drvinfo,
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001225 .get_settings = gelic_ether_get_settings,
Masakazu Mokuno7bc56b92008-02-07 19:58:20 +09001226 .get_link = ethtool_op_get_link,
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001227 .nway_reset = gelic_net_nway_reset,
Masakazu Mokuno7bc56b92008-02-07 19:58:20 +09001228 .get_tx_csum = ethtool_op_get_tx_csum,
1229 .set_tx_csum = ethtool_op_set_tx_csum,
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001230 .get_rx_csum = gelic_net_get_rx_csum,
1231 .set_rx_csum = gelic_net_set_rx_csum,
1232};
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001233
1234/**
1235 * gelic_net_tx_timeout_task - task scheduled by the watchdog timeout
1236 * function (to be called not under interrupt status)
1237 * @work: work is context of tx timout task
1238 *
1239 * called as task when tx hangs, resets interface (if interface is up)
1240 */
1241static void gelic_net_tx_timeout_task(struct work_struct *work)
1242{
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001243 struct gelic_card *card =
1244 container_of(work, struct gelic_card, tx_timeout_task);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001245 struct net_device *netdev = card->netdev;
1246
1247 dev_info(ctodev(card), "%s:Timed out. Restarting... \n", __func__);
1248
1249 if (!(netdev->flags & IFF_UP))
1250 goto out;
1251
1252 netif_device_detach(netdev);
1253 gelic_net_stop(netdev);
1254
1255 gelic_net_open(netdev);
1256 netif_device_attach(netdev);
1257
1258out:
1259 atomic_dec(&card->tx_timeout_task_counter);
1260}
1261
1262/**
1263 * gelic_net_tx_timeout - called when the tx timeout watchdog kicks in.
1264 * @netdev: interface device structure
1265 *
1266 * called, if tx hangs. Schedules a task that resets the interface
1267 */
1268static void gelic_net_tx_timeout(struct net_device *netdev)
1269{
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001270 struct gelic_card *card;
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001271
1272 card = netdev_priv(netdev);
1273 atomic_inc(&card->tx_timeout_task_counter);
1274 if (netdev->flags & IFF_UP)
1275 schedule_work(&card->tx_timeout_task);
1276 else
1277 atomic_dec(&card->tx_timeout_task_counter);
1278}
1279
1280/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001281 * gelic_ether_setup_netdev_ops - initialization of net_device operations
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001282 * @netdev: net_device structure
1283 *
1284 * fills out function pointers in the net_device structure
1285 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001286static void gelic_ether_setup_netdev_ops(struct net_device *netdev)
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001287{
1288 netdev->open = &gelic_net_open;
1289 netdev->stop = &gelic_net_stop;
1290 netdev->hard_start_xmit = &gelic_net_xmit;
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001291 netdev->set_multicast_list = &gelic_net_set_multi;
1292 netdev->change_mtu = &gelic_net_change_mtu;
1293 /* tx watchdog */
1294 netdev->tx_timeout = &gelic_net_tx_timeout;
1295 netdev->watchdog_timeo = GELIC_NET_WATCHDOG_TIMEOUT;
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001296 netdev->ethtool_ops = &gelic_net_ethtool_ops;
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001297}
1298
1299/**
1300 * gelic_net_setup_netdev - initialization of net_device
1301 * @card: card structure
1302 *
1303 * Returns 0 on success or <0 on failure
1304 *
1305 * gelic_net_setup_netdev initializes the net_device structure
1306 **/
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001307static int gelic_net_setup_netdev(struct gelic_card *card)
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001308{
1309 struct net_device *netdev = card->netdev;
1310 struct sockaddr addr;
1311 unsigned int i;
1312 int status;
1313 u64 v1, v2;
Joe Perches0795af52007-10-03 17:59:30 -07001314 DECLARE_MAC_BUF(mac);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001315
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001316 SET_NETDEV_DEV(netdev, &card->dev->core);
1317 spin_lock_init(&card->tx_dma_lock);
1318
1319 card->rx_csum = GELIC_NET_RX_CSUM_DEFAULT;
1320
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001321 gelic_ether_setup_netdev_ops(netdev);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001322
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001323 netif_napi_add(netdev, &card->napi,
1324 gelic_net_poll, GELIC_NET_NAPI_WEIGHT);
1325
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001326 netdev->features = NETIF_F_IP_CSUM;
1327
1328 status = lv1_net_control(bus_id(card), dev_id(card),
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001329 GELIC_LV1_GET_MAC_ADDRESS,
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001330 0, 0, 0, &v1, &v2);
1331 if (status || !is_valid_ether_addr((u8 *)&v1)) {
1332 dev_info(ctodev(card),
1333 "%s:lv1_net_control GET_MAC_ADDR failed %d\n",
1334 __func__, status);
1335 return -EINVAL;
1336 }
1337 v1 <<= 16;
1338 memcpy(addr.sa_data, &v1, ETH_ALEN);
1339 memcpy(netdev->dev_addr, addr.sa_data, ETH_ALEN);
Joe Perches0795af52007-10-03 17:59:30 -07001340 dev_info(ctodev(card), "MAC addr %s\n",
1341 print_mac(mac, netdev->dev_addr));
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001342
1343 card->vlan_index = -1; /* no vlan */
1344 for (i = 0; i < GELIC_NET_VLAN_MAX; i++) {
1345 status = lv1_net_control(bus_id(card), dev_id(card),
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001346 GELIC_LV1_GET_VLAN_ID,
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001347 i + 1, /* index; one based */
1348 0, 0, &v1, &v2);
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001349 if (status == LV1_NO_ENTRY) {
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001350 dev_dbg(ctodev(card),
1351 "GELIC_VLAN_ID no entry:%d, VLAN disabled\n",
1352 status);
1353 card->vlan_id[i] = 0;
1354 } else if (status) {
1355 dev_dbg(ctodev(card),
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001356 "%s:get vlan id faild, status=%d\n",
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001357 __func__, status);
1358 card->vlan_id[i] = 0;
1359 } else {
1360 card->vlan_id[i] = (u32)v1;
1361 dev_dbg(ctodev(card), "vlan_id:%d, %lx\n", i, v1);
1362 }
1363 }
Masakazu Mokuno173261e2007-08-31 22:22:32 +09001364
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001365 if (card->vlan_id[GELIC_LV1_VLAN_TX_ETHERNET - 1]) {
1366 card->vlan_index = GELIC_LV1_VLAN_TX_ETHERNET - 1;
Masakazu Mokuno173261e2007-08-31 22:22:32 +09001367 netdev->hard_header_len += VLAN_HLEN;
1368 }
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001369
1370 status = register_netdev(netdev);
1371 if (status) {
1372 dev_err(ctodev(card), "%s:Couldn't register net_device: %d\n",
1373 __func__, status);
1374 return status;
1375 }
1376
1377 return 0;
1378}
1379
1380/**
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001381 * gelic_alloc_card_net - allocates net_device and card structure
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001382 *
1383 * returns the card structure or NULL in case of errors
1384 *
1385 * the card and net_device structures are linked to each other
1386 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001387static struct gelic_card *gelic_alloc_card_net(void)
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001388{
1389 struct net_device *netdev;
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001390 struct gelic_card *card;
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001391 size_t alloc_size;
1392
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001393 alloc_size = sizeof(*card) +
1394 sizeof(struct gelic_descr) * GELIC_NET_RX_DESCRIPTORS +
1395 sizeof(struct gelic_descr) * GELIC_NET_TX_DESCRIPTORS;
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001396 /*
1397 * we assume private data is allocated 32 bytes (or more) aligned
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001398 * so that gelic_descr should be 32 bytes aligned.
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001399 * Current alloc_etherdev() does do it because NETDEV_ALIGN
1400 * is 32.
1401 * check this assumption here.
1402 */
1403 BUILD_BUG_ON(NETDEV_ALIGN < 32);
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001404 BUILD_BUG_ON(offsetof(struct gelic_card, irq_status) % 8);
1405 BUILD_BUG_ON(offsetof(struct gelic_card, descr) % 32);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001406
1407 netdev = alloc_etherdev(alloc_size);
1408 if (!netdev)
1409 return NULL;
1410
1411 card = netdev_priv(netdev);
1412 card->netdev = netdev;
1413 INIT_WORK(&card->tx_timeout_task, gelic_net_tx_timeout_task);
1414 init_waitqueue_head(&card->waitq);
1415 atomic_set(&card->tx_timeout_task_counter, 0);
1416
1417 return card;
1418}
1419
1420/**
1421 * ps3_gelic_driver_probe - add a device to the control of this driver
1422 */
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001423static int ps3_gelic_driver_probe(struct ps3_system_bus_device *dev)
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001424{
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001425 struct gelic_card *card = gelic_alloc_card_net();
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001426 int result;
1427
1428 if (!card) {
1429 dev_info(&dev->core, "gelic_net_alloc_card failed\n");
1430 result = -ENOMEM;
1431 goto fail_alloc_card;
1432 }
1433
1434 ps3_system_bus_set_driver_data(dev, card);
1435 card->dev = dev;
1436
1437 result = ps3_open_hv_device(dev);
1438
1439 if (result) {
1440 dev_dbg(&dev->core, "ps3_open_hv_device failed\n");
1441 goto fail_open;
1442 }
1443
1444 result = ps3_dma_region_create(dev->d_region);
1445
1446 if (result) {
1447 dev_dbg(&dev->core, "ps3_dma_region_create failed(%d)\n",
1448 result);
1449 BUG_ON("check region type");
1450 goto fail_dma_region;
1451 }
1452
1453 result = lv1_net_set_interrupt_status_indicator(bus_id(card),
1454 dev_id(card),
1455 ps3_mm_phys_to_lpar(__pa(&card->irq_status)),
1456 0);
1457
1458 if (result) {
1459 dev_dbg(&dev->core,
1460 "lv1_net_set_interrupt_status_indicator failed: %s\n",
1461 ps3_result(result));
1462 result = -EIO;
1463 goto fail_status_indicator;
1464 }
1465
1466 result = gelic_net_setup_netdev(card);
1467
1468 if (result) {
1469 dev_dbg(&dev->core, "%s:%d: ps3_dma_region_create failed: "
1470 "(%d)\n", __func__, __LINE__, result);
1471 goto fail_setup_netdev;
1472 }
1473
1474 return 0;
1475
1476fail_setup_netdev:
1477 lv1_net_set_interrupt_status_indicator(bus_id(card),
Masakazu Mokunob94e1d42008-02-07 19:57:41 +09001478 dev_id(card),
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001479 0 , 0);
1480fail_status_indicator:
1481 ps3_dma_region_free(dev->d_region);
1482fail_dma_region:
1483 ps3_close_hv_device(dev);
1484fail_open:
1485 ps3_system_bus_set_driver_data(dev, NULL);
1486 free_netdev(card->netdev);
1487fail_alloc_card:
1488 return result;
1489}
1490
1491/**
1492 * ps3_gelic_driver_remove - remove a device from the control of this driver
1493 */
1494
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001495static int ps3_gelic_driver_remove(struct ps3_system_bus_device *dev)
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001496{
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001497 struct gelic_card *card = ps3_system_bus_get_driver_data(dev);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001498
1499 wait_event(card->waitq,
1500 atomic_read(&card->tx_timeout_task_counter) == 0);
1501
1502 lv1_net_set_interrupt_status_indicator(bus_id(card), dev_id(card),
1503 0 , 0);
1504
1505 unregister_netdev(card->netdev);
1506 free_netdev(card->netdev);
1507
1508 ps3_system_bus_set_driver_data(dev, NULL);
1509
1510 ps3_dma_region_free(dev->d_region);
1511
1512 ps3_close_hv_device(dev);
1513
1514 return 0;
1515}
1516
1517static struct ps3_system_bus_driver ps3_gelic_driver = {
1518 .match_id = PS3_MATCH_ID_GELIC,
1519 .probe = ps3_gelic_driver_probe,
1520 .remove = ps3_gelic_driver_remove,
1521 .shutdown = ps3_gelic_driver_remove,
1522 .core.name = "ps3_gelic_driver",
1523 .core.owner = THIS_MODULE,
1524};
1525
1526static int __init ps3_gelic_driver_init (void)
1527{
1528 return firmware_has_feature(FW_FEATURE_PS3_LV1)
1529 ? ps3_system_bus_driver_register(&ps3_gelic_driver)
1530 : -ENODEV;
1531}
1532
1533static void __exit ps3_gelic_driver_exit (void)
1534{
1535 ps3_system_bus_driver_unregister(&ps3_gelic_driver);
1536}
1537
Masakazu Mokuno59e97322008-02-07 19:58:08 +09001538module_init(ps3_gelic_driver_init);
1539module_exit(ps3_gelic_driver_exit);
Masakazu Mokuno02c18892007-07-05 20:11:16 +09001540
1541MODULE_ALIAS(PS3_MODULE_ALIAS_GELIC);
1542