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