Merge git://git.infradead.org/embedded-2.6
[linux-2.6] / drivers / infiniband / hw / nes / nes_nic.c
1 /*
2  * Copyright (c) 2006 - 2008 NetEffect, Inc. All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  *
32  */
33
34 #include <linux/module.h>
35 #include <linux/moduleparam.h>
36 #include <linux/netdevice.h>
37 #include <linux/etherdevice.h>
38 #include <linux/ip.h>
39 #include <linux/tcp.h>
40 #include <linux/if_arp.h>
41 #include <linux/if_vlan.h>
42 #include <linux/ethtool.h>
43 #include <net/tcp.h>
44
45 #include <net/inet_common.h>
46 #include <linux/inet.h>
47
48 #include "nes.h"
49
50 static struct nic_qp_map nic_qp_mapping_0[] = {
51         {16,0,0,1},{24,4,0,0},{28,8,0,0},{32,12,0,0},
52         {20,2,2,1},{26,6,2,0},{30,10,2,0},{34,14,2,0},
53         {18,1,1,1},{25,5,1,0},{29,9,1,0},{33,13,1,0},
54         {22,3,3,1},{27,7,3,0},{31,11,3,0},{35,15,3,0}
55 };
56
57 static struct nic_qp_map nic_qp_mapping_1[] = {
58         {18,1,1,1},{25,5,1,0},{29,9,1,0},{33,13,1,0},
59         {22,3,3,1},{27,7,3,0},{31,11,3,0},{35,15,3,0}
60 };
61
62 static struct nic_qp_map nic_qp_mapping_2[] = {
63         {20,2,2,1},{26,6,2,0},{30,10,2,0},{34,14,2,0}
64 };
65
66 static struct nic_qp_map nic_qp_mapping_3[] = {
67         {22,3,3,1},{27,7,3,0},{31,11,3,0},{35,15,3,0}
68 };
69
70 static struct nic_qp_map nic_qp_mapping_4[] = {
71         {28,8,0,0},{32,12,0,0}
72 };
73
74 static struct nic_qp_map nic_qp_mapping_5[] = {
75         {29,9,1,0},{33,13,1,0}
76 };
77
78 static struct nic_qp_map nic_qp_mapping_6[] = {
79         {30,10,2,0},{34,14,2,0}
80 };
81
82 static struct nic_qp_map nic_qp_mapping_7[] = {
83         {31,11,3,0},{35,15,3,0}
84 };
85
86 static struct nic_qp_map *nic_qp_mapping_per_function[] = {
87         nic_qp_mapping_0, nic_qp_mapping_1, nic_qp_mapping_2, nic_qp_mapping_3,
88         nic_qp_mapping_4, nic_qp_mapping_5, nic_qp_mapping_6, nic_qp_mapping_7
89 };
90
91 static const u32 default_msg = NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK
92                 | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN;
93 static int debug = -1;
94
95 /**
96  * nes_netdev_poll
97  */
98 static int nes_netdev_poll(struct napi_struct *napi, int budget)
99 {
100         struct nes_vnic *nesvnic = container_of(napi, struct nes_vnic, napi);
101         struct net_device *netdev = nesvnic->netdev;
102         struct nes_device *nesdev = nesvnic->nesdev;
103         struct nes_hw_nic_cq *nescq = &nesvnic->nic_cq;
104
105         nesvnic->budget = budget;
106         nescq->cqes_pending = 0;
107         nescq->rx_cqes_completed = 0;
108         nescq->cqe_allocs_pending = 0;
109         nescq->rx_pkts_indicated = 0;
110
111         nes_nic_ce_handler(nesdev, nescq);
112
113         if (nescq->cqes_pending == 0) {
114                 netif_rx_complete(netdev, napi);
115                 /* clear out completed cqes and arm */
116                 nes_write32(nesdev->regs+NES_CQE_ALLOC, NES_CQE_ALLOC_NOTIFY_NEXT |
117                                 nescq->cq_number | (nescq->cqe_allocs_pending << 16));
118                 nes_read32(nesdev->regs+NES_CQE_ALLOC);
119         } else {
120                 /* clear out completed cqes but don't arm */
121                 nes_write32(nesdev->regs+NES_CQE_ALLOC,
122                                 nescq->cq_number | (nescq->cqe_allocs_pending << 16));
123                 nes_debug(NES_DBG_NETDEV, "%s: exiting with work pending\n",
124                                 nesvnic->netdev->name);
125         }
126         return nescq->rx_pkts_indicated;
127 }
128
129
130 /**
131  * nes_netdev_open - Activate the network interface; ifconfig
132  * ethx up.
133  */
134 static int nes_netdev_open(struct net_device *netdev)
135 {
136         u32 macaddr_low;
137         u16 macaddr_high;
138         struct nes_vnic *nesvnic = netdev_priv(netdev);
139         struct nes_device *nesdev = nesvnic->nesdev;
140         int ret;
141         int i;
142         struct nes_vnic *first_nesvnic = NULL;
143         u32 nic_active_bit;
144         u32 nic_active;
145         struct list_head *list_pos, *list_temp;
146
147         assert(nesdev != NULL);
148
149         if (nesvnic->netdev_open == 1)
150                 return 0;
151
152         if (netif_msg_ifup(nesvnic))
153                 printk(KERN_INFO PFX "%s: enabling interface\n", netdev->name);
154
155         ret = nes_init_nic_qp(nesdev, netdev);
156         if (ret) {
157                 return ret;
158         }
159
160         netif_carrier_off(netdev);
161         netif_stop_queue(netdev);
162
163         if ((!nesvnic->of_device_registered) && (nesvnic->rdma_enabled)) {
164                 nesvnic->nesibdev = nes_init_ofa_device(netdev);
165                 if (nesvnic->nesibdev == NULL) {
166                         printk(KERN_ERR PFX "%s: nesvnic->nesibdev alloc failed", netdev->name);
167                 } else {
168                         nesvnic->nesibdev->nesvnic = nesvnic;
169                         ret = nes_register_ofa_device(nesvnic->nesibdev);
170                         if (ret) {
171                                 printk(KERN_ERR PFX "%s: Unable to register RDMA device, ret = %d\n",
172                                                 netdev->name, ret);
173                         }
174                 }
175         }
176         /* Set packet filters */
177         nic_active_bit = 1 << nesvnic->nic_index;
178         nic_active = nes_read_indexed(nesdev, NES_IDX_NIC_ACTIVE);
179         nic_active |= nic_active_bit;
180         nes_write_indexed(nesdev, NES_IDX_NIC_ACTIVE, nic_active);
181         nic_active = nes_read_indexed(nesdev, NES_IDX_NIC_MULTICAST_ENABLE);
182         nic_active |= nic_active_bit;
183         nes_write_indexed(nesdev, NES_IDX_NIC_MULTICAST_ENABLE, nic_active);
184         nic_active = nes_read_indexed(nesdev, NES_IDX_NIC_BROADCAST_ON);
185         nic_active |= nic_active_bit;
186         nes_write_indexed(nesdev, NES_IDX_NIC_BROADCAST_ON, nic_active);
187
188         macaddr_high  = ((u16)netdev->dev_addr[0]) << 8;
189         macaddr_high += (u16)netdev->dev_addr[1];
190
191         macaddr_low   = ((u32)netdev->dev_addr[2]) << 24;
192         macaddr_low  += ((u32)netdev->dev_addr[3]) << 16;
193         macaddr_low  += ((u32)netdev->dev_addr[4]) << 8;
194         macaddr_low  += (u32)netdev->dev_addr[5];
195
196         /* Program the various MAC regs */
197         for (i = 0; i < NES_MAX_PORT_COUNT; i++) {
198                 if (nesvnic->qp_nic_index[i] == 0xf) {
199                         break;
200                 }
201                 nes_debug(NES_DBG_NETDEV, "i=%d, perfect filter table index= %d, PERF FILTER LOW"
202                                 " (Addr:%08X) = %08X, HIGH = %08X.\n",
203                                 i, nesvnic->qp_nic_index[i],
204                                 NES_IDX_PERFECT_FILTER_LOW+((nesvnic->perfect_filter_index + i) * 8),
205                                 macaddr_low,
206                                 (u32)macaddr_high | NES_MAC_ADDR_VALID |
207                                 ((((u32)nesvnic->nic_index) << 16)));
208                 nes_write_indexed(nesdev,
209                                 NES_IDX_PERFECT_FILTER_LOW + (nesvnic->qp_nic_index[i] * 8),
210                                 macaddr_low);
211                 nes_write_indexed(nesdev,
212                                 NES_IDX_PERFECT_FILTER_HIGH + (nesvnic->qp_nic_index[i] * 8),
213                                 (u32)macaddr_high | NES_MAC_ADDR_VALID |
214                                 ((((u32)nesvnic->nic_index) << 16)));
215         }
216
217
218         nes_write32(nesdev->regs+NES_CQE_ALLOC, NES_CQE_ALLOC_NOTIFY_NEXT |
219                         nesvnic->nic_cq.cq_number);
220         nes_read32(nesdev->regs+NES_CQE_ALLOC);
221         list_for_each_safe(list_pos, list_temp, &nesdev->nesadapter->nesvnic_list[nesdev->mac_index]) {
222                 first_nesvnic = container_of(list_pos, struct nes_vnic, list);
223                 if (first_nesvnic->netdev_open == 1)
224                         break;
225         }
226         if (first_nesvnic->netdev_open == 0) {
227                 nes_debug(NES_DBG_INIT, "Setting up MAC interrupt mask.\n");
228                 nes_write_indexed(nesdev, NES_IDX_MAC_INT_MASK + (0x200 * nesdev->mac_index),
229                                 ~(NES_MAC_INT_LINK_STAT_CHG | NES_MAC_INT_XGMII_EXT |
230                                 NES_MAC_INT_TX_UNDERFLOW | NES_MAC_INT_TX_ERROR));
231                 first_nesvnic = nesvnic;
232         }
233         if (first_nesvnic->linkup) {
234                 /* Enable network packets */
235                 nesvnic->linkup = 1;
236                 netif_start_queue(netdev);
237                 netif_carrier_on(netdev);
238         }
239         napi_enable(&nesvnic->napi);
240         nesvnic->netdev_open = 1;
241
242         return 0;
243 }
244
245
246 /**
247  * nes_netdev_stop
248  */
249 static int nes_netdev_stop(struct net_device *netdev)
250 {
251         struct nes_vnic *nesvnic = netdev_priv(netdev);
252         struct nes_device *nesdev = nesvnic->nesdev;
253         u32 nic_active_mask;
254         u32 nic_active;
255         struct nes_vnic *first_nesvnic = NULL;
256         struct list_head *list_pos, *list_temp;
257
258         nes_debug(NES_DBG_SHUTDOWN, "nesvnic=%p, nesdev=%p, netdev=%p %s\n",
259                         nesvnic, nesdev, netdev, netdev->name);
260         if (nesvnic->netdev_open == 0)
261                 return 0;
262
263         if (netif_msg_ifdown(nesvnic))
264                 printk(KERN_INFO PFX "%s: disabling interface\n", netdev->name);
265
266         /* Disable network packets */
267         napi_disable(&nesvnic->napi);
268         netif_stop_queue(netdev);
269         list_for_each_safe(list_pos, list_temp, &nesdev->nesadapter->nesvnic_list[nesdev->mac_index]) {
270                 first_nesvnic = container_of(list_pos, struct nes_vnic, list);
271                 if ((first_nesvnic->netdev_open == 1) && (first_nesvnic != nesvnic))
272                         break;
273         }
274
275         if (first_nesvnic->netdev_open == 0)
276                 nes_write_indexed(nesdev, NES_IDX_MAC_INT_MASK+(0x200*nesdev->mac_index), 0xffffffff);
277         else if ((first_nesvnic != nesvnic) &&
278                  (PCI_FUNC(first_nesvnic->nesdev->pcidev->devfn) != PCI_FUNC(nesvnic->nesdev->pcidev->devfn))) {
279                 nes_write_indexed(nesdev, NES_IDX_MAC_INT_MASK + (0x200 * nesdev->mac_index), 0xffffffff);
280                 nes_write_indexed(first_nesvnic->nesdev, NES_IDX_MAC_INT_MASK + (0x200 * first_nesvnic->nesdev->mac_index),
281                                 ~(NES_MAC_INT_LINK_STAT_CHG | NES_MAC_INT_XGMII_EXT |
282                                 NES_MAC_INT_TX_UNDERFLOW | NES_MAC_INT_TX_ERROR));
283         }
284
285         nic_active_mask = ~((u32)(1 << nesvnic->nic_index));
286         nes_write_indexed(nesdev, NES_IDX_PERFECT_FILTER_HIGH+
287                         (nesvnic->perfect_filter_index*8), 0);
288         nic_active = nes_read_indexed(nesdev, NES_IDX_NIC_ACTIVE);
289         nic_active &= nic_active_mask;
290         nes_write_indexed(nesdev, NES_IDX_NIC_ACTIVE, nic_active);
291         nic_active = nes_read_indexed(nesdev, NES_IDX_NIC_MULTICAST_ALL);
292         nic_active &= nic_active_mask;
293         nes_write_indexed(nesdev, NES_IDX_NIC_MULTICAST_ALL, nic_active);
294         nic_active = nes_read_indexed(nesdev, NES_IDX_NIC_MULTICAST_ENABLE);
295         nic_active &= nic_active_mask;
296         nes_write_indexed(nesdev, NES_IDX_NIC_MULTICAST_ENABLE, nic_active);
297         nic_active = nes_read_indexed(nesdev, NES_IDX_NIC_UNICAST_ALL);
298         nic_active &= nic_active_mask;
299         nes_write_indexed(nesdev, NES_IDX_NIC_UNICAST_ALL, nic_active);
300         nic_active = nes_read_indexed(nesdev, NES_IDX_NIC_BROADCAST_ON);
301         nic_active &= nic_active_mask;
302         nes_write_indexed(nesdev, NES_IDX_NIC_BROADCAST_ON, nic_active);
303
304
305         if (nesvnic->of_device_registered) {
306                 nes_destroy_ofa_device(nesvnic->nesibdev);
307                 nesvnic->nesibdev = NULL;
308                 nesvnic->of_device_registered = 0;
309         }
310         nes_destroy_nic_qp(nesvnic);
311
312         nesvnic->netdev_open = 0;
313
314         return 0;
315 }
316
317
318 /**
319  * nes_nic_send
320  */
321 static int nes_nic_send(struct sk_buff *skb, struct net_device *netdev)
322 {
323         struct nes_vnic *nesvnic = netdev_priv(netdev);
324         struct nes_device *nesdev = nesvnic->nesdev;
325         struct nes_hw_nic *nesnic = &nesvnic->nic;
326         struct nes_hw_nic_sq_wqe *nic_sqe;
327         struct tcphdr *tcph;
328         __le16 *wqe_fragment_length;
329         u32 wqe_misc;
330         u16 wqe_fragment_index = 1;     /* first fragment (0) is used by copy buffer */
331         u16 skb_fragment_index;
332         dma_addr_t bus_address;
333
334         nic_sqe = &nesnic->sq_vbase[nesnic->sq_head];
335         wqe_fragment_length = (__le16 *)&nic_sqe->wqe_words[NES_NIC_SQ_WQE_LENGTH_0_TAG_IDX];
336
337         /* setup the VLAN tag if present */
338         if (vlan_tx_tag_present(skb)) {
339                 nes_debug(NES_DBG_NIC_TX, "%s: VLAN packet to send... VLAN = %08X\n",
340                                 netdev->name, vlan_tx_tag_get(skb));
341                 wqe_misc = NES_NIC_SQ_WQE_TAGVALUE_ENABLE;
342                 wqe_fragment_length[0] = (__force __le16) vlan_tx_tag_get(skb);
343         } else
344                 wqe_misc = 0;
345
346         /* bump past the vlan tag */
347         wqe_fragment_length++;
348         /*      wqe_fragment_address = (u64 *)&nic_sqe->wqe_words[NES_NIC_SQ_WQE_FRAG0_LOW_IDX]; */
349
350         if (skb->ip_summed == CHECKSUM_PARTIAL) {
351                 tcph = tcp_hdr(skb);
352                 if (1) {
353                         if (skb_is_gso(skb)) {
354                                 /* nes_debug(NES_DBG_NIC_TX, "%s: TSO request... seg size = %u\n",
355                                                 netdev->name, skb_is_gso(skb)); */
356                                 wqe_misc |= NES_NIC_SQ_WQE_LSO_ENABLE |
357                                                 NES_NIC_SQ_WQE_COMPLETION | (u16)skb_is_gso(skb);
358                                 set_wqe_32bit_value(nic_sqe->wqe_words, NES_NIC_SQ_WQE_LSO_INFO_IDX,
359                                                 ((u32)tcph->doff) |
360                                                 (((u32)(((unsigned char *)tcph) - skb->data)) << 4));
361                         } else {
362                                 wqe_misc |= NES_NIC_SQ_WQE_COMPLETION;
363                         }
364                 }
365         } else {        /* CHECKSUM_HW */
366                 wqe_misc |= NES_NIC_SQ_WQE_DISABLE_CHKSUM | NES_NIC_SQ_WQE_COMPLETION;
367         }
368
369         set_wqe_32bit_value(nic_sqe->wqe_words, NES_NIC_SQ_WQE_TOTAL_LENGTH_IDX,
370                                 skb->len);
371         memcpy(&nesnic->first_frag_vbase[nesnic->sq_head].buffer,
372                         skb->data, min(((unsigned int)NES_FIRST_FRAG_SIZE), skb_headlen(skb)));
373         wqe_fragment_length[0] = cpu_to_le16(min(((unsigned int)NES_FIRST_FRAG_SIZE),
374                         skb_headlen(skb)));
375         wqe_fragment_length[1] = 0;
376         if (skb_headlen(skb) > NES_FIRST_FRAG_SIZE) {
377                 if ((skb_shinfo(skb)->nr_frags + 1) > 4) {
378                         nes_debug(NES_DBG_NIC_TX, "%s: Packet with %u fragments not sent, skb_headlen=%u\n",
379                                         netdev->name, skb_shinfo(skb)->nr_frags + 2, skb_headlen(skb));
380                         kfree_skb(skb);
381                         nesvnic->tx_sw_dropped++;
382                         return NETDEV_TX_LOCKED;
383                 }
384                 set_bit(nesnic->sq_head, nesnic->first_frag_overflow);
385                 bus_address = pci_map_single(nesdev->pcidev, skb->data + NES_FIRST_FRAG_SIZE,
386                                 skb_headlen(skb) - NES_FIRST_FRAG_SIZE, PCI_DMA_TODEVICE);
387                 wqe_fragment_length[wqe_fragment_index++] =
388                                 cpu_to_le16(skb_headlen(skb) - NES_FIRST_FRAG_SIZE);
389                 wqe_fragment_length[wqe_fragment_index] = 0;
390                 set_wqe_64bit_value(nic_sqe->wqe_words, NES_NIC_SQ_WQE_FRAG1_LOW_IDX,
391                                 ((u64)(bus_address)));
392                 nesnic->tx_skb[nesnic->sq_head] = skb;
393         }
394
395         if (skb_headlen(skb) == skb->len) {
396                 if (skb_headlen(skb) <= NES_FIRST_FRAG_SIZE) {
397                         nic_sqe->wqe_words[NES_NIC_SQ_WQE_LENGTH_2_1_IDX] = 0;
398                         nesnic->tx_skb[nesnic->sq_head] = NULL;
399                         dev_kfree_skb(skb);
400                 }
401         } else {
402                 /* Deal with Fragments */
403                 nesnic->tx_skb[nesnic->sq_head] = skb;
404                 for (skb_fragment_index = 0; skb_fragment_index < skb_shinfo(skb)->nr_frags;
405                                 skb_fragment_index++) {
406                         bus_address = pci_map_page( nesdev->pcidev,
407                                         skb_shinfo(skb)->frags[skb_fragment_index].page,
408                                         skb_shinfo(skb)->frags[skb_fragment_index].page_offset,
409                                         skb_shinfo(skb)->frags[skb_fragment_index].size,
410                                         PCI_DMA_TODEVICE);
411                         wqe_fragment_length[wqe_fragment_index] =
412                                         cpu_to_le16(skb_shinfo(skb)->frags[skb_fragment_index].size);
413                         set_wqe_64bit_value(nic_sqe->wqe_words, NES_NIC_SQ_WQE_FRAG0_LOW_IDX+(2*wqe_fragment_index),
414                                 bus_address);
415                         wqe_fragment_index++;
416                         if (wqe_fragment_index < 5)
417                                 wqe_fragment_length[wqe_fragment_index] = 0;
418                 }
419         }
420
421         set_wqe_32bit_value(nic_sqe->wqe_words, NES_NIC_SQ_WQE_MISC_IDX, wqe_misc);
422         nesnic->sq_head++;
423         nesnic->sq_head &= nesnic->sq_size - 1;
424
425         return NETDEV_TX_OK;
426 }
427
428
429 /**
430  * nes_netdev_start_xmit
431  */
432 static int nes_netdev_start_xmit(struct sk_buff *skb, struct net_device *netdev)
433 {
434         struct nes_vnic *nesvnic = netdev_priv(netdev);
435         struct nes_device *nesdev = nesvnic->nesdev;
436         struct nes_hw_nic *nesnic = &nesvnic->nic;
437         struct nes_hw_nic_sq_wqe *nic_sqe;
438         struct tcphdr *tcph;
439         /* struct udphdr *udph; */
440 #define NES_MAX_TSO_FRAGS 18
441         /* 64K segment plus overflow on each side */
442         dma_addr_t tso_bus_address[NES_MAX_TSO_FRAGS];
443         dma_addr_t bus_address;
444         u32 tso_frag_index;
445         u32 tso_frag_count;
446         u32 tso_wqe_length;
447         u32 curr_tcp_seq;
448         u32 wqe_count=1;
449         u32 send_rc;
450         struct iphdr *iph;
451         unsigned long flags;
452         __le16 *wqe_fragment_length;
453         u32 nr_frags;
454         u32 original_first_length;
455         /* u64 *wqe_fragment_address; */
456         /* first fragment (0) is used by copy buffer */
457         u16 wqe_fragment_index=1;
458         u16 hoffset;
459         u16 nhoffset;
460         u16 wqes_needed;
461         u16 wqes_available;
462         u32 old_head;
463         u32 wqe_misc;
464
465         /*
466          * nes_debug(NES_DBG_NIC_TX, "%s Request to tx NIC packet length %u, headlen %u,"
467          *              " (%u frags), tso_size=%u\n",
468          *              netdev->name, skb->len, skb_headlen(skb),
469          *              skb_shinfo(skb)->nr_frags, skb_is_gso(skb));
470          */
471
472         if (!netif_carrier_ok(netdev))
473                 return NETDEV_TX_OK;
474
475         if (netif_queue_stopped(netdev))
476                 return NETDEV_TX_BUSY;
477
478         local_irq_save(flags);
479         if (!spin_trylock(&nesnic->sq_lock)) {
480                 local_irq_restore(flags);
481                 nesvnic->sq_locked++;
482                 return NETDEV_TX_LOCKED;
483         }
484
485         /* Check if SQ is full */
486         if ((((nesnic->sq_tail+(nesnic->sq_size*2))-nesnic->sq_head) & (nesnic->sq_size - 1)) == 1) {
487                 if (!netif_queue_stopped(netdev)) {
488                         netif_stop_queue(netdev);
489                         barrier();
490                         if ((((((volatile u16)nesnic->sq_tail)+(nesnic->sq_size*2))-nesnic->sq_head) & (nesnic->sq_size - 1)) != 1) {
491                                 netif_start_queue(netdev);
492                                 goto sq_no_longer_full;
493                         }
494                 }
495                 nesvnic->sq_full++;
496                 spin_unlock_irqrestore(&nesnic->sq_lock, flags);
497                 return NETDEV_TX_BUSY;
498         }
499
500 sq_no_longer_full:
501         nr_frags = skb_shinfo(skb)->nr_frags;
502         if (skb_headlen(skb) > NES_FIRST_FRAG_SIZE) {
503                 nr_frags++;
504         }
505         /* Check if too many fragments */
506         if (unlikely((nr_frags > 4))) {
507                 if (skb_is_gso(skb)) {
508                         nesvnic->segmented_tso_requests++;
509                         nesvnic->tso_requests++;
510                         old_head = nesnic->sq_head;
511                         /* Basically 4 fragments available per WQE with extended fragments */
512                         wqes_needed = nr_frags >> 2;
513                         wqes_needed += (nr_frags&3)?1:0;
514                         wqes_available = (((nesnic->sq_tail+nesnic->sq_size)-nesnic->sq_head) - 1) &
515                                         (nesnic->sq_size - 1);
516
517                         if (unlikely(wqes_needed > wqes_available)) {
518                                 if (!netif_queue_stopped(netdev)) {
519                                         netif_stop_queue(netdev);
520                                         barrier();
521                                         wqes_available = (((((volatile u16)nesnic->sq_tail)+nesnic->sq_size)-nesnic->sq_head) - 1) &
522                                                 (nesnic->sq_size - 1);
523                                         if (wqes_needed <= wqes_available) {
524                                                 netif_start_queue(netdev);
525                                                 goto tso_sq_no_longer_full;
526                                         }
527                                 }
528                                 nesvnic->sq_full++;
529                                 spin_unlock_irqrestore(&nesnic->sq_lock, flags);
530                                 nes_debug(NES_DBG_NIC_TX, "%s: HNIC SQ full- TSO request has too many frags!\n",
531                                                 netdev->name);
532                                 return NETDEV_TX_BUSY;
533                         }
534 tso_sq_no_longer_full:
535                         /* Map all the buffers */
536                         for (tso_frag_count=0; tso_frag_count < skb_shinfo(skb)->nr_frags;
537                                         tso_frag_count++) {
538                                 tso_bus_address[tso_frag_count] = pci_map_page( nesdev->pcidev,
539                                                 skb_shinfo(skb)->frags[tso_frag_count].page,
540                                                 skb_shinfo(skb)->frags[tso_frag_count].page_offset,
541                                                 skb_shinfo(skb)->frags[tso_frag_count].size,
542                                                 PCI_DMA_TODEVICE);
543                         }
544
545                         tso_frag_index = 0;
546                         curr_tcp_seq = ntohl(tcp_hdr(skb)->seq);
547                         hoffset = skb_transport_header(skb) - skb->data;
548                         nhoffset = skb_network_header(skb) - skb->data;
549                         original_first_length = hoffset + ((((struct tcphdr *)skb_transport_header(skb))->doff)<<2);
550
551                         for (wqe_count=0; wqe_count<((u32)wqes_needed); wqe_count++) {
552                                 tso_wqe_length = 0;
553                                 nic_sqe = &nesnic->sq_vbase[nesnic->sq_head];
554                                 wqe_fragment_length =
555                                                 (__le16 *)&nic_sqe->wqe_words[NES_NIC_SQ_WQE_LENGTH_0_TAG_IDX];
556                                 /* setup the VLAN tag if present */
557                                 if (vlan_tx_tag_present(skb)) {
558                                         nes_debug(NES_DBG_NIC_TX, "%s: VLAN packet to send... VLAN = %08X\n",
559                                                         netdev->name, vlan_tx_tag_get(skb) );
560                                         wqe_misc = NES_NIC_SQ_WQE_TAGVALUE_ENABLE;
561                                         wqe_fragment_length[0] = (__force __le16) vlan_tx_tag_get(skb);
562                                 } else
563                                         wqe_misc = 0;
564
565                                 /* bump past the vlan tag */
566                                 wqe_fragment_length++;
567
568                                 /* Assumes header totally fits in allocated buffer and is in first fragment */
569                                 if (original_first_length > NES_FIRST_FRAG_SIZE) {
570                                         nes_debug(NES_DBG_NIC_TX, "ERROR: SKB header too big, headlen=%u, FIRST_FRAG_SIZE=%u\n",
571                                                         original_first_length, NES_FIRST_FRAG_SIZE);
572                                         nes_debug(NES_DBG_NIC_TX, "%s Request to tx NIC packet length %u, headlen %u,"
573                                                         " (%u frags), tso_size=%u\n",
574                                                         netdev->name,
575                                                         skb->len, skb_headlen(skb),
576                                                         skb_shinfo(skb)->nr_frags, skb_is_gso(skb));
577                                 }
578                                 memcpy(&nesnic->first_frag_vbase[nesnic->sq_head].buffer,
579                                                 skb->data, min(((unsigned int)NES_FIRST_FRAG_SIZE),
580                                                 original_first_length));
581                                 iph = (struct iphdr *)
582                                 (&nesnic->first_frag_vbase[nesnic->sq_head].buffer[nhoffset]);
583                                 tcph = (struct tcphdr *)
584                                 (&nesnic->first_frag_vbase[nesnic->sq_head].buffer[hoffset]);
585                                 if ((wqe_count+1)!=(u32)wqes_needed) {
586                                         tcph->fin = 0;
587                                         tcph->psh = 0;
588                                         tcph->rst = 0;
589                                         tcph->urg = 0;
590                                 }
591                                 if (wqe_count) {
592                                         tcph->syn = 0;
593                                 }
594                                 tcph->seq = htonl(curr_tcp_seq);
595                                 wqe_fragment_length[0] = cpu_to_le16(min(((unsigned int)NES_FIRST_FRAG_SIZE),
596                                                 original_first_length));
597
598                                 wqe_fragment_index = 1;
599                                 if ((wqe_count==0) && (skb_headlen(skb) > original_first_length)) {
600                                         set_bit(nesnic->sq_head, nesnic->first_frag_overflow);
601                                         bus_address = pci_map_single(nesdev->pcidev, skb->data + original_first_length,
602                                                         skb_headlen(skb) - original_first_length, PCI_DMA_TODEVICE);
603                                         wqe_fragment_length[wqe_fragment_index++] =
604                                                 cpu_to_le16(skb_headlen(skb) - original_first_length);
605                                         wqe_fragment_length[wqe_fragment_index] = 0;
606                                         set_wqe_64bit_value(nic_sqe->wqe_words, NES_NIC_SQ_WQE_FRAG1_LOW_IDX,
607                                                                         bus_address);
608                                 }
609                                 while (wqe_fragment_index < 5) {
610                                         wqe_fragment_length[wqe_fragment_index] =
611                                                         cpu_to_le16(skb_shinfo(skb)->frags[tso_frag_index].size);
612                                         set_wqe_64bit_value(nic_sqe->wqe_words, NES_NIC_SQ_WQE_FRAG0_LOW_IDX+(2*wqe_fragment_index),
613                                                 (u64)tso_bus_address[tso_frag_index]);
614                                         wqe_fragment_index++;
615                                         tso_wqe_length += skb_shinfo(skb)->frags[tso_frag_index++].size;
616                                         if (wqe_fragment_index < 5)
617                                                 wqe_fragment_length[wqe_fragment_index] = 0;
618                                         if (tso_frag_index == tso_frag_count)
619                                                 break;
620                                 }
621                                 if ((wqe_count+1) == (u32)wqes_needed) {
622                                         nesnic->tx_skb[nesnic->sq_head] = skb;
623                                 } else {
624                                         nesnic->tx_skb[nesnic->sq_head] = NULL;
625                                 }
626                                 wqe_misc |= NES_NIC_SQ_WQE_COMPLETION | (u16)skb_is_gso(skb);
627                                 if ((tso_wqe_length + original_first_length) > skb_is_gso(skb)) {
628                                         wqe_misc |= NES_NIC_SQ_WQE_LSO_ENABLE;
629                                 } else {
630                                         iph->tot_len = htons(tso_wqe_length + original_first_length - nhoffset);
631                                 }
632
633                                 set_wqe_32bit_value(nic_sqe->wqe_words, NES_NIC_SQ_WQE_MISC_IDX,
634                                                  wqe_misc);
635                                 set_wqe_32bit_value(nic_sqe->wqe_words, NES_NIC_SQ_WQE_LSO_INFO_IDX,
636                                                 ((u32)tcph->doff) | (((u32)hoffset) << 4));
637
638                                 set_wqe_32bit_value(nic_sqe->wqe_words, NES_NIC_SQ_WQE_TOTAL_LENGTH_IDX,
639                                                 tso_wqe_length + original_first_length);
640                                 curr_tcp_seq += tso_wqe_length;
641                                 nesnic->sq_head++;
642                                 nesnic->sq_head &= nesnic->sq_size-1;
643                         }
644                 } else {
645                         nesvnic->linearized_skbs++;
646                         hoffset = skb_transport_header(skb) - skb->data;
647                         nhoffset = skb_network_header(skb) - skb->data;
648                         skb_linearize(skb);
649                         skb_set_transport_header(skb, hoffset);
650                         skb_set_network_header(skb, nhoffset);
651                         send_rc = nes_nic_send(skb, netdev);
652                         if (send_rc != NETDEV_TX_OK) {
653                                 spin_unlock_irqrestore(&nesnic->sq_lock, flags);
654                                 return NETDEV_TX_OK;
655                         }
656                 }
657         } else {
658                 send_rc = nes_nic_send(skb, netdev);
659                 if (send_rc != NETDEV_TX_OK) {
660                         spin_unlock_irqrestore(&nesnic->sq_lock, flags);
661                         return NETDEV_TX_OK;
662                 }
663         }
664
665         barrier();
666
667         if (wqe_count)
668                 nes_write32(nesdev->regs+NES_WQE_ALLOC,
669                                 (wqe_count << 24) | (1 << 23) | nesvnic->nic.qp_id);
670
671         netdev->trans_start = jiffies;
672         spin_unlock_irqrestore(&nesnic->sq_lock, flags);
673
674         return NETDEV_TX_OK;
675 }
676
677
678 /**
679  * nes_netdev_get_stats
680  */
681 static struct net_device_stats *nes_netdev_get_stats(struct net_device *netdev)
682 {
683         struct nes_vnic *nesvnic = netdev_priv(netdev);
684         struct nes_device *nesdev = nesvnic->nesdev;
685         u64 u64temp;
686         u32 u32temp;
687
688         u32temp = nes_read_indexed(nesdev,
689                         NES_IDX_ENDNODE0_NSTAT_RX_DISCARD + (nesvnic->nic_index*0x200));
690         nesvnic->netstats.rx_dropped += u32temp;
691         nesvnic->endnode_nstat_rx_discard += u32temp;
692
693         u64temp = (u64)nes_read_indexed(nesdev,
694                         NES_IDX_ENDNODE0_NSTAT_RX_OCTETS_LO + (nesvnic->nic_index*0x200));
695         u64temp += ((u64)nes_read_indexed(nesdev,
696                         NES_IDX_ENDNODE0_NSTAT_RX_OCTETS_HI + (nesvnic->nic_index*0x200))) << 32;
697
698         nesvnic->endnode_nstat_rx_octets += u64temp;
699         nesvnic->netstats.rx_bytes += u64temp;
700
701         u64temp = (u64)nes_read_indexed(nesdev,
702                         NES_IDX_ENDNODE0_NSTAT_RX_FRAMES_LO + (nesvnic->nic_index*0x200));
703         u64temp += ((u64)nes_read_indexed(nesdev,
704                         NES_IDX_ENDNODE0_NSTAT_RX_FRAMES_HI + (nesvnic->nic_index*0x200))) << 32;
705
706         nesvnic->endnode_nstat_rx_frames += u64temp;
707         nesvnic->netstats.rx_packets += u64temp;
708
709         u64temp = (u64)nes_read_indexed(nesdev,
710                         NES_IDX_ENDNODE0_NSTAT_TX_OCTETS_LO + (nesvnic->nic_index*0x200));
711         u64temp += ((u64)nes_read_indexed(nesdev,
712                         NES_IDX_ENDNODE0_NSTAT_TX_OCTETS_HI + (nesvnic->nic_index*0x200))) << 32;
713
714         nesvnic->endnode_nstat_tx_octets += u64temp;
715         nesvnic->netstats.tx_bytes += u64temp;
716
717         u64temp = (u64)nes_read_indexed(nesdev,
718                         NES_IDX_ENDNODE0_NSTAT_TX_FRAMES_LO + (nesvnic->nic_index*0x200));
719         u64temp += ((u64)nes_read_indexed(nesdev,
720                         NES_IDX_ENDNODE0_NSTAT_TX_FRAMES_HI + (nesvnic->nic_index*0x200))) << 32;
721
722         nesvnic->endnode_nstat_tx_frames += u64temp;
723         nesvnic->netstats.tx_packets += u64temp;
724
725         u32temp = nes_read_indexed(nesdev,
726                         NES_IDX_MAC_RX_SHORT_FRAMES + (nesvnic->nesdev->mac_index*0x200));
727         nesvnic->netstats.rx_dropped += u32temp;
728         nesvnic->nesdev->mac_rx_errors += u32temp;
729         nesvnic->nesdev->mac_rx_short_frames += u32temp;
730
731         u32temp = nes_read_indexed(nesdev,
732                         NES_IDX_MAC_RX_OVERSIZED_FRAMES + (nesvnic->nesdev->mac_index*0x200));
733         nesvnic->netstats.rx_dropped += u32temp;
734         nesvnic->nesdev->mac_rx_errors += u32temp;
735         nesvnic->nesdev->mac_rx_oversized_frames += u32temp;
736
737         u32temp = nes_read_indexed(nesdev,
738                         NES_IDX_MAC_RX_JABBER_FRAMES + (nesvnic->nesdev->mac_index*0x200));
739         nesvnic->netstats.rx_dropped += u32temp;
740         nesvnic->nesdev->mac_rx_errors += u32temp;
741         nesvnic->nesdev->mac_rx_jabber_frames += u32temp;
742
743         u32temp = nes_read_indexed(nesdev,
744                         NES_IDX_MAC_RX_SYMBOL_ERR_FRAMES + (nesvnic->nesdev->mac_index*0x200));
745         nesvnic->netstats.rx_dropped += u32temp;
746         nesvnic->nesdev->mac_rx_errors += u32temp;
747         nesvnic->nesdev->mac_rx_symbol_err_frames += u32temp;
748
749         u32temp = nes_read_indexed(nesdev,
750                         NES_IDX_MAC_RX_LENGTH_ERR_FRAMES + (nesvnic->nesdev->mac_index*0x200));
751         nesvnic->netstats.rx_length_errors += u32temp;
752         nesvnic->nesdev->mac_rx_errors += u32temp;
753
754         u32temp = nes_read_indexed(nesdev,
755                         NES_IDX_MAC_RX_CRC_ERR_FRAMES + (nesvnic->nesdev->mac_index*0x200));
756         nesvnic->nesdev->mac_rx_errors += u32temp;
757         nesvnic->nesdev->mac_rx_crc_errors += u32temp;
758         nesvnic->netstats.rx_crc_errors += u32temp;
759
760         u32temp = nes_read_indexed(nesdev,
761                         NES_IDX_MAC_TX_ERRORS + (nesvnic->nesdev->mac_index*0x200));
762         nesvnic->nesdev->mac_tx_errors += u32temp;
763         nesvnic->netstats.tx_errors += u32temp;
764
765         return &nesvnic->netstats;
766 }
767
768
769 /**
770  * nes_netdev_tx_timeout
771  */
772 static void nes_netdev_tx_timeout(struct net_device *netdev)
773 {
774         struct nes_vnic *nesvnic = netdev_priv(netdev);
775
776         if (netif_msg_timer(nesvnic))
777                 nes_debug(NES_DBG_NIC_TX, "%s: tx timeout\n", netdev->name);
778 }
779
780
781 /**
782  * nes_netdev_set_mac_address
783  */
784 static int nes_netdev_set_mac_address(struct net_device *netdev, void *p)
785 {
786         struct nes_vnic *nesvnic = netdev_priv(netdev);
787         struct nes_device *nesdev = nesvnic->nesdev;
788         struct sockaddr *mac_addr = p;
789         int i;
790         u32 macaddr_low;
791         u16 macaddr_high;
792         DECLARE_MAC_BUF(mac);
793
794         if (!is_valid_ether_addr(mac_addr->sa_data))
795                 return -EADDRNOTAVAIL;
796
797         memcpy(netdev->dev_addr, mac_addr->sa_data, netdev->addr_len);
798         printk(PFX "%s: Address length = %d, Address = %s\n",
799                __func__, netdev->addr_len, print_mac(mac, mac_addr->sa_data));
800         macaddr_high  = ((u16)netdev->dev_addr[0]) << 8;
801         macaddr_high += (u16)netdev->dev_addr[1];
802         macaddr_low   = ((u32)netdev->dev_addr[2]) << 24;
803         macaddr_low  += ((u32)netdev->dev_addr[3]) << 16;
804         macaddr_low  += ((u32)netdev->dev_addr[4]) << 8;
805         macaddr_low  += (u32)netdev->dev_addr[5];
806
807         for (i = 0; i < NES_MAX_PORT_COUNT; i++) {
808                 if (nesvnic->qp_nic_index[i] == 0xf) {
809                         break;
810                 }
811                 nes_write_indexed(nesdev,
812                                 NES_IDX_PERFECT_FILTER_LOW + (nesvnic->qp_nic_index[i] * 8),
813                                 macaddr_low);
814                 nes_write_indexed(nesdev,
815                                 NES_IDX_PERFECT_FILTER_HIGH + (nesvnic->qp_nic_index[i] * 8),
816                                 (u32)macaddr_high | NES_MAC_ADDR_VALID |
817                                 ((((u32)nesvnic->nic_index) << 16)));
818         }
819         return 0;
820 }
821
822
823 /**
824  * nes_netdev_set_multicast_list
825  */
826 static void nes_netdev_set_multicast_list(struct net_device *netdev)
827 {
828         struct nes_vnic *nesvnic = netdev_priv(netdev);
829         struct nes_device *nesdev = nesvnic->nesdev;
830         struct dev_mc_list *multicast_addr;
831         u32 nic_active_bit;
832         u32 nic_active;
833         u32 perfect_filter_register_address;
834         u32 macaddr_low;
835         u16 macaddr_high;
836         u8 mc_all_on = 0;
837         u8 mc_index;
838         int mc_nic_index = -1;
839
840         nic_active_bit = 1 << nesvnic->nic_index;
841
842         if (netdev->flags & IFF_PROMISC) {
843                 nic_active = nes_read_indexed(nesdev, NES_IDX_NIC_MULTICAST_ALL);
844                 nic_active |= nic_active_bit;
845                 nes_write_indexed(nesdev, NES_IDX_NIC_MULTICAST_ALL, nic_active);
846                 nic_active = nes_read_indexed(nesdev, NES_IDX_NIC_UNICAST_ALL);
847                 nic_active |= nic_active_bit;
848                 nes_write_indexed(nesdev, NES_IDX_NIC_UNICAST_ALL, nic_active);
849                 mc_all_on = 1;
850         } else if ((netdev->flags & IFF_ALLMULTI) || (netdev->mc_count > NES_MULTICAST_PF_MAX) ||
851                            (nesvnic->nic_index > 3)) {
852                 nic_active = nes_read_indexed(nesdev, NES_IDX_NIC_MULTICAST_ALL);
853                 nic_active |= nic_active_bit;
854                 nes_write_indexed(nesdev, NES_IDX_NIC_MULTICAST_ALL, nic_active);
855                 nic_active = nes_read_indexed(nesdev, NES_IDX_NIC_UNICAST_ALL);
856                 nic_active &= ~nic_active_bit;
857                 nes_write_indexed(nesdev, NES_IDX_NIC_UNICAST_ALL, nic_active);
858                 mc_all_on = 1;
859         } else {
860                 nic_active = nes_read_indexed(nesdev, NES_IDX_NIC_MULTICAST_ALL);
861                 nic_active &= ~nic_active_bit;
862                 nes_write_indexed(nesdev, NES_IDX_NIC_MULTICAST_ALL, nic_active);
863                 nic_active = nes_read_indexed(nesdev, NES_IDX_NIC_UNICAST_ALL);
864                 nic_active &= ~nic_active_bit;
865                 nes_write_indexed(nesdev, NES_IDX_NIC_UNICAST_ALL, nic_active);
866         }
867
868         nes_debug(NES_DBG_NIC_RX, "Number of MC entries = %d, Promiscous = %d, All Multicast = %d.\n",
869                           netdev->mc_count, (netdev->flags & IFF_PROMISC)?1:0,
870                           (netdev->flags & IFF_ALLMULTI)?1:0);
871         if (!mc_all_on) {
872                 multicast_addr = netdev->mc_list;
873                 perfect_filter_register_address = NES_IDX_PERFECT_FILTER_LOW + 0x80;
874                 perfect_filter_register_address += nesvnic->nic_index*0x40;
875                 for (mc_index=0; mc_index < NES_MULTICAST_PF_MAX; mc_index++) {
876                         while (multicast_addr && nesvnic->mcrq_mcast_filter && ((mc_nic_index = nesvnic->mcrq_mcast_filter(nesvnic, multicast_addr->dmi_addr)) == 0))
877                                 multicast_addr = multicast_addr->next;
878                         if (mc_nic_index < 0)
879                                 mc_nic_index = nesvnic->nic_index;
880                         if (multicast_addr) {
881                                 DECLARE_MAC_BUF(mac);
882                                 nes_debug(NES_DBG_NIC_RX, "Assigning MC Address %s to register 0x%04X nic_idx=%d\n",
883                                           print_mac(mac, multicast_addr->dmi_addr),
884                                           perfect_filter_register_address+(mc_index * 8),
885                                           mc_nic_index);
886                                 macaddr_high  = ((u16)multicast_addr->dmi_addr[0]) << 8;
887                                 macaddr_high += (u16)multicast_addr->dmi_addr[1];
888                                 macaddr_low   = ((u32)multicast_addr->dmi_addr[2]) << 24;
889                                 macaddr_low  += ((u32)multicast_addr->dmi_addr[3]) << 16;
890                                 macaddr_low  += ((u32)multicast_addr->dmi_addr[4]) << 8;
891                                 macaddr_low  += (u32)multicast_addr->dmi_addr[5];
892                                 nes_write_indexed(nesdev,
893                                                 perfect_filter_register_address+(mc_index * 8),
894                                                 macaddr_low);
895                                 nes_write_indexed(nesdev,
896                                                 perfect_filter_register_address+4+(mc_index * 8),
897                                                 (u32)macaddr_high | NES_MAC_ADDR_VALID |
898                                                 ((((u32)(1<<mc_nic_index)) << 16)));
899                                 multicast_addr = multicast_addr->next;
900                         } else {
901                                 nes_debug(NES_DBG_NIC_RX, "Clearing MC Address at register 0x%04X\n",
902                                                   perfect_filter_register_address+(mc_index * 8));
903                                 nes_write_indexed(nesdev,
904                                                 perfect_filter_register_address+4+(mc_index * 8),
905                                                 0);
906                         }
907                 }
908         }
909 }
910
911
912 /**
913  * nes_netdev_change_mtu
914  */
915 static int nes_netdev_change_mtu(struct net_device *netdev, int new_mtu)
916 {
917         struct nes_vnic *nesvnic = netdev_priv(netdev);
918         struct nes_device *nesdev = nesvnic->nesdev;
919         int ret = 0;
920         u8 jumbomode = 0;
921
922         if ((new_mtu < ETH_ZLEN) || (new_mtu > max_mtu))
923                 return -EINVAL;
924
925         netdev->mtu = new_mtu;
926         nesvnic->max_frame_size = new_mtu + VLAN_ETH_HLEN;
927
928         if (netdev->mtu > 1500) {
929                 jumbomode=1;
930         }
931         nes_nic_init_timer_defaults(nesdev, jumbomode);
932
933         if (netif_running(netdev)) {
934                 nes_netdev_stop(netdev);
935                 nes_netdev_open(netdev);
936         }
937
938         return ret;
939 }
940
941 static const char nes_ethtool_stringset[][ETH_GSTRING_LEN] = {
942         "Link Change Interrupts",
943         "Linearized SKBs",
944         "T/GSO Requests",
945         "Pause Frames Sent",
946         "Pause Frames Received",
947         "Internal Routing Errors",
948         "SQ SW Dropped SKBs",
949         "SQ Locked",
950         "SQ Full",
951         "Segmented TSO Requests",
952         "Rx Symbol Errors",
953         "Rx Jabber Errors",
954         "Rx Oversized Frames",
955         "Rx Short Frames",
956         "Endnode Rx Discards",
957         "Endnode Rx Octets",
958         "Endnode Rx Frames",
959         "Endnode Tx Octets",
960         "Endnode Tx Frames",
961         "mh detected",
962         "mh pauses",
963         "Retransmission Count",
964         "CM Connects",
965         "CM Accepts",
966         "Disconnects",
967         "Connected Events",
968         "Connect Requests",
969         "CM Rejects",
970         "ModifyQP Timeouts",
971         "CreateQPs",
972         "SW DestroyQPs",
973         "DestroyQPs",
974         "CM Closes",
975         "CM Packets Sent",
976         "CM Packets Bounced",
977         "CM Packets Created",
978         "CM Packets Rcvd",
979         "CM Packets Dropped",
980         "CM Packets Retrans",
981         "CM Listens Created",
982         "CM Listens Destroyed",
983         "CM Backlog Drops",
984         "CM Loopbacks",
985         "CM Nodes Created",
986         "CM Nodes Destroyed",
987         "CM Accel Drops",
988         "CM Resets Received",
989         "Timer Inits",
990         "CQ Depth 1",
991         "CQ Depth 4",
992         "CQ Depth 16",
993         "CQ Depth 24",
994         "CQ Depth 32",
995         "CQ Depth 128",
996         "CQ Depth 256",
997         "LRO aggregated",
998         "LRO flushed",
999         "LRO no_desc",
1000 };
1001
1002 #define NES_ETHTOOL_STAT_COUNT  ARRAY_SIZE(nes_ethtool_stringset)
1003
1004 /**
1005  * nes_netdev_get_rx_csum
1006  */
1007 static u32 nes_netdev_get_rx_csum (struct net_device *netdev)
1008 {
1009         struct nes_vnic *nesvnic = netdev_priv(netdev);
1010
1011         if (nesvnic->rx_checksum_disabled)
1012                 return 0;
1013         else
1014                 return 1;
1015 }
1016
1017
1018 /**
1019  * nes_netdev_set_rc_csum
1020  */
1021 static int nes_netdev_set_rx_csum(struct net_device *netdev, u32 enable)
1022 {
1023         struct nes_vnic *nesvnic = netdev_priv(netdev);
1024
1025         if (enable)
1026                 nesvnic->rx_checksum_disabled = 0;
1027         else
1028                 nesvnic->rx_checksum_disabled = 1;
1029         return 0;
1030 }
1031
1032
1033 /**
1034  * nes_netdev_get_stats_count
1035  */
1036 static int nes_netdev_get_stats_count(struct net_device *netdev)
1037 {
1038         return NES_ETHTOOL_STAT_COUNT;
1039 }
1040
1041
1042 /**
1043  * nes_netdev_get_strings
1044  */
1045 static void nes_netdev_get_strings(struct net_device *netdev, u32 stringset,
1046                 u8 *ethtool_strings)
1047 {
1048         if (stringset == ETH_SS_STATS)
1049                 memcpy(ethtool_strings,
1050                                 &nes_ethtool_stringset,
1051                                 sizeof(nes_ethtool_stringset));
1052 }
1053
1054
1055 /**
1056  * nes_netdev_get_ethtool_stats
1057  */
1058 static void nes_netdev_get_ethtool_stats(struct net_device *netdev,
1059                 struct ethtool_stats *target_ethtool_stats, u64 *target_stat_values)
1060 {
1061         u64 u64temp;
1062         struct nes_vnic *nesvnic = netdev_priv(netdev);
1063         struct nes_device *nesdev = nesvnic->nesdev;
1064         u32 nic_count;
1065         u32 u32temp;
1066
1067         target_ethtool_stats->n_stats = NES_ETHTOOL_STAT_COUNT;
1068         target_stat_values[0] = nesvnic->nesdev->link_status_interrupts;
1069         target_stat_values[1] = nesvnic->linearized_skbs;
1070         target_stat_values[2] = nesvnic->tso_requests;
1071
1072         u32temp = nes_read_indexed(nesdev,
1073                         NES_IDX_MAC_TX_PAUSE_FRAMES + (nesvnic->nesdev->mac_index*0x200));
1074         nesvnic->nesdev->mac_pause_frames_sent += u32temp;
1075         target_stat_values[3] = nesvnic->nesdev->mac_pause_frames_sent;
1076
1077         u32temp = nes_read_indexed(nesdev,
1078                         NES_IDX_MAC_RX_PAUSE_FRAMES + (nesvnic->nesdev->mac_index*0x200));
1079         nesvnic->nesdev->mac_pause_frames_received += u32temp;
1080
1081         u32temp = nes_read_indexed(nesdev,
1082                         NES_IDX_PORT_RX_DISCARDS + (nesvnic->nesdev->mac_index*0x40));
1083         nesvnic->nesdev->port_rx_discards += u32temp;
1084         nesvnic->netstats.rx_dropped += u32temp;
1085
1086         u32temp = nes_read_indexed(nesdev,
1087                         NES_IDX_PORT_TX_DISCARDS + (nesvnic->nesdev->mac_index*0x40));
1088         nesvnic->nesdev->port_tx_discards += u32temp;
1089         nesvnic->netstats.tx_dropped += u32temp;
1090
1091         for (nic_count = 0; nic_count < NES_MAX_PORT_COUNT; nic_count++) {
1092                 if (nesvnic->qp_nic_index[nic_count] == 0xf)
1093                         break;
1094
1095                 u32temp = nes_read_indexed(nesdev,
1096                                 NES_IDX_ENDNODE0_NSTAT_RX_DISCARD +
1097                                 (nesvnic->qp_nic_index[nic_count]*0x200));
1098                 nesvnic->netstats.rx_dropped += u32temp;
1099                 nesvnic->endnode_nstat_rx_discard += u32temp;
1100
1101                 u64temp = (u64)nes_read_indexed(nesdev,
1102                                 NES_IDX_ENDNODE0_NSTAT_RX_OCTETS_LO +
1103                                 (nesvnic->qp_nic_index[nic_count]*0x200));
1104                 u64temp += ((u64)nes_read_indexed(nesdev,
1105                                 NES_IDX_ENDNODE0_NSTAT_RX_OCTETS_HI +
1106                                 (nesvnic->qp_nic_index[nic_count]*0x200))) << 32;
1107
1108                 nesvnic->endnode_nstat_rx_octets += u64temp;
1109                 nesvnic->netstats.rx_bytes += u64temp;
1110
1111                 u64temp = (u64)nes_read_indexed(nesdev,
1112                                 NES_IDX_ENDNODE0_NSTAT_RX_FRAMES_LO +
1113                                 (nesvnic->qp_nic_index[nic_count]*0x200));
1114                 u64temp += ((u64)nes_read_indexed(nesdev,
1115                                 NES_IDX_ENDNODE0_NSTAT_RX_FRAMES_HI +
1116                                 (nesvnic->qp_nic_index[nic_count]*0x200))) << 32;
1117
1118                 nesvnic->endnode_nstat_rx_frames += u64temp;
1119                 nesvnic->netstats.rx_packets += u64temp;
1120
1121                 u64temp = (u64)nes_read_indexed(nesdev,
1122                                 NES_IDX_ENDNODE0_NSTAT_TX_OCTETS_LO +
1123                                 (nesvnic->qp_nic_index[nic_count]*0x200));
1124                 u64temp += ((u64)nes_read_indexed(nesdev,
1125                                 NES_IDX_ENDNODE0_NSTAT_TX_OCTETS_HI +
1126                                 (nesvnic->qp_nic_index[nic_count]*0x200))) << 32;
1127
1128                 nesvnic->endnode_nstat_tx_octets += u64temp;
1129                 nesvnic->netstats.tx_bytes += u64temp;
1130
1131                 u64temp = (u64)nes_read_indexed(nesdev,
1132                                 NES_IDX_ENDNODE0_NSTAT_TX_FRAMES_LO +
1133                                 (nesvnic->qp_nic_index[nic_count]*0x200));
1134                 u64temp += ((u64)nes_read_indexed(nesdev,
1135                                 NES_IDX_ENDNODE0_NSTAT_TX_FRAMES_HI +
1136                                 (nesvnic->qp_nic_index[nic_count]*0x200))) << 32;
1137
1138                 nesvnic->endnode_nstat_tx_frames += u64temp;
1139                 nesvnic->netstats.tx_packets += u64temp;
1140
1141                 u32temp = nes_read_indexed(nesdev,
1142                                 NES_IDX_IPV4_TCP_REXMITS + (nesvnic->qp_nic_index[nic_count]*0x200));
1143                 nesvnic->endnode_ipv4_tcp_retransmits += u32temp;
1144         }
1145
1146         target_stat_values[4] = nesvnic->nesdev->mac_pause_frames_received;
1147         target_stat_values[5] = nesdev->nesadapter->nic_rx_eth_route_err;
1148         target_stat_values[6] = nesvnic->tx_sw_dropped;
1149         target_stat_values[7] = nesvnic->sq_locked;
1150         target_stat_values[8] = nesvnic->sq_full;
1151         target_stat_values[9] = nesvnic->segmented_tso_requests;
1152         target_stat_values[10] = nesvnic->nesdev->mac_rx_symbol_err_frames;
1153         target_stat_values[11] = nesvnic->nesdev->mac_rx_jabber_frames;
1154         target_stat_values[12] = nesvnic->nesdev->mac_rx_oversized_frames;
1155         target_stat_values[13] = nesvnic->nesdev->mac_rx_short_frames;
1156         target_stat_values[14] = nesvnic->endnode_nstat_rx_discard;
1157         target_stat_values[15] = nesvnic->endnode_nstat_rx_octets;
1158         target_stat_values[16] = nesvnic->endnode_nstat_rx_frames;
1159         target_stat_values[17] = nesvnic->endnode_nstat_tx_octets;
1160         target_stat_values[18] = nesvnic->endnode_nstat_tx_frames;
1161         target_stat_values[19] = mh_detected;
1162         target_stat_values[20] = mh_pauses_sent;
1163         target_stat_values[21] = nesvnic->endnode_ipv4_tcp_retransmits;
1164         target_stat_values[22] = atomic_read(&cm_connects);
1165         target_stat_values[23] = atomic_read(&cm_accepts);
1166         target_stat_values[24] = atomic_read(&cm_disconnects);
1167         target_stat_values[25] = atomic_read(&cm_connecteds);
1168         target_stat_values[26] = atomic_read(&cm_connect_reqs);
1169         target_stat_values[27] = atomic_read(&cm_rejects);
1170         target_stat_values[28] = atomic_read(&mod_qp_timouts);
1171         target_stat_values[29] = atomic_read(&qps_created);
1172         target_stat_values[30] = atomic_read(&sw_qps_destroyed);
1173         target_stat_values[31] = atomic_read(&qps_destroyed);
1174         target_stat_values[32] = atomic_read(&cm_closes);
1175         target_stat_values[33] = cm_packets_sent;
1176         target_stat_values[34] = cm_packets_bounced;
1177         target_stat_values[35] = cm_packets_created;
1178         target_stat_values[36] = cm_packets_received;
1179         target_stat_values[37] = cm_packets_dropped;
1180         target_stat_values[38] = cm_packets_retrans;
1181         target_stat_values[39] = cm_listens_created;
1182         target_stat_values[40] = cm_listens_destroyed;
1183         target_stat_values[41] = cm_backlog_drops;
1184         target_stat_values[42] = atomic_read(&cm_loopbacks);
1185         target_stat_values[43] = atomic_read(&cm_nodes_created);
1186         target_stat_values[44] = atomic_read(&cm_nodes_destroyed);
1187         target_stat_values[45] = atomic_read(&cm_accel_dropped_pkts);
1188         target_stat_values[46] = atomic_read(&cm_resets_recvd);
1189         target_stat_values[47] = int_mod_timer_init;
1190         target_stat_values[48] = int_mod_cq_depth_1;
1191         target_stat_values[49] = int_mod_cq_depth_4;
1192         target_stat_values[50] = int_mod_cq_depth_16;
1193         target_stat_values[51] = int_mod_cq_depth_24;
1194         target_stat_values[52] = int_mod_cq_depth_32;
1195         target_stat_values[53] = int_mod_cq_depth_128;
1196         target_stat_values[54] = int_mod_cq_depth_256;
1197         target_stat_values[55] = nesvnic->lro_mgr.stats.aggregated;
1198         target_stat_values[56] = nesvnic->lro_mgr.stats.flushed;
1199         target_stat_values[57] = nesvnic->lro_mgr.stats.no_desc;
1200
1201 }
1202
1203
1204 /**
1205  * nes_netdev_get_drvinfo
1206  */
1207 static void nes_netdev_get_drvinfo(struct net_device *netdev,
1208                 struct ethtool_drvinfo *drvinfo)
1209 {
1210         struct nes_vnic *nesvnic = netdev_priv(netdev);
1211
1212         strcpy(drvinfo->driver, DRV_NAME);
1213         strcpy(drvinfo->bus_info, pci_name(nesvnic->nesdev->pcidev));
1214         strcpy(drvinfo->fw_version, "TBD");
1215         strcpy(drvinfo->version, DRV_VERSION);
1216         drvinfo->n_stats = nes_netdev_get_stats_count(netdev);
1217         drvinfo->testinfo_len = 0;
1218         drvinfo->eedump_len = 0;
1219         drvinfo->regdump_len = 0;
1220 }
1221
1222
1223 /**
1224  * nes_netdev_set_coalesce
1225  */
1226 static int nes_netdev_set_coalesce(struct net_device *netdev,
1227                 struct ethtool_coalesce *et_coalesce)
1228 {
1229         struct nes_vnic *nesvnic = netdev_priv(netdev);
1230         struct nes_device *nesdev = nesvnic->nesdev;
1231         struct nes_adapter *nesadapter = nesdev->nesadapter;
1232         struct nes_hw_tune_timer *shared_timer = &nesadapter->tune_timer;
1233         unsigned long flags;
1234
1235         spin_lock_irqsave(&nesadapter->periodic_timer_lock, flags);
1236         if (et_coalesce->rx_max_coalesced_frames_low) {
1237                 shared_timer->threshold_low = et_coalesce->rx_max_coalesced_frames_low;
1238         }
1239         if (et_coalesce->rx_max_coalesced_frames_irq) {
1240                 shared_timer->threshold_target = et_coalesce->rx_max_coalesced_frames_irq;
1241         }
1242         if (et_coalesce->rx_max_coalesced_frames_high) {
1243                 shared_timer->threshold_high = et_coalesce->rx_max_coalesced_frames_high;
1244         }
1245         if (et_coalesce->rx_coalesce_usecs_low) {
1246                 shared_timer->timer_in_use_min = et_coalesce->rx_coalesce_usecs_low;
1247         }
1248         if (et_coalesce->rx_coalesce_usecs_high) {
1249                 shared_timer->timer_in_use_max = et_coalesce->rx_coalesce_usecs_high;
1250         }
1251         spin_unlock_irqrestore(&nesadapter->periodic_timer_lock, flags);
1252
1253         /* using this to drive total interrupt moderation */
1254         nesadapter->et_rx_coalesce_usecs_irq = et_coalesce->rx_coalesce_usecs_irq;
1255         if (et_coalesce->use_adaptive_rx_coalesce) {
1256                 nesadapter->et_use_adaptive_rx_coalesce = 1;
1257                 nesadapter->timer_int_limit = NES_TIMER_INT_LIMIT_DYNAMIC;
1258                 nesadapter->et_rx_coalesce_usecs_irq = 0;
1259                 if (et_coalesce->pkt_rate_low) {
1260                         nesadapter->et_pkt_rate_low = et_coalesce->pkt_rate_low;
1261                 }
1262         } else {
1263                 nesadapter->et_use_adaptive_rx_coalesce = 0;
1264                 nesadapter->timer_int_limit = NES_TIMER_INT_LIMIT;
1265                 if (nesadapter->et_rx_coalesce_usecs_irq) {
1266                         nes_write32(nesdev->regs+NES_PERIODIC_CONTROL,
1267                                         0x80000000 | ((u32)(nesadapter->et_rx_coalesce_usecs_irq*8)));
1268                 }
1269         }
1270         return 0;
1271 }
1272
1273
1274 /**
1275  * nes_netdev_get_coalesce
1276  */
1277 static int nes_netdev_get_coalesce(struct net_device *netdev,
1278                 struct ethtool_coalesce *et_coalesce)
1279 {
1280         struct nes_vnic *nesvnic = netdev_priv(netdev);
1281         struct nes_device *nesdev = nesvnic->nesdev;
1282         struct nes_adapter *nesadapter = nesdev->nesadapter;
1283         struct ethtool_coalesce temp_et_coalesce;
1284         struct nes_hw_tune_timer *shared_timer = &nesadapter->tune_timer;
1285         unsigned long flags;
1286
1287         memset(&temp_et_coalesce, 0, sizeof(temp_et_coalesce));
1288         temp_et_coalesce.rx_coalesce_usecs_irq    = nesadapter->et_rx_coalesce_usecs_irq;
1289         temp_et_coalesce.use_adaptive_rx_coalesce = nesadapter->et_use_adaptive_rx_coalesce;
1290         temp_et_coalesce.rate_sample_interval     = nesadapter->et_rate_sample_interval;
1291         temp_et_coalesce.pkt_rate_low = nesadapter->et_pkt_rate_low;
1292         spin_lock_irqsave(&nesadapter->periodic_timer_lock,     flags);
1293         temp_et_coalesce.rx_max_coalesced_frames_low  = shared_timer->threshold_low;
1294         temp_et_coalesce.rx_max_coalesced_frames_irq  = shared_timer->threshold_target;
1295         temp_et_coalesce.rx_max_coalesced_frames_high = shared_timer->threshold_high;
1296         temp_et_coalesce.rx_coalesce_usecs_low  = shared_timer->timer_in_use_min;
1297         temp_et_coalesce.rx_coalesce_usecs_high = shared_timer->timer_in_use_max;
1298         if (nesadapter->et_use_adaptive_rx_coalesce) {
1299                 temp_et_coalesce.rx_coalesce_usecs_irq = shared_timer->timer_in_use;
1300         }
1301         spin_unlock_irqrestore(&nesadapter->periodic_timer_lock, flags);
1302         memcpy(et_coalesce, &temp_et_coalesce, sizeof(*et_coalesce));
1303         return 0;
1304 }
1305
1306
1307 /**
1308  * nes_netdev_get_pauseparam
1309  */
1310 static void nes_netdev_get_pauseparam(struct net_device *netdev,
1311                 struct ethtool_pauseparam *et_pauseparam)
1312 {
1313         struct nes_vnic *nesvnic = netdev_priv(netdev);
1314
1315         et_pauseparam->autoneg = 0;
1316         et_pauseparam->rx_pause = (nesvnic->nesdev->disable_rx_flow_control == 0) ? 1:0;
1317         et_pauseparam->tx_pause = (nesvnic->nesdev->disable_tx_flow_control == 0) ? 1:0;
1318 }
1319
1320
1321 /**
1322  * nes_netdev_set_pauseparam
1323  */
1324 static int nes_netdev_set_pauseparam(struct net_device *netdev,
1325                 struct ethtool_pauseparam *et_pauseparam)
1326 {
1327         struct nes_vnic *nesvnic = netdev_priv(netdev);
1328         struct nes_device *nesdev = nesvnic->nesdev;
1329         u32 u32temp;
1330
1331         if (et_pauseparam->autoneg) {
1332                 /* TODO: should return unsupported */
1333                 return 0;
1334         }
1335         if ((et_pauseparam->tx_pause == 1) && (nesdev->disable_tx_flow_control == 1)) {
1336                 u32temp = nes_read_indexed(nesdev,
1337                                 NES_IDX_MAC_TX_CONFIG + (nesdev->mac_index*0x200));
1338                 u32temp |= NES_IDX_MAC_TX_CONFIG_ENABLE_PAUSE;
1339                 nes_write_indexed(nesdev,
1340                                 NES_IDX_MAC_TX_CONFIG_ENABLE_PAUSE + (nesdev->mac_index*0x200), u32temp);
1341                 nesdev->disable_tx_flow_control = 0;
1342         } else if ((et_pauseparam->tx_pause == 0) && (nesdev->disable_tx_flow_control == 0)) {
1343                 u32temp = nes_read_indexed(nesdev,
1344                                 NES_IDX_MAC_TX_CONFIG + (nesdev->mac_index*0x200));
1345                 u32temp &= ~NES_IDX_MAC_TX_CONFIG_ENABLE_PAUSE;
1346                 nes_write_indexed(nesdev,
1347                                 NES_IDX_MAC_TX_CONFIG_ENABLE_PAUSE + (nesdev->mac_index*0x200), u32temp);
1348                 nesdev->disable_tx_flow_control = 1;
1349         }
1350         if ((et_pauseparam->rx_pause == 1) && (nesdev->disable_rx_flow_control == 1)) {
1351                 u32temp = nes_read_indexed(nesdev,
1352                                 NES_IDX_MPP_DEBUG + (nesdev->mac_index*0x40));
1353                 u32temp &= ~NES_IDX_MPP_DEBUG_PORT_DISABLE_PAUSE;
1354                 nes_write_indexed(nesdev,
1355                                 NES_IDX_MPP_DEBUG + (nesdev->mac_index*0x40), u32temp);
1356                 nesdev->disable_rx_flow_control = 0;
1357         } else if ((et_pauseparam->rx_pause == 0) && (nesdev->disable_rx_flow_control == 0)) {
1358                 u32temp = nes_read_indexed(nesdev,
1359                                 NES_IDX_MPP_DEBUG + (nesdev->mac_index*0x40));
1360                 u32temp |= NES_IDX_MPP_DEBUG_PORT_DISABLE_PAUSE;
1361                 nes_write_indexed(nesdev,
1362                                 NES_IDX_MPP_DEBUG + (nesdev->mac_index*0x40), u32temp);
1363                 nesdev->disable_rx_flow_control = 1;
1364         }
1365
1366         return 0;
1367 }
1368
1369
1370 /**
1371  * nes_netdev_get_settings
1372  */
1373 static int nes_netdev_get_settings(struct net_device *netdev, struct ethtool_cmd *et_cmd)
1374 {
1375         struct nes_vnic *nesvnic = netdev_priv(netdev);
1376         struct nes_device *nesdev = nesvnic->nesdev;
1377         struct nes_adapter *nesadapter = nesdev->nesadapter;
1378         u16 phy_data;
1379
1380         et_cmd->duplex = DUPLEX_FULL;
1381         et_cmd->port   = PORT_MII;
1382
1383         if (nesadapter->OneG_Mode) {
1384                 et_cmd->speed = SPEED_1000;
1385                 if (nesadapter->phy_type[nesdev->mac_index] == NES_PHY_TYPE_PUMA_1G) {
1386                         et_cmd->supported   = SUPPORTED_1000baseT_Full;
1387                         et_cmd->advertising = ADVERTISED_1000baseT_Full;
1388                         et_cmd->autoneg     = AUTONEG_DISABLE;
1389                         et_cmd->transceiver = XCVR_INTERNAL;
1390                         et_cmd->phy_address = nesdev->mac_index;
1391                 } else {
1392                         et_cmd->supported   = SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg;
1393                         et_cmd->advertising = ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg;
1394                         nes_read_1G_phy_reg(nesdev, 0, nesadapter->phy_index[nesdev->mac_index], &phy_data);
1395                         if (phy_data & 0x1000)
1396                                 et_cmd->autoneg = AUTONEG_ENABLE;
1397                         else
1398                                 et_cmd->autoneg = AUTONEG_DISABLE;
1399                         et_cmd->transceiver = XCVR_EXTERNAL;
1400                         et_cmd->phy_address = nesadapter->phy_index[nesdev->mac_index];
1401                 }
1402         } else {
1403                 if ((nesadapter->phy_type[nesdev->mac_index] == NES_PHY_TYPE_IRIS) ||
1404                     (nesadapter->phy_type[nesdev->mac_index] == NES_PHY_TYPE_ARGUS)) {
1405                         et_cmd->transceiver = XCVR_EXTERNAL;
1406                         et_cmd->port        = PORT_FIBRE;
1407                         et_cmd->supported   = SUPPORTED_FIBRE;
1408                         et_cmd->advertising = ADVERTISED_FIBRE;
1409                         et_cmd->phy_address = nesadapter->phy_index[nesdev->mac_index];
1410                 } else {
1411                         et_cmd->transceiver = XCVR_INTERNAL;
1412                         et_cmd->supported   = SUPPORTED_10000baseT_Full;
1413                         et_cmd->advertising = ADVERTISED_10000baseT_Full;
1414                         et_cmd->phy_address = nesdev->mac_index;
1415                 }
1416                 et_cmd->speed = SPEED_10000;
1417                 et_cmd->autoneg = AUTONEG_DISABLE;
1418         }
1419         et_cmd->maxtxpkt = 511;
1420         et_cmd->maxrxpkt = 511;
1421         return 0;
1422 }
1423
1424
1425 /**
1426  * nes_netdev_set_settings
1427  */
1428 static int nes_netdev_set_settings(struct net_device *netdev, struct ethtool_cmd *et_cmd)
1429 {
1430         struct nes_vnic *nesvnic = netdev_priv(netdev);
1431         struct nes_device *nesdev = nesvnic->nesdev;
1432         struct nes_adapter *nesadapter = nesdev->nesadapter;
1433         u16 phy_data;
1434
1435         if ((nesadapter->OneG_Mode) &&
1436             (nesadapter->phy_type[nesdev->mac_index] != NES_PHY_TYPE_PUMA_1G)) {
1437                 nes_read_1G_phy_reg(nesdev, 0, nesadapter->phy_index[nesdev->mac_index],
1438                                 &phy_data);
1439                 if (et_cmd->autoneg) {
1440                         /* Turn on Full duplex, Autoneg, and restart autonegotiation */
1441                         phy_data |= 0x1300;
1442                 } else {
1443                         /* Turn off autoneg */
1444                         phy_data &= ~0x1000;
1445                 }
1446                 nes_write_1G_phy_reg(nesdev, 0, nesadapter->phy_index[nesdev->mac_index],
1447                                 phy_data);
1448         }
1449
1450         return 0;
1451 }
1452
1453
1454 static struct ethtool_ops nes_ethtool_ops = {
1455         .get_link = ethtool_op_get_link,
1456         .get_settings = nes_netdev_get_settings,
1457         .set_settings = nes_netdev_set_settings,
1458         .get_tx_csum = ethtool_op_get_tx_csum,
1459         .get_rx_csum = nes_netdev_get_rx_csum,
1460         .get_sg = ethtool_op_get_sg,
1461         .get_strings = nes_netdev_get_strings,
1462         .get_stats_count = nes_netdev_get_stats_count,
1463         .get_ethtool_stats = nes_netdev_get_ethtool_stats,
1464         .get_drvinfo = nes_netdev_get_drvinfo,
1465         .get_coalesce = nes_netdev_get_coalesce,
1466         .set_coalesce = nes_netdev_set_coalesce,
1467         .get_pauseparam = nes_netdev_get_pauseparam,
1468         .set_pauseparam = nes_netdev_set_pauseparam,
1469         .set_tx_csum = ethtool_op_set_tx_csum,
1470         .set_rx_csum = nes_netdev_set_rx_csum,
1471         .set_sg = ethtool_op_set_sg,
1472         .get_tso = ethtool_op_get_tso,
1473         .set_tso = ethtool_op_set_tso,
1474         .get_flags = ethtool_op_get_flags,
1475         .set_flags = ethtool_op_set_flags,
1476 };
1477
1478
1479 static void nes_netdev_vlan_rx_register(struct net_device *netdev, struct vlan_group *grp)
1480 {
1481         struct nes_vnic *nesvnic = netdev_priv(netdev);
1482         struct nes_device *nesdev = nesvnic->nesdev;
1483         struct nes_adapter *nesadapter = nesdev->nesadapter;
1484         u32 u32temp;
1485         unsigned long flags;
1486
1487         spin_lock_irqsave(&nesadapter->phy_lock, flags);
1488         nesvnic->vlan_grp = grp;
1489
1490         nes_debug(NES_DBG_NETDEV, "%s: %s\n", __func__, netdev->name);
1491
1492         /* Enable/Disable VLAN Stripping */
1493         u32temp = nes_read_indexed(nesdev, NES_IDX_PCIX_DIAG);
1494         if (grp)
1495                 u32temp &= 0xfdffffff;
1496         else
1497                 u32temp |= 0x02000000;
1498
1499         nes_write_indexed(nesdev, NES_IDX_PCIX_DIAG, u32temp);
1500         spin_unlock_irqrestore(&nesadapter->phy_lock, flags);
1501 }
1502
1503
1504 /**
1505  * nes_netdev_init - initialize network device
1506  */
1507 struct net_device *nes_netdev_init(struct nes_device *nesdev,
1508                 void __iomem *mmio_addr)
1509 {
1510         u64 u64temp;
1511         struct nes_vnic *nesvnic = NULL;
1512         struct net_device *netdev;
1513         struct nic_qp_map *curr_qp_map;
1514         u32 u32temp;
1515         u16 phy_data;
1516         u16 temp_phy_data;
1517
1518         netdev = alloc_etherdev(sizeof(struct nes_vnic));
1519         if (!netdev) {
1520                 printk(KERN_ERR PFX "nesvnic etherdev alloc failed");
1521                 return NULL;
1522         }
1523
1524         nes_debug(NES_DBG_INIT, "netdev = %p, %s\n", netdev, netdev->name);
1525
1526         SET_NETDEV_DEV(netdev, &nesdev->pcidev->dev);
1527
1528         nesvnic = netdev_priv(netdev);
1529         memset(nesvnic, 0, sizeof(*nesvnic));
1530
1531         netdev->open = nes_netdev_open;
1532         netdev->stop = nes_netdev_stop;
1533         netdev->hard_start_xmit = nes_netdev_start_xmit;
1534         netdev->get_stats = nes_netdev_get_stats;
1535         netdev->tx_timeout = nes_netdev_tx_timeout;
1536         netdev->set_mac_address = nes_netdev_set_mac_address;
1537         netdev->set_multicast_list = nes_netdev_set_multicast_list;
1538         netdev->change_mtu = nes_netdev_change_mtu;
1539         netdev->watchdog_timeo = NES_TX_TIMEOUT;
1540         netdev->irq = nesdev->pcidev->irq;
1541         netdev->mtu = ETH_DATA_LEN;
1542         netdev->hard_header_len = ETH_HLEN;
1543         netdev->addr_len = ETH_ALEN;
1544         netdev->type = ARPHRD_ETHER;
1545         netdev->features = NETIF_F_HIGHDMA;
1546         netdev->ethtool_ops = &nes_ethtool_ops;
1547         netif_napi_add(netdev, &nesvnic->napi, nes_netdev_poll, 128);
1548         nes_debug(NES_DBG_INIT, "Enabling VLAN Insert/Delete.\n");
1549         netdev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
1550         netdev->vlan_rx_register = nes_netdev_vlan_rx_register;
1551         netdev->features |= NETIF_F_LLTX;
1552
1553         /* Fill in the port structure */
1554         nesvnic->netdev = netdev;
1555         nesvnic->nesdev = nesdev;
1556         nesvnic->msg_enable = netif_msg_init(debug, default_msg);
1557         nesvnic->netdev_index = nesdev->netdev_count;
1558         nesvnic->perfect_filter_index = nesdev->nesadapter->netdev_count;
1559         nesvnic->max_frame_size = netdev->mtu + netdev->hard_header_len + VLAN_HLEN;
1560
1561         curr_qp_map = nic_qp_mapping_per_function[PCI_FUNC(nesdev->pcidev->devfn)];
1562         nesvnic->nic.qp_id = curr_qp_map[nesdev->netdev_count].qpid;
1563         nesvnic->nic_index = curr_qp_map[nesdev->netdev_count].nic_index;
1564         nesvnic->logical_port = curr_qp_map[nesdev->netdev_count].logical_port;
1565
1566         /* Setup the burned in MAC address */
1567         u64temp = (u64)nesdev->nesadapter->mac_addr_low;
1568         u64temp += ((u64)nesdev->nesadapter->mac_addr_high) << 32;
1569         u64temp += nesvnic->nic_index;
1570         netdev->dev_addr[0] = (u8)(u64temp>>40);
1571         netdev->dev_addr[1] = (u8)(u64temp>>32);
1572         netdev->dev_addr[2] = (u8)(u64temp>>24);
1573         netdev->dev_addr[3] = (u8)(u64temp>>16);
1574         netdev->dev_addr[4] = (u8)(u64temp>>8);
1575         netdev->dev_addr[5] = (u8)u64temp;
1576         memcpy(netdev->perm_addr, netdev->dev_addr, 6);
1577
1578         if ((nesvnic->logical_port < 2) || (nesdev->nesadapter->hw_rev != NE020_REV)) {
1579                 netdev->features |= NETIF_F_TSO | NETIF_F_SG | NETIF_F_IP_CSUM;
1580                 netdev->features |= NETIF_F_GSO | NETIF_F_TSO | NETIF_F_SG | NETIF_F_IP_CSUM;
1581         } else {
1582                 netdev->features |= NETIF_F_SG | NETIF_F_IP_CSUM;
1583         }
1584
1585         nes_debug(NES_DBG_INIT, "nesvnic = %p, reported features = 0x%lX, QPid = %d,"
1586                         " nic_index = %d, logical_port = %d, mac_index = %d.\n",
1587                         nesvnic, (unsigned long)netdev->features, nesvnic->nic.qp_id,
1588                         nesvnic->nic_index, nesvnic->logical_port,  nesdev->mac_index);
1589
1590         if (nesvnic->nesdev->nesadapter->port_count == 1) {
1591                 nesvnic->qp_nic_index[0] = nesvnic->nic_index;
1592                 nesvnic->qp_nic_index[1] = nesvnic->nic_index + 1;
1593                 if (nes_drv_opt & NES_DRV_OPT_DUAL_LOGICAL_PORT) {
1594                         nesvnic->qp_nic_index[2] = 0xf;
1595                         nesvnic->qp_nic_index[3] = 0xf;
1596                 } else {
1597                         nesvnic->qp_nic_index[2] = nesvnic->nic_index + 2;
1598                         nesvnic->qp_nic_index[3] = nesvnic->nic_index + 3;
1599                 }
1600         } else {
1601                 if (nesvnic->nesdev->nesadapter->port_count == 2) {
1602                         nesvnic->qp_nic_index[0] = nesvnic->nic_index;
1603                         nesvnic->qp_nic_index[1] = nesvnic->nic_index + 2;
1604                         nesvnic->qp_nic_index[2] = 0xf;
1605                         nesvnic->qp_nic_index[3] = 0xf;
1606                 } else {
1607                         nesvnic->qp_nic_index[0] = nesvnic->nic_index;
1608                         nesvnic->qp_nic_index[1] = 0xf;
1609                         nesvnic->qp_nic_index[2] = 0xf;
1610                         nesvnic->qp_nic_index[3] = 0xf;
1611                 }
1612         }
1613         nesvnic->next_qp_nic_index = 0;
1614
1615         if (nesdev->netdev_count == 0) {
1616                 nesvnic->rdma_enabled = 1;
1617         } else {
1618                 nesvnic->rdma_enabled = 0;
1619         }
1620         nesvnic->nic_cq.cq_number = nesvnic->nic.qp_id;
1621         spin_lock_init(&nesvnic->tx_lock);
1622         nesdev->netdev[nesdev->netdev_count] = netdev;
1623
1624         nes_debug(NES_DBG_INIT, "Adding nesvnic (%p) to the adapters nesvnic_list for MAC%d.\n",
1625                         nesvnic, nesdev->mac_index);
1626         list_add_tail(&nesvnic->list, &nesdev->nesadapter->nesvnic_list[nesdev->mac_index]);
1627
1628         if ((nesdev->netdev_count == 0) &&
1629             ((PCI_FUNC(nesdev->pcidev->devfn) == nesdev->mac_index) ||
1630              ((nesdev->nesadapter->phy_type[nesdev->mac_index] == NES_PHY_TYPE_PUMA_1G) &&
1631               (((PCI_FUNC(nesdev->pcidev->devfn) == 1) && (nesdev->mac_index == 2)) ||
1632                ((PCI_FUNC(nesdev->pcidev->devfn) == 2) && (nesdev->mac_index == 1)))))) {
1633                 /*
1634                  * nes_debug(NES_DBG_INIT, "Setting up PHY interrupt mask. Using register index 0x%04X\n",
1635                  *              NES_IDX_PHY_PCS_CONTROL_STATUS0 + (0x200 * (nesvnic->logical_port & 1)));
1636                  */
1637                 u32temp = nes_read_indexed(nesdev, NES_IDX_PHY_PCS_CONTROL_STATUS0 +
1638                                 (0x200 * (nesdev->mac_index & 1)));
1639                 if (nesdev->nesadapter->phy_type[nesdev->mac_index] != NES_PHY_TYPE_PUMA_1G) {
1640                         u32temp |= 0x00200000;
1641                         nes_write_indexed(nesdev, NES_IDX_PHY_PCS_CONTROL_STATUS0 +
1642                                 (0x200 * (nesdev->mac_index & 1)), u32temp);
1643                 }
1644
1645                 u32temp = nes_read_indexed(nesdev, NES_IDX_PHY_PCS_CONTROL_STATUS0 +
1646                                 (0x200 * (nesdev->mac_index & 1)));
1647
1648                 if ((u32temp&0x0f1f0000) == 0x0f0f0000) {
1649                         if (nesdev->nesadapter->phy_type[nesdev->mac_index] == NES_PHY_TYPE_IRIS) {
1650                                 nes_init_phy(nesdev);
1651                                 nes_read_10G_phy_reg(nesdev, nesdev->nesadapter->phy_index[nesdev->mac_index], 1, 1);
1652                                 temp_phy_data = (u16)nes_read_indexed(nesdev,
1653                                                                         NES_IDX_MAC_MDIO_CONTROL);
1654                                 u32temp = 20;
1655                                 do {
1656                                         nes_read_10G_phy_reg(nesdev, nesdev->nesadapter->phy_index[nesdev->mac_index], 1, 1);
1657                                         phy_data = (u16)nes_read_indexed(nesdev,
1658                                                                         NES_IDX_MAC_MDIO_CONTROL);
1659                                         if ((phy_data == temp_phy_data) || (!(--u32temp)))
1660                                                 break;
1661                                         temp_phy_data = phy_data;
1662                                 } while (1);
1663                                 if (phy_data & 4) {
1664                                         nes_debug(NES_DBG_INIT, "The Link is UP!!.\n");
1665                                         nesvnic->linkup = 1;
1666                                 } else {
1667                                         nes_debug(NES_DBG_INIT, "The Link is DOWN!!.\n");
1668                                 }
1669                         } else {
1670                                 nes_debug(NES_DBG_INIT, "The Link is UP!!.\n");
1671                                 nesvnic->linkup = 1;
1672                         }
1673                 } else if (nesdev->nesadapter->phy_type[nesdev->mac_index] == NES_PHY_TYPE_PUMA_1G) {
1674                         nes_debug(NES_DBG_INIT, "mac_index=%d, logical_port=%d, u32temp=0x%04X, PCI_FUNC=%d\n",
1675                                 nesdev->mac_index, nesvnic->logical_port, u32temp, PCI_FUNC(nesdev->pcidev->devfn));
1676                         if (((nesdev->mac_index < 2) && ((u32temp&0x01010000) == 0x01010000)) ||
1677                             ((nesdev->mac_index > 1) && ((u32temp&0x02020000) == 0x02020000)))  {
1678                                 nes_debug(NES_DBG_INIT, "The Link is UP!!.\n");
1679                                 nesvnic->linkup = 1;
1680                         }
1681                 }
1682                 /* clear the MAC interrupt status, assumes direct logical to physical mapping */
1683                 u32temp = nes_read_indexed(nesdev, NES_IDX_MAC_INT_STATUS + (0x200 * nesdev->mac_index));
1684                 nes_debug(NES_DBG_INIT, "Phy interrupt status = 0x%X.\n", u32temp);
1685                 nes_write_indexed(nesdev, NES_IDX_MAC_INT_STATUS + (0x200 * nesdev->mac_index), u32temp);
1686
1687                 if (nesdev->nesadapter->phy_type[nesdev->mac_index] != NES_PHY_TYPE_IRIS)
1688                         nes_init_phy(nesdev);
1689
1690         }
1691
1692         return netdev;
1693 }
1694
1695
1696 /**
1697  * nes_netdev_destroy - destroy network device structure
1698  */
1699 void nes_netdev_destroy(struct net_device *netdev)
1700 {
1701         struct nes_vnic *nesvnic = netdev_priv(netdev);
1702
1703         /* make sure 'stop' method is called by Linux stack */
1704         /* nes_netdev_stop(netdev); */
1705
1706         list_del(&nesvnic->list);
1707
1708         if (nesvnic->of_device_registered) {
1709                 nes_destroy_ofa_device(nesvnic->nesibdev);
1710         }
1711
1712         free_netdev(netdev);
1713 }
1714
1715
1716 /**
1717  * nes_nic_cm_xmit -- CM calls this to send out pkts
1718  */
1719 int nes_nic_cm_xmit(struct sk_buff *skb, struct net_device *netdev)
1720 {
1721         int ret;
1722
1723         skb->dev = netdev;
1724         ret = dev_queue_xmit(skb);
1725         if (ret) {
1726                 nes_debug(NES_DBG_CM, "Bad return code from dev_queue_xmit %d\n", ret);
1727         }
1728
1729         return ret;
1730 }