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