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