PS3: gelic: Add support for dual network interface
[deliverable/linux.git] / drivers / net / ps3_gelic_net.c
CommitLineData
02c18891
MM
1/*
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"
589866f9 51#define DRV_VERSION "1.1"
02c18891
MM
52
53MODULE_AUTHOR("SCE Inc.");
54MODULE_DESCRIPTION("Gelic Network driver");
55MODULE_LICENSE("GPL");
56
589866f9
MM
57
58static inline void gelic_card_enable_rxdmac(struct gelic_card *card);
59static inline void gelic_card_disable_rxdmac(struct gelic_card *card);
60static inline void gelic_card_disable_txdmac(struct gelic_card *card);
61static inline void gelic_card_reset_chain(struct gelic_card *card,
62 struct gelic_descr_chain *chain,
63 struct gelic_descr *start_descr);
02c18891
MM
64
65/* set irq_mask */
589866f9 66int gelic_card_set_irq_mask(struct gelic_card *card, u64 mask)
02c18891
MM
67{
68 int status;
69
70 status = lv1_net_set_interrupt_mask(bus_id(card), dev_id(card),
71 mask, 0);
72 if (status)
73 dev_info(ctodev(card),
589866f9 74 "%s failed %d\n", __func__, status);
02c18891
MM
75 return status;
76}
589866f9 77
59e97327 78static inline void gelic_card_rx_irq_on(struct gelic_card *card)
02c18891 79{
589866f9
MM
80 card->irq_mask |= GELIC_CARD_RXINT;
81 gelic_card_set_irq_mask(card, card->irq_mask);
02c18891 82}
59e97327 83static inline void gelic_card_rx_irq_off(struct gelic_card *card)
02c18891 84{
589866f9
MM
85 card->irq_mask &= ~GELIC_CARD_RXINT;
86 gelic_card_set_irq_mask(card, card->irq_mask);
02c18891 87}
01fed4c2 88
589866f9
MM
89static void gelic_card_get_ether_port_status(struct gelic_card *card,
90 int inform)
01fed4c2
MM
91{
92 u64 v2;
93 struct net_device *ether_netdev;
94
95 lv1_net_control(bus_id(card), dev_id(card),
96 GELIC_LV1_GET_ETH_PORT_STATUS,
97 GELIC_LV1_VLAN_TX_ETHERNET, 0, 0,
98 &card->ether_port_status, &v2);
99
100 if (inform) {
589866f9 101 ether_netdev = card->netdev[GELIC_PORT_ETHERNET];
01fed4c2
MM
102 if (card->ether_port_status & GELIC_LV1_ETHER_LINK_UP)
103 netif_carrier_on(ether_netdev);
104 else
105 netif_carrier_off(ether_netdev);
106 }
107}
108
589866f9
MM
109void gelic_card_up(struct gelic_card *card)
110{
111 pr_debug("%s: called\n", __func__);
112 down(&card->updown_lock);
113 if (atomic_inc_return(&card->users) == 1) {
114 pr_debug("%s: real do\n", __func__);
115 /* enable irq */
116 gelic_card_set_irq_mask(card, card->irq_mask);
117 /* start rx */
118 gelic_card_enable_rxdmac(card);
119
120 napi_enable(&card->napi);
121 }
122 up(&card->updown_lock);
123 pr_debug("%s: done\n", __func__);
124}
125
126void gelic_card_down(struct gelic_card *card)
127{
128 u64 mask;
129 pr_debug("%s: called\n", __func__);
130 down(&card->updown_lock);
131 if (atomic_dec_if_positive(&card->users) == 0) {
132 pr_debug("%s: real do\n", __func__);
133 napi_disable(&card->napi);
134 /*
135 * Disable irq. Wireless interrupts will
136 * be disabled later if any
137 */
138 mask = card->irq_mask & (GELIC_CARD_WLAN_EVENT_RECEIVED |
139 GELIC_CARD_WLAN_COMMAND_COMPLETED);
140 gelic_card_set_irq_mask(card, mask);
141 /* stop rx */
142 gelic_card_disable_rxdmac(card);
143 gelic_card_reset_chain(card, &card->rx_chain,
144 card->descr + GELIC_NET_TX_DESCRIPTORS);
145 /* stop tx */
146 gelic_card_disable_txdmac(card);
147 }
148 up(&card->updown_lock);
149 pr_debug("%s: done\n", __func__);
150}
01fed4c2 151
02c18891 152/**
59e97327 153 * gelic_descr_get_status -- returns the status of a descriptor
02c18891
MM
154 * @descr: descriptor to look at
155 *
156 * returns the status as in the dmac_cmd_status field of the descriptor
157 */
59e97327
MM
158static enum gelic_descr_dma_status
159gelic_descr_get_status(struct gelic_descr *descr)
02c18891 160{
59e97327 161 return be32_to_cpu(descr->dmac_cmd_status) & GELIC_DESCR_DMA_STAT_MASK;
02c18891
MM
162}
163
164/**
59e97327 165 * gelic_descr_set_status -- sets the status of a descriptor
02c18891
MM
166 * @descr: descriptor to change
167 * @status: status to set in the descriptor
168 *
169 * changes the status to the specified value. Doesn't change other bits
170 * in the status
171 */
59e97327
MM
172static void gelic_descr_set_status(struct gelic_descr *descr,
173 enum gelic_descr_dma_status status)
02c18891 174{
59e97327 175 descr->dmac_cmd_status = cpu_to_be32(status |
589866f9
MM
176 (be32_to_cpu(descr->dmac_cmd_status) &
177 ~GELIC_DESCR_DMA_STAT_MASK));
02c18891
MM
178 /*
179 * dma_cmd_status field is used to indicate whether the descriptor
180 * is valid or not.
181 * Usually caller of this function wants to inform that to the
182 * hardware, so we assure here the hardware sees the change.
183 */
184 wmb();
185}
186
187/**
59e97327 188 * gelic_card_free_chain - free descriptor chain
02c18891
MM
189 * @card: card structure
190 * @descr_in: address of desc
191 */
59e97327
MM
192static void gelic_card_free_chain(struct gelic_card *card,
193 struct gelic_descr *descr_in)
02c18891 194{
59e97327 195 struct gelic_descr *descr;
02c18891
MM
196
197 for (descr = descr_in; descr && descr->bus_addr; descr = descr->next) {
198 dma_unmap_single(ctodev(card), descr->bus_addr,
59e97327 199 GELIC_DESCR_SIZE, DMA_BIDIRECTIONAL);
02c18891
MM
200 descr->bus_addr = 0;
201 }
202}
203
204/**
59e97327 205 * gelic_card_init_chain - links descriptor chain
02c18891
MM
206 * @card: card structure
207 * @chain: address of chain
208 * @start_descr: address of descriptor array
209 * @no: number of descriptors
210 *
211 * we manage a circular list that mirrors the hardware structure,
212 * except that the hardware uses bus addresses.
213 *
214 * returns 0 on success, <0 on failure
215 */
59e97327
MM
216static int gelic_card_init_chain(struct gelic_card *card,
217 struct gelic_descr_chain *chain,
218 struct gelic_descr *start_descr, int no)
02c18891
MM
219{
220 int i;
59e97327 221 struct gelic_descr *descr;
02c18891
MM
222
223 descr = start_descr;
224 memset(descr, 0, sizeof(*descr) * no);
225
226 /* set up the hardware pointers in each descriptor */
227 for (i = 0; i < no; i++, descr++) {
59e97327 228 gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
02c18891
MM
229 descr->bus_addr =
230 dma_map_single(ctodev(card), descr,
59e97327 231 GELIC_DESCR_SIZE,
02c18891
MM
232 DMA_BIDIRECTIONAL);
233
234 if (!descr->bus_addr)
235 goto iommu_error;
236
237 descr->next = descr + 1;
238 descr->prev = descr - 1;
239 }
240 /* make them as ring */
241 (descr - 1)->next = start_descr;
242 start_descr->prev = (descr - 1);
243
244 /* chain bus addr of hw descriptor */
245 descr = start_descr;
246 for (i = 0; i < no; i++, descr++) {
100e1d89 247 descr->next_descr_addr = cpu_to_be32(descr->next->bus_addr);
02c18891
MM
248 }
249
250 chain->head = start_descr;
251 chain->tail = start_descr;
252
253 /* do not chain last hw descriptor */
254 (descr - 1)->next_descr_addr = 0;
255
256 return 0;
257
258iommu_error:
259 for (i--, descr--; 0 <= i; i--, descr--)
260 if (descr->bus_addr)
261 dma_unmap_single(ctodev(card), descr->bus_addr,
59e97327 262 GELIC_DESCR_SIZE,
02c18891
MM
263 DMA_BIDIRECTIONAL);
264 return -ENOMEM;
265}
266
589866f9
MM
267/**
268 * gelic_card_reset_chain - reset status of a descriptor chain
269 * @card: card structure
270 * @chain: address of chain
271 * @start_descr: address of descriptor array
272 *
273 * Reset the status of dma descriptors to ready state
274 * and re-initialize the hardware chain for later use
275 */
276static void gelic_card_reset_chain(struct gelic_card *card,
277 struct gelic_descr_chain *chain,
278 struct gelic_descr *start_descr)
279{
280 struct gelic_descr *descr;
281
282 for (descr = start_descr; start_descr != descr->next; descr++) {
283 gelic_descr_set_status(descr, GELIC_DESCR_DMA_CARDOWNED);
284 descr->next_descr_addr = cpu_to_be32(descr->next->bus_addr);
285 }
286
287 chain->head = start_descr;
288 chain->tail = (descr - 1);
289
290 (descr - 1)->next_descr_addr = 0;
291}
02c18891 292/**
59e97327 293 * gelic_descr_prepare_rx - reinitializes a rx descriptor
02c18891
MM
294 * @card: card structure
295 * @descr: descriptor to re-init
296 *
297 * return 0 on succes, <0 on failure
298 *
299 * allocates a new rx skb, iommu-maps it and attaches it to the descriptor.
300 * Activate the descriptor state-wise
301 */
59e97327 302static int gelic_descr_prepare_rx(struct gelic_card *card,
589866f9 303 struct gelic_descr *descr)
02c18891
MM
304{
305 int offset;
306 unsigned int bufsize;
307
59e97327 308 if (gelic_descr_get_status(descr) != GELIC_DESCR_DMA_NOT_IN_USE)
02c18891 309 dev_info(ctodev(card), "%s: ERROR status \n", __func__);
02c18891
MM
310 /* we need to round up the buffer size to a multiple of 128 */
311 bufsize = ALIGN(GELIC_NET_MAX_MTU, GELIC_NET_RXBUF_ALIGN);
312
313 /* and we need to have it 128 byte aligned, therefore we allocate a
314 * bit more */
589866f9 315 descr->skb = dev_alloc_skb(bufsize + GELIC_NET_RXBUF_ALIGN - 1);
02c18891
MM
316 if (!descr->skb) {
317 descr->buf_addr = 0; /* tell DMAC don't touch memory */
318 dev_info(ctodev(card),
319 "%s:allocate skb failed !!\n", __func__);
320 return -ENOMEM;
321 }
100e1d89 322 descr->buf_size = cpu_to_be32(bufsize);
02c18891
MM
323 descr->dmac_cmd_status = 0;
324 descr->result_size = 0;
325 descr->valid_size = 0;
326 descr->data_error = 0;
327
328 offset = ((unsigned long)descr->skb->data) &
329 (GELIC_NET_RXBUF_ALIGN - 1);
330 if (offset)
331 skb_reserve(descr->skb, GELIC_NET_RXBUF_ALIGN - offset);
332 /* io-mmu-map the skb */
100e1d89
MM
333 descr->buf_addr = cpu_to_be32(dma_map_single(ctodev(card),
334 descr->skb->data,
335 GELIC_NET_MAX_MTU,
336 DMA_FROM_DEVICE));
02c18891
MM
337 if (!descr->buf_addr) {
338 dev_kfree_skb_any(descr->skb);
339 descr->skb = NULL;
340 dev_info(ctodev(card),
341 "%s:Could not iommu-map rx buffer\n", __func__);
59e97327 342 gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
02c18891
MM
343 return -ENOMEM;
344 } else {
59e97327 345 gelic_descr_set_status(descr, GELIC_DESCR_DMA_CARDOWNED);
02c18891
MM
346 return 0;
347 }
348}
349
350/**
59e97327 351 * gelic_card_release_rx_chain - free all skb of rx descr
02c18891
MM
352 * @card: card structure
353 *
354 */
59e97327 355static void gelic_card_release_rx_chain(struct gelic_card *card)
02c18891 356{
59e97327 357 struct gelic_descr *descr = card->rx_chain.head;
02c18891
MM
358
359 do {
360 if (descr->skb) {
361 dma_unmap_single(ctodev(card),
100e1d89 362 be32_to_cpu(descr->buf_addr),
02c18891
MM
363 descr->skb->len,
364 DMA_FROM_DEVICE);
365 descr->buf_addr = 0;
366 dev_kfree_skb_any(descr->skb);
367 descr->skb = NULL;
59e97327
MM
368 gelic_descr_set_status(descr,
369 GELIC_DESCR_DMA_NOT_IN_USE);
02c18891
MM
370 }
371 descr = descr->next;
372 } while (descr != card->rx_chain.head);
373}
374
375/**
59e97327 376 * gelic_card_fill_rx_chain - fills descriptors/skbs in the rx chains
02c18891
MM
377 * @card: card structure
378 *
379 * fills all descriptors in the rx chain: allocates skbs
380 * and iommu-maps them.
59e97327 381 * returns 0 on success, < 0 on failure
02c18891 382 */
59e97327 383static int gelic_card_fill_rx_chain(struct gelic_card *card)
02c18891 384{
59e97327 385 struct gelic_descr *descr = card->rx_chain.head;
02c18891
MM
386 int ret;
387
388 do {
389 if (!descr->skb) {
59e97327 390 ret = gelic_descr_prepare_rx(card, descr);
02c18891
MM
391 if (ret)
392 goto rewind;
393 }
394 descr = descr->next;
395 } while (descr != card->rx_chain.head);
396
397 return 0;
398rewind:
59e97327 399 gelic_card_release_rx_chain(card);
02c18891
MM
400 return ret;
401}
402
403/**
59e97327 404 * gelic_card_alloc_rx_skbs - allocates rx skbs in rx descriptor chains
02c18891
MM
405 * @card: card structure
406 *
59e97327 407 * returns 0 on success, < 0 on failure
02c18891 408 */
59e97327 409static int gelic_card_alloc_rx_skbs(struct gelic_card *card)
02c18891 410{
59e97327 411 struct gelic_descr_chain *chain;
02c18891
MM
412 int ret;
413 chain = &card->rx_chain;
59e97327 414 ret = gelic_card_fill_rx_chain(card);
589866f9 415 chain->tail = card->rx_top->prev; /* point to the last */
02c18891
MM
416 return ret;
417}
418
419/**
59e97327 420 * gelic_descr_release_tx - processes a used tx descriptor
02c18891
MM
421 * @card: card structure
422 * @descr: descriptor to release
423 *
424 * releases a used tx descriptor (unmapping, freeing of skb)
425 */
59e97327 426static void gelic_descr_release_tx(struct gelic_card *card,
589866f9 427 struct gelic_descr *descr)
02c18891 428{
173261ed 429 struct sk_buff *skb = descr->skb;
02c18891 430
589866f9
MM
431 BUG_ON(!(be32_to_cpu(descr->data_status) & GELIC_DESCR_TX_TAIL));
432
433 dma_unmap_single(ctodev(card), be32_to_cpu(descr->buf_addr), skb->len,
434 DMA_TO_DEVICE);
173261ed 435 dev_kfree_skb_any(skb);
02c18891
MM
436
437 descr->buf_addr = 0;
438 descr->buf_size = 0;
439 descr->next_descr_addr = 0;
440 descr->result_size = 0;
441 descr->valid_size = 0;
442 descr->data_status = 0;
443 descr->data_error = 0;
444 descr->skb = NULL;
445
446 /* set descr status */
59e97327 447 gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
02c18891
MM
448}
449
589866f9
MM
450static void gelic_card_stop_queues(struct gelic_card *card)
451{
452 netif_stop_queue(card->netdev[GELIC_PORT_ETHERNET]);
453
454 if (card->netdev[GELIC_PORT_WIRELESS])
455 netif_stop_queue(card->netdev[GELIC_PORT_WIRELESS]);
456}
457static void gelic_card_wake_queues(struct gelic_card *card)
458{
459 netif_wake_queue(card->netdev[GELIC_PORT_ETHERNET]);
460
461 if (card->netdev[GELIC_PORT_WIRELESS])
462 netif_wake_queue(card->netdev[GELIC_PORT_WIRELESS]);
463}
02c18891 464/**
59e97327 465 * gelic_card_release_tx_chain - processes sent tx descriptors
02c18891
MM
466 * @card: adapter structure
467 * @stop: net_stop sequence
468 *
469 * releases the tx descriptors that gelic has finished with
470 */
59e97327 471static void gelic_card_release_tx_chain(struct gelic_card *card, int stop)
02c18891 472{
59e97327
MM
473 struct gelic_descr_chain *tx_chain;
474 enum gelic_descr_dma_status status;
589866f9 475 struct net_device *netdev;
02c18891
MM
476 int release = 0;
477
478 for (tx_chain = &card->tx_chain;
479 tx_chain->head != tx_chain->tail && tx_chain->tail;
480 tx_chain->tail = tx_chain->tail->next) {
59e97327 481 status = gelic_descr_get_status(tx_chain->tail);
589866f9 482 netdev = tx_chain->tail->skb->dev;
02c18891 483 switch (status) {
59e97327
MM
484 case GELIC_DESCR_DMA_RESPONSE_ERROR:
485 case GELIC_DESCR_DMA_PROTECTION_ERROR:
486 case GELIC_DESCR_DMA_FORCE_END:
02c18891
MM
487 if (printk_ratelimit())
488 dev_info(ctodev(card),
489 "%s: forcing end of tx descriptor " \
490 "with status %x\n",
491 __func__, status);
589866f9 492 netdev->stats.tx_dropped++;
02c18891
MM
493 break;
494
59e97327 495 case GELIC_DESCR_DMA_COMPLETE:
48544cc2 496 if (tx_chain->tail->skb) {
589866f9
MM
497 netdev->stats.tx_packets++;
498 netdev->stats.tx_bytes +=
48544cc2
MM
499 tx_chain->tail->skb->len;
500 }
02c18891
MM
501 break;
502
59e97327 503 case GELIC_DESCR_DMA_CARDOWNED:
02c18891
MM
504 /* pending tx request */
505 default:
59e97327 506 /* any other value (== GELIC_DESCR_DMA_NOT_IN_USE) */
48544cc2
MM
507 if (!stop)
508 goto out;
02c18891 509 }
59e97327 510 gelic_descr_release_tx(card, tx_chain->tail);
48544cc2 511 release ++;
02c18891
MM
512 }
513out:
173261ed 514 if (!stop && release)
589866f9 515 gelic_card_wake_queues(card);
02c18891
MM
516}
517
518/**
519 * gelic_net_set_multi - sets multicast addresses and promisc flags
520 * @netdev: interface device structure
521 *
522 * gelic_net_set_multi configures multicast addresses as needed for the
523 * netdev interface. It also sets up multicast, allmulti and promisc
524 * flags appropriately
525 */
589866f9 526void gelic_net_set_multi(struct net_device *netdev)
02c18891 527{
589866f9 528 struct gelic_card *card = netdev_card(netdev);
02c18891
MM
529 struct dev_mc_list *mc;
530 unsigned int i;
531 uint8_t *p;
532 u64 addr;
533 int status;
534
535 /* clear all multicast address */
536 status = lv1_net_remove_multicast_address(bus_id(card), dev_id(card),
537 0, 1);
538 if (status)
539 dev_err(ctodev(card),
540 "lv1_net_remove_multicast_address failed %d\n",
541 status);
542 /* set broadcast address */
543 status = lv1_net_add_multicast_address(bus_id(card), dev_id(card),
544 GELIC_NET_BROADCAST_ADDR, 0);
545 if (status)
546 dev_err(ctodev(card),
547 "lv1_net_add_multicast_address failed, %d\n",
548 status);
549
589866f9
MM
550 if ((netdev->flags & IFF_ALLMULTI) ||
551 (netdev->mc_count > GELIC_NET_MC_COUNT_MAX)) {
02c18891
MM
552 status = lv1_net_add_multicast_address(bus_id(card),
553 dev_id(card),
554 0, 1);
555 if (status)
556 dev_err(ctodev(card),
557 "lv1_net_add_multicast_address failed, %d\n",
558 status);
559 return;
560 }
561
589866f9 562 /* set multicast addresses */
02c18891
MM
563 for (mc = netdev->mc_list; mc; mc = mc->next) {
564 addr = 0;
565 p = mc->dmi_addr;
566 for (i = 0; i < ETH_ALEN; i++) {
567 addr <<= 8;
568 addr |= *p++;
569 }
570 status = lv1_net_add_multicast_address(bus_id(card),
571 dev_id(card),
572 addr, 0);
573 if (status)
574 dev_err(ctodev(card),
575 "lv1_net_add_multicast_address failed, %d\n",
576 status);
577 }
578}
579
580/**
59e97327 581 * gelic_card_enable_rxdmac - enables the receive DMA controller
02c18891
MM
582 * @card: card structure
583 *
59e97327 584 * gelic_card_enable_rxdmac enables the DMA controller by setting RX_DMA_EN
02c18891
MM
585 * in the GDADMACCNTR register
586 */
59e97327 587static inline void gelic_card_enable_rxdmac(struct gelic_card *card)
02c18891
MM
588{
589 int status;
590
589866f9
MM
591#ifdef DEBUG
592 if (gelic_descr_get_status(card->rx_chain.head) !=
593 GELIC_DESCR_DMA_CARDOWNED) {
594 printk(KERN_ERR "%s: status=%x\n", __func__,
595 be32_to_cpu(card->rx_chain.head->dmac_cmd_status));
596 printk(KERN_ERR "%s: nextphy=%x\n", __func__,
597 be32_to_cpu(card->rx_chain.head->next_descr_addr));
598 printk(KERN_ERR "%s: head=%p\n", __func__,
599 card->rx_chain.head);
600 }
601#endif
02c18891 602 status = lv1_net_start_rx_dma(bus_id(card), dev_id(card),
589866f9 603 card->rx_chain.head->bus_addr, 0);
02c18891
MM
604 if (status)
605 dev_info(ctodev(card),
606 "lv1_net_start_rx_dma failed, status=%d\n", status);
607}
608
609/**
59e97327 610 * gelic_card_disable_rxdmac - disables the receive DMA controller
02c18891
MM
611 * @card: card structure
612 *
59e97327 613 * gelic_card_disable_rxdmac terminates processing on the DMA controller by
02c18891
MM
614 * turing off DMA and issueing a force end
615 */
59e97327 616static inline void gelic_card_disable_rxdmac(struct gelic_card *card)
02c18891
MM
617{
618 int status;
619
620 /* this hvc blocks until the DMA in progress really stopped */
621 status = lv1_net_stop_rx_dma(bus_id(card), dev_id(card), 0);
622 if (status)
623 dev_err(ctodev(card),
624 "lv1_net_stop_rx_dma faild, %d\n", status);
625}
626
627/**
59e97327 628 * gelic_card_disable_txdmac - disables the transmit DMA controller
02c18891
MM
629 * @card: card structure
630 *
59e97327 631 * gelic_card_disable_txdmac terminates processing on the DMA controller by
02c18891
MM
632 * turing off DMA and issueing a force end
633 */
59e97327 634static inline void gelic_card_disable_txdmac(struct gelic_card *card)
02c18891
MM
635{
636 int status;
637
638 /* this hvc blocks until the DMA in progress really stopped */
639 status = lv1_net_stop_tx_dma(bus_id(card), dev_id(card), 0);
640 if (status)
641 dev_err(ctodev(card),
642 "lv1_net_stop_tx_dma faild, status=%d\n", status);
643}
644
645/**
646 * gelic_net_stop - called upon ifconfig down
647 * @netdev: interface device structure
648 *
649 * always returns 0
650 */
589866f9 651int gelic_net_stop(struct net_device *netdev)
02c18891 652{
589866f9 653 struct gelic_card *card;
02c18891 654
589866f9 655 pr_debug("%s: start\n", __func__);
02c18891 656
589866f9 657 netif_stop_queue(netdev);
02c18891
MM
658 netif_carrier_off(netdev);
659
589866f9
MM
660 card = netdev_card(netdev);
661 gelic_card_down(card);
02c18891 662
589866f9 663 pr_debug("%s: done\n", __func__);
02c18891
MM
664 return 0;
665}
666
667/**
59e97327 668 * gelic_card_get_next_tx_descr - returns the next available tx descriptor
02c18891
MM
669 * @card: device structure to get descriptor from
670 *
671 * returns the address of the next descriptor, or NULL if not available.
672 */
59e97327
MM
673static struct gelic_descr *
674gelic_card_get_next_tx_descr(struct gelic_card *card)
02c18891
MM
675{
676 if (!card->tx_chain.head)
677 return NULL;
173261ed 678 /* see if the next descriptor is free */
02c18891 679 if (card->tx_chain.tail != card->tx_chain.head->next &&
59e97327
MM
680 gelic_descr_get_status(card->tx_chain.head) ==
681 GELIC_DESCR_DMA_NOT_IN_USE)
02c18891
MM
682 return card->tx_chain.head;
683 else
684 return NULL;
685
686}
687
688/**
589866f9 689 * gelic_net_set_txdescr_cmdstat - sets the tx descriptor command field
02c18891
MM
690 * @descr: descriptor structure to fill out
691 * @skb: packet to consider
02c18891
MM
692 *
693 * fills out the command and status field of the descriptor structure,
694 * depending on hardware checksum settings. This function assumes a wmb()
695 * has executed before.
696 */
59e97327
MM
697static void gelic_descr_set_tx_cmdstat(struct gelic_descr *descr,
698 struct sk_buff *skb)
02c18891 699{
02c18891 700 if (skb->ip_summed != CHECKSUM_PARTIAL)
100e1d89 701 descr->dmac_cmd_status =
59e97327
MM
702 cpu_to_be32(GELIC_DESCR_DMA_CMD_NO_CHKSUM |
703 GELIC_DESCR_TX_DMA_FRAME_TAIL);
02c18891
MM
704 else {
705 /* is packet ip?
706 * if yes: tcp? udp? */
707 if (skb->protocol == htons(ETH_P_IP)) {
708 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
709 descr->dmac_cmd_status =
59e97327
MM
710 cpu_to_be32(GELIC_DESCR_DMA_CMD_TCP_CHKSUM |
711 GELIC_DESCR_TX_DMA_FRAME_TAIL);
173261ed 712
02c18891
MM
713 else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
714 descr->dmac_cmd_status =
59e97327
MM
715 cpu_to_be32(GELIC_DESCR_DMA_CMD_UDP_CHKSUM |
716 GELIC_DESCR_TX_DMA_FRAME_TAIL);
02c18891
MM
717 else /*
718 * the stack should checksum non-tcp and non-udp
719 * packets on his own: NETIF_F_IP_CSUM
720 */
721 descr->dmac_cmd_status =
59e97327
MM
722 cpu_to_be32(GELIC_DESCR_DMA_CMD_NO_CHKSUM |
723 GELIC_DESCR_TX_DMA_FRAME_TAIL);
02c18891
MM
724 }
725 }
726}
727
173261ed
MM
728static inline struct sk_buff *gelic_put_vlan_tag(struct sk_buff *skb,
729 unsigned short tag)
730{
731 struct vlan_ethhdr *veth;
732 static unsigned int c;
733
734 if (skb_headroom(skb) < VLAN_HLEN) {
735 struct sk_buff *sk_tmp = skb;
736 pr_debug("%s: hd=%d c=%ud\n", __func__, skb_headroom(skb), c);
737 skb = skb_realloc_headroom(sk_tmp, VLAN_HLEN);
738 if (!skb)
739 return NULL;
740 dev_kfree_skb_any(sk_tmp);
741 }
742 veth = (struct vlan_ethhdr *)skb_push(skb, VLAN_HLEN);
743
744 /* Move the mac addresses to the top of buffer */
745 memmove(skb->data, skb->data + VLAN_HLEN, 2 * ETH_ALEN);
746
747 veth->h_vlan_proto = __constant_htons(ETH_P_8021Q);
748 veth->h_vlan_TCI = htons(tag);
749
750 return skb;
751}
752
02c18891 753/**
589866f9 754 * gelic_descr_prepare_tx - setup a descriptor for sending packets
02c18891
MM
755 * @card: card structure
756 * @descr: descriptor structure
757 * @skb: packet to use
758 *
759 * returns 0 on success, <0 on failure.
760 *
761 */
59e97327
MM
762static int gelic_descr_prepare_tx(struct gelic_card *card,
763 struct gelic_descr *descr,
764 struct sk_buff *skb)
02c18891 765{
173261ed 766 dma_addr_t buf;
02c18891 767
589866f9 768 if (card->vlan_required) {
173261ed 769 struct sk_buff *skb_tmp;
589866f9
MM
770 enum gelic_port_type type;
771
772 type = netdev_port(skb->dev)->type;
173261ed 773 skb_tmp = gelic_put_vlan_tag(skb,
589866f9 774 card->vlan[type].tx);
173261ed
MM
775 if (!skb_tmp)
776 return -ENOMEM;
777 skb = skb_tmp;
02c18891
MM
778 }
779
173261ed 780 buf = dma_map_single(ctodev(card), skb->data, skb->len, DMA_TO_DEVICE);
02c18891 781
173261ed 782 if (!buf) {
02c18891
MM
783 dev_err(ctodev(card),
784 "dma map 2 failed (%p, %i). Dropping packet\n",
173261ed 785 skb->data, skb->len);
02c18891
MM
786 return -ENOMEM;
787 }
788
100e1d89
MM
789 descr->buf_addr = cpu_to_be32(buf);
790 descr->buf_size = cpu_to_be32(skb->len);
173261ed 791 descr->skb = skb;
02c18891 792 descr->data_status = 0;
173261ed 793 descr->next_descr_addr = 0; /* terminate hw descr */
59e97327 794 gelic_descr_set_tx_cmdstat(descr, skb);
48544cc2
MM
795
796 /* bump free descriptor pointer */
173261ed 797 card->tx_chain.head = descr->next;
02c18891
MM
798 return 0;
799}
800
801/**
59e97327 802 * gelic_card_kick_txdma - enables TX DMA processing
02c18891
MM
803 * @card: card structure
804 * @descr: descriptor address to enable TX processing at
805 *
806 */
59e97327
MM
807static int gelic_card_kick_txdma(struct gelic_card *card,
808 struct gelic_descr *descr)
02c18891 809{
48544cc2 810 int status = 0;
02c18891
MM
811
812 if (card->tx_dma_progress)
813 return 0;
814
59e97327 815 if (gelic_descr_get_status(descr) == GELIC_DESCR_DMA_CARDOWNED) {
02c18891 816 card->tx_dma_progress = 1;
dc029ad9
MM
817 status = lv1_net_start_tx_dma(bus_id(card), dev_id(card),
818 descr->bus_addr, 0);
819 if (status)
02c18891 820 dev_info(ctodev(card), "lv1_net_start_txdma failed," \
dc029ad9 821 "status=%d\n", status);
02c18891
MM
822 }
823 return status;
824}
825
826/**
827 * gelic_net_xmit - transmits a frame over the device
828 * @skb: packet to send out
829 * @netdev: interface device structure
830 *
831 * returns 0 on success, <0 on failure
832 */
589866f9 833int gelic_net_xmit(struct sk_buff *skb, struct net_device *netdev)
02c18891 834{
589866f9 835 struct gelic_card *card = netdev_card(netdev);
59e97327 836 struct gelic_descr *descr;
02c18891
MM
837 int result;
838 unsigned long flags;
839
589866f9 840 spin_lock_irqsave(&card->tx_lock, flags);
02c18891 841
59e97327 842 gelic_card_release_tx_chain(card, 0);
48544cc2 843
59e97327 844 descr = gelic_card_get_next_tx_descr(card);
02c18891 845 if (!descr) {
48544cc2
MM
846 /*
847 * no more descriptors free
848 */
589866f9
MM
849 gelic_card_stop_queues(card);
850 spin_unlock_irqrestore(&card->tx_lock, flags);
02c18891
MM
851 return NETDEV_TX_BUSY;
852 }
02c18891 853
59e97327 854 result = gelic_descr_prepare_tx(card, descr, skb);
48544cc2
MM
855 if (result) {
856 /*
857 * DMA map failed. As chanses are that failure
858 * would continue, just release skb and return
859 */
589866f9 860 netdev->stats.tx_dropped++;
48544cc2 861 dev_kfree_skb_any(skb);
589866f9 862 spin_unlock_irqrestore(&card->tx_lock, flags);
48544cc2
MM
863 return NETDEV_TX_OK;
864 }
865 /*
866 * link this prepared descriptor to previous one
867 * to achieve high performance
868 */
100e1d89 869 descr->prev->next_descr_addr = cpu_to_be32(descr->bus_addr);
02c18891
MM
870 /*
871 * as hardware descriptor is modified in the above lines,
872 * ensure that the hardware sees it
873 */
874 wmb();
59e97327 875 if (gelic_card_kick_txdma(card, descr)) {
48544cc2
MM
876 /*
877 * kick failed.
878 * release descriptors which were just prepared
879 */
589866f9 880 netdev->stats.tx_dropped++;
59e97327
MM
881 gelic_descr_release_tx(card, descr);
882 gelic_descr_release_tx(card, descr->next);
48544cc2
MM
883 card->tx_chain.tail = descr->next->next;
884 dev_info(ctodev(card), "%s: kick failure\n", __func__);
885 } else {
886 /* OK, DMA started/reserved */
887 netdev->trans_start = jiffies;
888 }
02c18891 889
589866f9 890 spin_unlock_irqrestore(&card->tx_lock, flags);
02c18891 891 return NETDEV_TX_OK;
02c18891
MM
892}
893
894/**
895 * gelic_net_pass_skb_up - takes an skb from a descriptor and passes it on
896 * @descr: descriptor to process
897 * @card: card structure
589866f9 898 * @netdev: net_device structure to be passed packet
02c18891
MM
899 *
900 * iommu-unmaps the skb, fills out skb structure and passes the data to the
901 * stack. The descriptor state is not changed.
902 */
59e97327 903static void gelic_net_pass_skb_up(struct gelic_descr *descr,
589866f9
MM
904 struct gelic_card *card,
905 struct net_device *netdev)
906
02c18891 907{
589866f9 908 struct sk_buff *skb = descr->skb;
02c18891
MM
909 u32 data_status, data_error;
910
100e1d89
MM
911 data_status = be32_to_cpu(descr->data_status);
912 data_error = be32_to_cpu(descr->data_error);
02c18891 913 /* unmap skb buffer */
589866f9
MM
914 dma_unmap_single(ctodev(card), be32_to_cpu(descr->buf_addr),
915 GELIC_NET_MAX_MTU,
02c18891
MM
916 DMA_FROM_DEVICE);
917
589866f9 918 skb_put(skb, be32_to_cpu(descr->valid_size)?
100e1d89
MM
919 be32_to_cpu(descr->valid_size) :
920 be32_to_cpu(descr->result_size));
02c18891
MM
921 if (!descr->valid_size)
922 dev_info(ctodev(card), "buffer full %x %x %x\n",
100e1d89
MM
923 be32_to_cpu(descr->result_size),
924 be32_to_cpu(descr->buf_size),
925 be32_to_cpu(descr->dmac_cmd_status));
02c18891
MM
926
927 descr->skb = NULL;
928 /*
929 * the card put 2 bytes vlan tag in front
930 * of the ethernet frame
931 */
932 skb_pull(skb, 2);
933 skb->protocol = eth_type_trans(skb, netdev);
934
935 /* checksum offload */
936 if (card->rx_csum) {
59e97327
MM
937 if ((data_status & GELIC_DESCR_DATA_STATUS_CHK_MASK) &&
938 (!(data_error & GELIC_DESCR_DATA_ERROR_CHK_MASK)))
02c18891
MM
939 skb->ip_summed = CHECKSUM_UNNECESSARY;
940 else
941 skb->ip_summed = CHECKSUM_NONE;
942 } else
943 skb->ip_summed = CHECKSUM_NONE;
944
945 /* update netdevice statistics */
589866f9
MM
946 netdev->stats.rx_packets++;
947 netdev->stats.rx_bytes += skb->len;
02c18891
MM
948
949 /* pass skb up to stack */
950 netif_receive_skb(skb);
951}
952
953/**
59e97327 954 * gelic_card_decode_one_descr - processes an rx descriptor
02c18891
MM
955 * @card: card structure
956 *
957 * returns 1 if a packet has been sent to the stack, otherwise 0
958 *
959 * processes an rx descriptor by iommu-unmapping the data buffer and passing
960 * the packet up to the stack
961 */
59e97327 962static int gelic_card_decode_one_descr(struct gelic_card *card)
02c18891 963{
59e97327
MM
964 enum gelic_descr_dma_status status;
965 struct gelic_descr_chain *chain = &card->rx_chain;
589866f9
MM
966 struct gelic_descr *descr = chain->head;
967 struct net_device *netdev = NULL;
02c18891
MM
968 int dmac_chain_ended;
969
59e97327 970 status = gelic_descr_get_status(descr);
02c18891
MM
971 /* is this descriptor terminated with next_descr == NULL? */
972 dmac_chain_ended =
100e1d89 973 be32_to_cpu(descr->dmac_cmd_status) &
59e97327 974 GELIC_DESCR_RX_DMA_CHAIN_END;
02c18891 975
59e97327 976 if (status == GELIC_DESCR_DMA_CARDOWNED)
02c18891
MM
977 return 0;
978
59e97327 979 if (status == GELIC_DESCR_DMA_NOT_IN_USE) {
02c18891
MM
980 dev_dbg(ctodev(card), "dormant descr? %p\n", descr);
981 return 0;
982 }
983
589866f9
MM
984 /* netdevice select */
985 if (card->vlan_required) {
986 unsigned int i;
987 u16 vid;
988 vid = *(u16 *)(descr->skb->data) & VLAN_VID_MASK;
989 for (i = 0; i < GELIC_PORT_MAX; i++) {
990 if (card->vlan[i].rx == vid) {
991 netdev = card->netdev[i];
992 break;
993 }
994 };
995 if (GELIC_PORT_MAX <= i) {
996 pr_info("%s: unknown packet vid=%x\n", __func__, vid);
997 goto refill;
998 }
999 } else
1000 netdev = card->netdev[GELIC_PORT_ETHERNET];
1001
59e97327
MM
1002 if ((status == GELIC_DESCR_DMA_RESPONSE_ERROR) ||
1003 (status == GELIC_DESCR_DMA_PROTECTION_ERROR) ||
1004 (status == GELIC_DESCR_DMA_FORCE_END)) {
02c18891
MM
1005 dev_info(ctodev(card), "dropping RX descriptor with state %x\n",
1006 status);
589866f9 1007 netdev->stats.rx_dropped++;
02c18891
MM
1008 goto refill;
1009 }
1010
59e97327 1011 if (status == GELIC_DESCR_DMA_BUFFER_FULL) {
fe6d3a40
MM
1012 /*
1013 * Buffer full would occur if and only if
1014 * the frame length was longer than the size of this
1015 * descriptor's buffer. If the frame length was equal
1016 * to or shorter than buffer'size, FRAME_END condition
1017 * would occur.
1018 * Anyway this frame was longer than the MTU,
1019 * just drop it.
1020 */
1021 dev_info(ctodev(card), "overlength frame\n");
1022 goto refill;
1023 }
1024 /*
1025 * descriptoers any other than FRAME_END here should
1026 * be treated as error.
1027 */
59e97327 1028 if (status != GELIC_DESCR_DMA_FRAME_END) {
02c18891
MM
1029 dev_dbg(ctodev(card), "RX descriptor with state %x\n",
1030 status);
1031 goto refill;
1032 }
1033
1034 /* ok, we've got a packet in descr */
589866f9 1035 gelic_net_pass_skb_up(descr, card, netdev);
02c18891 1036refill:
fe6d3a40
MM
1037 /*
1038 * So that always DMAC can see the end
1039 * of the descriptor chain to avoid
1040 * from unwanted DMAC overrun.
1041 */
1042 descr->next_descr_addr = 0;
02c18891
MM
1043
1044 /* change the descriptor state: */
59e97327 1045 gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
02c18891 1046
fe6d3a40
MM
1047 /*
1048 * this call can fail, but for now, just leave this
1049 * decriptor without skb
02c18891 1050 */
59e97327 1051 gelic_descr_prepare_rx(card, descr);
fe6d3a40 1052
589866f9
MM
1053 chain->tail = descr;
1054 chain->head = descr->next;
fe6d3a40
MM
1055
1056 /*
1057 * Set this descriptor the end of the chain.
1058 */
100e1d89 1059 descr->prev->next_descr_addr = cpu_to_be32(descr->bus_addr);
02c18891 1060
fe6d3a40
MM
1061 /*
1062 * If dmac chain was met, DMAC stopped.
1063 * thus re-enable it
1064 */
02c18891 1065 if (dmac_chain_ended) {
583aae10
MM
1066 card->rx_dma_restart_required = 1;
1067 dev_dbg(ctodev(card), "reenable rx dma scheduled\n");
02c18891
MM
1068 }
1069
1070 return 1;
1071}
1072
1073/**
1074 * gelic_net_poll - NAPI poll function called by the stack to return packets
589866f9 1075 * @napi: napi structure
02c18891
MM
1076 * @budget: number of packets we can pass to the stack at most
1077 *
589866f9 1078 * returns the number of the processed packets
02c18891
MM
1079 *
1080 */
bea3348e 1081static int gelic_net_poll(struct napi_struct *napi, int budget)
02c18891 1082{
59e97327 1083 struct gelic_card *card = container_of(napi, struct gelic_card, napi);
bea3348e 1084 int packets_done = 0;
02c18891 1085
bea3348e 1086 while (packets_done < budget) {
59e97327 1087 if (!gelic_card_decode_one_descr(card))
02c18891 1088 break;
bea3348e
SH
1089
1090 packets_done++;
02c18891 1091 }
bea3348e
SH
1092
1093 if (packets_done < budget) {
589866f9 1094 napi_complete(napi);
59e97327 1095 gelic_card_rx_irq_on(card);
bea3348e
SH
1096 }
1097 return packets_done;
02c18891 1098}
02c18891
MM
1099/**
1100 * gelic_net_change_mtu - changes the MTU of an interface
1101 * @netdev: interface device structure
1102 * @new_mtu: new MTU value
1103 *
1104 * returns 0 on success, <0 on failure
1105 */
589866f9 1106int gelic_net_change_mtu(struct net_device *netdev, int new_mtu)
02c18891
MM
1107{
1108 /* no need to re-alloc skbs or so -- the max mtu is about 2.3k
1109 * and mtu is outbound only anyway */
1110 if ((new_mtu < GELIC_NET_MIN_MTU) ||
1111 (new_mtu > GELIC_NET_MAX_MTU)) {
1112 return -EINVAL;
1113 }
1114 netdev->mtu = new_mtu;
1115 return 0;
1116}
1117
1118/**
59e97327 1119 * gelic_card_interrupt - event handler for gelic_net
02c18891 1120 */
59e97327 1121static irqreturn_t gelic_card_interrupt(int irq, void *ptr)
02c18891
MM
1122{
1123 unsigned long flags;
589866f9 1124 struct gelic_card *card = ptr;
02c18891
MM
1125 u64 status;
1126
1127 status = card->irq_status;
1128
1129 if (!status)
1130 return IRQ_NONE;
1131
589866f9
MM
1132 status &= card->irq_mask;
1133
583aae10
MM
1134 if (card->rx_dma_restart_required) {
1135 card->rx_dma_restart_required = 0;
59e97327 1136 gelic_card_enable_rxdmac(card);
583aae10
MM
1137 }
1138
59e97327
MM
1139 if (status & GELIC_CARD_RXINT) {
1140 gelic_card_rx_irq_off(card);
589866f9 1141 napi_schedule(&card->napi);
02c18891
MM
1142 }
1143
59e97327 1144 if (status & GELIC_CARD_TXINT) {
589866f9 1145 spin_lock_irqsave(&card->tx_lock, flags);
02c18891 1146 card->tx_dma_progress = 0;
59e97327 1147 gelic_card_release_tx_chain(card, 0);
48544cc2 1148 /* kick outstanding tx descriptor if any */
59e97327 1149 gelic_card_kick_txdma(card, card->tx_chain.tail);
589866f9 1150 spin_unlock_irqrestore(&card->tx_lock, flags);
02c18891 1151 }
01fed4c2
MM
1152
1153 /* ether port status changed */
1154 if (status & GELIC_CARD_PORT_STATUS_CHANGED)
1155 gelic_card_get_ether_port_status(card, 1);
589866f9 1156
02c18891
MM
1157 return IRQ_HANDLED;
1158}
1159
1160#ifdef CONFIG_NET_POLL_CONTROLLER
1161/**
1162 * gelic_net_poll_controller - artificial interrupt for netconsole etc.
1163 * @netdev: interface device structure
1164 *
1165 * see Documentation/networking/netconsole.txt
1166 */
589866f9 1167void gelic_net_poll_controller(struct net_device *netdev)
02c18891 1168{
589866f9 1169 struct gelic_card *card = netdev_card(netdev);
02c18891 1170
59e97327
MM
1171 gelic_card_set_irq_mask(card, 0);
1172 gelic_card_interrupt(netdev->irq, netdev);
589866f9 1173 gelic_card_set_irq_mask(card, card->irq_mask);
02c18891
MM
1174}
1175#endif /* CONFIG_NET_POLL_CONTROLLER */
1176
02c18891
MM
1177/**
1178 * gelic_net_open - called upon ifonfig up
1179 * @netdev: interface device structure
1180 *
1181 * returns 0 on success, <0 on failure
1182 *
1183 * gelic_net_open allocates all the descriptors and memory needed for
1184 * operation, sets up multicast list and enables interrupts
1185 */
589866f9 1186int gelic_net_open(struct net_device *netdev)
02c18891 1187{
589866f9 1188 struct gelic_card *card = netdev_card(netdev);
02c18891 1189
589866f9 1190 dev_dbg(ctodev(card), " -> %s %p\n", __func__, netdev);
02c18891 1191
589866f9 1192 gelic_card_up(card);
02c18891
MM
1193
1194 netif_start_queue(netdev);
01fed4c2 1195 gelic_card_get_ether_port_status(card, 1);
02c18891 1196
589866f9 1197 dev_dbg(ctodev(card), " <- %s\n", __func__);
02c18891 1198 return 0;
02c18891
MM
1199}
1200
589866f9
MM
1201void gelic_net_get_drvinfo(struct net_device *netdev,
1202 struct ethtool_drvinfo *info)
02c18891
MM
1203{
1204 strncpy(info->driver, DRV_NAME, sizeof(info->driver) - 1);
1205 strncpy(info->version, DRV_VERSION, sizeof(info->version) - 1);
1206}
1207
59e97327
MM
1208static int gelic_ether_get_settings(struct net_device *netdev,
1209 struct ethtool_cmd *cmd)
02c18891 1210{
589866f9 1211 struct gelic_card *card = netdev_card(netdev);
02c18891 1212
01fed4c2
MM
1213 gelic_card_get_ether_port_status(card, 0);
1214
1215 if (card->ether_port_status & GELIC_LV1_ETHER_FULL_DUPLEX)
1216 cmd->duplex = DUPLEX_FULL;
1217 else
1218 cmd->duplex = DUPLEX_HALF;
1219
1220 switch (card->ether_port_status & GELIC_LV1_ETHER_SPEED_MASK) {
1221 case GELIC_LV1_ETHER_SPEED_10:
1222 cmd->speed = SPEED_10;
1223 break;
1224 case GELIC_LV1_ETHER_SPEED_100:
1225 cmd->speed = SPEED_100;
1226 break;
1227 case GELIC_LV1_ETHER_SPEED_1000:
1228 cmd->speed = SPEED_1000;
1229 break;
1230 default:
1231 pr_info("%s: speed unknown\n", __func__);
1232 cmd->speed = SPEED_10;
1233 break;
02c18891 1234 }
01fed4c2 1235
02c18891
MM
1236 cmd->supported = SUPPORTED_TP | SUPPORTED_Autoneg |
1237 SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full |
1238 SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full |
1239 SUPPORTED_1000baseT_Half | SUPPORTED_1000baseT_Full;
1240 cmd->advertising = cmd->supported;
02c18891
MM
1241 cmd->autoneg = AUTONEG_ENABLE; /* always enabled */
1242 cmd->port = PORT_TP;
1243
1244 return 0;
1245}
1246
589866f9 1247u32 gelic_net_get_rx_csum(struct net_device *netdev)
02c18891 1248{
589866f9 1249 struct gelic_card *card = netdev_card(netdev);
02c18891
MM
1250
1251 return card->rx_csum;
1252}
1253
589866f9 1254int gelic_net_set_rx_csum(struct net_device *netdev, u32 data)
02c18891 1255{
589866f9 1256 struct gelic_card *card = netdev_card(netdev);
02c18891
MM
1257
1258 card->rx_csum = data;
1259 return 0;
1260}
1261
589866f9 1262static struct ethtool_ops gelic_ether_ethtool_ops = {
02c18891 1263 .get_drvinfo = gelic_net_get_drvinfo,
59e97327 1264 .get_settings = gelic_ether_get_settings,
7bc56b92 1265 .get_link = ethtool_op_get_link,
7bc56b92
MM
1266 .get_tx_csum = ethtool_op_get_tx_csum,
1267 .set_tx_csum = ethtool_op_set_tx_csum,
02c18891
MM
1268 .get_rx_csum = gelic_net_get_rx_csum,
1269 .set_rx_csum = gelic_net_set_rx_csum,
1270};
02c18891
MM
1271
1272/**
1273 * gelic_net_tx_timeout_task - task scheduled by the watchdog timeout
1274 * function (to be called not under interrupt status)
1275 * @work: work is context of tx timout task
1276 *
1277 * called as task when tx hangs, resets interface (if interface is up)
1278 */
1279static void gelic_net_tx_timeout_task(struct work_struct *work)
1280{
59e97327
MM
1281 struct gelic_card *card =
1282 container_of(work, struct gelic_card, tx_timeout_task);
589866f9 1283 struct net_device *netdev = card->netdev[GELIC_PORT_ETHERNET];
02c18891
MM
1284
1285 dev_info(ctodev(card), "%s:Timed out. Restarting... \n", __func__);
1286
1287 if (!(netdev->flags & IFF_UP))
1288 goto out;
1289
1290 netif_device_detach(netdev);
1291 gelic_net_stop(netdev);
1292
1293 gelic_net_open(netdev);
1294 netif_device_attach(netdev);
1295
1296out:
1297 atomic_dec(&card->tx_timeout_task_counter);
1298}
1299
1300/**
1301 * gelic_net_tx_timeout - called when the tx timeout watchdog kicks in.
1302 * @netdev: interface device structure
1303 *
1304 * called, if tx hangs. Schedules a task that resets the interface
1305 */
589866f9 1306void gelic_net_tx_timeout(struct net_device *netdev)
02c18891 1307{
59e97327 1308 struct gelic_card *card;
02c18891 1309
589866f9 1310 card = netdev_card(netdev);
02c18891
MM
1311 atomic_inc(&card->tx_timeout_task_counter);
1312 if (netdev->flags & IFF_UP)
1313 schedule_work(&card->tx_timeout_task);
1314 else
1315 atomic_dec(&card->tx_timeout_task_counter);
1316}
1317
1318/**
59e97327 1319 * gelic_ether_setup_netdev_ops - initialization of net_device operations
02c18891
MM
1320 * @netdev: net_device structure
1321 *
1322 * fills out function pointers in the net_device structure
1323 */
589866f9
MM
1324static void gelic_ether_setup_netdev_ops(struct net_device *netdev,
1325 struct napi_struct *napi)
02c18891
MM
1326{
1327 netdev->open = &gelic_net_open;
1328 netdev->stop = &gelic_net_stop;
1329 netdev->hard_start_xmit = &gelic_net_xmit;
02c18891
MM
1330 netdev->set_multicast_list = &gelic_net_set_multi;
1331 netdev->change_mtu = &gelic_net_change_mtu;
1332 /* tx watchdog */
1333 netdev->tx_timeout = &gelic_net_tx_timeout;
1334 netdev->watchdog_timeo = GELIC_NET_WATCHDOG_TIMEOUT;
589866f9
MM
1335 /* NAPI */
1336 netif_napi_add(netdev, napi,
1337 gelic_net_poll, GELIC_NET_NAPI_WEIGHT);
1338 netdev->ethtool_ops = &gelic_ether_ethtool_ops;
1339#ifdef CONFIG_NET_POLL_CONTROLLER
1340 netdev->poll_controller = gelic_net_poll_controller;
1341#endif
02c18891
MM
1342}
1343
1344/**
589866f9
MM
1345 * gelic_ether_setup_netdev - initialization of net_device
1346 * @netdev: net_device structure
02c18891
MM
1347 * @card: card structure
1348 *
1349 * Returns 0 on success or <0 on failure
1350 *
589866f9
MM
1351 * gelic_ether_setup_netdev initializes the net_device structure
1352 * and register it.
02c18891 1353 **/
589866f9 1354int gelic_net_setup_netdev(struct net_device *netdev, struct gelic_card *card)
02c18891 1355{
02c18891
MM
1356 int status;
1357 u64 v1, v2;
0795af57 1358 DECLARE_MAC_BUF(mac);
02c18891 1359
02c18891
MM
1360 netdev->features = NETIF_F_IP_CSUM;
1361
1362 status = lv1_net_control(bus_id(card), dev_id(card),
59e97327 1363 GELIC_LV1_GET_MAC_ADDRESS,
02c18891 1364 0, 0, 0, &v1, &v2);
589866f9 1365 v1 <<= 16;
02c18891
MM
1366 if (status || !is_valid_ether_addr((u8 *)&v1)) {
1367 dev_info(ctodev(card),
1368 "%s:lv1_net_control GET_MAC_ADDR failed %d\n",
1369 __func__, status);
1370 return -EINVAL;
1371 }
589866f9 1372 memcpy(netdev->dev_addr, &v1, ETH_ALEN);
173261ed 1373
589866f9 1374 if (card->vlan_required) {
173261ed 1375 netdev->hard_header_len += VLAN_HLEN;
589866f9
MM
1376 /*
1377 * As vlan is internally used,
1378 * we can not receive vlan packets
1379 */
1380 netdev->features |= NETIF_F_VLAN_CHALLENGED;
173261ed 1381 }
02c18891
MM
1382
1383 status = register_netdev(netdev);
1384 if (status) {
589866f9
MM
1385 dev_err(ctodev(card), "%s:Couldn't register %s %d\n",
1386 __func__, netdev->name, status);
02c18891
MM
1387 return status;
1388 }
589866f9
MM
1389 dev_info(ctodev(card), "%s: MAC addr %s\n",
1390 netdev->name,
1391 print_mac(mac, netdev->dev_addr));
02c18891
MM
1392
1393 return 0;
1394}
1395
1396/**
59e97327 1397 * gelic_alloc_card_net - allocates net_device and card structure
02c18891
MM
1398 *
1399 * returns the card structure or NULL in case of errors
1400 *
1401 * the card and net_device structures are linked to each other
1402 */
589866f9
MM
1403#define GELIC_ALIGN (32)
1404static struct gelic_card *gelic_alloc_card_net(struct net_device **netdev)
02c18891 1405{
59e97327 1406 struct gelic_card *card;
589866f9
MM
1407 struct gelic_port *port;
1408 void *p;
02c18891 1409 size_t alloc_size;
02c18891 1410 /*
589866f9
MM
1411 * gelic requires dma descriptor is 32 bytes aligned and
1412 * the hypervisor requires irq_status is 8 bytes aligned.
02c18891 1413 */
59e97327
MM
1414 BUILD_BUG_ON(offsetof(struct gelic_card, irq_status) % 8);
1415 BUILD_BUG_ON(offsetof(struct gelic_card, descr) % 32);
589866f9
MM
1416 alloc_size =
1417 sizeof(struct gelic_card) +
1418 sizeof(struct gelic_descr) * GELIC_NET_RX_DESCRIPTORS +
1419 sizeof(struct gelic_descr) * GELIC_NET_TX_DESCRIPTORS +
1420 GELIC_ALIGN - 1;
02c18891 1421
589866f9
MM
1422 p = kzalloc(alloc_size, GFP_KERNEL);
1423 if (!p)
02c18891 1424 return NULL;
589866f9
MM
1425 card = PTR_ALIGN(p, GELIC_ALIGN);
1426 card->unalign = p;
1427
1428 /*
1429 * alloc netdev
1430 */
1431 *netdev = alloc_etherdev(sizeof(struct gelic_port));
1432 if (!netdev) {
1433 kfree(card->unalign);
1434 return NULL;
1435 }
1436 port = netdev_priv(*netdev);
1437
1438 /* gelic_port */
1439 port->netdev = *netdev;
1440 port->card = card;
1441 port->type = GELIC_PORT_ETHERNET;
1442
1443 /* gelic_card */
1444 card->netdev[GELIC_PORT_ETHERNET] = *netdev;
02c18891 1445
02c18891
MM
1446 INIT_WORK(&card->tx_timeout_task, gelic_net_tx_timeout_task);
1447 init_waitqueue_head(&card->waitq);
1448 atomic_set(&card->tx_timeout_task_counter, 0);
589866f9
MM
1449 init_MUTEX(&card->updown_lock);
1450 atomic_set(&card->users, 0);
02c18891
MM
1451
1452 return card;
1453}
1454
589866f9
MM
1455static void gelic_card_get_vlan_info(struct gelic_card *card)
1456{
1457 u64 v1, v2;
1458 int status;
1459 unsigned int i;
1460 struct {
1461 int tx;
1462 int rx;
1463 } vlan_id_ix[2] = {
1464 [GELIC_PORT_ETHERNET] = {
1465 .tx = GELIC_LV1_VLAN_TX_ETHERNET,
1466 .rx = GELIC_LV1_VLAN_RX_ETHERNET
1467 },
1468 [GELIC_PORT_WIRELESS] = {
1469 .tx = GELIC_LV1_VLAN_TX_WIRELESS,
1470 .rx = GELIC_LV1_VLAN_RX_WIRELESS
1471 }
1472 };
1473
1474 for (i = 0; i < ARRAY_SIZE(vlan_id_ix); i++) {
1475 /* tx tag */
1476 status = lv1_net_control(bus_id(card), dev_id(card),
1477 GELIC_LV1_GET_VLAN_ID,
1478 vlan_id_ix[i].tx,
1479 0, 0, &v1, &v2);
1480 if (status || !v1) {
1481 if (status != LV1_NO_ENTRY)
1482 dev_dbg(ctodev(card),
1483 "get vlan id for tx(%d) failed(%d)\n",
1484 vlan_id_ix[i].tx, status);
1485 card->vlan[i].tx = 0;
1486 card->vlan[i].rx = 0;
1487 continue;
1488 }
1489 card->vlan[i].tx = (u16)v1;
1490
1491 /* rx tag */
1492 status = lv1_net_control(bus_id(card), dev_id(card),
1493 GELIC_LV1_GET_VLAN_ID,
1494 vlan_id_ix[i].rx,
1495 0, 0, &v1, &v2);
1496 if (status || !v1) {
1497 if (status != LV1_NO_ENTRY)
1498 dev_info(ctodev(card),
1499 "get vlan id for rx(%d) failed(%d)\n",
1500 vlan_id_ix[i].rx, status);
1501 card->vlan[i].tx = 0;
1502 card->vlan[i].rx = 0;
1503 continue;
1504 }
1505 card->vlan[i].rx = (u16)v1;
1506
1507 dev_dbg(ctodev(card), "vlan_id[%d] tx=%02x rx=%02x\n",
1508 i, card->vlan[i].tx, card->vlan[i].rx);
1509 }
1510
1511 if (card->vlan[GELIC_PORT_ETHERNET].tx) {
1512 BUG_ON(!card->vlan[GELIC_PORT_WIRELESS].tx);
1513 card->vlan_required = 1;
1514 } else
1515 card->vlan_required = 0;
1516
1517 /* check wirelss capable firmware */
1518 if (ps3_compare_firmware_version(1, 6, 0) < 0) {
1519 card->vlan[GELIC_PORT_WIRELESS].tx = 0;
1520 card->vlan[GELIC_PORT_WIRELESS].rx = 0;
1521 }
1522
1523 dev_info(ctodev(card), "internal vlan %s\n",
1524 card->vlan_required? "enabled" : "disabled");
1525}
02c18891
MM
1526/**
1527 * ps3_gelic_driver_probe - add a device to the control of this driver
1528 */
59e97327 1529static int ps3_gelic_driver_probe(struct ps3_system_bus_device *dev)
02c18891 1530{
589866f9
MM
1531 struct gelic_card *card;
1532 struct net_device *netdev;
02c18891
MM
1533 int result;
1534
589866f9 1535 pr_debug("%s: called\n", __func__);
02c18891
MM
1536 result = ps3_open_hv_device(dev);
1537
1538 if (result) {
589866f9
MM
1539 dev_dbg(&dev->core, "%s:ps3_open_hv_device failed\n",
1540 __func__);
02c18891
MM
1541 goto fail_open;
1542 }
1543
1544 result = ps3_dma_region_create(dev->d_region);
1545
1546 if (result) {
589866f9
MM
1547 dev_dbg(&dev->core, "%s:ps3_dma_region_create failed(%d)\n",
1548 __func__, result);
02c18891
MM
1549 BUG_ON("check region type");
1550 goto fail_dma_region;
1551 }
1552
589866f9
MM
1553 /* alloc card/netdevice */
1554 card = gelic_alloc_card_net(&netdev);
1555 if (!card) {
1556 dev_info(&dev->core, "%s:gelic_net_alloc_card failed\n",
1557 __func__);
1558 result = -ENOMEM;
1559 goto fail_alloc_card;
1560 }
1561 ps3_system_bus_set_driver_data(dev, card);
1562 card->dev = dev;
1563
1564 /* get internal vlan info */
1565 gelic_card_get_vlan_info(card);
1566
1567 /* setup interrupt */
02c18891
MM
1568 result = lv1_net_set_interrupt_status_indicator(bus_id(card),
1569 dev_id(card),
1570 ps3_mm_phys_to_lpar(__pa(&card->irq_status)),
1571 0);
1572
1573 if (result) {
1574 dev_dbg(&dev->core,
589866f9
MM
1575 "%s:set_interrupt_status_indicator failed: %s\n",
1576 __func__, ps3_result(result));
02c18891
MM
1577 result = -EIO;
1578 goto fail_status_indicator;
1579 }
1580
589866f9
MM
1581 result = ps3_sb_event_receive_port_setup(dev, PS3_BINDING_CPU_ANY,
1582 &card->irq);
1583
1584 if (result) {
1585 dev_info(ctodev(card),
1586 "%s:gelic_net_open_device failed (%d)\n",
1587 __func__, result);
1588 result = -EPERM;
1589 goto fail_alloc_irq;
1590 }
1591 result = request_irq(card->irq, gelic_card_interrupt,
1592 IRQF_DISABLED, netdev->name, card);
1593
1594 if (result) {
1595 dev_info(ctodev(card), "%s:request_irq failed (%d)\n",
1596 __func__, result);
1597 goto fail_request_irq;
1598 }
1599
1600 /* setup card structure */
1601 card->irq_mask = GELIC_CARD_RXINT | GELIC_CARD_TXINT |
1602 GELIC_CARD_PORT_STATUS_CHANGED;
1603 card->rx_csum = GELIC_CARD_RX_CSUM_DEFAULT;
1604
1605
1606 if (gelic_card_init_chain(card, &card->tx_chain,
1607 card->descr, GELIC_NET_TX_DESCRIPTORS))
1608 goto fail_alloc_tx;
1609 if (gelic_card_init_chain(card, &card->rx_chain,
1610 card->descr + GELIC_NET_TX_DESCRIPTORS,
1611 GELIC_NET_RX_DESCRIPTORS))
1612 goto fail_alloc_rx;
1613
1614 /* head of chain */
1615 card->tx_top = card->tx_chain.head;
1616 card->rx_top = card->rx_chain.head;
1617 dev_dbg(ctodev(card), "descr rx %p, tx %p, size %#lx, num %#x\n",
1618 card->rx_top, card->tx_top, sizeof(struct gelic_descr),
1619 GELIC_NET_RX_DESCRIPTORS);
1620 /* allocate rx skbs */
1621 if (gelic_card_alloc_rx_skbs(card))
1622 goto fail_alloc_skbs;
1623
1624 spin_lock_init(&card->tx_lock);
1625 card->tx_dma_progress = 0;
02c18891 1626
589866f9
MM
1627 /* setup net_device structure */
1628 netdev->irq = card->irq;
1629 SET_NETDEV_DEV(netdev, &card->dev->core);
1630 gelic_ether_setup_netdev_ops(netdev, &card->napi);
1631 result = gelic_net_setup_netdev(netdev, card);
02c18891 1632 if (result) {
589866f9
MM
1633 dev_dbg(&dev->core, "%s: setup_netdev failed %d",
1634 __func__, result);
02c18891
MM
1635 goto fail_setup_netdev;
1636 }
1637
589866f9 1638 pr_debug("%s: done\n", __func__);
02c18891
MM
1639 return 0;
1640
1641fail_setup_netdev:
589866f9
MM
1642fail_alloc_skbs:
1643 gelic_card_free_chain(card, card->rx_chain.head);
1644fail_alloc_rx:
1645 gelic_card_free_chain(card, card->tx_chain.head);
1646fail_alloc_tx:
1647 free_irq(card->irq, card);
1648 netdev->irq = NO_IRQ;
1649fail_request_irq:
1650 ps3_sb_event_receive_port_destroy(dev, card->irq);
1651fail_alloc_irq:
02c18891 1652 lv1_net_set_interrupt_status_indicator(bus_id(card),
589866f9
MM
1653 bus_id(card),
1654 0, 0);
02c18891 1655fail_status_indicator:
589866f9
MM
1656 ps3_system_bus_set_driver_data(dev, NULL);
1657 kfree(netdev_card(netdev)->unalign);
1658 free_netdev(netdev);
1659fail_alloc_card:
02c18891
MM
1660 ps3_dma_region_free(dev->d_region);
1661fail_dma_region:
1662 ps3_close_hv_device(dev);
1663fail_open:
02c18891
MM
1664 return result;
1665}
1666
1667/**
1668 * ps3_gelic_driver_remove - remove a device from the control of this driver
1669 */
1670
59e97327 1671static int ps3_gelic_driver_remove(struct ps3_system_bus_device *dev)
02c18891 1672{
59e97327 1673 struct gelic_card *card = ps3_system_bus_get_driver_data(dev);
589866f9
MM
1674 struct net_device *netdev0;
1675 pr_debug("%s: called\n", __func__);
1676
1677 /* stop interrupt */
1678 gelic_card_set_irq_mask(card, 0);
1679
1680 /* turn off DMA, force end */
1681 gelic_card_disable_rxdmac(card);
1682 gelic_card_disable_txdmac(card);
1683
1684 /* release chains */
1685 gelic_card_release_tx_chain(card, 1);
1686 gelic_card_release_rx_chain(card);
1687
1688 gelic_card_free_chain(card, card->tx_top);
1689 gelic_card_free_chain(card, card->rx_top);
1690
1691 netdev0 = card->netdev[GELIC_PORT_ETHERNET];
1692 /* disconnect event port */
1693 free_irq(card->irq, card);
1694 netdev0->irq = NO_IRQ;
1695 ps3_sb_event_receive_port_destroy(card->dev, card->irq);
02c18891
MM
1696
1697 wait_event(card->waitq,
1698 atomic_read(&card->tx_timeout_task_counter) == 0);
1699
1700 lv1_net_set_interrupt_status_indicator(bus_id(card), dev_id(card),
1701 0 , 0);
1702
589866f9
MM
1703 unregister_netdev(netdev0);
1704 kfree(netdev_card(netdev0)->unalign);
1705 free_netdev(netdev0);
02c18891
MM
1706
1707 ps3_system_bus_set_driver_data(dev, NULL);
1708
1709 ps3_dma_region_free(dev->d_region);
1710
1711 ps3_close_hv_device(dev);
1712
589866f9 1713 pr_debug("%s: done\n", __func__);
02c18891
MM
1714 return 0;
1715}
1716
1717static struct ps3_system_bus_driver ps3_gelic_driver = {
1718 .match_id = PS3_MATCH_ID_GELIC,
1719 .probe = ps3_gelic_driver_probe,
1720 .remove = ps3_gelic_driver_remove,
1721 .shutdown = ps3_gelic_driver_remove,
1722 .core.name = "ps3_gelic_driver",
1723 .core.owner = THIS_MODULE,
1724};
1725
1726static int __init ps3_gelic_driver_init (void)
1727{
1728 return firmware_has_feature(FW_FEATURE_PS3_LV1)
1729 ? ps3_system_bus_driver_register(&ps3_gelic_driver)
1730 : -ENODEV;
1731}
1732
1733static void __exit ps3_gelic_driver_exit (void)
1734{
1735 ps3_system_bus_driver_unregister(&ps3_gelic_driver);
1736}
1737
59e97327
MM
1738module_init(ps3_gelic_driver_init);
1739module_exit(ps3_gelic_driver_exit);
02c18891
MM
1740
1741MODULE_ALIAS(PS3_MODULE_ALIAS_GELIC);
1742
This page took 0.215449 seconds and 5 git commands to generate.