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