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