RDMA/nes: Fill in firmware version for ethtool
[linux-2.6.git] / 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 MAX_SKB_FRAGS
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         u32 nic_active;
922         u32 nic_active_bit;
923         u32 uc_all_active;
924         u32 mc_all_active;
925
926         if ((new_mtu < ETH_ZLEN) || (new_mtu > max_mtu))
927                 return -EINVAL;
928
929         netdev->mtu = new_mtu;
930         nesvnic->max_frame_size = new_mtu + VLAN_ETH_HLEN;
931
932         if (netdev->mtu > 1500) {
933                 jumbomode=1;
934         }
935         nes_nic_init_timer_defaults(nesdev, jumbomode);
936
937         if (netif_running(netdev)) {
938                 nic_active_bit = 1 << nesvnic->nic_index;
939                 mc_all_active = nes_read_indexed(nesdev,
940                                 NES_IDX_NIC_MULTICAST_ALL) & nic_active_bit;
941                 uc_all_active = nes_read_indexed(nesdev,
942                                 NES_IDX_NIC_UNICAST_ALL)  & nic_active_bit;
943
944                 nes_netdev_stop(netdev);
945                 nes_netdev_open(netdev);
946
947                 nic_active = nes_read_indexed(nesdev,
948                                         NES_IDX_NIC_MULTICAST_ALL);
949                 nic_active |= mc_all_active;
950                 nes_write_indexed(nesdev, NES_IDX_NIC_MULTICAST_ALL,
951                                                         nic_active);
952
953                 nic_active = nes_read_indexed(nesdev, NES_IDX_NIC_UNICAST_ALL);
954                 nic_active |= uc_all_active;
955                 nes_write_indexed(nesdev, NES_IDX_NIC_UNICAST_ALL, nic_active);
956         }
957
958         return ret;
959 }
960
961 static const char nes_ethtool_stringset[][ETH_GSTRING_LEN] = {
962         "Link Change Interrupts",
963         "Linearized SKBs",
964         "T/GSO Requests",
965         "Pause Frames Sent",
966         "Pause Frames Received",
967         "Internal Routing Errors",
968         "SQ SW Dropped SKBs",
969         "SQ Locked",
970         "SQ Full",
971         "Segmented TSO Requests",
972         "Rx Symbol Errors",
973         "Rx Jabber Errors",
974         "Rx Oversized Frames",
975         "Rx Short Frames",
976         "Endnode Rx Discards",
977         "Endnode Rx Octets",
978         "Endnode Rx Frames",
979         "Endnode Tx Octets",
980         "Endnode Tx Frames",
981         "mh detected",
982         "mh pauses",
983         "Retransmission Count",
984         "CM Connects",
985         "CM Accepts",
986         "Disconnects",
987         "Connected Events",
988         "Connect Requests",
989         "CM Rejects",
990         "ModifyQP Timeouts",
991         "CreateQPs",
992         "SW DestroyQPs",
993         "DestroyQPs",
994         "CM Closes",
995         "CM Packets Sent",
996         "CM Packets Bounced",
997         "CM Packets Created",
998         "CM Packets Rcvd",
999         "CM Packets Dropped",
1000         "CM Packets Retrans",
1001         "CM Listens Created",
1002         "CM Listens Destroyed",
1003         "CM Backlog Drops",
1004         "CM Loopbacks",
1005         "CM Nodes Created",
1006         "CM Nodes Destroyed",
1007         "CM Accel Drops",
1008         "CM Resets Received",
1009         "Timer Inits",
1010         "CQ Depth 1",
1011         "CQ Depth 4",
1012         "CQ Depth 16",
1013         "CQ Depth 24",
1014         "CQ Depth 32",
1015         "CQ Depth 128",
1016         "CQ Depth 256",
1017         "LRO aggregated",
1018         "LRO flushed",
1019         "LRO no_desc",
1020 };
1021
1022 #define NES_ETHTOOL_STAT_COUNT  ARRAY_SIZE(nes_ethtool_stringset)
1023
1024 /**
1025  * nes_netdev_get_rx_csum
1026  */
1027 static u32 nes_netdev_get_rx_csum (struct net_device *netdev)
1028 {
1029         struct nes_vnic *nesvnic = netdev_priv(netdev);
1030
1031         if (nesvnic->rx_checksum_disabled)
1032                 return 0;
1033         else
1034                 return 1;
1035 }
1036
1037
1038 /**
1039  * nes_netdev_set_rc_csum
1040  */
1041 static int nes_netdev_set_rx_csum(struct net_device *netdev, u32 enable)
1042 {
1043         struct nes_vnic *nesvnic = netdev_priv(netdev);
1044
1045         if (enable)
1046                 nesvnic->rx_checksum_disabled = 0;
1047         else
1048                 nesvnic->rx_checksum_disabled = 1;
1049         return 0;
1050 }
1051
1052
1053 /**
1054  * nes_netdev_get_stats_count
1055  */
1056 static int nes_netdev_get_stats_count(struct net_device *netdev)
1057 {
1058         return NES_ETHTOOL_STAT_COUNT;
1059 }
1060
1061
1062 /**
1063  * nes_netdev_get_strings
1064  */
1065 static void nes_netdev_get_strings(struct net_device *netdev, u32 stringset,
1066                 u8 *ethtool_strings)
1067 {
1068         if (stringset == ETH_SS_STATS)
1069                 memcpy(ethtool_strings,
1070                                 &nes_ethtool_stringset,
1071                                 sizeof(nes_ethtool_stringset));
1072 }
1073
1074
1075 /**
1076  * nes_netdev_get_ethtool_stats
1077  */
1078 static void nes_netdev_get_ethtool_stats(struct net_device *netdev,
1079                 struct ethtool_stats *target_ethtool_stats, u64 *target_stat_values)
1080 {
1081         u64 u64temp;
1082         struct nes_vnic *nesvnic = netdev_priv(netdev);
1083         struct nes_device *nesdev = nesvnic->nesdev;
1084         u32 nic_count;
1085         u32 u32temp;
1086
1087         target_ethtool_stats->n_stats = NES_ETHTOOL_STAT_COUNT;
1088         target_stat_values[0] = nesvnic->nesdev->link_status_interrupts;
1089         target_stat_values[1] = nesvnic->linearized_skbs;
1090         target_stat_values[2] = nesvnic->tso_requests;
1091
1092         u32temp = nes_read_indexed(nesdev,
1093                         NES_IDX_MAC_TX_PAUSE_FRAMES + (nesvnic->nesdev->mac_index*0x200));
1094         nesvnic->nesdev->mac_pause_frames_sent += u32temp;
1095         target_stat_values[3] = nesvnic->nesdev->mac_pause_frames_sent;
1096
1097         u32temp = nes_read_indexed(nesdev,
1098                         NES_IDX_MAC_RX_PAUSE_FRAMES + (nesvnic->nesdev->mac_index*0x200));
1099         nesvnic->nesdev->mac_pause_frames_received += u32temp;
1100
1101         u32temp = nes_read_indexed(nesdev,
1102                         NES_IDX_PORT_RX_DISCARDS + (nesvnic->nesdev->mac_index*0x40));
1103         nesvnic->nesdev->port_rx_discards += u32temp;
1104         nesvnic->netstats.rx_dropped += u32temp;
1105
1106         u32temp = nes_read_indexed(nesdev,
1107                         NES_IDX_PORT_TX_DISCARDS + (nesvnic->nesdev->mac_index*0x40));
1108         nesvnic->nesdev->port_tx_discards += u32temp;
1109         nesvnic->netstats.tx_dropped += u32temp;
1110
1111         for (nic_count = 0; nic_count < NES_MAX_PORT_COUNT; nic_count++) {
1112                 if (nesvnic->qp_nic_index[nic_count] == 0xf)
1113                         break;
1114
1115                 u32temp = nes_read_indexed(nesdev,
1116                                 NES_IDX_ENDNODE0_NSTAT_RX_DISCARD +
1117                                 (nesvnic->qp_nic_index[nic_count]*0x200));
1118                 nesvnic->netstats.rx_dropped += u32temp;
1119                 nesvnic->endnode_nstat_rx_discard += u32temp;
1120
1121                 u64temp = (u64)nes_read_indexed(nesdev,
1122                                 NES_IDX_ENDNODE0_NSTAT_RX_OCTETS_LO +
1123                                 (nesvnic->qp_nic_index[nic_count]*0x200));
1124                 u64temp += ((u64)nes_read_indexed(nesdev,
1125                                 NES_IDX_ENDNODE0_NSTAT_RX_OCTETS_HI +
1126                                 (nesvnic->qp_nic_index[nic_count]*0x200))) << 32;
1127
1128                 nesvnic->endnode_nstat_rx_octets += u64temp;
1129                 nesvnic->netstats.rx_bytes += u64temp;
1130
1131                 u64temp = (u64)nes_read_indexed(nesdev,
1132                                 NES_IDX_ENDNODE0_NSTAT_RX_FRAMES_LO +
1133                                 (nesvnic->qp_nic_index[nic_count]*0x200));
1134                 u64temp += ((u64)nes_read_indexed(nesdev,
1135                                 NES_IDX_ENDNODE0_NSTAT_RX_FRAMES_HI +
1136                                 (nesvnic->qp_nic_index[nic_count]*0x200))) << 32;
1137
1138                 nesvnic->endnode_nstat_rx_frames += u64temp;
1139                 nesvnic->netstats.rx_packets += u64temp;
1140
1141                 u64temp = (u64)nes_read_indexed(nesdev,
1142                                 NES_IDX_ENDNODE0_NSTAT_TX_OCTETS_LO +
1143                                 (nesvnic->qp_nic_index[nic_count]*0x200));
1144                 u64temp += ((u64)nes_read_indexed(nesdev,
1145                                 NES_IDX_ENDNODE0_NSTAT_TX_OCTETS_HI +
1146                                 (nesvnic->qp_nic_index[nic_count]*0x200))) << 32;
1147
1148                 nesvnic->endnode_nstat_tx_octets += u64temp;
1149                 nesvnic->netstats.tx_bytes += u64temp;
1150
1151                 u64temp = (u64)nes_read_indexed(nesdev,
1152                                 NES_IDX_ENDNODE0_NSTAT_TX_FRAMES_LO +
1153                                 (nesvnic->qp_nic_index[nic_count]*0x200));
1154                 u64temp += ((u64)nes_read_indexed(nesdev,
1155                                 NES_IDX_ENDNODE0_NSTAT_TX_FRAMES_HI +
1156                                 (nesvnic->qp_nic_index[nic_count]*0x200))) << 32;
1157
1158                 nesvnic->endnode_nstat_tx_frames += u64temp;
1159                 nesvnic->netstats.tx_packets += u64temp;
1160
1161                 u32temp = nes_read_indexed(nesdev,
1162                                 NES_IDX_IPV4_TCP_REXMITS + (nesvnic->qp_nic_index[nic_count]*0x200));
1163                 nesvnic->endnode_ipv4_tcp_retransmits += u32temp;
1164         }
1165
1166         target_stat_values[4] = nesvnic->nesdev->mac_pause_frames_received;
1167         target_stat_values[5] = nesdev->nesadapter->nic_rx_eth_route_err;
1168         target_stat_values[6] = nesvnic->tx_sw_dropped;
1169         target_stat_values[7] = nesvnic->sq_locked;
1170         target_stat_values[8] = nesvnic->sq_full;
1171         target_stat_values[9] = nesvnic->segmented_tso_requests;
1172         target_stat_values[10] = nesvnic->nesdev->mac_rx_symbol_err_frames;
1173         target_stat_values[11] = nesvnic->nesdev->mac_rx_jabber_frames;
1174         target_stat_values[12] = nesvnic->nesdev->mac_rx_oversized_frames;
1175         target_stat_values[13] = nesvnic->nesdev->mac_rx_short_frames;
1176         target_stat_values[14] = nesvnic->endnode_nstat_rx_discard;
1177         target_stat_values[15] = nesvnic->endnode_nstat_rx_octets;
1178         target_stat_values[16] = nesvnic->endnode_nstat_rx_frames;
1179         target_stat_values[17] = nesvnic->endnode_nstat_tx_octets;
1180         target_stat_values[18] = nesvnic->endnode_nstat_tx_frames;
1181         target_stat_values[19] = mh_detected;
1182         target_stat_values[20] = mh_pauses_sent;
1183         target_stat_values[21] = nesvnic->endnode_ipv4_tcp_retransmits;
1184         target_stat_values[22] = atomic_read(&cm_connects);
1185         target_stat_values[23] = atomic_read(&cm_accepts);
1186         target_stat_values[24] = atomic_read(&cm_disconnects);
1187         target_stat_values[25] = atomic_read(&cm_connecteds);
1188         target_stat_values[26] = atomic_read(&cm_connect_reqs);
1189         target_stat_values[27] = atomic_read(&cm_rejects);
1190         target_stat_values[28] = atomic_read(&mod_qp_timouts);
1191         target_stat_values[29] = atomic_read(&qps_created);
1192         target_stat_values[30] = atomic_read(&sw_qps_destroyed);
1193         target_stat_values[31] = atomic_read(&qps_destroyed);
1194         target_stat_values[32] = atomic_read(&cm_closes);
1195         target_stat_values[33] = cm_packets_sent;
1196         target_stat_values[34] = cm_packets_bounced;
1197         target_stat_values[35] = cm_packets_created;
1198         target_stat_values[36] = cm_packets_received;
1199         target_stat_values[37] = cm_packets_dropped;
1200         target_stat_values[38] = cm_packets_retrans;
1201         target_stat_values[39] = cm_listens_created;
1202         target_stat_values[40] = cm_listens_destroyed;
1203         target_stat_values[41] = cm_backlog_drops;
1204         target_stat_values[42] = atomic_read(&cm_loopbacks);
1205         target_stat_values[43] = atomic_read(&cm_nodes_created);
1206         target_stat_values[44] = atomic_read(&cm_nodes_destroyed);
1207         target_stat_values[45] = atomic_read(&cm_accel_dropped_pkts);
1208         target_stat_values[46] = atomic_read(&cm_resets_recvd);
1209         target_stat_values[47] = int_mod_timer_init;
1210         target_stat_values[48] = int_mod_cq_depth_1;
1211         target_stat_values[49] = int_mod_cq_depth_4;
1212         target_stat_values[50] = int_mod_cq_depth_16;
1213         target_stat_values[51] = int_mod_cq_depth_24;
1214         target_stat_values[52] = int_mod_cq_depth_32;
1215         target_stat_values[53] = int_mod_cq_depth_128;
1216         target_stat_values[54] = int_mod_cq_depth_256;
1217         target_stat_values[55] = nesvnic->lro_mgr.stats.aggregated;
1218         target_stat_values[56] = nesvnic->lro_mgr.stats.flushed;
1219         target_stat_values[57] = nesvnic->lro_mgr.stats.no_desc;
1220
1221 }
1222
1223
1224 /**
1225  * nes_netdev_get_drvinfo
1226  */
1227 static void nes_netdev_get_drvinfo(struct net_device *netdev,
1228                 struct ethtool_drvinfo *drvinfo)
1229 {
1230         struct nes_vnic *nesvnic = netdev_priv(netdev);
1231         struct nes_adapter *nesadapter = nesvnic->nesdev->nesadapter;
1232
1233         strcpy(drvinfo->driver, DRV_NAME);
1234         strcpy(drvinfo->bus_info, pci_name(nesvnic->nesdev->pcidev));
1235         sprintf(drvinfo->fw_version, "%u.%u", nesadapter->firmware_version>>16,
1236                                 nesadapter->firmware_version & 0x000000ff);
1237         strcpy(drvinfo->version, DRV_VERSION);
1238         drvinfo->n_stats = nes_netdev_get_stats_count(netdev);
1239         drvinfo->testinfo_len = 0;
1240         drvinfo->eedump_len = 0;
1241         drvinfo->regdump_len = 0;
1242 }
1243
1244
1245 /**
1246  * nes_netdev_set_coalesce
1247  */
1248 static int nes_netdev_set_coalesce(struct net_device *netdev,
1249                 struct ethtool_coalesce *et_coalesce)
1250 {
1251         struct nes_vnic *nesvnic = netdev_priv(netdev);
1252         struct nes_device *nesdev = nesvnic->nesdev;
1253         struct nes_adapter *nesadapter = nesdev->nesadapter;
1254         struct nes_hw_tune_timer *shared_timer = &nesadapter->tune_timer;
1255         unsigned long flags;
1256
1257         spin_lock_irqsave(&nesadapter->periodic_timer_lock, flags);
1258         if (et_coalesce->rx_max_coalesced_frames_low) {
1259                 shared_timer->threshold_low = et_coalesce->rx_max_coalesced_frames_low;
1260         }
1261         if (et_coalesce->rx_max_coalesced_frames_irq) {
1262                 shared_timer->threshold_target = et_coalesce->rx_max_coalesced_frames_irq;
1263         }
1264         if (et_coalesce->rx_max_coalesced_frames_high) {
1265                 shared_timer->threshold_high = et_coalesce->rx_max_coalesced_frames_high;
1266         }
1267         if (et_coalesce->rx_coalesce_usecs_low) {
1268                 shared_timer->timer_in_use_min = et_coalesce->rx_coalesce_usecs_low;
1269         }
1270         if (et_coalesce->rx_coalesce_usecs_high) {
1271                 shared_timer->timer_in_use_max = et_coalesce->rx_coalesce_usecs_high;
1272         }
1273         spin_unlock_irqrestore(&nesadapter->periodic_timer_lock, flags);
1274
1275         /* using this to drive total interrupt moderation */
1276         nesadapter->et_rx_coalesce_usecs_irq = et_coalesce->rx_coalesce_usecs_irq;
1277         if (et_coalesce->use_adaptive_rx_coalesce) {
1278                 nesadapter->et_use_adaptive_rx_coalesce = 1;
1279                 nesadapter->timer_int_limit = NES_TIMER_INT_LIMIT_DYNAMIC;
1280                 nesadapter->et_rx_coalesce_usecs_irq = 0;
1281                 if (et_coalesce->pkt_rate_low) {
1282                         nesadapter->et_pkt_rate_low = et_coalesce->pkt_rate_low;
1283                 }
1284         } else {
1285                 nesadapter->et_use_adaptive_rx_coalesce = 0;
1286                 nesadapter->timer_int_limit = NES_TIMER_INT_LIMIT;
1287                 if (nesadapter->et_rx_coalesce_usecs_irq) {
1288                         nes_write32(nesdev->regs+NES_PERIODIC_CONTROL,
1289                                         0x80000000 | ((u32)(nesadapter->et_rx_coalesce_usecs_irq*8)));
1290                 }
1291         }
1292         return 0;
1293 }
1294
1295
1296 /**
1297  * nes_netdev_get_coalesce
1298  */
1299 static int nes_netdev_get_coalesce(struct net_device *netdev,
1300                 struct ethtool_coalesce *et_coalesce)
1301 {
1302         struct nes_vnic *nesvnic = netdev_priv(netdev);
1303         struct nes_device *nesdev = nesvnic->nesdev;
1304         struct nes_adapter *nesadapter = nesdev->nesadapter;
1305         struct ethtool_coalesce temp_et_coalesce;
1306         struct nes_hw_tune_timer *shared_timer = &nesadapter->tune_timer;
1307         unsigned long flags;
1308
1309         memset(&temp_et_coalesce, 0, sizeof(temp_et_coalesce));
1310         temp_et_coalesce.rx_coalesce_usecs_irq    = nesadapter->et_rx_coalesce_usecs_irq;
1311         temp_et_coalesce.use_adaptive_rx_coalesce = nesadapter->et_use_adaptive_rx_coalesce;
1312         temp_et_coalesce.rate_sample_interval     = nesadapter->et_rate_sample_interval;
1313         temp_et_coalesce.pkt_rate_low = nesadapter->et_pkt_rate_low;
1314         spin_lock_irqsave(&nesadapter->periodic_timer_lock,     flags);
1315         temp_et_coalesce.rx_max_coalesced_frames_low  = shared_timer->threshold_low;
1316         temp_et_coalesce.rx_max_coalesced_frames_irq  = shared_timer->threshold_target;
1317         temp_et_coalesce.rx_max_coalesced_frames_high = shared_timer->threshold_high;
1318         temp_et_coalesce.rx_coalesce_usecs_low  = shared_timer->timer_in_use_min;
1319         temp_et_coalesce.rx_coalesce_usecs_high = shared_timer->timer_in_use_max;
1320         if (nesadapter->et_use_adaptive_rx_coalesce) {
1321                 temp_et_coalesce.rx_coalesce_usecs_irq = shared_timer->timer_in_use;
1322         }
1323         spin_unlock_irqrestore(&nesadapter->periodic_timer_lock, flags);
1324         memcpy(et_coalesce, &temp_et_coalesce, sizeof(*et_coalesce));
1325         return 0;
1326 }
1327
1328
1329 /**
1330  * nes_netdev_get_pauseparam
1331  */
1332 static void nes_netdev_get_pauseparam(struct net_device *netdev,
1333                 struct ethtool_pauseparam *et_pauseparam)
1334 {
1335         struct nes_vnic *nesvnic = netdev_priv(netdev);
1336
1337         et_pauseparam->autoneg = 0;
1338         et_pauseparam->rx_pause = (nesvnic->nesdev->disable_rx_flow_control == 0) ? 1:0;
1339         et_pauseparam->tx_pause = (nesvnic->nesdev->disable_tx_flow_control == 0) ? 1:0;
1340 }
1341
1342
1343 /**
1344  * nes_netdev_set_pauseparam
1345  */
1346 static int nes_netdev_set_pauseparam(struct net_device *netdev,
1347                 struct ethtool_pauseparam *et_pauseparam)
1348 {
1349         struct nes_vnic *nesvnic = netdev_priv(netdev);
1350         struct nes_device *nesdev = nesvnic->nesdev;
1351         u32 u32temp;
1352
1353         if (et_pauseparam->autoneg) {
1354                 /* TODO: should return unsupported */
1355                 return 0;
1356         }
1357         if ((et_pauseparam->tx_pause == 1) && (nesdev->disable_tx_flow_control == 1)) {
1358                 u32temp = nes_read_indexed(nesdev,
1359                                 NES_IDX_MAC_TX_CONFIG + (nesdev->mac_index*0x200));
1360                 u32temp |= NES_IDX_MAC_TX_CONFIG_ENABLE_PAUSE;
1361                 nes_write_indexed(nesdev,
1362                                 NES_IDX_MAC_TX_CONFIG_ENABLE_PAUSE + (nesdev->mac_index*0x200), u32temp);
1363                 nesdev->disable_tx_flow_control = 0;
1364         } else if ((et_pauseparam->tx_pause == 0) && (nesdev->disable_tx_flow_control == 0)) {
1365                 u32temp = nes_read_indexed(nesdev,
1366                                 NES_IDX_MAC_TX_CONFIG + (nesdev->mac_index*0x200));
1367                 u32temp &= ~NES_IDX_MAC_TX_CONFIG_ENABLE_PAUSE;
1368                 nes_write_indexed(nesdev,
1369                                 NES_IDX_MAC_TX_CONFIG_ENABLE_PAUSE + (nesdev->mac_index*0x200), u32temp);
1370                 nesdev->disable_tx_flow_control = 1;
1371         }
1372         if ((et_pauseparam->rx_pause == 1) && (nesdev->disable_rx_flow_control == 1)) {
1373                 u32temp = nes_read_indexed(nesdev,
1374                                 NES_IDX_MPP_DEBUG + (nesdev->mac_index*0x40));
1375                 u32temp &= ~NES_IDX_MPP_DEBUG_PORT_DISABLE_PAUSE;
1376                 nes_write_indexed(nesdev,
1377                                 NES_IDX_MPP_DEBUG + (nesdev->mac_index*0x40), u32temp);
1378                 nesdev->disable_rx_flow_control = 0;
1379         } else if ((et_pauseparam->rx_pause == 0) && (nesdev->disable_rx_flow_control == 0)) {
1380                 u32temp = nes_read_indexed(nesdev,
1381                                 NES_IDX_MPP_DEBUG + (nesdev->mac_index*0x40));
1382                 u32temp |= NES_IDX_MPP_DEBUG_PORT_DISABLE_PAUSE;
1383                 nes_write_indexed(nesdev,
1384                                 NES_IDX_MPP_DEBUG + (nesdev->mac_index*0x40), u32temp);
1385                 nesdev->disable_rx_flow_control = 1;
1386         }
1387
1388         return 0;
1389 }
1390
1391
1392 /**
1393  * nes_netdev_get_settings
1394  */
1395 static int nes_netdev_get_settings(struct net_device *netdev, struct ethtool_cmd *et_cmd)
1396 {
1397         struct nes_vnic *nesvnic = netdev_priv(netdev);
1398         struct nes_device *nesdev = nesvnic->nesdev;
1399         struct nes_adapter *nesadapter = nesdev->nesadapter;
1400         u16 phy_data;
1401
1402         et_cmd->duplex = DUPLEX_FULL;
1403         et_cmd->port   = PORT_MII;
1404
1405         if (nesadapter->OneG_Mode) {
1406                 et_cmd->speed = SPEED_1000;
1407                 if (nesadapter->phy_type[nesdev->mac_index] == NES_PHY_TYPE_PUMA_1G) {
1408                         et_cmd->supported   = SUPPORTED_1000baseT_Full;
1409                         et_cmd->advertising = ADVERTISED_1000baseT_Full;
1410                         et_cmd->autoneg     = AUTONEG_DISABLE;
1411                         et_cmd->transceiver = XCVR_INTERNAL;
1412                         et_cmd->phy_address = nesdev->mac_index;
1413                 } else {
1414                         et_cmd->supported   = SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg;
1415                         et_cmd->advertising = ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg;
1416                         nes_read_1G_phy_reg(nesdev, 0, nesadapter->phy_index[nesdev->mac_index], &phy_data);
1417                         if (phy_data & 0x1000)
1418                                 et_cmd->autoneg = AUTONEG_ENABLE;
1419                         else
1420                                 et_cmd->autoneg = AUTONEG_DISABLE;
1421                         et_cmd->transceiver = XCVR_EXTERNAL;
1422                         et_cmd->phy_address = nesadapter->phy_index[nesdev->mac_index];
1423                 }
1424         } else {
1425                 if ((nesadapter->phy_type[nesdev->mac_index] == NES_PHY_TYPE_IRIS) ||
1426                     (nesadapter->phy_type[nesdev->mac_index] == NES_PHY_TYPE_ARGUS)) {
1427                         et_cmd->transceiver = XCVR_EXTERNAL;
1428                         et_cmd->port        = PORT_FIBRE;
1429                         et_cmd->supported   = SUPPORTED_FIBRE;
1430                         et_cmd->advertising = ADVERTISED_FIBRE;
1431                         et_cmd->phy_address = nesadapter->phy_index[nesdev->mac_index];
1432                 } else {
1433                         et_cmd->transceiver = XCVR_INTERNAL;
1434                         et_cmd->supported   = SUPPORTED_10000baseT_Full;
1435                         et_cmd->advertising = ADVERTISED_10000baseT_Full;
1436                         et_cmd->phy_address = nesdev->mac_index;
1437                 }
1438                 et_cmd->speed = SPEED_10000;
1439                 et_cmd->autoneg = AUTONEG_DISABLE;
1440         }
1441         et_cmd->maxtxpkt = 511;
1442         et_cmd->maxrxpkt = 511;
1443         return 0;
1444 }
1445
1446
1447 /**
1448  * nes_netdev_set_settings
1449  */
1450 static int nes_netdev_set_settings(struct net_device *netdev, struct ethtool_cmd *et_cmd)
1451 {
1452         struct nes_vnic *nesvnic = netdev_priv(netdev);
1453         struct nes_device *nesdev = nesvnic->nesdev;
1454         struct nes_adapter *nesadapter = nesdev->nesadapter;
1455         u16 phy_data;
1456
1457         if ((nesadapter->OneG_Mode) &&
1458             (nesadapter->phy_type[nesdev->mac_index] != NES_PHY_TYPE_PUMA_1G)) {
1459                 nes_read_1G_phy_reg(nesdev, 0, nesadapter->phy_index[nesdev->mac_index],
1460                                 &phy_data);
1461                 if (et_cmd->autoneg) {
1462                         /* Turn on Full duplex, Autoneg, and restart autonegotiation */
1463                         phy_data |= 0x1300;
1464                 } else {
1465                         /* Turn off autoneg */
1466                         phy_data &= ~0x1000;
1467                 }
1468                 nes_write_1G_phy_reg(nesdev, 0, nesadapter->phy_index[nesdev->mac_index],
1469                                 phy_data);
1470         }
1471
1472         return 0;
1473 }
1474
1475
1476 static struct ethtool_ops nes_ethtool_ops = {
1477         .get_link = ethtool_op_get_link,
1478         .get_settings = nes_netdev_get_settings,
1479         .set_settings = nes_netdev_set_settings,
1480         .get_tx_csum = ethtool_op_get_tx_csum,
1481         .get_rx_csum = nes_netdev_get_rx_csum,
1482         .get_sg = ethtool_op_get_sg,
1483         .get_strings = nes_netdev_get_strings,
1484         .get_stats_count = nes_netdev_get_stats_count,
1485         .get_ethtool_stats = nes_netdev_get_ethtool_stats,
1486         .get_drvinfo = nes_netdev_get_drvinfo,
1487         .get_coalesce = nes_netdev_get_coalesce,
1488         .set_coalesce = nes_netdev_set_coalesce,
1489         .get_pauseparam = nes_netdev_get_pauseparam,
1490         .set_pauseparam = nes_netdev_set_pauseparam,
1491         .set_tx_csum = ethtool_op_set_tx_csum,
1492         .set_rx_csum = nes_netdev_set_rx_csum,
1493         .set_sg = ethtool_op_set_sg,
1494         .get_tso = ethtool_op_get_tso,
1495         .set_tso = ethtool_op_set_tso,
1496         .get_flags = ethtool_op_get_flags,
1497         .set_flags = ethtool_op_set_flags,
1498 };
1499
1500
1501 static void nes_netdev_vlan_rx_register(struct net_device *netdev, struct vlan_group *grp)
1502 {
1503         struct nes_vnic *nesvnic = netdev_priv(netdev);
1504         struct nes_device *nesdev = nesvnic->nesdev;
1505         struct nes_adapter *nesadapter = nesdev->nesadapter;
1506         u32 u32temp;
1507         unsigned long flags;
1508
1509         spin_lock_irqsave(&nesadapter->phy_lock, flags);
1510         nesvnic->vlan_grp = grp;
1511
1512         nes_debug(NES_DBG_NETDEV, "%s: %s\n", __func__, netdev->name);
1513
1514         /* Enable/Disable VLAN Stripping */
1515         u32temp = nes_read_indexed(nesdev, NES_IDX_PCIX_DIAG);
1516         if (grp)
1517                 u32temp &= 0xfdffffff;
1518         else
1519                 u32temp |= 0x02000000;
1520
1521         nes_write_indexed(nesdev, NES_IDX_PCIX_DIAG, u32temp);
1522         spin_unlock_irqrestore(&nesadapter->phy_lock, flags);
1523 }
1524
1525
1526 /**
1527  * nes_netdev_init - initialize network device
1528  */
1529 struct net_device *nes_netdev_init(struct nes_device *nesdev,
1530                 void __iomem *mmio_addr)
1531 {
1532         u64 u64temp;
1533         struct nes_vnic *nesvnic = NULL;
1534         struct net_device *netdev;
1535         struct nic_qp_map *curr_qp_map;
1536         u32 u32temp;
1537         u16 phy_data;
1538         u16 temp_phy_data;
1539
1540         netdev = alloc_etherdev(sizeof(struct nes_vnic));
1541         if (!netdev) {
1542                 printk(KERN_ERR PFX "nesvnic etherdev alloc failed");
1543                 return NULL;
1544         }
1545
1546         nes_debug(NES_DBG_INIT, "netdev = %p, %s\n", netdev, netdev->name);
1547
1548         SET_NETDEV_DEV(netdev, &nesdev->pcidev->dev);
1549
1550         nesvnic = netdev_priv(netdev);
1551         memset(nesvnic, 0, sizeof(*nesvnic));
1552
1553         netdev->open = nes_netdev_open;
1554         netdev->stop = nes_netdev_stop;
1555         netdev->hard_start_xmit = nes_netdev_start_xmit;
1556         netdev->get_stats = nes_netdev_get_stats;
1557         netdev->tx_timeout = nes_netdev_tx_timeout;
1558         netdev->set_mac_address = nes_netdev_set_mac_address;
1559         netdev->set_multicast_list = nes_netdev_set_multicast_list;
1560         netdev->change_mtu = nes_netdev_change_mtu;
1561         netdev->watchdog_timeo = NES_TX_TIMEOUT;
1562         netdev->irq = nesdev->pcidev->irq;
1563         netdev->mtu = ETH_DATA_LEN;
1564         netdev->hard_header_len = ETH_HLEN;
1565         netdev->addr_len = ETH_ALEN;
1566         netdev->type = ARPHRD_ETHER;
1567         netdev->features = NETIF_F_HIGHDMA;
1568         netdev->ethtool_ops = &nes_ethtool_ops;
1569         netif_napi_add(netdev, &nesvnic->napi, nes_netdev_poll, 128);
1570         nes_debug(NES_DBG_INIT, "Enabling VLAN Insert/Delete.\n");
1571         netdev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
1572         netdev->vlan_rx_register = nes_netdev_vlan_rx_register;
1573         netdev->features |= NETIF_F_LLTX;
1574
1575         /* Fill in the port structure */
1576         nesvnic->netdev = netdev;
1577         nesvnic->nesdev = nesdev;
1578         nesvnic->msg_enable = netif_msg_init(debug, default_msg);
1579         nesvnic->netdev_index = nesdev->netdev_count;
1580         nesvnic->perfect_filter_index = nesdev->nesadapter->netdev_count;
1581         nesvnic->max_frame_size = netdev->mtu + netdev->hard_header_len + VLAN_HLEN;
1582
1583         curr_qp_map = nic_qp_mapping_per_function[PCI_FUNC(nesdev->pcidev->devfn)];
1584         nesvnic->nic.qp_id = curr_qp_map[nesdev->netdev_count].qpid;
1585         nesvnic->nic_index = curr_qp_map[nesdev->netdev_count].nic_index;
1586         nesvnic->logical_port = curr_qp_map[nesdev->netdev_count].logical_port;
1587
1588         /* Setup the burned in MAC address */
1589         u64temp = (u64)nesdev->nesadapter->mac_addr_low;
1590         u64temp += ((u64)nesdev->nesadapter->mac_addr_high) << 32;
1591         u64temp += nesvnic->nic_index;
1592         netdev->dev_addr[0] = (u8)(u64temp>>40);
1593         netdev->dev_addr[1] = (u8)(u64temp>>32);
1594         netdev->dev_addr[2] = (u8)(u64temp>>24);
1595         netdev->dev_addr[3] = (u8)(u64temp>>16);
1596         netdev->dev_addr[4] = (u8)(u64temp>>8);
1597         netdev->dev_addr[5] = (u8)u64temp;
1598         memcpy(netdev->perm_addr, netdev->dev_addr, 6);
1599
1600         if ((nesvnic->logical_port < 2) || (nesdev->nesadapter->hw_rev != NE020_REV)) {
1601                 netdev->features |= NETIF_F_TSO | NETIF_F_SG | NETIF_F_IP_CSUM;
1602                 netdev->features |= NETIF_F_GSO | NETIF_F_TSO | NETIF_F_SG | NETIF_F_IP_CSUM;
1603         } else {
1604                 netdev->features |= NETIF_F_SG | NETIF_F_IP_CSUM;
1605         }
1606
1607         nes_debug(NES_DBG_INIT, "nesvnic = %p, reported features = 0x%lX, QPid = %d,"
1608                         " nic_index = %d, logical_port = %d, mac_index = %d.\n",
1609                         nesvnic, (unsigned long)netdev->features, nesvnic->nic.qp_id,
1610                         nesvnic->nic_index, nesvnic->logical_port,  nesdev->mac_index);
1611
1612         if (nesvnic->nesdev->nesadapter->port_count == 1) {
1613                 nesvnic->qp_nic_index[0] = nesvnic->nic_index;
1614                 nesvnic->qp_nic_index[1] = nesvnic->nic_index + 1;
1615                 if (nes_drv_opt & NES_DRV_OPT_DUAL_LOGICAL_PORT) {
1616                         nesvnic->qp_nic_index[2] = 0xf;
1617                         nesvnic->qp_nic_index[3] = 0xf;
1618                 } else {
1619                         nesvnic->qp_nic_index[2] = nesvnic->nic_index + 2;
1620                         nesvnic->qp_nic_index[3] = nesvnic->nic_index + 3;
1621                 }
1622         } else {
1623                 if (nesvnic->nesdev->nesadapter->port_count == 2) {
1624                         nesvnic->qp_nic_index[0] = nesvnic->nic_index;
1625                         nesvnic->qp_nic_index[1] = nesvnic->nic_index + 2;
1626                         nesvnic->qp_nic_index[2] = 0xf;
1627                         nesvnic->qp_nic_index[3] = 0xf;
1628                 } else {
1629                         nesvnic->qp_nic_index[0] = nesvnic->nic_index;
1630                         nesvnic->qp_nic_index[1] = 0xf;
1631                         nesvnic->qp_nic_index[2] = 0xf;
1632                         nesvnic->qp_nic_index[3] = 0xf;
1633                 }
1634         }
1635         nesvnic->next_qp_nic_index = 0;
1636
1637         if (nesdev->netdev_count == 0) {
1638                 nesvnic->rdma_enabled = 1;
1639         } else {
1640                 nesvnic->rdma_enabled = 0;
1641         }
1642         nesvnic->nic_cq.cq_number = nesvnic->nic.qp_id;
1643         spin_lock_init(&nesvnic->tx_lock);
1644         nesdev->netdev[nesdev->netdev_count] = netdev;
1645
1646         nes_debug(NES_DBG_INIT, "Adding nesvnic (%p) to the adapters nesvnic_list for MAC%d.\n",
1647                         nesvnic, nesdev->mac_index);
1648         list_add_tail(&nesvnic->list, &nesdev->nesadapter->nesvnic_list[nesdev->mac_index]);
1649
1650         if ((nesdev->netdev_count == 0) &&
1651             ((PCI_FUNC(nesdev->pcidev->devfn) == nesdev->mac_index) ||
1652              ((nesdev->nesadapter->phy_type[nesdev->mac_index] == NES_PHY_TYPE_PUMA_1G) &&
1653               (((PCI_FUNC(nesdev->pcidev->devfn) == 1) && (nesdev->mac_index == 2)) ||
1654                ((PCI_FUNC(nesdev->pcidev->devfn) == 2) && (nesdev->mac_index == 1)))))) {
1655                 /*
1656                  * nes_debug(NES_DBG_INIT, "Setting up PHY interrupt mask. Using register index 0x%04X\n",
1657                  *              NES_IDX_PHY_PCS_CONTROL_STATUS0 + (0x200 * (nesvnic->logical_port & 1)));
1658                  */
1659                 u32temp = nes_read_indexed(nesdev, NES_IDX_PHY_PCS_CONTROL_STATUS0 +
1660                                 (0x200 * (nesdev->mac_index & 1)));
1661                 if (nesdev->nesadapter->phy_type[nesdev->mac_index] != NES_PHY_TYPE_PUMA_1G) {
1662                         u32temp |= 0x00200000;
1663                         nes_write_indexed(nesdev, NES_IDX_PHY_PCS_CONTROL_STATUS0 +
1664                                 (0x200 * (nesdev->mac_index & 1)), u32temp);
1665                 }
1666
1667                 u32temp = nes_read_indexed(nesdev, NES_IDX_PHY_PCS_CONTROL_STATUS0 +
1668                                 (0x200 * (nesdev->mac_index & 1)));
1669
1670                 if ((u32temp&0x0f1f0000) == 0x0f0f0000) {
1671                         if (nesdev->nesadapter->phy_type[nesdev->mac_index] == NES_PHY_TYPE_IRIS) {
1672                                 nes_init_phy(nesdev);
1673                                 nes_read_10G_phy_reg(nesdev, nesdev->nesadapter->phy_index[nesdev->mac_index], 1, 1);
1674                                 temp_phy_data = (u16)nes_read_indexed(nesdev,
1675                                                                         NES_IDX_MAC_MDIO_CONTROL);
1676                                 u32temp = 20;
1677                                 do {
1678                                         nes_read_10G_phy_reg(nesdev, nesdev->nesadapter->phy_index[nesdev->mac_index], 1, 1);
1679                                         phy_data = (u16)nes_read_indexed(nesdev,
1680                                                                         NES_IDX_MAC_MDIO_CONTROL);
1681                                         if ((phy_data == temp_phy_data) || (!(--u32temp)))
1682                                                 break;
1683                                         temp_phy_data = phy_data;
1684                                 } while (1);
1685                                 if (phy_data & 4) {
1686                                         nes_debug(NES_DBG_INIT, "The Link is UP!!.\n");
1687                                         nesvnic->linkup = 1;
1688                                 } else {
1689                                         nes_debug(NES_DBG_INIT, "The Link is DOWN!!.\n");
1690                                 }
1691                         } else {
1692                                 nes_debug(NES_DBG_INIT, "The Link is UP!!.\n");
1693                                 nesvnic->linkup = 1;
1694                         }
1695                 } else if (nesdev->nesadapter->phy_type[nesdev->mac_index] == NES_PHY_TYPE_PUMA_1G) {
1696                         nes_debug(NES_DBG_INIT, "mac_index=%d, logical_port=%d, u32temp=0x%04X, PCI_FUNC=%d\n",
1697                                 nesdev->mac_index, nesvnic->logical_port, u32temp, PCI_FUNC(nesdev->pcidev->devfn));
1698                         if (((nesdev->mac_index < 2) && ((u32temp&0x01010000) == 0x01010000)) ||
1699                             ((nesdev->mac_index > 1) && ((u32temp&0x02020000) == 0x02020000)))  {
1700                                 nes_debug(NES_DBG_INIT, "The Link is UP!!.\n");
1701                                 nesvnic->linkup = 1;
1702                         }
1703                 }
1704                 /* clear the MAC interrupt status, assumes direct logical to physical mapping */
1705                 u32temp = nes_read_indexed(nesdev, NES_IDX_MAC_INT_STATUS + (0x200 * nesdev->mac_index));
1706                 nes_debug(NES_DBG_INIT, "Phy interrupt status = 0x%X.\n", u32temp);
1707                 nes_write_indexed(nesdev, NES_IDX_MAC_INT_STATUS + (0x200 * nesdev->mac_index), u32temp);
1708
1709                 if (nesdev->nesadapter->phy_type[nesdev->mac_index] != NES_PHY_TYPE_IRIS)
1710                         nes_init_phy(nesdev);
1711
1712         }
1713
1714         return netdev;
1715 }
1716
1717
1718 /**
1719  * nes_netdev_destroy - destroy network device structure
1720  */
1721 void nes_netdev_destroy(struct net_device *netdev)
1722 {
1723         struct nes_vnic *nesvnic = netdev_priv(netdev);
1724
1725         /* make sure 'stop' method is called by Linux stack */
1726         /* nes_netdev_stop(netdev); */
1727
1728         list_del(&nesvnic->list);
1729
1730         if (nesvnic->of_device_registered) {
1731                 nes_destroy_ofa_device(nesvnic->nesibdev);
1732         }
1733
1734         free_netdev(netdev);
1735 }
1736
1737
1738 /**
1739  * nes_nic_cm_xmit -- CM calls this to send out pkts
1740  */
1741 int nes_nic_cm_xmit(struct sk_buff *skb, struct net_device *netdev)
1742 {
1743         int ret;
1744
1745         skb->dev = netdev;
1746         ret = dev_queue_xmit(skb);
1747         if (ret) {
1748                 nes_debug(NES_DBG_CM, "Bad return code from dev_queue_xmit %d\n", ret);
1749         }
1750
1751         return ret;
1752 }