PS3: gelic: Add support for dual network interface
[safe/jmp/linux-2.6] / drivers / net / ps3_gelic_net.c
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"
51 #define DRV_VERSION "1.1"
52
53 MODULE_AUTHOR("SCE Inc.");
54 MODULE_DESCRIPTION("Gelic Network driver");
55 MODULE_LICENSE("GPL");
56
57
58 static inline void gelic_card_enable_rxdmac(struct gelic_card *card);
59 static inline void gelic_card_disable_rxdmac(struct gelic_card *card);
60 static inline void gelic_card_disable_txdmac(struct gelic_card *card);
61 static inline void gelic_card_reset_chain(struct gelic_card *card,
62                                           struct gelic_descr_chain *chain,
63                                           struct gelic_descr *start_descr);
64
65 /* set irq_mask */
66 int gelic_card_set_irq_mask(struct gelic_card *card, u64 mask)
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),
74                          "%s failed %d\n", __func__, status);
75         return status;
76 }
77
78 static inline void gelic_card_rx_irq_on(struct gelic_card *card)
79 {
80         card->irq_mask |= GELIC_CARD_RXINT;
81         gelic_card_set_irq_mask(card, card->irq_mask);
82 }
83 static inline void gelic_card_rx_irq_off(struct gelic_card *card)
84 {
85         card->irq_mask &= ~GELIC_CARD_RXINT;
86         gelic_card_set_irq_mask(card, card->irq_mask);
87 }
88
89 static void gelic_card_get_ether_port_status(struct gelic_card *card,
90                                              int inform)
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) {
101                 ether_netdev = card->netdev[GELIC_PORT_ETHERNET];
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
109 void 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
126 void 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 }
151
152 /**
153  * gelic_descr_get_status -- returns the status of a descriptor
154  * @descr: descriptor to look at
155  *
156  * returns the status as in the dmac_cmd_status field of the descriptor
157  */
158 static enum gelic_descr_dma_status
159 gelic_descr_get_status(struct gelic_descr *descr)
160 {
161         return be32_to_cpu(descr->dmac_cmd_status) & GELIC_DESCR_DMA_STAT_MASK;
162 }
163
164 /**
165  * gelic_descr_set_status -- sets the status of a descriptor
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  */
172 static void gelic_descr_set_status(struct gelic_descr *descr,
173                                    enum gelic_descr_dma_status status)
174 {
175         descr->dmac_cmd_status = cpu_to_be32(status |
176                         (be32_to_cpu(descr->dmac_cmd_status) &
177                          ~GELIC_DESCR_DMA_STAT_MASK));
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 /**
188  * gelic_card_free_chain - free descriptor chain
189  * @card: card structure
190  * @descr_in: address of desc
191  */
192 static void gelic_card_free_chain(struct gelic_card *card,
193                                   struct gelic_descr *descr_in)
194 {
195         struct gelic_descr *descr;
196
197         for (descr = descr_in; descr && descr->bus_addr; descr = descr->next) {
198                 dma_unmap_single(ctodev(card), descr->bus_addr,
199                                  GELIC_DESCR_SIZE, DMA_BIDIRECTIONAL);
200                 descr->bus_addr = 0;
201         }
202 }
203
204 /**
205  * gelic_card_init_chain - links descriptor chain
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  */
216 static int gelic_card_init_chain(struct gelic_card *card,
217                                  struct gelic_descr_chain *chain,
218                                  struct gelic_descr *start_descr, int no)
219 {
220         int i;
221         struct gelic_descr *descr;
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++) {
228                 gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
229                 descr->bus_addr =
230                         dma_map_single(ctodev(card), descr,
231                                        GELIC_DESCR_SIZE,
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++) {
247                 descr->next_descr_addr = cpu_to_be32(descr->next->bus_addr);
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
258 iommu_error:
259         for (i--, descr--; 0 <= i; i--, descr--)
260                 if (descr->bus_addr)
261                         dma_unmap_single(ctodev(card), descr->bus_addr,
262                                          GELIC_DESCR_SIZE,
263                                          DMA_BIDIRECTIONAL);
264         return -ENOMEM;
265 }
266
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  */
276 static 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 }
292 /**
293  * gelic_descr_prepare_rx - reinitializes a rx descriptor
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  */
302 static int gelic_descr_prepare_rx(struct gelic_card *card,
303                                   struct gelic_descr *descr)
304 {
305         int offset;
306         unsigned int bufsize;
307
308         if (gelic_descr_get_status(descr) !=  GELIC_DESCR_DMA_NOT_IN_USE)
309                 dev_info(ctodev(card), "%s: ERROR status \n", __func__);
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 */
315         descr->skb = dev_alloc_skb(bufsize + GELIC_NET_RXBUF_ALIGN - 1);
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         }
322         descr->buf_size = cpu_to_be32(bufsize);
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 */
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));
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__);
342                 gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
343                 return -ENOMEM;
344         } else {
345                 gelic_descr_set_status(descr, GELIC_DESCR_DMA_CARDOWNED);
346                 return 0;
347         }
348 }
349
350 /**
351  * gelic_card_release_rx_chain - free all skb of rx descr
352  * @card: card structure
353  *
354  */
355 static void gelic_card_release_rx_chain(struct gelic_card *card)
356 {
357         struct gelic_descr *descr = card->rx_chain.head;
358
359         do {
360                 if (descr->skb) {
361                         dma_unmap_single(ctodev(card),
362                                          be32_to_cpu(descr->buf_addr),
363                                          descr->skb->len,
364                                          DMA_FROM_DEVICE);
365                         descr->buf_addr = 0;
366                         dev_kfree_skb_any(descr->skb);
367                         descr->skb = NULL;
368                         gelic_descr_set_status(descr,
369                                                GELIC_DESCR_DMA_NOT_IN_USE);
370                 }
371                 descr = descr->next;
372         } while (descr != card->rx_chain.head);
373 }
374
375 /**
376  * gelic_card_fill_rx_chain - fills descriptors/skbs in the rx chains
377  * @card: card structure
378  *
379  * fills all descriptors in the rx chain: allocates skbs
380  * and iommu-maps them.
381  * returns 0 on success, < 0 on failure
382  */
383 static int gelic_card_fill_rx_chain(struct gelic_card *card)
384 {
385         struct gelic_descr *descr = card->rx_chain.head;
386         int ret;
387
388         do {
389                 if (!descr->skb) {
390                         ret = gelic_descr_prepare_rx(card, descr);
391                         if (ret)
392                                 goto rewind;
393                 }
394                 descr = descr->next;
395         } while (descr != card->rx_chain.head);
396
397         return 0;
398 rewind:
399         gelic_card_release_rx_chain(card);
400         return ret;
401 }
402
403 /**
404  * gelic_card_alloc_rx_skbs - allocates rx skbs in rx descriptor chains
405  * @card: card structure
406  *
407  * returns 0 on success, < 0 on failure
408  */
409 static int gelic_card_alloc_rx_skbs(struct gelic_card *card)
410 {
411         struct gelic_descr_chain *chain;
412         int ret;
413         chain = &card->rx_chain;
414         ret = gelic_card_fill_rx_chain(card);
415         chain->tail = card->rx_top->prev; /* point to the last */
416         return ret;
417 }
418
419 /**
420  * gelic_descr_release_tx - processes a used tx descriptor
421  * @card: card structure
422  * @descr: descriptor to release
423  *
424  * releases a used tx descriptor (unmapping, freeing of skb)
425  */
426 static void gelic_descr_release_tx(struct gelic_card *card,
427                                        struct gelic_descr *descr)
428 {
429         struct sk_buff *skb = descr->skb;
430
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);
435         dev_kfree_skb_any(skb);
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 */
447         gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
448 }
449
450 static 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 }
457 static 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 }
464 /**
465  * gelic_card_release_tx_chain - processes sent tx descriptors
466  * @card: adapter structure
467  * @stop: net_stop sequence
468  *
469  * releases the tx descriptors that gelic has finished with
470  */
471 static void gelic_card_release_tx_chain(struct gelic_card *card, int stop)
472 {
473         struct gelic_descr_chain *tx_chain;
474         enum gelic_descr_dma_status status;
475         struct net_device *netdev;
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) {
481                 status = gelic_descr_get_status(tx_chain->tail);
482                 netdev = tx_chain->tail->skb->dev;
483                 switch (status) {
484                 case GELIC_DESCR_DMA_RESPONSE_ERROR:
485                 case GELIC_DESCR_DMA_PROTECTION_ERROR:
486                 case GELIC_DESCR_DMA_FORCE_END:
487                         if (printk_ratelimit())
488                                 dev_info(ctodev(card),
489                                          "%s: forcing end of tx descriptor " \
490                                          "with status %x\n",
491                                          __func__, status);
492                         netdev->stats.tx_dropped++;
493                         break;
494
495                 case GELIC_DESCR_DMA_COMPLETE:
496                         if (tx_chain->tail->skb) {
497                                 netdev->stats.tx_packets++;
498                                 netdev->stats.tx_bytes +=
499                                         tx_chain->tail->skb->len;
500                         }
501                         break;
502
503                 case GELIC_DESCR_DMA_CARDOWNED:
504                         /* pending tx request */
505                 default:
506                         /* any other value (== GELIC_DESCR_DMA_NOT_IN_USE) */
507                         if (!stop)
508                                 goto out;
509                 }
510                 gelic_descr_release_tx(card, tx_chain->tail);
511                 release ++;
512         }
513 out:
514         if (!stop && release)
515                 gelic_card_wake_queues(card);
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  */
526 void gelic_net_set_multi(struct net_device *netdev)
527 {
528         struct gelic_card *card = netdev_card(netdev);
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
550         if ((netdev->flags & IFF_ALLMULTI) ||
551             (netdev->mc_count > GELIC_NET_MC_COUNT_MAX)) {
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
562         /* set multicast addresses */
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 /**
581  * gelic_card_enable_rxdmac - enables the receive DMA controller
582  * @card: card structure
583  *
584  * gelic_card_enable_rxdmac enables the DMA controller by setting RX_DMA_EN
585  * in the GDADMACCNTR register
586  */
587 static inline void gelic_card_enable_rxdmac(struct gelic_card *card)
588 {
589         int status;
590
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
602         status = lv1_net_start_rx_dma(bus_id(card), dev_id(card),
603                                 card->rx_chain.head->bus_addr, 0);
604         if (status)
605                 dev_info(ctodev(card),
606                          "lv1_net_start_rx_dma failed, status=%d\n", status);
607 }
608
609 /**
610  * gelic_card_disable_rxdmac - disables the receive DMA controller
611  * @card: card structure
612  *
613  * gelic_card_disable_rxdmac terminates processing on the DMA controller by
614  * turing off DMA and issueing a force end
615  */
616 static inline void gelic_card_disable_rxdmac(struct gelic_card *card)
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 /**
628  * gelic_card_disable_txdmac - disables the transmit DMA controller
629  * @card: card structure
630  *
631  * gelic_card_disable_txdmac terminates processing on the DMA controller by
632  * turing off DMA and issueing a force end
633  */
634 static inline void gelic_card_disable_txdmac(struct gelic_card *card)
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  */
651 int gelic_net_stop(struct net_device *netdev)
652 {
653         struct gelic_card *card;
654
655         pr_debug("%s: start\n", __func__);
656
657         netif_stop_queue(netdev);
658         netif_carrier_off(netdev);
659
660         card = netdev_card(netdev);
661         gelic_card_down(card);
662
663         pr_debug("%s: done\n", __func__);
664         return 0;
665 }
666
667 /**
668  * gelic_card_get_next_tx_descr - returns the next available tx descriptor
669  * @card: device structure to get descriptor from
670  *
671  * returns the address of the next descriptor, or NULL if not available.
672  */
673 static struct gelic_descr *
674 gelic_card_get_next_tx_descr(struct gelic_card *card)
675 {
676         if (!card->tx_chain.head)
677                 return NULL;
678         /*  see if the next descriptor is free */
679         if (card->tx_chain.tail != card->tx_chain.head->next &&
680             gelic_descr_get_status(card->tx_chain.head) ==
681             GELIC_DESCR_DMA_NOT_IN_USE)
682                 return card->tx_chain.head;
683         else
684                 return NULL;
685
686 }
687
688 /**
689  * gelic_net_set_txdescr_cmdstat - sets the tx descriptor command field
690  * @descr: descriptor structure to fill out
691  * @skb: packet to consider
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  */
697 static void gelic_descr_set_tx_cmdstat(struct gelic_descr *descr,
698                                        struct sk_buff *skb)
699 {
700         if (skb->ip_summed != CHECKSUM_PARTIAL)
701                 descr->dmac_cmd_status =
702                         cpu_to_be32(GELIC_DESCR_DMA_CMD_NO_CHKSUM |
703                                     GELIC_DESCR_TX_DMA_FRAME_TAIL);
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 =
710                                 cpu_to_be32(GELIC_DESCR_DMA_CMD_TCP_CHKSUM |
711                                             GELIC_DESCR_TX_DMA_FRAME_TAIL);
712
713                         else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
714                                 descr->dmac_cmd_status =
715                                 cpu_to_be32(GELIC_DESCR_DMA_CMD_UDP_CHKSUM |
716                                             GELIC_DESCR_TX_DMA_FRAME_TAIL);
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 =
722                                 cpu_to_be32(GELIC_DESCR_DMA_CMD_NO_CHKSUM |
723                                             GELIC_DESCR_TX_DMA_FRAME_TAIL);
724                 }
725         }
726 }
727
728 static 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
753 /**
754  * gelic_descr_prepare_tx - setup a descriptor for sending packets
755  * @card: card structure
756  * @descr: descriptor structure
757  * @skb: packet to use
758  *
759  * returns 0 on success, <0 on failure.
760  *
761  */
762 static int gelic_descr_prepare_tx(struct gelic_card *card,
763                                   struct gelic_descr *descr,
764                                   struct sk_buff *skb)
765 {
766         dma_addr_t buf;
767
768         if (card->vlan_required) {
769                 struct sk_buff *skb_tmp;
770                 enum gelic_port_type type;
771
772                 type = netdev_port(skb->dev)->type;
773                 skb_tmp = gelic_put_vlan_tag(skb,
774                                              card->vlan[type].tx);
775                 if (!skb_tmp)
776                         return -ENOMEM;
777                 skb = skb_tmp;
778         }
779
780         buf = dma_map_single(ctodev(card), skb->data, skb->len, DMA_TO_DEVICE);
781
782         if (!buf) {
783                 dev_err(ctodev(card),
784                         "dma map 2 failed (%p, %i). Dropping packet\n",
785                         skb->data, skb->len);
786                 return -ENOMEM;
787         }
788
789         descr->buf_addr = cpu_to_be32(buf);
790         descr->buf_size = cpu_to_be32(skb->len);
791         descr->skb = skb;
792         descr->data_status = 0;
793         descr->next_descr_addr = 0; /* terminate hw descr */
794         gelic_descr_set_tx_cmdstat(descr, skb);
795
796         /* bump free descriptor pointer */
797         card->tx_chain.head = descr->next;
798         return 0;
799 }
800
801 /**
802  * gelic_card_kick_txdma - enables TX DMA processing
803  * @card: card structure
804  * @descr: descriptor address to enable TX processing at
805  *
806  */
807 static int gelic_card_kick_txdma(struct gelic_card *card,
808                                  struct gelic_descr *descr)
809 {
810         int status = 0;
811
812         if (card->tx_dma_progress)
813                 return 0;
814
815         if (gelic_descr_get_status(descr) == GELIC_DESCR_DMA_CARDOWNED) {
816                 card->tx_dma_progress = 1;
817                 status = lv1_net_start_tx_dma(bus_id(card), dev_id(card),
818                                               descr->bus_addr, 0);
819                 if (status)
820                         dev_info(ctodev(card), "lv1_net_start_txdma failed," \
821                                  "status=%d\n", status);
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  */
833 int gelic_net_xmit(struct sk_buff *skb, struct net_device *netdev)
834 {
835         struct gelic_card *card = netdev_card(netdev);
836         struct gelic_descr *descr;
837         int result;
838         unsigned long flags;
839
840         spin_lock_irqsave(&card->tx_lock, flags);
841
842         gelic_card_release_tx_chain(card, 0);
843
844         descr = gelic_card_get_next_tx_descr(card);
845         if (!descr) {
846                 /*
847                  * no more descriptors free
848                  */
849                 gelic_card_stop_queues(card);
850                 spin_unlock_irqrestore(&card->tx_lock, flags);
851                 return NETDEV_TX_BUSY;
852         }
853
854         result = gelic_descr_prepare_tx(card, descr, skb);
855         if (result) {
856                 /*
857                  * DMA map failed.  As chanses are that failure
858                  * would continue, just release skb and return
859                  */
860                 netdev->stats.tx_dropped++;
861                 dev_kfree_skb_any(skb);
862                 spin_unlock_irqrestore(&card->tx_lock, flags);
863                 return NETDEV_TX_OK;
864         }
865         /*
866          * link this prepared descriptor to previous one
867          * to achieve high performance
868          */
869         descr->prev->next_descr_addr = cpu_to_be32(descr->bus_addr);
870         /*
871          * as hardware descriptor is modified in the above lines,
872          * ensure that the hardware sees it
873          */
874         wmb();
875         if (gelic_card_kick_txdma(card, descr)) {
876                 /*
877                  * kick failed.
878                  * release descriptors which were just prepared
879                  */
880                 netdev->stats.tx_dropped++;
881                 gelic_descr_release_tx(card, descr);
882                 gelic_descr_release_tx(card, descr->next);
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         }
889
890         spin_unlock_irqrestore(&card->tx_lock, flags);
891         return NETDEV_TX_OK;
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
898  * @netdev: net_device structure to be passed packet
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  */
903 static void gelic_net_pass_skb_up(struct gelic_descr *descr,
904                                   struct gelic_card *card,
905                                   struct net_device *netdev)
906
907 {
908         struct sk_buff *skb = descr->skb;
909         u32 data_status, data_error;
910
911         data_status = be32_to_cpu(descr->data_status);
912         data_error = be32_to_cpu(descr->data_error);
913         /* unmap skb buffer */
914         dma_unmap_single(ctodev(card), be32_to_cpu(descr->buf_addr),
915                          GELIC_NET_MAX_MTU,
916                          DMA_FROM_DEVICE);
917
918         skb_put(skb, be32_to_cpu(descr->valid_size)?
919                 be32_to_cpu(descr->valid_size) :
920                 be32_to_cpu(descr->result_size));
921         if (!descr->valid_size)
922                 dev_info(ctodev(card), "buffer full %x %x %x\n",
923                          be32_to_cpu(descr->result_size),
924                          be32_to_cpu(descr->buf_size),
925                          be32_to_cpu(descr->dmac_cmd_status));
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) {
937                 if ((data_status & GELIC_DESCR_DATA_STATUS_CHK_MASK) &&
938                     (!(data_error & GELIC_DESCR_DATA_ERROR_CHK_MASK)))
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 */
946         netdev->stats.rx_packets++;
947         netdev->stats.rx_bytes += skb->len;
948
949         /* pass skb up to stack */
950         netif_receive_skb(skb);
951 }
952
953 /**
954  * gelic_card_decode_one_descr - processes an rx descriptor
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  */
962 static int gelic_card_decode_one_descr(struct gelic_card *card)
963 {
964         enum gelic_descr_dma_status status;
965         struct gelic_descr_chain *chain = &card->rx_chain;
966         struct gelic_descr *descr = chain->head;
967         struct net_device *netdev = NULL;
968         int dmac_chain_ended;
969
970         status = gelic_descr_get_status(descr);
971         /* is this descriptor terminated with next_descr == NULL? */
972         dmac_chain_ended =
973                 be32_to_cpu(descr->dmac_cmd_status) &
974                 GELIC_DESCR_RX_DMA_CHAIN_END;
975
976         if (status == GELIC_DESCR_DMA_CARDOWNED)
977                 return 0;
978
979         if (status == GELIC_DESCR_DMA_NOT_IN_USE) {
980                 dev_dbg(ctodev(card), "dormant descr? %p\n", descr);
981                 return 0;
982         }
983
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
1002         if ((status == GELIC_DESCR_DMA_RESPONSE_ERROR) ||
1003             (status == GELIC_DESCR_DMA_PROTECTION_ERROR) ||
1004             (status == GELIC_DESCR_DMA_FORCE_END)) {
1005                 dev_info(ctodev(card), "dropping RX descriptor with state %x\n",
1006                          status);
1007                 netdev->stats.rx_dropped++;
1008                 goto refill;
1009         }
1010
1011         if (status == GELIC_DESCR_DMA_BUFFER_FULL) {
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          */
1028         if (status != GELIC_DESCR_DMA_FRAME_END) {
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 */
1035         gelic_net_pass_skb_up(descr, card, netdev);
1036 refill:
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;
1043
1044         /* change the descriptor state: */
1045         gelic_descr_set_status(descr, GELIC_DESCR_DMA_NOT_IN_USE);
1046
1047         /*
1048          * this call can fail, but for now, just leave this
1049          * decriptor without skb
1050          */
1051         gelic_descr_prepare_rx(card, descr);
1052
1053         chain->tail = descr;
1054         chain->head = descr->next;
1055
1056         /*
1057          * Set this descriptor the end of the chain.
1058          */
1059         descr->prev->next_descr_addr = cpu_to_be32(descr->bus_addr);
1060
1061         /*
1062          * If dmac chain was met, DMAC stopped.
1063          * thus re-enable it
1064          */
1065         if (dmac_chain_ended) {
1066                 card->rx_dma_restart_required = 1;
1067                 dev_dbg(ctodev(card), "reenable rx dma scheduled\n");
1068         }
1069
1070         return 1;
1071 }
1072
1073 /**
1074  * gelic_net_poll - NAPI poll function called by the stack to return packets
1075  * @napi: napi structure
1076  * @budget: number of packets we can pass to the stack at most
1077  *
1078  * returns the number of the processed packets
1079  *
1080  */
1081 static int gelic_net_poll(struct napi_struct *napi, int budget)
1082 {
1083         struct gelic_card *card = container_of(napi, struct gelic_card, napi);
1084         int packets_done = 0;
1085
1086         while (packets_done < budget) {
1087                 if (!gelic_card_decode_one_descr(card))
1088                         break;
1089
1090                 packets_done++;
1091         }
1092
1093         if (packets_done < budget) {
1094                 napi_complete(napi);
1095                 gelic_card_rx_irq_on(card);
1096         }
1097         return packets_done;
1098 }
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  */
1106 int gelic_net_change_mtu(struct net_device *netdev, int new_mtu)
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 /**
1119  * gelic_card_interrupt - event handler for gelic_net
1120  */
1121 static irqreturn_t gelic_card_interrupt(int irq, void *ptr)
1122 {
1123         unsigned long flags;
1124         struct gelic_card *card = ptr;
1125         u64 status;
1126
1127         status = card->irq_status;
1128
1129         if (!status)
1130                 return IRQ_NONE;
1131
1132         status &= card->irq_mask;
1133
1134         if (card->rx_dma_restart_required) {
1135                 card->rx_dma_restart_required = 0;
1136                 gelic_card_enable_rxdmac(card);
1137         }
1138
1139         if (status & GELIC_CARD_RXINT) {
1140                 gelic_card_rx_irq_off(card);
1141                 napi_schedule(&card->napi);
1142         }
1143
1144         if (status & GELIC_CARD_TXINT) {
1145                 spin_lock_irqsave(&card->tx_lock, flags);
1146                 card->tx_dma_progress = 0;
1147                 gelic_card_release_tx_chain(card, 0);
1148                 /* kick outstanding tx descriptor if any */
1149                 gelic_card_kick_txdma(card, card->tx_chain.tail);
1150                 spin_unlock_irqrestore(&card->tx_lock, flags);
1151         }
1152
1153         /* ether port status changed */
1154         if (status & GELIC_CARD_PORT_STATUS_CHANGED)
1155                 gelic_card_get_ether_port_status(card, 1);
1156
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  */
1167 void gelic_net_poll_controller(struct net_device *netdev)
1168 {
1169         struct gelic_card *card = netdev_card(netdev);
1170
1171         gelic_card_set_irq_mask(card, 0);
1172         gelic_card_interrupt(netdev->irq, netdev);
1173         gelic_card_set_irq_mask(card, card->irq_mask);
1174 }
1175 #endif /* CONFIG_NET_POLL_CONTROLLER */
1176
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  */
1186 int gelic_net_open(struct net_device *netdev)
1187 {
1188         struct gelic_card *card = netdev_card(netdev);
1189
1190         dev_dbg(ctodev(card), " -> %s %p\n", __func__, netdev);
1191
1192         gelic_card_up(card);
1193
1194         netif_start_queue(netdev);
1195         gelic_card_get_ether_port_status(card, 1);
1196
1197         dev_dbg(ctodev(card), " <- %s\n", __func__);
1198         return 0;
1199 }
1200
1201 void gelic_net_get_drvinfo(struct net_device *netdev,
1202                            struct ethtool_drvinfo *info)
1203 {
1204         strncpy(info->driver, DRV_NAME, sizeof(info->driver) - 1);
1205         strncpy(info->version, DRV_VERSION, sizeof(info->version) - 1);
1206 }
1207
1208 static int gelic_ether_get_settings(struct net_device *netdev,
1209                                     struct ethtool_cmd *cmd)
1210 {
1211         struct gelic_card *card = netdev_card(netdev);
1212
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;
1234         }
1235
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;
1241         cmd->autoneg = AUTONEG_ENABLE; /* always enabled */
1242         cmd->port = PORT_TP;
1243
1244         return 0;
1245 }
1246
1247 u32 gelic_net_get_rx_csum(struct net_device *netdev)
1248 {
1249         struct gelic_card *card = netdev_card(netdev);
1250
1251         return card->rx_csum;
1252 }
1253
1254 int gelic_net_set_rx_csum(struct net_device *netdev, u32 data)
1255 {
1256         struct gelic_card *card = netdev_card(netdev);
1257
1258         card->rx_csum = data;
1259         return 0;
1260 }
1261
1262 static struct ethtool_ops gelic_ether_ethtool_ops = {
1263         .get_drvinfo    = gelic_net_get_drvinfo,
1264         .get_settings   = gelic_ether_get_settings,
1265         .get_link       = ethtool_op_get_link,
1266         .get_tx_csum    = ethtool_op_get_tx_csum,
1267         .set_tx_csum    = ethtool_op_set_tx_csum,
1268         .get_rx_csum    = gelic_net_get_rx_csum,
1269         .set_rx_csum    = gelic_net_set_rx_csum,
1270 };
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  */
1279 static void gelic_net_tx_timeout_task(struct work_struct *work)
1280 {
1281         struct gelic_card *card =
1282                 container_of(work, struct gelic_card, tx_timeout_task);
1283         struct net_device *netdev = card->netdev[GELIC_PORT_ETHERNET];
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
1296 out:
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  */
1306 void gelic_net_tx_timeout(struct net_device *netdev)
1307 {
1308         struct gelic_card *card;
1309
1310         card = netdev_card(netdev);
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 /**
1319  * gelic_ether_setup_netdev_ops - initialization of net_device operations
1320  * @netdev: net_device structure
1321  *
1322  * fills out function pointers in the net_device structure
1323  */
1324 static void gelic_ether_setup_netdev_ops(struct net_device *netdev,
1325                                          struct napi_struct *napi)
1326 {
1327         netdev->open = &gelic_net_open;
1328         netdev->stop = &gelic_net_stop;
1329         netdev->hard_start_xmit = &gelic_net_xmit;
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;
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
1342 }
1343
1344 /**
1345  * gelic_ether_setup_netdev - initialization of net_device
1346  * @netdev: net_device structure
1347  * @card: card structure
1348  *
1349  * Returns 0 on success or <0 on failure
1350  *
1351  * gelic_ether_setup_netdev initializes the net_device structure
1352  * and register it.
1353  **/
1354 int gelic_net_setup_netdev(struct net_device *netdev, struct gelic_card *card)
1355 {
1356         int status;
1357         u64 v1, v2;
1358         DECLARE_MAC_BUF(mac);
1359
1360         netdev->features = NETIF_F_IP_CSUM;
1361
1362         status = lv1_net_control(bus_id(card), dev_id(card),
1363                                  GELIC_LV1_GET_MAC_ADDRESS,
1364                                  0, 0, 0, &v1, &v2);
1365         v1 <<= 16;
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         }
1372         memcpy(netdev->dev_addr, &v1, ETH_ALEN);
1373
1374         if (card->vlan_required) {
1375                 netdev->hard_header_len += VLAN_HLEN;
1376                 /*
1377                  * As vlan is internally used,
1378                  * we can not receive vlan packets
1379                  */
1380                 netdev->features |= NETIF_F_VLAN_CHALLENGED;
1381         }
1382
1383         status = register_netdev(netdev);
1384         if (status) {
1385                 dev_err(ctodev(card), "%s:Couldn't register %s %d\n",
1386                         __func__, netdev->name, status);
1387                 return status;
1388         }
1389         dev_info(ctodev(card), "%s: MAC addr %s\n",
1390                  netdev->name,
1391                  print_mac(mac, netdev->dev_addr));
1392
1393         return 0;
1394 }
1395
1396 /**
1397  * gelic_alloc_card_net - allocates net_device and card structure
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  */
1403 #define GELIC_ALIGN (32)
1404 static struct gelic_card *gelic_alloc_card_net(struct net_device **netdev)
1405 {
1406         struct gelic_card *card;
1407         struct gelic_port *port;
1408         void *p;
1409         size_t alloc_size;
1410         /*
1411          * gelic requires dma descriptor is 32 bytes aligned and
1412          * the hypervisor requires irq_status is 8 bytes aligned.
1413          */
1414         BUILD_BUG_ON(offsetof(struct gelic_card, irq_status) % 8);
1415         BUILD_BUG_ON(offsetof(struct gelic_card, descr) % 32);
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;
1421
1422         p  = kzalloc(alloc_size, GFP_KERNEL);
1423         if (!p)
1424                 return NULL;
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;
1445
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);
1449         init_MUTEX(&card->updown_lock);
1450         atomic_set(&card->users, 0);
1451
1452         return card;
1453 }
1454
1455 static 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 }
1526 /**
1527  * ps3_gelic_driver_probe - add a device to the control of this driver
1528  */
1529 static int ps3_gelic_driver_probe(struct ps3_system_bus_device *dev)
1530 {
1531         struct gelic_card *card;
1532         struct net_device *netdev;
1533         int result;
1534
1535         pr_debug("%s: called\n", __func__);
1536         result = ps3_open_hv_device(dev);
1537
1538         if (result) {
1539                 dev_dbg(&dev->core, "%s:ps3_open_hv_device failed\n",
1540                         __func__);
1541                 goto fail_open;
1542         }
1543
1544         result = ps3_dma_region_create(dev->d_region);
1545
1546         if (result) {
1547                 dev_dbg(&dev->core, "%s:ps3_dma_region_create failed(%d)\n",
1548                         __func__, result);
1549                 BUG_ON("check region type");
1550                 goto fail_dma_region;
1551         }
1552
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 */
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,
1575                         "%s:set_interrupt_status_indicator failed: %s\n",
1576                         __func__, ps3_result(result));
1577                 result = -EIO;
1578                 goto fail_status_indicator;
1579         }
1580
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;
1626
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);
1632         if (result) {
1633                 dev_dbg(&dev->core, "%s: setup_netdev failed %d",
1634                         __func__, result);
1635                 goto fail_setup_netdev;
1636         }
1637
1638         pr_debug("%s: done\n", __func__);
1639         return 0;
1640
1641 fail_setup_netdev:
1642 fail_alloc_skbs:
1643         gelic_card_free_chain(card, card->rx_chain.head);
1644 fail_alloc_rx:
1645         gelic_card_free_chain(card, card->tx_chain.head);
1646 fail_alloc_tx:
1647         free_irq(card->irq, card);
1648         netdev->irq = NO_IRQ;
1649 fail_request_irq:
1650         ps3_sb_event_receive_port_destroy(dev, card->irq);
1651 fail_alloc_irq:
1652         lv1_net_set_interrupt_status_indicator(bus_id(card),
1653                                                bus_id(card),
1654                                                0, 0);
1655 fail_status_indicator:
1656         ps3_system_bus_set_driver_data(dev, NULL);
1657         kfree(netdev_card(netdev)->unalign);
1658         free_netdev(netdev);
1659 fail_alloc_card:
1660         ps3_dma_region_free(dev->d_region);
1661 fail_dma_region:
1662         ps3_close_hv_device(dev);
1663 fail_open:
1664         return result;
1665 }
1666
1667 /**
1668  * ps3_gelic_driver_remove - remove a device from the control of this driver
1669  */
1670
1671 static int ps3_gelic_driver_remove(struct ps3_system_bus_device *dev)
1672 {
1673         struct gelic_card *card = ps3_system_bus_get_driver_data(dev);
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);
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
1703         unregister_netdev(netdev0);
1704         kfree(netdev_card(netdev0)->unalign);
1705         free_netdev(netdev0);
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
1713         pr_debug("%s: done\n", __func__);
1714         return 0;
1715 }
1716
1717 static 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
1726 static 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
1733 static void __exit ps3_gelic_driver_exit (void)
1734 {
1735         ps3_system_bus_driver_unregister(&ps3_gelic_driver);
1736 }
1737
1738 module_init(ps3_gelic_driver_init);
1739 module_exit(ps3_gelic_driver_exit);
1740
1741 MODULE_ALIAS(PS3_MODULE_ALIAS_GELIC);
1742