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