vxge: Remove queue_state references
[linux-2.6.git] / drivers / net / vxge / vxge-main.c
1 /******************************************************************************
2 * This software may be used and distributed according to the terms of
3 * the GNU General Public License (GPL), incorporated herein by reference.
4 * Drivers based on or derived from this code fall under the GPL and must
5 * retain the authorship, copyright and license notice.  This file is not
6 * a complete program and may only be used when the entire operating
7 * system is licensed under the GPL.
8 * See the file COPYING in this distribution for more information.
9 *
10 * vxge-main.c: Driver for Neterion Inc's X3100 Series 10GbE PCIe I/O
11 *              Virtualized Server Adapter.
12 * Copyright(c) 2002-2009 Neterion Inc.
13 *
14 * The module loadable parameters that are supported by the driver and a brief
15 * explanation of all the variables:
16 * vlan_tag_strip:
17 *       Strip VLAN Tag enable/disable. Instructs the device to remove
18 *       the VLAN tag from all received tagged frames that are not
19 *       replicated at the internal L2 switch.
20 *               0 - Do not strip the VLAN tag.
21 *               1 - Strip the VLAN tag.
22 *
23 * addr_learn_en:
24 *       Enable learning the mac address of the guest OS interface in
25 *       a virtualization environment.
26 *               0 - DISABLE
27 *               1 - ENABLE
28 *
29 * max_config_port:
30 *       Maximum number of port to be supported.
31 *               MIN -1 and MAX - 2
32 *
33 * max_config_vpath:
34 *       This configures the maximum no of VPATH configures for each
35 *       device function.
36 *               MIN - 1 and MAX - 17
37 *
38 * max_config_dev:
39 *       This configures maximum no of Device function to be enabled.
40 *               MIN - 1 and MAX - 17
41 *
42 ******************************************************************************/
43
44 #include <linux/if_vlan.h>
45 #include <linux/pci.h>
46 #include <linux/slab.h>
47 #include <linux/tcp.h>
48 #include <net/ip.h>
49 #include <linux/netdevice.h>
50 #include <linux/etherdevice.h>
51 #include "vxge-main.h"
52 #include "vxge-reg.h"
53
54 MODULE_LICENSE("Dual BSD/GPL");
55 MODULE_DESCRIPTION("Neterion's X3100 Series 10GbE PCIe I/O"
56         "Virtualized Server Adapter");
57
58 static DEFINE_PCI_DEVICE_TABLE(vxge_id_table) = {
59         {PCI_VENDOR_ID_S2IO, PCI_DEVICE_ID_TITAN_WIN, PCI_ANY_ID,
60         PCI_ANY_ID},
61         {PCI_VENDOR_ID_S2IO, PCI_DEVICE_ID_TITAN_UNI, PCI_ANY_ID,
62         PCI_ANY_ID},
63         {0}
64 };
65
66 MODULE_DEVICE_TABLE(pci, vxge_id_table);
67
68 VXGE_MODULE_PARAM_INT(vlan_tag_strip, VXGE_HW_VPATH_RPA_STRIP_VLAN_TAG_ENABLE);
69 VXGE_MODULE_PARAM_INT(addr_learn_en, VXGE_HW_MAC_ADDR_LEARN_DEFAULT);
70 VXGE_MODULE_PARAM_INT(max_config_port, VXGE_MAX_CONFIG_PORT);
71 VXGE_MODULE_PARAM_INT(max_config_vpath, VXGE_USE_DEFAULT);
72 VXGE_MODULE_PARAM_INT(max_mac_vpath, VXGE_MAX_MAC_ADDR_COUNT);
73 VXGE_MODULE_PARAM_INT(max_config_dev, VXGE_MAX_CONFIG_DEV);
74
75 static u16 vpath_selector[VXGE_HW_MAX_VIRTUAL_PATHS] =
76                 {0, 1, 3, 3, 7, 7, 7, 7, 15, 15, 15, 15, 15, 15, 15, 15, 31};
77 static unsigned int bw_percentage[VXGE_HW_MAX_VIRTUAL_PATHS] =
78         {[0 ...(VXGE_HW_MAX_VIRTUAL_PATHS - 1)] = 0xFF};
79 module_param_array(bw_percentage, uint, NULL, 0);
80
81 static struct vxge_drv_config *driver_config;
82
83 static inline int is_vxge_card_up(struct vxgedev *vdev)
84 {
85         return test_bit(__VXGE_STATE_CARD_UP, &vdev->state);
86 }
87
88 static inline void VXGE_COMPLETE_VPATH_TX(struct vxge_fifo *fifo)
89 {
90         unsigned long flags = 0;
91         struct sk_buff **skb_ptr = NULL;
92         struct sk_buff **temp;
93 #define NR_SKB_COMPLETED 128
94         struct sk_buff *completed[NR_SKB_COMPLETED];
95         int more;
96
97         do {
98                 more = 0;
99                 skb_ptr = completed;
100
101                 if (spin_trylock_irqsave(&fifo->tx_lock, flags)) {
102                         vxge_hw_vpath_poll_tx(fifo->handle, &skb_ptr,
103                                                 NR_SKB_COMPLETED, &more);
104                         spin_unlock_irqrestore(&fifo->tx_lock, flags);
105                 }
106                 /* free SKBs */
107                 for (temp = completed; temp != skb_ptr; temp++)
108                         dev_kfree_skb_irq(*temp);
109         } while (more) ;
110 }
111
112 static inline void VXGE_COMPLETE_ALL_TX(struct vxgedev *vdev)
113 {
114         int i;
115
116         /* Complete all transmits */
117         for (i = 0; i < vdev->no_of_vpath; i++)
118                 VXGE_COMPLETE_VPATH_TX(&vdev->vpaths[i].fifo);
119 }
120
121 static inline void VXGE_COMPLETE_ALL_RX(struct vxgedev *vdev)
122 {
123         int i;
124         struct vxge_ring *ring;
125
126         /* Complete all receives*/
127         for (i = 0; i < vdev->no_of_vpath; i++) {
128                 ring = &vdev->vpaths[i].ring;
129                 vxge_hw_vpath_poll_rx(ring->handle);
130         }
131 }
132
133 /*
134  * MultiQ manipulation helper functions
135  */
136 static inline int vxge_netif_queue_stopped(struct vxge_fifo *fifo)
137 {
138         struct net_device *dev = fifo->ndev;
139         struct netdev_queue *txq = NULL;
140         int vpath_no = fifo->driver_id;
141         int ret = 0;
142
143         if (fifo->tx_steering_type)
144                 txq = netdev_get_tx_queue(dev, vpath_no);
145         else
146                 txq = netdev_get_tx_queue(dev, 0);
147
148         ret = netif_tx_queue_stopped(txq);
149         return ret;
150 }
151
152 void vxge_stop_tx_queue(struct vxge_fifo *fifo)
153 {
154         struct net_device *dev = fifo->ndev;
155         struct netdev_queue *txq = NULL;
156
157         if (fifo->tx_steering_type)
158                 txq = netdev_get_tx_queue(dev, fifo->driver_id);
159         else
160                 txq = netdev_get_tx_queue(dev, 0);
161
162         netif_tx_stop_queue(txq);
163 }
164
165 void vxge_wake_tx_queue(struct vxge_fifo *fifo)
166 {
167         struct net_device *dev = fifo->ndev;
168         struct netdev_queue *txq = NULL;
169         int vpath_no = fifo->driver_id;
170
171         if (fifo->tx_steering_type)
172                 txq = netdev_get_tx_queue(dev, vpath_no);
173         else
174                 txq = netdev_get_tx_queue(dev, 0);
175
176         if (netif_tx_queue_stopped(txq))
177                 netif_tx_wake_queue(txq);
178 }
179
180 /*
181  * vxge_callback_link_up
182  *
183  * This function is called during interrupt context to notify link up state
184  * change.
185  */
186 void
187 vxge_callback_link_up(struct __vxge_hw_device *hldev)
188 {
189         struct net_device *dev = hldev->ndev;
190         struct vxgedev *vdev = (struct vxgedev *)netdev_priv(dev);
191
192         vxge_debug_entryexit(VXGE_TRACE, "%s: %s:%d",
193                 vdev->ndev->name, __func__, __LINE__);
194         printk(KERN_NOTICE "%s: Link Up\n", vdev->ndev->name);
195         vdev->stats.link_up++;
196
197         netif_carrier_on(vdev->ndev);
198         netif_tx_wake_all_queues(vdev->ndev);
199
200         vxge_debug_entryexit(VXGE_TRACE,
201                 "%s: %s:%d Exiting...", vdev->ndev->name, __func__, __LINE__);
202 }
203
204 /*
205  * vxge_callback_link_down
206  *
207  * This function is called during interrupt context to notify link down state
208  * change.
209  */
210 void
211 vxge_callback_link_down(struct __vxge_hw_device *hldev)
212 {
213         struct net_device *dev = hldev->ndev;
214         struct vxgedev *vdev = (struct vxgedev *)netdev_priv(dev);
215
216         vxge_debug_entryexit(VXGE_TRACE,
217                 "%s: %s:%d", vdev->ndev->name, __func__, __LINE__);
218         printk(KERN_NOTICE "%s: Link Down\n", vdev->ndev->name);
219
220         vdev->stats.link_down++;
221         netif_carrier_off(vdev->ndev);
222         netif_tx_stop_all_queues(vdev->ndev);
223
224         vxge_debug_entryexit(VXGE_TRACE,
225                 "%s: %s:%d Exiting...", vdev->ndev->name, __func__, __LINE__);
226 }
227
228 /*
229  * vxge_rx_alloc
230  *
231  * Allocate SKB.
232  */
233 static struct sk_buff*
234 vxge_rx_alloc(void *dtrh, struct vxge_ring *ring, const int skb_size)
235 {
236         struct net_device    *dev;
237         struct sk_buff       *skb;
238         struct vxge_rx_priv *rx_priv;
239
240         dev = ring->ndev;
241         vxge_debug_entryexit(VXGE_TRACE, "%s: %s:%d",
242                 ring->ndev->name, __func__, __LINE__);
243
244         rx_priv = vxge_hw_ring_rxd_private_get(dtrh);
245
246         /* try to allocate skb first. this one may fail */
247         skb = netdev_alloc_skb(dev, skb_size +
248         VXGE_HW_HEADER_ETHERNET_II_802_3_ALIGN);
249         if (skb == NULL) {
250                 vxge_debug_mem(VXGE_ERR,
251                         "%s: out of memory to allocate SKB", dev->name);
252                 ring->stats.skb_alloc_fail++;
253                 return NULL;
254         }
255
256         vxge_debug_mem(VXGE_TRACE,
257                 "%s: %s:%d  Skb : 0x%p", ring->ndev->name,
258                 __func__, __LINE__, skb);
259
260         skb_reserve(skb, VXGE_HW_HEADER_ETHERNET_II_802_3_ALIGN);
261
262         rx_priv->skb = skb;
263         rx_priv->skb_data = NULL;
264         rx_priv->data_size = skb_size;
265         vxge_debug_entryexit(VXGE_TRACE,
266                 "%s: %s:%d Exiting...", ring->ndev->name, __func__, __LINE__);
267
268         return skb;
269 }
270
271 /*
272  * vxge_rx_map
273  */
274 static int vxge_rx_map(void *dtrh, struct vxge_ring *ring)
275 {
276         struct vxge_rx_priv *rx_priv;
277         dma_addr_t dma_addr;
278
279         vxge_debug_entryexit(VXGE_TRACE, "%s: %s:%d",
280                 ring->ndev->name, __func__, __LINE__);
281         rx_priv = vxge_hw_ring_rxd_private_get(dtrh);
282
283         rx_priv->skb_data = rx_priv->skb->data;
284         dma_addr = pci_map_single(ring->pdev, rx_priv->skb_data,
285                                 rx_priv->data_size, PCI_DMA_FROMDEVICE);
286
287         if (unlikely(pci_dma_mapping_error(ring->pdev, dma_addr))) {
288                 ring->stats.pci_map_fail++;
289                 return -EIO;
290         }
291         vxge_debug_mem(VXGE_TRACE,
292                 "%s: %s:%d  1 buffer mode dma_addr = 0x%llx",
293                 ring->ndev->name, __func__, __LINE__,
294                 (unsigned long long)dma_addr);
295         vxge_hw_ring_rxd_1b_set(dtrh, dma_addr, rx_priv->data_size);
296
297         rx_priv->data_dma = dma_addr;
298         vxge_debug_entryexit(VXGE_TRACE,
299                 "%s: %s:%d Exiting...", ring->ndev->name, __func__, __LINE__);
300
301         return 0;
302 }
303
304 /*
305  * vxge_rx_initial_replenish
306  * Allocation of RxD as an initial replenish procedure.
307  */
308 static enum vxge_hw_status
309 vxge_rx_initial_replenish(void *dtrh, void *userdata)
310 {
311         struct vxge_ring *ring = (struct vxge_ring *)userdata;
312         struct vxge_rx_priv *rx_priv;
313
314         vxge_debug_entryexit(VXGE_TRACE, "%s: %s:%d",
315                 ring->ndev->name, __func__, __LINE__);
316         if (vxge_rx_alloc(dtrh, ring,
317                           VXGE_LL_MAX_FRAME_SIZE(ring->ndev)) == NULL)
318                 return VXGE_HW_FAIL;
319
320         if (vxge_rx_map(dtrh, ring)) {
321                 rx_priv = vxge_hw_ring_rxd_private_get(dtrh);
322                 dev_kfree_skb(rx_priv->skb);
323
324                 return VXGE_HW_FAIL;
325         }
326         vxge_debug_entryexit(VXGE_TRACE,
327                 "%s: %s:%d Exiting...", ring->ndev->name, __func__, __LINE__);
328
329         return VXGE_HW_OK;
330 }
331
332 static inline void
333 vxge_rx_complete(struct vxge_ring *ring, struct sk_buff *skb, u16 vlan,
334                  int pkt_length, struct vxge_hw_ring_rxd_info *ext_info)
335 {
336
337         vxge_debug_entryexit(VXGE_TRACE, "%s: %s:%d",
338                         ring->ndev->name, __func__, __LINE__);
339         skb_record_rx_queue(skb, ring->driver_id);
340         skb->protocol = eth_type_trans(skb, ring->ndev);
341
342         ring->stats.rx_frms++;
343         ring->stats.rx_bytes += pkt_length;
344
345         if (skb->pkt_type == PACKET_MULTICAST)
346                 ring->stats.rx_mcast++;
347
348         vxge_debug_rx(VXGE_TRACE,
349                 "%s: %s:%d  skb protocol = %d",
350                 ring->ndev->name, __func__, __LINE__, skb->protocol);
351
352         if (ring->gro_enable) {
353                 if (ring->vlgrp && ext_info->vlan &&
354                         (ring->vlan_tag_strip ==
355                                 VXGE_HW_VPATH_RPA_STRIP_VLAN_TAG_ENABLE))
356                         vlan_gro_receive(ring->napi_p, ring->vlgrp,
357                                         ext_info->vlan, skb);
358                 else
359                         napi_gro_receive(ring->napi_p, skb);
360         } else {
361                 if (ring->vlgrp && vlan &&
362                         (ring->vlan_tag_strip ==
363                                 VXGE_HW_VPATH_RPA_STRIP_VLAN_TAG_ENABLE))
364                         vlan_hwaccel_receive_skb(skb, ring->vlgrp, vlan);
365                 else
366                         netif_receive_skb(skb);
367         }
368         vxge_debug_entryexit(VXGE_TRACE,
369                 "%s: %s:%d Exiting...", ring->ndev->name, __func__, __LINE__);
370 }
371
372 static inline void vxge_re_pre_post(void *dtr, struct vxge_ring *ring,
373                                     struct vxge_rx_priv *rx_priv)
374 {
375         pci_dma_sync_single_for_device(ring->pdev,
376                 rx_priv->data_dma, rx_priv->data_size, PCI_DMA_FROMDEVICE);
377
378         vxge_hw_ring_rxd_1b_set(dtr, rx_priv->data_dma, rx_priv->data_size);
379         vxge_hw_ring_rxd_pre_post(ring->handle, dtr);
380 }
381
382 static inline void vxge_post(int *dtr_cnt, void **first_dtr,
383                              void *post_dtr, struct __vxge_hw_ring *ringh)
384 {
385         int dtr_count = *dtr_cnt;
386         if ((*dtr_cnt % VXGE_HW_RXSYNC_FREQ_CNT) == 0) {
387                 if (*first_dtr)
388                         vxge_hw_ring_rxd_post_post_wmb(ringh, *first_dtr);
389                 *first_dtr = post_dtr;
390         } else
391                 vxge_hw_ring_rxd_post_post(ringh, post_dtr);
392         dtr_count++;
393         *dtr_cnt = dtr_count;
394 }
395
396 /*
397  * vxge_rx_1b_compl
398  *
399  * If the interrupt is because of a received frame or if the receive ring
400  * contains fresh as yet un-processed frames, this function is called.
401  */
402 enum vxge_hw_status
403 vxge_rx_1b_compl(struct __vxge_hw_ring *ringh, void *dtr,
404                  u8 t_code, void *userdata)
405 {
406         struct vxge_ring *ring = (struct vxge_ring *)userdata;
407         struct  net_device *dev = ring->ndev;
408         unsigned int dma_sizes;
409         void *first_dtr = NULL;
410         int dtr_cnt = 0;
411         int data_size;
412         dma_addr_t data_dma;
413         int pkt_length;
414         struct sk_buff *skb;
415         struct vxge_rx_priv *rx_priv;
416         struct vxge_hw_ring_rxd_info ext_info;
417         vxge_debug_entryexit(VXGE_TRACE, "%s: %s:%d",
418                 ring->ndev->name, __func__, __LINE__);
419         ring->pkts_processed = 0;
420
421         vxge_hw_ring_replenish(ringh);
422
423         do {
424                 prefetch((char *)dtr + L1_CACHE_BYTES);
425                 rx_priv = vxge_hw_ring_rxd_private_get(dtr);
426                 skb = rx_priv->skb;
427                 data_size = rx_priv->data_size;
428                 data_dma = rx_priv->data_dma;
429                 prefetch(rx_priv->skb_data);
430
431                 vxge_debug_rx(VXGE_TRACE,
432                         "%s: %s:%d  skb = 0x%p",
433                         ring->ndev->name, __func__, __LINE__, skb);
434
435                 vxge_hw_ring_rxd_1b_get(ringh, dtr, &dma_sizes);
436                 pkt_length = dma_sizes;
437
438                 pkt_length -= ETH_FCS_LEN;
439
440                 vxge_debug_rx(VXGE_TRACE,
441                         "%s: %s:%d  Packet Length = %d",
442                         ring->ndev->name, __func__, __LINE__, pkt_length);
443
444                 vxge_hw_ring_rxd_1b_info_get(ringh, dtr, &ext_info);
445
446                 /* check skb validity */
447                 vxge_assert(skb);
448
449                 prefetch((char *)skb + L1_CACHE_BYTES);
450                 if (unlikely(t_code)) {
451
452                         if (vxge_hw_ring_handle_tcode(ringh, dtr, t_code) !=
453                                 VXGE_HW_OK) {
454
455                                 ring->stats.rx_errors++;
456                                 vxge_debug_rx(VXGE_TRACE,
457                                         "%s: %s :%d Rx T_code is %d",
458                                         ring->ndev->name, __func__,
459                                         __LINE__, t_code);
460
461                                 /* If the t_code is not supported and if the
462                                  * t_code is other than 0x5 (unparseable packet
463                                  * such as unknown UPV6 header), Drop it !!!
464                                  */
465                                 vxge_re_pre_post(dtr, ring, rx_priv);
466
467                                 vxge_post(&dtr_cnt, &first_dtr, dtr, ringh);
468                                 ring->stats.rx_dropped++;
469                                 continue;
470                         }
471                 }
472
473                 if (pkt_length > VXGE_LL_RX_COPY_THRESHOLD) {
474
475                         if (vxge_rx_alloc(dtr, ring, data_size) != NULL) {
476
477                                 if (!vxge_rx_map(dtr, ring)) {
478                                         skb_put(skb, pkt_length);
479
480                                         pci_unmap_single(ring->pdev, data_dma,
481                                                 data_size, PCI_DMA_FROMDEVICE);
482
483                                         vxge_hw_ring_rxd_pre_post(ringh, dtr);
484                                         vxge_post(&dtr_cnt, &first_dtr, dtr,
485                                                 ringh);
486                                 } else {
487                                         dev_kfree_skb(rx_priv->skb);
488                                         rx_priv->skb = skb;
489                                         rx_priv->data_size = data_size;
490                                         vxge_re_pre_post(dtr, ring, rx_priv);
491
492                                         vxge_post(&dtr_cnt, &first_dtr, dtr,
493                                                 ringh);
494                                         ring->stats.rx_dropped++;
495                                         break;
496                                 }
497                         } else {
498                                 vxge_re_pre_post(dtr, ring, rx_priv);
499
500                                 vxge_post(&dtr_cnt, &first_dtr, dtr, ringh);
501                                 ring->stats.rx_dropped++;
502                                 break;
503                         }
504                 } else {
505                         struct sk_buff *skb_up;
506
507                         skb_up = netdev_alloc_skb(dev, pkt_length +
508                                 VXGE_HW_HEADER_ETHERNET_II_802_3_ALIGN);
509                         if (skb_up != NULL) {
510                                 skb_reserve(skb_up,
511                                     VXGE_HW_HEADER_ETHERNET_II_802_3_ALIGN);
512
513                                 pci_dma_sync_single_for_cpu(ring->pdev,
514                                         data_dma, data_size,
515                                         PCI_DMA_FROMDEVICE);
516
517                                 vxge_debug_mem(VXGE_TRACE,
518                                         "%s: %s:%d  skb_up = %p",
519                                         ring->ndev->name, __func__,
520                                         __LINE__, skb);
521                                 memcpy(skb_up->data, skb->data, pkt_length);
522
523                                 vxge_re_pre_post(dtr, ring, rx_priv);
524
525                                 vxge_post(&dtr_cnt, &first_dtr, dtr,
526                                         ringh);
527                                 /* will netif_rx small SKB instead */
528                                 skb = skb_up;
529                                 skb_put(skb, pkt_length);
530                         } else {
531                                 vxge_re_pre_post(dtr, ring, rx_priv);
532
533                                 vxge_post(&dtr_cnt, &first_dtr, dtr, ringh);
534                                 vxge_debug_rx(VXGE_ERR,
535                                         "%s: vxge_rx_1b_compl: out of "
536                                         "memory", dev->name);
537                                 ring->stats.skb_alloc_fail++;
538                                 break;
539                         }
540                 }
541
542                 if ((ext_info.proto & VXGE_HW_FRAME_PROTO_TCP_OR_UDP) &&
543                     !(ext_info.proto & VXGE_HW_FRAME_PROTO_IP_FRAG) &&
544                     ring->rx_csum && /* Offload Rx side CSUM */
545                     ext_info.l3_cksum == VXGE_HW_L3_CKSUM_OK &&
546                     ext_info.l4_cksum == VXGE_HW_L4_CKSUM_OK)
547                         skb->ip_summed = CHECKSUM_UNNECESSARY;
548                 else
549                         skb->ip_summed = CHECKSUM_NONE;
550
551                 vxge_rx_complete(ring, skb, ext_info.vlan,
552                         pkt_length, &ext_info);
553
554                 ring->budget--;
555                 ring->pkts_processed++;
556                 if (!ring->budget)
557                         break;
558
559         } while (vxge_hw_ring_rxd_next_completed(ringh, &dtr,
560                 &t_code) == VXGE_HW_OK);
561
562         if (first_dtr)
563                 vxge_hw_ring_rxd_post_post_wmb(ringh, first_dtr);
564
565         vxge_debug_entryexit(VXGE_TRACE,
566                                 "%s:%d  Exiting...",
567                                 __func__, __LINE__);
568         return VXGE_HW_OK;
569 }
570
571 /*
572  * vxge_xmit_compl
573  *
574  * If an interrupt was raised to indicate DMA complete of the Tx packet,
575  * this function is called. It identifies the last TxD whose buffer was
576  * freed and frees all skbs whose data have already DMA'ed into the NICs
577  * internal memory.
578  */
579 enum vxge_hw_status
580 vxge_xmit_compl(struct __vxge_hw_fifo *fifo_hw, void *dtr,
581                 enum vxge_hw_fifo_tcode t_code, void *userdata,
582                 struct sk_buff ***skb_ptr, int nr_skb, int *more)
583 {
584         struct vxge_fifo *fifo = (struct vxge_fifo *)userdata;
585         struct sk_buff *skb, **done_skb = *skb_ptr;
586         int pkt_cnt = 0;
587
588         vxge_debug_entryexit(VXGE_TRACE,
589                 "%s:%d Entered....", __func__, __LINE__);
590
591         do {
592                 int frg_cnt;
593                 skb_frag_t *frag;
594                 int i = 0, j;
595                 struct vxge_tx_priv *txd_priv =
596                         vxge_hw_fifo_txdl_private_get(dtr);
597
598                 skb = txd_priv->skb;
599                 frg_cnt = skb_shinfo(skb)->nr_frags;
600                 frag = &skb_shinfo(skb)->frags[0];
601
602                 vxge_debug_tx(VXGE_TRACE,
603                                 "%s: %s:%d fifo_hw = %p dtr = %p "
604                                 "tcode = 0x%x", fifo->ndev->name, __func__,
605                                 __LINE__, fifo_hw, dtr, t_code);
606                 /* check skb validity */
607                 vxge_assert(skb);
608                 vxge_debug_tx(VXGE_TRACE,
609                         "%s: %s:%d skb = %p itxd_priv = %p frg_cnt = %d",
610                         fifo->ndev->name, __func__, __LINE__,
611                         skb, txd_priv, frg_cnt);
612                 if (unlikely(t_code)) {
613                         fifo->stats.tx_errors++;
614                         vxge_debug_tx(VXGE_ERR,
615                                 "%s: tx: dtr %p completed due to "
616                                 "error t_code %01x", fifo->ndev->name,
617                                 dtr, t_code);
618                         vxge_hw_fifo_handle_tcode(fifo_hw, dtr, t_code);
619                 }
620
621                 /*  for unfragmented skb */
622                 pci_unmap_single(fifo->pdev, txd_priv->dma_buffers[i++],
623                                 skb_headlen(skb), PCI_DMA_TODEVICE);
624
625                 for (j = 0; j < frg_cnt; j++) {
626                         pci_unmap_page(fifo->pdev,
627                                         txd_priv->dma_buffers[i++],
628                                         frag->size, PCI_DMA_TODEVICE);
629                         frag += 1;
630                 }
631
632                 vxge_hw_fifo_txdl_free(fifo_hw, dtr);
633
634                 /* Updating the statistics block */
635                 fifo->stats.tx_frms++;
636                 fifo->stats.tx_bytes += skb->len;
637
638                 *done_skb++ = skb;
639
640                 if (--nr_skb <= 0) {
641                         *more = 1;
642                         break;
643                 }
644
645                 pkt_cnt++;
646                 if (pkt_cnt > fifo->indicate_max_pkts)
647                         break;
648
649         } while (vxge_hw_fifo_txdl_next_completed(fifo_hw,
650                                 &dtr, &t_code) == VXGE_HW_OK);
651
652         *skb_ptr = done_skb;
653         vxge_wake_tx_queue(fifo);
654
655         vxge_debug_entryexit(VXGE_TRACE,
656                                 "%s: %s:%d  Exiting...",
657                                 fifo->ndev->name, __func__, __LINE__);
658         return VXGE_HW_OK;
659 }
660
661 /* select a vpath to transmit the packet */
662 static u32 vxge_get_vpath_no(struct vxgedev *vdev, struct sk_buff *skb,
663         int *do_lock)
664 {
665         u16 queue_len, counter = 0;
666         if (skb->protocol == htons(ETH_P_IP)) {
667                 struct iphdr *ip;
668                 struct tcphdr *th;
669
670                 ip = ip_hdr(skb);
671
672                 if ((ip->frag_off & htons(IP_OFFSET|IP_MF)) == 0) {
673                         th = (struct tcphdr *)(((unsigned char *)ip) +
674                                         ip->ihl*4);
675
676                         queue_len = vdev->no_of_vpath;
677                         counter = (ntohs(th->source) +
678                                 ntohs(th->dest)) &
679                                 vdev->vpath_selector[queue_len - 1];
680                         if (counter >= queue_len)
681                                 counter = queue_len - 1;
682
683                         if (ip->protocol == IPPROTO_UDP) {
684 #ifdef NETIF_F_LLTX
685                                 *do_lock = 0;
686 #endif
687                         }
688                 }
689         }
690         return counter;
691 }
692
693 static enum vxge_hw_status vxge_search_mac_addr_in_list(
694         struct vxge_vpath *vpath, u64 del_mac)
695 {
696         struct list_head *entry, *next;
697         list_for_each_safe(entry, next, &vpath->mac_addr_list) {
698                 if (((struct vxge_mac_addrs *)entry)->macaddr == del_mac)
699                         return TRUE;
700         }
701         return FALSE;
702 }
703
704 static int vxge_learn_mac(struct vxgedev *vdev, u8 *mac_header)
705 {
706         struct macInfo mac_info;
707         u8 *mac_address = NULL;
708         u64 mac_addr = 0, vpath_vector = 0;
709         int vpath_idx = 0;
710         enum vxge_hw_status status = VXGE_HW_OK;
711         struct vxge_vpath *vpath = NULL;
712         struct __vxge_hw_device *hldev;
713
714         hldev = (struct __vxge_hw_device *) pci_get_drvdata(vdev->pdev);
715
716         mac_address = (u8 *)&mac_addr;
717         memcpy(mac_address, mac_header, ETH_ALEN);
718
719         /* Is this mac address already in the list? */
720         for (vpath_idx = 0; vpath_idx < vdev->no_of_vpath; vpath_idx++) {
721                 vpath = &vdev->vpaths[vpath_idx];
722                 if (vxge_search_mac_addr_in_list(vpath, mac_addr))
723                         return vpath_idx;
724         }
725
726         memset(&mac_info, 0, sizeof(struct macInfo));
727         memcpy(mac_info.macaddr, mac_header, ETH_ALEN);
728
729         /* Any vpath has room to add mac address to its da table? */
730         for (vpath_idx = 0; vpath_idx < vdev->no_of_vpath; vpath_idx++) {
731                 vpath = &vdev->vpaths[vpath_idx];
732                 if (vpath->mac_addr_cnt < vpath->max_mac_addr_cnt) {
733                         /* Add this mac address to this vpath */
734                         mac_info.vpath_no = vpath_idx;
735                         mac_info.state = VXGE_LL_MAC_ADDR_IN_DA_TABLE;
736                         status = vxge_add_mac_addr(vdev, &mac_info);
737                         if (status != VXGE_HW_OK)
738                                 return -EPERM;
739                         return vpath_idx;
740                 }
741         }
742
743         mac_info.state = VXGE_LL_MAC_ADDR_IN_LIST;
744         vpath_idx = 0;
745         mac_info.vpath_no = vpath_idx;
746         /* Is the first vpath already selected as catch-basin ? */
747         vpath = &vdev->vpaths[vpath_idx];
748         if (vpath->mac_addr_cnt > vpath->max_mac_addr_cnt) {
749                 /* Add this mac address to this vpath */
750                 if (FALSE == vxge_mac_list_add(vpath, &mac_info))
751                         return -EPERM;
752                 return vpath_idx;
753         }
754
755         /* Select first vpath as catch-basin */
756         vpath_vector = vxge_mBIT(vpath->device_id);
757         status = vxge_hw_mgmt_reg_write(vpath->vdev->devh,
758                                 vxge_hw_mgmt_reg_type_mrpcim,
759                                 0,
760                                 (ulong)offsetof(
761                                         struct vxge_hw_mrpcim_reg,
762                                         rts_mgr_cbasin_cfg),
763                                 vpath_vector);
764         if (status != VXGE_HW_OK) {
765                 vxge_debug_tx(VXGE_ERR,
766                         "%s: Unable to set the vpath-%d in catch-basin mode",
767                         VXGE_DRIVER_NAME, vpath->device_id);
768                 return -EPERM;
769         }
770
771         if (FALSE == vxge_mac_list_add(vpath, &mac_info))
772                 return -EPERM;
773
774         return vpath_idx;
775 }
776
777 /**
778  * vxge_xmit
779  * @skb : the socket buffer containing the Tx data.
780  * @dev : device pointer.
781  *
782  * This function is the Tx entry point of the driver. Neterion NIC supports
783  * certain protocol assist features on Tx side, namely  CSO, S/G, LSO.
784  * NOTE: when device cant queue the pkt, just the trans_start variable will
785  * not be upadted.
786 */
787 static netdev_tx_t
788 vxge_xmit(struct sk_buff *skb, struct net_device *dev)
789 {
790         struct vxge_fifo *fifo = NULL;
791         void *dtr_priv;
792         void *dtr = NULL;
793         struct vxgedev *vdev = NULL;
794         enum vxge_hw_status status;
795         int frg_cnt, first_frg_len;
796         skb_frag_t *frag;
797         int i = 0, j = 0, avail;
798         u64 dma_pointer;
799         struct vxge_tx_priv *txdl_priv = NULL;
800         struct __vxge_hw_fifo *fifo_hw;
801         int offload_type;
802         unsigned long flags = 0;
803         int vpath_no = 0;
804         int do_spin_tx_lock = 1;
805
806         vxge_debug_entryexit(VXGE_TRACE, "%s: %s:%d",
807                         dev->name, __func__, __LINE__);
808
809         /* A buffer with no data will be dropped */
810         if (unlikely(skb->len <= 0)) {
811                 vxge_debug_tx(VXGE_ERR,
812                         "%s: Buffer has no data..", dev->name);
813                 dev_kfree_skb(skb);
814                 return NETDEV_TX_OK;
815         }
816
817         vdev = (struct vxgedev *)netdev_priv(dev);
818
819         if (unlikely(!is_vxge_card_up(vdev))) {
820                 vxge_debug_tx(VXGE_ERR,
821                         "%s: vdev not initialized", dev->name);
822                 dev_kfree_skb(skb);
823                 return NETDEV_TX_OK;
824         }
825
826         if (vdev->config.addr_learn_en) {
827                 vpath_no = vxge_learn_mac(vdev, skb->data + ETH_ALEN);
828                 if (vpath_no == -EPERM) {
829                         vxge_debug_tx(VXGE_ERR,
830                                 "%s: Failed to store the mac address",
831                                 dev->name);
832                         dev_kfree_skb(skb);
833                         return NETDEV_TX_OK;
834                 }
835         }
836
837         if (vdev->config.tx_steering_type == TX_MULTIQ_STEERING)
838                 vpath_no = skb_get_queue_mapping(skb);
839         else if (vdev->config.tx_steering_type == TX_PORT_STEERING)
840                 vpath_no = vxge_get_vpath_no(vdev, skb, &do_spin_tx_lock);
841
842         vxge_debug_tx(VXGE_TRACE, "%s: vpath_no= %d", dev->name, vpath_no);
843
844         if (vpath_no >= vdev->no_of_vpath)
845                 vpath_no = 0;
846
847         fifo = &vdev->vpaths[vpath_no].fifo;
848         fifo_hw = fifo->handle;
849
850         if (do_spin_tx_lock)
851                 spin_lock_irqsave(&fifo->tx_lock, flags);
852         else {
853                 if (unlikely(!spin_trylock_irqsave(&fifo->tx_lock, flags)))
854                         return NETDEV_TX_LOCKED;
855         }
856
857         if (vxge_netif_queue_stopped(fifo)) {
858                 spin_unlock_irqrestore(&fifo->tx_lock, flags);
859                 return NETDEV_TX_BUSY;
860         }
861
862         avail = vxge_hw_fifo_free_txdl_count_get(fifo_hw);
863         if (avail == 0) {
864                 vxge_debug_tx(VXGE_ERR,
865                         "%s: No free TXDs available", dev->name);
866                 fifo->stats.txd_not_free++;
867                 vxge_stop_tx_queue(fifo);
868                 goto _exit2;
869         }
870
871         /* Last TXD?  Stop tx queue to avoid dropping packets.  TX
872          * completion will resume the queue.
873          */
874         if (avail == 1)
875                 vxge_stop_tx_queue(fifo);
876
877         status = vxge_hw_fifo_txdl_reserve(fifo_hw, &dtr, &dtr_priv);
878         if (unlikely(status != VXGE_HW_OK)) {
879                 vxge_debug_tx(VXGE_ERR,
880                    "%s: Out of descriptors .", dev->name);
881                 fifo->stats.txd_out_of_desc++;
882                 vxge_stop_tx_queue(fifo);
883                 goto _exit2;
884         }
885
886         vxge_debug_tx(VXGE_TRACE,
887                 "%s: %s:%d fifo_hw = %p dtr = %p dtr_priv = %p",
888                 dev->name, __func__, __LINE__,
889                 fifo_hw, dtr, dtr_priv);
890
891         if (vdev->vlgrp && vlan_tx_tag_present(skb)) {
892                 u16 vlan_tag = vlan_tx_tag_get(skb);
893                 vxge_hw_fifo_txdl_vlan_set(dtr, vlan_tag);
894         }
895
896         first_frg_len = skb_headlen(skb);
897
898         dma_pointer = pci_map_single(fifo->pdev, skb->data, first_frg_len,
899                                 PCI_DMA_TODEVICE);
900
901         if (unlikely(pci_dma_mapping_error(fifo->pdev, dma_pointer))) {
902                 vxge_hw_fifo_txdl_free(fifo_hw, dtr);
903                 vxge_stop_tx_queue(fifo);
904                 fifo->stats.pci_map_fail++;
905                 goto _exit2;
906         }
907
908         txdl_priv = vxge_hw_fifo_txdl_private_get(dtr);
909         txdl_priv->skb = skb;
910         txdl_priv->dma_buffers[j] = dma_pointer;
911
912         frg_cnt = skb_shinfo(skb)->nr_frags;
913         vxge_debug_tx(VXGE_TRACE,
914                         "%s: %s:%d skb = %p txdl_priv = %p "
915                         "frag_cnt = %d dma_pointer = 0x%llx", dev->name,
916                         __func__, __LINE__, skb, txdl_priv,
917                         frg_cnt, (unsigned long long)dma_pointer);
918
919         vxge_hw_fifo_txdl_buffer_set(fifo_hw, dtr, j++, dma_pointer,
920                 first_frg_len);
921
922         frag = &skb_shinfo(skb)->frags[0];
923         for (i = 0; i < frg_cnt; i++) {
924                 /* ignore 0 length fragment */
925                 if (!frag->size)
926                         continue;
927
928                 dma_pointer =
929                         (u64)pci_map_page(fifo->pdev, frag->page,
930                                 frag->page_offset, frag->size,
931                                 PCI_DMA_TODEVICE);
932
933                 if (unlikely(pci_dma_mapping_error(fifo->pdev, dma_pointer)))
934                         goto _exit0;
935                 vxge_debug_tx(VXGE_TRACE,
936                         "%s: %s:%d frag = %d dma_pointer = 0x%llx",
937                                 dev->name, __func__, __LINE__, i,
938                                 (unsigned long long)dma_pointer);
939
940                 txdl_priv->dma_buffers[j] = dma_pointer;
941                 vxge_hw_fifo_txdl_buffer_set(fifo_hw, dtr, j++, dma_pointer,
942                                         frag->size);
943                 frag += 1;
944         }
945
946         offload_type = vxge_offload_type(skb);
947
948         if (offload_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)) {
949
950                 int mss = vxge_tcp_mss(skb);
951                 if (mss) {
952                         vxge_debug_tx(VXGE_TRACE,
953                                 "%s: %s:%d mss = %d",
954                                 dev->name, __func__, __LINE__, mss);
955                         vxge_hw_fifo_txdl_mss_set(dtr, mss);
956                 } else {
957                         vxge_assert(skb->len <=
958                                 dev->mtu + VXGE_HW_MAC_HEADER_MAX_SIZE);
959                         vxge_assert(0);
960                         goto _exit1;
961                 }
962         }
963
964         if (skb->ip_summed == CHECKSUM_PARTIAL)
965                 vxge_hw_fifo_txdl_cksum_set_bits(dtr,
966                                         VXGE_HW_FIFO_TXD_TX_CKO_IPV4_EN |
967                                         VXGE_HW_FIFO_TXD_TX_CKO_TCP_EN |
968                                         VXGE_HW_FIFO_TXD_TX_CKO_UDP_EN);
969
970         vxge_hw_fifo_txdl_post(fifo_hw, dtr);
971 #ifdef NETIF_F_LLTX
972         dev->trans_start = jiffies; /* NETIF_F_LLTX driver :( */
973 #endif
974         spin_unlock_irqrestore(&fifo->tx_lock, flags);
975
976         VXGE_COMPLETE_VPATH_TX(fifo);
977         vxge_debug_entryexit(VXGE_TRACE, "%s: %s:%d  Exiting...",
978                 dev->name, __func__, __LINE__);
979         return NETDEV_TX_OK;
980
981 _exit0:
982         vxge_debug_tx(VXGE_TRACE, "%s: pci_map_page failed", dev->name);
983
984 _exit1:
985         j = 0;
986         frag = &skb_shinfo(skb)->frags[0];
987
988         pci_unmap_single(fifo->pdev, txdl_priv->dma_buffers[j++],
989                         skb_headlen(skb), PCI_DMA_TODEVICE);
990
991         for (; j < i; j++) {
992                 pci_unmap_page(fifo->pdev, txdl_priv->dma_buffers[j],
993                         frag->size, PCI_DMA_TODEVICE);
994                 frag += 1;
995         }
996
997         vxge_hw_fifo_txdl_free(fifo_hw, dtr);
998 _exit2:
999         dev_kfree_skb(skb);
1000         spin_unlock_irqrestore(&fifo->tx_lock, flags);
1001         VXGE_COMPLETE_VPATH_TX(fifo);
1002
1003         return NETDEV_TX_OK;
1004 }
1005
1006 /*
1007  * vxge_rx_term
1008  *
1009  * Function will be called by hw function to abort all outstanding receive
1010  * descriptors.
1011  */
1012 static void
1013 vxge_rx_term(void *dtrh, enum vxge_hw_rxd_state state, void *userdata)
1014 {
1015         struct vxge_ring *ring = (struct vxge_ring *)userdata;
1016         struct vxge_rx_priv *rx_priv =
1017                 vxge_hw_ring_rxd_private_get(dtrh);
1018
1019         vxge_debug_entryexit(VXGE_TRACE, "%s: %s:%d",
1020                         ring->ndev->name, __func__, __LINE__);
1021         if (state != VXGE_HW_RXD_STATE_POSTED)
1022                 return;
1023
1024         pci_unmap_single(ring->pdev, rx_priv->data_dma,
1025                 rx_priv->data_size, PCI_DMA_FROMDEVICE);
1026
1027         dev_kfree_skb(rx_priv->skb);
1028         rx_priv->skb_data = NULL;
1029
1030         vxge_debug_entryexit(VXGE_TRACE,
1031                 "%s: %s:%d  Exiting...",
1032                 ring->ndev->name, __func__, __LINE__);
1033 }
1034
1035 /*
1036  * vxge_tx_term
1037  *
1038  * Function will be called to abort all outstanding tx descriptors
1039  */
1040 static void
1041 vxge_tx_term(void *dtrh, enum vxge_hw_txdl_state state, void *userdata)
1042 {
1043         struct vxge_fifo *fifo = (struct vxge_fifo *)userdata;
1044         skb_frag_t *frag;
1045         int i = 0, j, frg_cnt;
1046         struct vxge_tx_priv *txd_priv = vxge_hw_fifo_txdl_private_get(dtrh);
1047         struct sk_buff *skb = txd_priv->skb;
1048
1049         vxge_debug_entryexit(VXGE_TRACE, "%s:%d", __func__, __LINE__);
1050
1051         if (state != VXGE_HW_TXDL_STATE_POSTED)
1052                 return;
1053
1054         /* check skb validity */
1055         vxge_assert(skb);
1056         frg_cnt = skb_shinfo(skb)->nr_frags;
1057         frag = &skb_shinfo(skb)->frags[0];
1058
1059         /*  for unfragmented skb */
1060         pci_unmap_single(fifo->pdev, txd_priv->dma_buffers[i++],
1061                 skb_headlen(skb), PCI_DMA_TODEVICE);
1062
1063         for (j = 0; j < frg_cnt; j++) {
1064                 pci_unmap_page(fifo->pdev, txd_priv->dma_buffers[i++],
1065                                frag->size, PCI_DMA_TODEVICE);
1066                 frag += 1;
1067         }
1068
1069         dev_kfree_skb(skb);
1070
1071         vxge_debug_entryexit(VXGE_TRACE,
1072                 "%s:%d  Exiting...", __func__, __LINE__);
1073 }
1074
1075 /**
1076  * vxge_set_multicast
1077  * @dev: pointer to the device structure
1078  *
1079  * Entry point for multicast address enable/disable
1080  * This function is a driver entry point which gets called by the kernel
1081  * whenever multicast addresses must be enabled/disabled. This also gets
1082  * called to set/reset promiscuous mode. Depending on the deivce flag, we
1083  * determine, if multicast address must be enabled or if promiscuous mode
1084  * is to be disabled etc.
1085  */
1086 static void vxge_set_multicast(struct net_device *dev)
1087 {
1088         struct netdev_hw_addr *ha;
1089         struct vxgedev *vdev;
1090         int i, mcast_cnt = 0;
1091         struct __vxge_hw_device  *hldev;
1092         enum vxge_hw_status status = VXGE_HW_OK;
1093         struct macInfo mac_info;
1094         int vpath_idx = 0;
1095         struct vxge_mac_addrs *mac_entry;
1096         struct list_head *list_head;
1097         struct list_head *entry, *next;
1098         u8 *mac_address = NULL;
1099
1100         vxge_debug_entryexit(VXGE_TRACE,
1101                 "%s:%d", __func__, __LINE__);
1102
1103         vdev = (struct vxgedev *)netdev_priv(dev);
1104         hldev = (struct __vxge_hw_device  *)vdev->devh;
1105
1106         if (unlikely(!is_vxge_card_up(vdev)))
1107                 return;
1108
1109         if ((dev->flags & IFF_ALLMULTI) && (!vdev->all_multi_flg)) {
1110                 for (i = 0; i < vdev->no_of_vpath; i++) {
1111                         vxge_assert(vdev->vpaths[i].is_open);
1112                         status = vxge_hw_vpath_mcast_enable(
1113                                                 vdev->vpaths[i].handle);
1114                         vdev->all_multi_flg = 1;
1115                 }
1116         } else if ((dev->flags & IFF_ALLMULTI) && (vdev->all_multi_flg)) {
1117                 for (i = 0; i < vdev->no_of_vpath; i++) {
1118                         vxge_assert(vdev->vpaths[i].is_open);
1119                         status = vxge_hw_vpath_mcast_disable(
1120                                                 vdev->vpaths[i].handle);
1121                         vdev->all_multi_flg = 1;
1122                 }
1123         }
1124
1125         if (status != VXGE_HW_OK)
1126                 vxge_debug_init(VXGE_ERR,
1127                         "failed to %s multicast, status %d",
1128                         dev->flags & IFF_ALLMULTI ?
1129                         "enable" : "disable", status);
1130
1131         if (!vdev->config.addr_learn_en) {
1132                 if (dev->flags & IFF_PROMISC) {
1133                         for (i = 0; i < vdev->no_of_vpath; i++) {
1134                                 vxge_assert(vdev->vpaths[i].is_open);
1135                                 status = vxge_hw_vpath_promisc_enable(
1136                                                 vdev->vpaths[i].handle);
1137                         }
1138                 } else {
1139                         for (i = 0; i < vdev->no_of_vpath; i++) {
1140                                 vxge_assert(vdev->vpaths[i].is_open);
1141                                 status = vxge_hw_vpath_promisc_disable(
1142                                                 vdev->vpaths[i].handle);
1143                         }
1144                 }
1145         }
1146
1147         memset(&mac_info, 0, sizeof(struct macInfo));
1148         /* Update individual M_CAST address list */
1149         if ((!vdev->all_multi_flg) && netdev_mc_count(dev)) {
1150
1151                 mcast_cnt = vdev->vpaths[0].mcast_addr_cnt;
1152                 list_head = &vdev->vpaths[0].mac_addr_list;
1153                 if ((netdev_mc_count(dev) +
1154                         (vdev->vpaths[0].mac_addr_cnt - mcast_cnt)) >
1155                                 vdev->vpaths[0].max_mac_addr_cnt)
1156                         goto _set_all_mcast;
1157
1158                 /* Delete previous MC's */
1159                 for (i = 0; i < mcast_cnt; i++) {
1160                         if (!list_empty(list_head))
1161                                 mac_entry = (struct vxge_mac_addrs *)
1162                                         list_first_entry(list_head,
1163                                                 struct vxge_mac_addrs,
1164                                                 item);
1165
1166                         list_for_each_safe(entry, next, list_head) {
1167
1168                                 mac_entry = (struct vxge_mac_addrs *) entry;
1169                                 /* Copy the mac address to delete */
1170                                 mac_address = (u8 *)&mac_entry->macaddr;
1171                                 memcpy(mac_info.macaddr, mac_address, ETH_ALEN);
1172
1173                                 /* Is this a multicast address */
1174                                 if (0x01 & mac_info.macaddr[0]) {
1175                                         for (vpath_idx = 0; vpath_idx <
1176                                                 vdev->no_of_vpath;
1177                                                 vpath_idx++) {
1178                                                 mac_info.vpath_no = vpath_idx;
1179                                                 status = vxge_del_mac_addr(
1180                                                                 vdev,
1181                                                                 &mac_info);
1182                                         }
1183                                 }
1184                         }
1185                 }
1186
1187                 /* Add new ones */
1188                 netdev_for_each_mc_addr(ha, dev) {
1189                         memcpy(mac_info.macaddr, ha->addr, ETH_ALEN);
1190                         for (vpath_idx = 0; vpath_idx < vdev->no_of_vpath;
1191                                         vpath_idx++) {
1192                                 mac_info.vpath_no = vpath_idx;
1193                                 mac_info.state = VXGE_LL_MAC_ADDR_IN_DA_TABLE;
1194                                 status = vxge_add_mac_addr(vdev, &mac_info);
1195                                 if (status != VXGE_HW_OK) {
1196                                         vxge_debug_init(VXGE_ERR,
1197                                                 "%s:%d Setting individual"
1198                                                 "multicast address failed",
1199                                                 __func__, __LINE__);
1200                                         goto _set_all_mcast;
1201                                 }
1202                         }
1203                 }
1204
1205                 return;
1206 _set_all_mcast:
1207                 mcast_cnt = vdev->vpaths[0].mcast_addr_cnt;
1208                 /* Delete previous MC's */
1209                 for (i = 0; i < mcast_cnt; i++) {
1210
1211                         list_for_each_safe(entry, next, list_head) {
1212
1213                                 mac_entry = (struct vxge_mac_addrs *) entry;
1214                                 /* Copy the mac address to delete */
1215                                 mac_address = (u8 *)&mac_entry->macaddr;
1216                                 memcpy(mac_info.macaddr, mac_address, ETH_ALEN);
1217
1218                                 /* Is this a multicast address */
1219                                 if (0x01 & mac_info.macaddr[0])
1220                                         break;
1221                         }
1222
1223                         for (vpath_idx = 0; vpath_idx < vdev->no_of_vpath;
1224                                         vpath_idx++) {
1225                                 mac_info.vpath_no = vpath_idx;
1226                                 status = vxge_del_mac_addr(vdev, &mac_info);
1227                         }
1228                 }
1229
1230                 /* Enable all multicast */
1231                 for (i = 0; i < vdev->no_of_vpath; i++) {
1232                         vxge_assert(vdev->vpaths[i].is_open);
1233                         status = vxge_hw_vpath_mcast_enable(
1234                                                 vdev->vpaths[i].handle);
1235                         if (status != VXGE_HW_OK) {
1236                                 vxge_debug_init(VXGE_ERR,
1237                                         "%s:%d Enabling all multicasts failed",
1238                                          __func__, __LINE__);
1239                         }
1240                         vdev->all_multi_flg = 1;
1241                 }
1242                 dev->flags |= IFF_ALLMULTI;
1243         }
1244
1245         vxge_debug_entryexit(VXGE_TRACE,
1246                 "%s:%d  Exiting...", __func__, __LINE__);
1247 }
1248
1249 /**
1250  * vxge_set_mac_addr
1251  * @dev: pointer to the device structure
1252  *
1253  * Update entry "0" (default MAC addr)
1254  */
1255 static int vxge_set_mac_addr(struct net_device *dev, void *p)
1256 {
1257         struct sockaddr *addr = p;
1258         struct vxgedev *vdev;
1259         struct __vxge_hw_device  *hldev;
1260         enum vxge_hw_status status = VXGE_HW_OK;
1261         struct macInfo mac_info_new, mac_info_old;
1262         int vpath_idx = 0;
1263
1264         vxge_debug_entryexit(VXGE_TRACE, "%s:%d", __func__, __LINE__);
1265
1266         vdev = (struct vxgedev *)netdev_priv(dev);
1267         hldev = vdev->devh;
1268
1269         if (!is_valid_ether_addr(addr->sa_data))
1270                 return -EINVAL;
1271
1272         memset(&mac_info_new, 0, sizeof(struct macInfo));
1273         memset(&mac_info_old, 0, sizeof(struct macInfo));
1274
1275         vxge_debug_entryexit(VXGE_TRACE, "%s:%d  Exiting...",
1276                 __func__, __LINE__);
1277
1278         /* Get the old address */
1279         memcpy(mac_info_old.macaddr, dev->dev_addr, dev->addr_len);
1280
1281         /* Copy the new address */
1282         memcpy(mac_info_new.macaddr, addr->sa_data, dev->addr_len);
1283
1284         /* First delete the old mac address from all the vpaths
1285         as we can't specify the index while adding new mac address */
1286         for (vpath_idx = 0; vpath_idx < vdev->no_of_vpath; vpath_idx++) {
1287                 struct vxge_vpath *vpath = &vdev->vpaths[vpath_idx];
1288                 if (!vpath->is_open) {
1289                         /* This can happen when this interface is added/removed
1290                         to the bonding interface. Delete this station address
1291                         from the linked list */
1292                         vxge_mac_list_del(vpath, &mac_info_old);
1293
1294                         /* Add this new address to the linked list
1295                         for later restoring */
1296                         vxge_mac_list_add(vpath, &mac_info_new);
1297
1298                         continue;
1299                 }
1300                 /* Delete the station address */
1301                 mac_info_old.vpath_no = vpath_idx;
1302                 status = vxge_del_mac_addr(vdev, &mac_info_old);
1303         }
1304
1305         if (unlikely(!is_vxge_card_up(vdev))) {
1306                 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
1307                 return VXGE_HW_OK;
1308         }
1309
1310         /* Set this mac address to all the vpaths */
1311         for (vpath_idx = 0; vpath_idx < vdev->no_of_vpath; vpath_idx++) {
1312                 mac_info_new.vpath_no = vpath_idx;
1313                 mac_info_new.state = VXGE_LL_MAC_ADDR_IN_DA_TABLE;
1314                 status = vxge_add_mac_addr(vdev, &mac_info_new);
1315                 if (status != VXGE_HW_OK)
1316                         return -EINVAL;
1317         }
1318
1319         memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
1320
1321         return status;
1322 }
1323
1324 /*
1325  * vxge_vpath_intr_enable
1326  * @vdev: pointer to vdev
1327  * @vp_id: vpath for which to enable the interrupts
1328  *
1329  * Enables the interrupts for the vpath
1330 */
1331 void vxge_vpath_intr_enable(struct vxgedev *vdev, int vp_id)
1332 {
1333         struct vxge_vpath *vpath = &vdev->vpaths[vp_id];
1334         int msix_id = 0;
1335         int tim_msix_id[4] = {0, 1, 0, 0};
1336         int alarm_msix_id = VXGE_ALARM_MSIX_ID;
1337
1338         vxge_hw_vpath_intr_enable(vpath->handle);
1339
1340         if (vdev->config.intr_type == INTA)
1341                 vxge_hw_vpath_inta_unmask_tx_rx(vpath->handle);
1342         else {
1343                 vxge_hw_vpath_msix_set(vpath->handle, tim_msix_id,
1344                         alarm_msix_id);
1345
1346                 msix_id = vpath->device_id * VXGE_HW_VPATH_MSIX_ACTIVE;
1347                 vxge_hw_vpath_msix_unmask(vpath->handle, msix_id);
1348                 vxge_hw_vpath_msix_unmask(vpath->handle, msix_id + 1);
1349
1350                 /* enable the alarm vector */
1351                 msix_id = (vpath->handle->vpath->hldev->first_vp_id *
1352                         VXGE_HW_VPATH_MSIX_ACTIVE) + alarm_msix_id;
1353                 vxge_hw_vpath_msix_unmask(vpath->handle, msix_id);
1354         }
1355 }
1356
1357 /*
1358  * vxge_vpath_intr_disable
1359  * @vdev: pointer to vdev
1360  * @vp_id: vpath for which to disable the interrupts
1361  *
1362  * Disables the interrupts for the vpath
1363 */
1364 void vxge_vpath_intr_disable(struct vxgedev *vdev, int vp_id)
1365 {
1366         struct vxge_vpath *vpath = &vdev->vpaths[vp_id];
1367         int msix_id;
1368
1369         vxge_hw_vpath_intr_disable(vpath->handle);
1370
1371         if (vdev->config.intr_type == INTA)
1372                 vxge_hw_vpath_inta_mask_tx_rx(vpath->handle);
1373         else {
1374                 msix_id = vpath->device_id * VXGE_HW_VPATH_MSIX_ACTIVE;
1375                 vxge_hw_vpath_msix_mask(vpath->handle, msix_id);
1376                 vxge_hw_vpath_msix_mask(vpath->handle, msix_id + 1);
1377
1378                 /* disable the alarm vector */
1379                 msix_id = (vpath->handle->vpath->hldev->first_vp_id *
1380                         VXGE_HW_VPATH_MSIX_ACTIVE) + VXGE_ALARM_MSIX_ID;
1381                 vxge_hw_vpath_msix_mask(vpath->handle, msix_id);
1382         }
1383 }
1384
1385 /*
1386  * vxge_reset_vpath
1387  * @vdev: pointer to vdev
1388  * @vp_id: vpath to reset
1389  *
1390  * Resets the vpath
1391 */
1392 static int vxge_reset_vpath(struct vxgedev *vdev, int vp_id)
1393 {
1394         enum vxge_hw_status status = VXGE_HW_OK;
1395         int ret = 0;
1396
1397         /* check if device is down already */
1398         if (unlikely(!is_vxge_card_up(vdev)))
1399                 return 0;
1400
1401         /* is device reset already scheduled */
1402         if (test_bit(__VXGE_STATE_RESET_CARD, &vdev->state))
1403                 return 0;
1404
1405         if (vdev->vpaths[vp_id].handle) {
1406                 if (vxge_hw_vpath_reset(vdev->vpaths[vp_id].handle)
1407                                 == VXGE_HW_OK) {
1408                         if (is_vxge_card_up(vdev) &&
1409                                 vxge_hw_vpath_recover_from_reset(
1410                                         vdev->vpaths[vp_id].handle)
1411                                         != VXGE_HW_OK) {
1412                                 vxge_debug_init(VXGE_ERR,
1413                                         "vxge_hw_vpath_recover_from_reset"
1414                                         "failed for vpath:%d", vp_id);
1415                                 return status;
1416                         }
1417                 } else {
1418                         vxge_debug_init(VXGE_ERR,
1419                                 "vxge_hw_vpath_reset failed for"
1420                                 "vpath:%d", vp_id);
1421                                 return status;
1422                 }
1423         } else
1424                 return VXGE_HW_FAIL;
1425
1426         vxge_restore_vpath_mac_addr(&vdev->vpaths[vp_id]);
1427         vxge_restore_vpath_vid_table(&vdev->vpaths[vp_id]);
1428
1429         /* Enable all broadcast */
1430         vxge_hw_vpath_bcast_enable(vdev->vpaths[vp_id].handle);
1431
1432         /* Enable the interrupts */
1433         vxge_vpath_intr_enable(vdev, vp_id);
1434
1435         smp_wmb();
1436
1437         /* Enable the flow of traffic through the vpath */
1438         vxge_hw_vpath_enable(vdev->vpaths[vp_id].handle);
1439
1440         smp_wmb();
1441         vxge_hw_vpath_rx_doorbell_init(vdev->vpaths[vp_id].handle);
1442         vdev->vpaths[vp_id].ring.last_status = VXGE_HW_OK;
1443
1444         /* Vpath reset done */
1445         clear_bit(vp_id, &vdev->vp_reset);
1446
1447         /* Start the vpath queue */
1448         vxge_wake_tx_queue(&vdev->vpaths[vp_id].fifo);
1449
1450         return ret;
1451 }
1452
1453 static int do_vxge_reset(struct vxgedev *vdev, int event)
1454 {
1455         enum vxge_hw_status status;
1456         int ret = 0, vp_id, i;
1457
1458         vxge_debug_entryexit(VXGE_TRACE, "%s:%d", __func__, __LINE__);
1459
1460         if ((event == VXGE_LL_FULL_RESET) || (event == VXGE_LL_START_RESET)) {
1461                 /* check if device is down already */
1462                 if (unlikely(!is_vxge_card_up(vdev)))
1463                         return 0;
1464
1465                 /* is reset already scheduled */
1466                 if (test_and_set_bit(__VXGE_STATE_RESET_CARD, &vdev->state))
1467                         return 0;
1468         }
1469
1470         if (event == VXGE_LL_FULL_RESET) {
1471                 /* wait for all the vpath reset to complete */
1472                 for (vp_id = 0; vp_id < vdev->no_of_vpath; vp_id++) {
1473                         while (test_bit(vp_id, &vdev->vp_reset))
1474                                 msleep(50);
1475                 }
1476
1477                 /* if execution mode is set to debug, don't reset the adapter */
1478                 if (unlikely(vdev->exec_mode)) {
1479                         vxge_debug_init(VXGE_ERR,
1480                                 "%s: execution mode is debug, returning..",
1481                                 vdev->ndev->name);
1482                 clear_bit(__VXGE_STATE_CARD_UP, &vdev->state);
1483                 netif_tx_stop_all_queues(vdev->ndev);
1484                 return 0;
1485                 }
1486         }
1487
1488         if (event == VXGE_LL_FULL_RESET) {
1489                 vxge_hw_device_intr_disable(vdev->devh);
1490
1491                 switch (vdev->cric_err_event) {
1492                 case VXGE_HW_EVENT_UNKNOWN:
1493                         netif_tx_stop_all_queues(vdev->ndev);
1494                         vxge_debug_init(VXGE_ERR,
1495                                 "fatal: %s: Disabling device due to"
1496                                 "unknown error",
1497                                 vdev->ndev->name);
1498                         ret = -EPERM;
1499                         goto out;
1500                 case VXGE_HW_EVENT_RESET_START:
1501                         break;
1502                 case VXGE_HW_EVENT_RESET_COMPLETE:
1503                 case VXGE_HW_EVENT_LINK_DOWN:
1504                 case VXGE_HW_EVENT_LINK_UP:
1505                 case VXGE_HW_EVENT_ALARM_CLEARED:
1506                 case VXGE_HW_EVENT_ECCERR:
1507                 case VXGE_HW_EVENT_MRPCIM_ECCERR:
1508                         ret = -EPERM;
1509                         goto out;
1510                 case VXGE_HW_EVENT_FIFO_ERR:
1511                 case VXGE_HW_EVENT_VPATH_ERR:
1512                         break;
1513                 case VXGE_HW_EVENT_CRITICAL_ERR:
1514                         netif_tx_stop_all_queues(vdev->ndev);
1515                         vxge_debug_init(VXGE_ERR,
1516                                 "fatal: %s: Disabling device due to"
1517                                 "serious error",
1518                                 vdev->ndev->name);
1519                         /* SOP or device reset required */
1520                         /* This event is not currently used */
1521                         ret = -EPERM;
1522                         goto out;
1523                 case VXGE_HW_EVENT_SERR:
1524                         netif_tx_stop_all_queues(vdev->ndev);
1525                         vxge_debug_init(VXGE_ERR,
1526                                 "fatal: %s: Disabling device due to"
1527                                 "serious error",
1528                                 vdev->ndev->name);
1529                         ret = -EPERM;
1530                         goto out;
1531                 case VXGE_HW_EVENT_SRPCIM_SERR:
1532                 case VXGE_HW_EVENT_MRPCIM_SERR:
1533                         ret = -EPERM;
1534                         goto out;
1535                 case VXGE_HW_EVENT_SLOT_FREEZE:
1536                         netif_tx_stop_all_queues(vdev->ndev);
1537                         vxge_debug_init(VXGE_ERR,
1538                                 "fatal: %s: Disabling device due to"
1539                                 "slot freeze",
1540                                 vdev->ndev->name);
1541                         ret = -EPERM;
1542                         goto out;
1543                 default:
1544                         break;
1545
1546                 }
1547         }
1548
1549         if ((event == VXGE_LL_FULL_RESET) || (event == VXGE_LL_START_RESET))
1550                 netif_tx_stop_all_queues(vdev->ndev);
1551
1552         if (event == VXGE_LL_FULL_RESET) {
1553                 status = vxge_reset_all_vpaths(vdev);
1554                 if (status != VXGE_HW_OK) {
1555                         vxge_debug_init(VXGE_ERR,
1556                                 "fatal: %s: can not reset vpaths",
1557                                 vdev->ndev->name);
1558                         ret = -EPERM;
1559                         goto out;
1560                 }
1561         }
1562
1563         if (event == VXGE_LL_COMPL_RESET) {
1564                 for (i = 0; i < vdev->no_of_vpath; i++)
1565                         if (vdev->vpaths[i].handle) {
1566                                 if (vxge_hw_vpath_recover_from_reset(
1567                                         vdev->vpaths[i].handle)
1568                                                 != VXGE_HW_OK) {
1569                                         vxge_debug_init(VXGE_ERR,
1570                                                 "vxge_hw_vpath_recover_"
1571                                                 "from_reset failed for vpath: "
1572                                                 "%d", i);
1573                                         ret = -EPERM;
1574                                         goto out;
1575                                 }
1576                                 } else {
1577                                         vxge_debug_init(VXGE_ERR,
1578                                         "vxge_hw_vpath_reset failed for "
1579                                                 "vpath:%d", i);
1580                                         ret = -EPERM;
1581                                         goto out;
1582                                 }
1583         }
1584
1585         if ((event == VXGE_LL_FULL_RESET) || (event == VXGE_LL_COMPL_RESET)) {
1586                 /* Reprogram the DA table with populated mac addresses */
1587                 for (vp_id = 0; vp_id < vdev->no_of_vpath; vp_id++) {
1588                         vxge_restore_vpath_mac_addr(&vdev->vpaths[vp_id]);
1589                         vxge_restore_vpath_vid_table(&vdev->vpaths[vp_id]);
1590                 }
1591
1592                 /* enable vpath interrupts */
1593                 for (i = 0; i < vdev->no_of_vpath; i++)
1594                         vxge_vpath_intr_enable(vdev, i);
1595
1596                 vxge_hw_device_intr_enable(vdev->devh);
1597
1598                 smp_wmb();
1599
1600                 /* Indicate card up */
1601                 set_bit(__VXGE_STATE_CARD_UP, &vdev->state);
1602
1603                 /* Get the traffic to flow through the vpaths */
1604                 for (i = 0; i < vdev->no_of_vpath; i++) {
1605                         vxge_hw_vpath_enable(vdev->vpaths[i].handle);
1606                         smp_wmb();
1607                         vxge_hw_vpath_rx_doorbell_init(vdev->vpaths[i].handle);
1608                 }
1609
1610                 netif_tx_wake_all_queues(vdev->ndev);
1611         }
1612
1613 out:
1614         vxge_debug_entryexit(VXGE_TRACE,
1615                 "%s:%d  Exiting...", __func__, __LINE__);
1616
1617         /* Indicate reset done */
1618         if ((event == VXGE_LL_FULL_RESET) || (event == VXGE_LL_COMPL_RESET))
1619                 clear_bit(__VXGE_STATE_RESET_CARD, &vdev->state);
1620         return ret;
1621 }
1622
1623 /*
1624  * vxge_reset
1625  * @vdev: pointer to ll device
1626  *
1627  * driver may reset the chip on events of serr, eccerr, etc
1628  */
1629 int vxge_reset(struct vxgedev *vdev)
1630 {
1631         do_vxge_reset(vdev, VXGE_LL_FULL_RESET);
1632         return 0;
1633 }
1634
1635 /**
1636  * vxge_poll - Receive handler when Receive Polling is used.
1637  * @dev: pointer to the device structure.
1638  * @budget: Number of packets budgeted to be processed in this iteration.
1639  *
1640  * This function comes into picture only if Receive side is being handled
1641  * through polling (called NAPI in linux). It mostly does what the normal
1642  * Rx interrupt handler does in terms of descriptor and packet processing
1643  * but not in an interrupt context. Also it will process a specified number
1644  * of packets at most in one iteration. This value is passed down by the
1645  * kernel as the function argument 'budget'.
1646  */
1647 static int vxge_poll_msix(struct napi_struct *napi, int budget)
1648 {
1649         struct vxge_ring *ring =
1650                 container_of(napi, struct vxge_ring, napi);
1651         int budget_org = budget;
1652         ring->budget = budget;
1653
1654         vxge_hw_vpath_poll_rx(ring->handle);
1655
1656         if (ring->pkts_processed < budget_org) {
1657                 napi_complete(napi);
1658                 /* Re enable the Rx interrupts for the vpath */
1659                 vxge_hw_channel_msix_unmask(
1660                                 (struct __vxge_hw_channel *)ring->handle,
1661                                 ring->rx_vector_no);
1662         }
1663
1664         return ring->pkts_processed;
1665 }
1666
1667 static int vxge_poll_inta(struct napi_struct *napi, int budget)
1668 {
1669         struct vxgedev *vdev = container_of(napi, struct vxgedev, napi);
1670         int pkts_processed = 0;
1671         int i;
1672         int budget_org = budget;
1673         struct vxge_ring *ring;
1674
1675         struct __vxge_hw_device  *hldev = (struct __vxge_hw_device *)
1676                 pci_get_drvdata(vdev->pdev);
1677
1678         for (i = 0; i < vdev->no_of_vpath; i++) {
1679                 ring = &vdev->vpaths[i].ring;
1680                 ring->budget = budget;
1681                 vxge_hw_vpath_poll_rx(ring->handle);
1682                 pkts_processed += ring->pkts_processed;
1683                 budget -= ring->pkts_processed;
1684                 if (budget <= 0)
1685                         break;
1686         }
1687
1688         VXGE_COMPLETE_ALL_TX(vdev);
1689
1690         if (pkts_processed < budget_org) {
1691                 napi_complete(napi);
1692                 /* Re enable the Rx interrupts for the ring */
1693                 vxge_hw_device_unmask_all(hldev);
1694                 vxge_hw_device_flush_io(hldev);
1695         }
1696
1697         return pkts_processed;
1698 }
1699
1700 #ifdef CONFIG_NET_POLL_CONTROLLER
1701 /**
1702  * vxge_netpoll - netpoll event handler entry point
1703  * @dev : pointer to the device structure.
1704  * Description:
1705  *      This function will be called by upper layer to check for events on the
1706  * interface in situations where interrupts are disabled. It is used for
1707  * specific in-kernel networking tasks, such as remote consoles and kernel
1708  * debugging over the network (example netdump in RedHat).
1709  */
1710 static void vxge_netpoll(struct net_device *dev)
1711 {
1712         struct __vxge_hw_device  *hldev;
1713         struct vxgedev *vdev;
1714
1715         vdev = (struct vxgedev *)netdev_priv(dev);
1716         hldev = (struct __vxge_hw_device  *)pci_get_drvdata(vdev->pdev);
1717
1718         vxge_debug_entryexit(VXGE_TRACE, "%s:%d", __func__, __LINE__);
1719
1720         if (pci_channel_offline(vdev->pdev))
1721                 return;
1722
1723         disable_irq(dev->irq);
1724         vxge_hw_device_clear_tx_rx(hldev);
1725
1726         vxge_hw_device_clear_tx_rx(hldev);
1727         VXGE_COMPLETE_ALL_RX(vdev);
1728         VXGE_COMPLETE_ALL_TX(vdev);
1729
1730         enable_irq(dev->irq);
1731
1732         vxge_debug_entryexit(VXGE_TRACE,
1733                 "%s:%d  Exiting...", __func__, __LINE__);
1734 }
1735 #endif
1736
1737 /* RTH configuration */
1738 static enum vxge_hw_status vxge_rth_configure(struct vxgedev *vdev)
1739 {
1740         enum vxge_hw_status status = VXGE_HW_OK;
1741         struct vxge_hw_rth_hash_types hash_types;
1742         u8 itable[256] = {0}; /* indirection table */
1743         u8 mtable[256] = {0}; /* CPU to vpath mapping  */
1744         int index;
1745
1746         /*
1747          * Filling
1748          *      - itable with bucket numbers
1749          *      - mtable with bucket-to-vpath mapping
1750          */
1751         for (index = 0; index < (1 << vdev->config.rth_bkt_sz); index++) {
1752                 itable[index] = index;
1753                 mtable[index] = index % vdev->no_of_vpath;
1754         }
1755
1756         /* Fill RTH hash types */
1757         hash_types.hash_type_tcpipv4_en   = vdev->config.rth_hash_type_tcpipv4;
1758         hash_types.hash_type_ipv4_en      = vdev->config.rth_hash_type_ipv4;
1759         hash_types.hash_type_tcpipv6_en   = vdev->config.rth_hash_type_tcpipv6;
1760         hash_types.hash_type_ipv6_en      = vdev->config.rth_hash_type_ipv6;
1761         hash_types.hash_type_tcpipv6ex_en =
1762                                         vdev->config.rth_hash_type_tcpipv6ex;
1763         hash_types.hash_type_ipv6ex_en    = vdev->config.rth_hash_type_ipv6ex;
1764
1765         /* set indirection table, bucket-to-vpath mapping */
1766         status = vxge_hw_vpath_rts_rth_itable_set(vdev->vp_handles,
1767                                                 vdev->no_of_vpath,
1768                                                 mtable, itable,
1769                                                 vdev->config.rth_bkt_sz);
1770         if (status != VXGE_HW_OK) {
1771                 vxge_debug_init(VXGE_ERR,
1772                         "RTH indirection table configuration failed "
1773                         "for vpath:%d", vdev->vpaths[0].device_id);
1774                 return status;
1775         }
1776
1777         /*
1778         * Because the itable_set() method uses the active_table field
1779         * for the target virtual path the RTH config should be updated
1780         * for all VPATHs. The h/w only uses the lowest numbered VPATH
1781         * when steering frames.
1782         */
1783          for (index = 0; index < vdev->no_of_vpath; index++) {
1784                 status = vxge_hw_vpath_rts_rth_set(
1785                                 vdev->vpaths[index].handle,
1786                                 vdev->config.rth_algorithm,
1787                                 &hash_types,
1788                                 vdev->config.rth_bkt_sz);
1789
1790                  if (status != VXGE_HW_OK) {
1791                         vxge_debug_init(VXGE_ERR,
1792                                 "RTH configuration failed for vpath:%d",
1793                                 vdev->vpaths[index].device_id);
1794                         return status;
1795                  }
1796          }
1797
1798         return status;
1799 }
1800
1801 int vxge_mac_list_add(struct vxge_vpath *vpath, struct macInfo *mac)
1802 {
1803         struct vxge_mac_addrs *new_mac_entry;
1804         u8 *mac_address = NULL;
1805
1806         if (vpath->mac_addr_cnt >= VXGE_MAX_LEARN_MAC_ADDR_CNT)
1807                 return TRUE;
1808
1809         new_mac_entry = kzalloc(sizeof(struct vxge_mac_addrs), GFP_ATOMIC);
1810         if (!new_mac_entry) {
1811                 vxge_debug_mem(VXGE_ERR,
1812                         "%s: memory allocation failed",
1813                         VXGE_DRIVER_NAME);
1814                 return FALSE;
1815         }
1816
1817         list_add(&new_mac_entry->item, &vpath->mac_addr_list);
1818
1819         /* Copy the new mac address to the list */
1820         mac_address = (u8 *)&new_mac_entry->macaddr;
1821         memcpy(mac_address, mac->macaddr, ETH_ALEN);
1822
1823         new_mac_entry->state = mac->state;
1824         vpath->mac_addr_cnt++;
1825
1826         /* Is this a multicast address */
1827         if (0x01 & mac->macaddr[0])
1828                 vpath->mcast_addr_cnt++;
1829
1830         return TRUE;
1831 }
1832
1833 /* Add a mac address to DA table */
1834 enum vxge_hw_status vxge_add_mac_addr(struct vxgedev *vdev, struct macInfo *mac)
1835 {
1836         enum vxge_hw_status status = VXGE_HW_OK;
1837         struct vxge_vpath *vpath;
1838         enum vxge_hw_vpath_mac_addr_add_mode duplicate_mode;
1839
1840         if (0x01 & mac->macaddr[0]) /* multicast address */
1841                 duplicate_mode = VXGE_HW_VPATH_MAC_ADDR_ADD_DUPLICATE;
1842         else
1843                 duplicate_mode = VXGE_HW_VPATH_MAC_ADDR_REPLACE_DUPLICATE;
1844
1845         vpath = &vdev->vpaths[mac->vpath_no];
1846         status = vxge_hw_vpath_mac_addr_add(vpath->handle, mac->macaddr,
1847                                                 mac->macmask, duplicate_mode);
1848         if (status != VXGE_HW_OK) {
1849                 vxge_debug_init(VXGE_ERR,
1850                         "DA config add entry failed for vpath:%d",
1851                         vpath->device_id);
1852         } else
1853                 if (FALSE == vxge_mac_list_add(vpath, mac))
1854                         status = -EPERM;
1855
1856         return status;
1857 }
1858
1859 int vxge_mac_list_del(struct vxge_vpath *vpath, struct macInfo *mac)
1860 {
1861         struct list_head *entry, *next;
1862         u64 del_mac = 0;
1863         u8 *mac_address = (u8 *) (&del_mac);
1864
1865         /* Copy the mac address to delete from the list */
1866         memcpy(mac_address, mac->macaddr, ETH_ALEN);
1867
1868         list_for_each_safe(entry, next, &vpath->mac_addr_list) {
1869                 if (((struct vxge_mac_addrs *)entry)->macaddr == del_mac) {
1870                         list_del(entry);
1871                         kfree((struct vxge_mac_addrs *)entry);
1872                         vpath->mac_addr_cnt--;
1873
1874                         /* Is this a multicast address */
1875                         if (0x01 & mac->macaddr[0])
1876                                 vpath->mcast_addr_cnt--;
1877                         return TRUE;
1878                 }
1879         }
1880
1881         return FALSE;
1882 }
1883 /* delete a mac address from DA table */
1884 enum vxge_hw_status vxge_del_mac_addr(struct vxgedev *vdev, struct macInfo *mac)
1885 {
1886         enum vxge_hw_status status = VXGE_HW_OK;
1887         struct vxge_vpath *vpath;
1888
1889         vpath = &vdev->vpaths[mac->vpath_no];
1890         status = vxge_hw_vpath_mac_addr_delete(vpath->handle, mac->macaddr,
1891                                                 mac->macmask);
1892         if (status != VXGE_HW_OK) {
1893                 vxge_debug_init(VXGE_ERR,
1894                         "DA config delete entry failed for vpath:%d",
1895                         vpath->device_id);
1896         } else
1897                 vxge_mac_list_del(vpath, mac);
1898         return status;
1899 }
1900
1901 /* list all mac addresses from DA table */
1902 enum vxge_hw_status
1903 static vxge_search_mac_addr_in_da_table(struct vxge_vpath *vpath,
1904                                         struct macInfo *mac)
1905 {
1906         enum vxge_hw_status status = VXGE_HW_OK;
1907         unsigned char macmask[ETH_ALEN];
1908         unsigned char macaddr[ETH_ALEN];
1909
1910         status = vxge_hw_vpath_mac_addr_get(vpath->handle,
1911                                 macaddr, macmask);
1912         if (status != VXGE_HW_OK) {
1913                 vxge_debug_init(VXGE_ERR,
1914                         "DA config list entry failed for vpath:%d",
1915                         vpath->device_id);
1916                 return status;
1917         }
1918
1919         while (memcmp(mac->macaddr, macaddr, ETH_ALEN)) {
1920
1921                 status = vxge_hw_vpath_mac_addr_get_next(vpath->handle,
1922                                 macaddr, macmask);
1923                 if (status != VXGE_HW_OK)
1924                         break;
1925         }
1926
1927         return status;
1928 }
1929
1930 /* Store all vlan ids from the list to the vid table */
1931 enum vxge_hw_status vxge_restore_vpath_vid_table(struct vxge_vpath *vpath)
1932 {
1933         enum vxge_hw_status status = VXGE_HW_OK;
1934         struct vxgedev *vdev = vpath->vdev;
1935         u16 vid;
1936
1937         if (vdev->vlgrp && vpath->is_open) {
1938
1939                 for (vid = 0; vid < VLAN_GROUP_ARRAY_LEN; vid++) {
1940                         if (!vlan_group_get_device(vdev->vlgrp, vid))
1941                                 continue;
1942                         /* Add these vlan to the vid table */
1943                         status = vxge_hw_vpath_vid_add(vpath->handle, vid);
1944                 }
1945         }
1946
1947         return status;
1948 }
1949
1950 /* Store all mac addresses from the list to the DA table */
1951 enum vxge_hw_status vxge_restore_vpath_mac_addr(struct vxge_vpath *vpath)
1952 {
1953         enum vxge_hw_status status = VXGE_HW_OK;
1954         struct macInfo mac_info;
1955         u8 *mac_address = NULL;
1956         struct list_head *entry, *next;
1957
1958         memset(&mac_info, 0, sizeof(struct macInfo));
1959
1960         if (vpath->is_open) {
1961
1962                 list_for_each_safe(entry, next, &vpath->mac_addr_list) {
1963                         mac_address =
1964                                 (u8 *)&
1965                                 ((struct vxge_mac_addrs *)entry)->macaddr;
1966                         memcpy(mac_info.macaddr, mac_address, ETH_ALEN);
1967                         ((struct vxge_mac_addrs *)entry)->state =
1968                                 VXGE_LL_MAC_ADDR_IN_DA_TABLE;
1969                         /* does this mac address already exist in da table? */
1970                         status = vxge_search_mac_addr_in_da_table(vpath,
1971                                 &mac_info);
1972                         if (status != VXGE_HW_OK) {
1973                                 /* Add this mac address to the DA table */
1974                                 status = vxge_hw_vpath_mac_addr_add(
1975                                         vpath->handle, mac_info.macaddr,
1976                                         mac_info.macmask,
1977                                     VXGE_HW_VPATH_MAC_ADDR_ADD_DUPLICATE);
1978                                 if (status != VXGE_HW_OK) {
1979                                         vxge_debug_init(VXGE_ERR,
1980                                             "DA add entry failed for vpath:%d",
1981                                             vpath->device_id);
1982                                         ((struct vxge_mac_addrs *)entry)->state
1983                                                 = VXGE_LL_MAC_ADDR_IN_LIST;
1984                                 }
1985                         }
1986                 }
1987         }
1988
1989         return status;
1990 }
1991
1992 /* reset vpaths */
1993 enum vxge_hw_status vxge_reset_all_vpaths(struct vxgedev *vdev)
1994 {
1995         int i;
1996         enum vxge_hw_status status = VXGE_HW_OK;
1997
1998         for (i = 0; i < vdev->no_of_vpath; i++)
1999                 if (vdev->vpaths[i].handle) {
2000                         if (vxge_hw_vpath_reset(vdev->vpaths[i].handle)
2001                                         == VXGE_HW_OK) {
2002                                 if (is_vxge_card_up(vdev) &&
2003                                         vxge_hw_vpath_recover_from_reset(
2004                                                 vdev->vpaths[i].handle)
2005                                                 != VXGE_HW_OK) {
2006                                         vxge_debug_init(VXGE_ERR,
2007                                                 "vxge_hw_vpath_recover_"
2008                                                 "from_reset failed for vpath: "
2009                                                 "%d", i);
2010                                         return status;
2011                                 }
2012                         } else {
2013                                 vxge_debug_init(VXGE_ERR,
2014                                         "vxge_hw_vpath_reset failed for "
2015                                         "vpath:%d", i);
2016                                         return status;
2017                         }
2018                 }
2019         return status;
2020 }
2021
2022 /* close vpaths */
2023 void vxge_close_vpaths(struct vxgedev *vdev, int index)
2024 {
2025         int i;
2026         for (i = index; i < vdev->no_of_vpath; i++) {
2027                 if (vdev->vpaths[i].handle && vdev->vpaths[i].is_open) {
2028                         vxge_hw_vpath_close(vdev->vpaths[i].handle);
2029                         vdev->stats.vpaths_open--;
2030                 }
2031                 vdev->vpaths[i].is_open = 0;
2032                 vdev->vpaths[i].handle  = NULL;
2033         }
2034 }
2035
2036 /* open vpaths */
2037 int vxge_open_vpaths(struct vxgedev *vdev)
2038 {
2039         enum vxge_hw_status status;
2040         int i;
2041         u32 vp_id = 0;
2042         struct vxge_hw_vpath_attr attr;
2043
2044         for (i = 0; i < vdev->no_of_vpath; i++) {
2045                 vxge_assert(vdev->vpaths[i].is_configured);
2046                 attr.vp_id = vdev->vpaths[i].device_id;
2047                 attr.fifo_attr.callback = vxge_xmit_compl;
2048                 attr.fifo_attr.txdl_term = vxge_tx_term;
2049                 attr.fifo_attr.per_txdl_space = sizeof(struct vxge_tx_priv);
2050                 attr.fifo_attr.userdata = (void *)&vdev->vpaths[i].fifo;
2051
2052                 attr.ring_attr.callback = vxge_rx_1b_compl;
2053                 attr.ring_attr.rxd_init = vxge_rx_initial_replenish;
2054                 attr.ring_attr.rxd_term = vxge_rx_term;
2055                 attr.ring_attr.per_rxd_space = sizeof(struct vxge_rx_priv);
2056                 attr.ring_attr.userdata = (void *)&vdev->vpaths[i].ring;
2057
2058                 vdev->vpaths[i].ring.ndev = vdev->ndev;
2059                 vdev->vpaths[i].ring.pdev = vdev->pdev;
2060                 status = vxge_hw_vpath_open(vdev->devh, &attr,
2061                                 &(vdev->vpaths[i].handle));
2062                 if (status == VXGE_HW_OK) {
2063                         vdev->vpaths[i].fifo.handle =
2064                             (struct __vxge_hw_fifo *)attr.fifo_attr.userdata;
2065                         vdev->vpaths[i].ring.handle =
2066                             (struct __vxge_hw_ring *)attr.ring_attr.userdata;
2067                         vdev->vpaths[i].fifo.tx_steering_type =
2068                                 vdev->config.tx_steering_type;
2069                         vdev->vpaths[i].fifo.ndev = vdev->ndev;
2070                         vdev->vpaths[i].fifo.pdev = vdev->pdev;
2071                         vdev->vpaths[i].fifo.indicate_max_pkts =
2072                                 vdev->config.fifo_indicate_max_pkts;
2073                         vdev->vpaths[i].ring.rx_vector_no = 0;
2074                         vdev->vpaths[i].ring.rx_csum = vdev->rx_csum;
2075                         vdev->vpaths[i].is_open = 1;
2076                         vdev->vp_handles[i] = vdev->vpaths[i].handle;
2077                         vdev->vpaths[i].ring.gro_enable =
2078                                                 vdev->config.gro_enable;
2079                         vdev->vpaths[i].ring.vlan_tag_strip =
2080                                                 vdev->vlan_tag_strip;
2081                         vdev->stats.vpaths_open++;
2082                 } else {
2083                         vdev->stats.vpath_open_fail++;
2084                         vxge_debug_init(VXGE_ERR,
2085                                 "%s: vpath: %d failed to open "
2086                                 "with status: %d",
2087                             vdev->ndev->name, vdev->vpaths[i].device_id,
2088                                 status);
2089                         vxge_close_vpaths(vdev, 0);
2090                         return -EPERM;
2091                 }
2092
2093                 vp_id =
2094                   ((struct __vxge_hw_vpath_handle *)vdev->vpaths[i].handle)->
2095                   vpath->vp_id;
2096                 vdev->vpaths_deployed |= vxge_mBIT(vp_id);
2097         }
2098         return VXGE_HW_OK;
2099 }
2100
2101 /*
2102  *  vxge_isr_napi
2103  *  @irq: the irq of the device.
2104  *  @dev_id: a void pointer to the hldev structure of the Titan device
2105  *  @ptregs: pointer to the registers pushed on the stack.
2106  *
2107  *  This function is the ISR handler of the device when napi is enabled. It
2108  *  identifies the reason for the interrupt and calls the relevant service
2109  *  routines.
2110  */
2111 static irqreturn_t vxge_isr_napi(int irq, void *dev_id)
2112 {
2113         struct net_device *dev;
2114         struct __vxge_hw_device *hldev;
2115         u64 reason;
2116         enum vxge_hw_status status;
2117         struct vxgedev *vdev = (struct vxgedev *) dev_id;;
2118
2119         vxge_debug_intr(VXGE_TRACE, "%s:%d", __func__, __LINE__);
2120
2121         dev = vdev->ndev;
2122         hldev = (struct __vxge_hw_device *)pci_get_drvdata(vdev->pdev);
2123
2124         if (pci_channel_offline(vdev->pdev))
2125                 return IRQ_NONE;
2126
2127         if (unlikely(!is_vxge_card_up(vdev)))
2128                 return IRQ_NONE;
2129
2130         status = vxge_hw_device_begin_irq(hldev, vdev->exec_mode,
2131                         &reason);
2132         if (status == VXGE_HW_OK) {
2133                 vxge_hw_device_mask_all(hldev);
2134
2135                 if (reason &
2136                         VXGE_HW_TITAN_GENERAL_INT_STATUS_VPATH_TRAFFIC_INT(
2137                         vdev->vpaths_deployed >>
2138                         (64 - VXGE_HW_MAX_VIRTUAL_PATHS))) {
2139
2140                         vxge_hw_device_clear_tx_rx(hldev);
2141                         napi_schedule(&vdev->napi);
2142                         vxge_debug_intr(VXGE_TRACE,
2143                                 "%s:%d  Exiting...", __func__, __LINE__);
2144                         return IRQ_HANDLED;
2145                 } else
2146                         vxge_hw_device_unmask_all(hldev);
2147         } else if (unlikely((status == VXGE_HW_ERR_VPATH) ||
2148                 (status == VXGE_HW_ERR_CRITICAL) ||
2149                 (status == VXGE_HW_ERR_FIFO))) {
2150                 vxge_hw_device_mask_all(hldev);
2151                 vxge_hw_device_flush_io(hldev);
2152                 return IRQ_HANDLED;
2153         } else if (unlikely(status == VXGE_HW_ERR_SLOT_FREEZE))
2154                 return IRQ_HANDLED;
2155
2156         vxge_debug_intr(VXGE_TRACE, "%s:%d  Exiting...", __func__, __LINE__);
2157         return IRQ_NONE;
2158 }
2159
2160 #ifdef CONFIG_PCI_MSI
2161
2162 static irqreturn_t
2163 vxge_tx_msix_handle(int irq, void *dev_id)
2164 {
2165         struct vxge_fifo *fifo = (struct vxge_fifo *)dev_id;
2166
2167         VXGE_COMPLETE_VPATH_TX(fifo);
2168
2169         return IRQ_HANDLED;
2170 }
2171
2172 static irqreturn_t
2173 vxge_rx_msix_napi_handle(int irq, void *dev_id)
2174 {
2175         struct vxge_ring *ring = (struct vxge_ring *)dev_id;
2176
2177         /* MSIX_IDX for Rx is 1 */
2178         vxge_hw_channel_msix_mask((struct __vxge_hw_channel *)ring->handle,
2179                                         ring->rx_vector_no);
2180
2181         napi_schedule(&ring->napi);
2182         return IRQ_HANDLED;
2183 }
2184
2185 static irqreturn_t
2186 vxge_alarm_msix_handle(int irq, void *dev_id)
2187 {
2188         int i;
2189         enum vxge_hw_status status;
2190         struct vxge_vpath *vpath = (struct vxge_vpath *)dev_id;
2191         struct vxgedev *vdev = vpath->vdev;
2192         int msix_id = (vpath->handle->vpath->vp_id *
2193                 VXGE_HW_VPATH_MSIX_ACTIVE) + VXGE_ALARM_MSIX_ID;
2194
2195         for (i = 0; i < vdev->no_of_vpath; i++) {
2196                 vxge_hw_vpath_msix_mask(vdev->vpaths[i].handle, msix_id);
2197
2198                 status = vxge_hw_vpath_alarm_process(vdev->vpaths[i].handle,
2199                         vdev->exec_mode);
2200                 if (status == VXGE_HW_OK) {
2201
2202                         vxge_hw_vpath_msix_unmask(vdev->vpaths[i].handle,
2203                                         msix_id);
2204                         continue;
2205                 }
2206                 vxge_debug_intr(VXGE_ERR,
2207                         "%s: vxge_hw_vpath_alarm_process failed %x ",
2208                         VXGE_DRIVER_NAME, status);
2209         }
2210         return IRQ_HANDLED;
2211 }
2212
2213 static int vxge_alloc_msix(struct vxgedev *vdev)
2214 {
2215         int j, i, ret = 0;
2216         int msix_intr_vect = 0, temp;
2217         vdev->intr_cnt = 0;
2218
2219 start:
2220         /* Tx/Rx MSIX Vectors count */
2221         vdev->intr_cnt = vdev->no_of_vpath * 2;
2222
2223         /* Alarm MSIX Vectors count */
2224         vdev->intr_cnt++;
2225
2226         vdev->entries = kzalloc(vdev->intr_cnt * sizeof(struct msix_entry),
2227                                                 GFP_KERNEL);
2228         if (!vdev->entries) {
2229                 vxge_debug_init(VXGE_ERR,
2230                         "%s: memory allocation failed",
2231                         VXGE_DRIVER_NAME);
2232                 ret = -ENOMEM;
2233                 goto alloc_entries_failed;
2234         }
2235
2236         vdev->vxge_entries =
2237                 kzalloc(vdev->intr_cnt * sizeof(struct vxge_msix_entry),
2238                                 GFP_KERNEL);
2239         if (!vdev->vxge_entries) {
2240                 vxge_debug_init(VXGE_ERR, "%s: memory allocation failed",
2241                         VXGE_DRIVER_NAME);
2242                 ret = -ENOMEM;
2243                 goto alloc_vxge_entries_failed;
2244         }
2245
2246         for (i = 0, j = 0; i < vdev->no_of_vpath; i++) {
2247
2248                 msix_intr_vect = i * VXGE_HW_VPATH_MSIX_ACTIVE;
2249
2250                 /* Initialize the fifo vector */
2251                 vdev->entries[j].entry = msix_intr_vect;
2252                 vdev->vxge_entries[j].entry = msix_intr_vect;
2253                 vdev->vxge_entries[j].in_use = 0;
2254                 j++;
2255
2256                 /* Initialize the ring vector */
2257                 vdev->entries[j].entry = msix_intr_vect + 1;
2258                 vdev->vxge_entries[j].entry = msix_intr_vect + 1;
2259                 vdev->vxge_entries[j].in_use = 0;
2260                 j++;
2261         }
2262
2263         /* Initialize the alarm vector */
2264         vdev->entries[j].entry = VXGE_ALARM_MSIX_ID;
2265         vdev->vxge_entries[j].entry = VXGE_ALARM_MSIX_ID;
2266         vdev->vxge_entries[j].in_use = 0;
2267
2268         ret = pci_enable_msix(vdev->pdev, vdev->entries, vdev->intr_cnt);
2269
2270         if (ret > 0) {
2271                 vxge_debug_init(VXGE_ERR,
2272                         "%s: MSI-X enable failed for %d vectors, ret: %d",
2273                         VXGE_DRIVER_NAME, vdev->intr_cnt, ret);
2274                 if ((max_config_vpath != VXGE_USE_DEFAULT) || (ret < 3)) {
2275                         ret = -ENODEV;
2276                         goto enable_msix_failed;
2277                 }
2278
2279                 kfree(vdev->entries);
2280                 kfree(vdev->vxge_entries);
2281                 vdev->entries = NULL;
2282                 vdev->vxge_entries = NULL;
2283                 /* Try with less no of vector by reducing no of vpaths count */
2284                 temp = (ret - 1)/2;
2285                 vxge_close_vpaths(vdev, temp);
2286                 vdev->no_of_vpath = temp;
2287                 goto start;
2288         } else if (ret < 0) {
2289                 ret = -ENODEV;
2290                 goto enable_msix_failed;
2291         }
2292         return 0;
2293
2294 enable_msix_failed:
2295         kfree(vdev->vxge_entries);
2296 alloc_vxge_entries_failed:
2297         kfree(vdev->entries);
2298 alloc_entries_failed:
2299         return ret;
2300 }
2301
2302 static int vxge_enable_msix(struct vxgedev *vdev)
2303 {
2304
2305         int i, ret = 0;
2306         /* 0 - Tx, 1 - Rx  */
2307         int tim_msix_id[4] = {0, 1, 0, 0};
2308
2309         vdev->intr_cnt = 0;
2310
2311         /* allocate msix vectors */
2312         ret = vxge_alloc_msix(vdev);
2313         if (!ret) {
2314                 for (i = 0; i < vdev->no_of_vpath; i++) {
2315
2316                         /* If fifo or ring are not enabled
2317                            the MSIX vector for that should be set to 0
2318                            Hence initializeing this array to all 0s.
2319                         */
2320                         vdev->vpaths[i].ring.rx_vector_no =
2321                                 (vdev->vpaths[i].device_id *
2322                                         VXGE_HW_VPATH_MSIX_ACTIVE) + 1;
2323
2324                         vxge_hw_vpath_msix_set(vdev->vpaths[i].handle,
2325                                         tim_msix_id, VXGE_ALARM_MSIX_ID);
2326                 }
2327         }
2328
2329         return ret;
2330 }
2331
2332 static void vxge_rem_msix_isr(struct vxgedev *vdev)
2333 {
2334         int intr_cnt;
2335
2336         for (intr_cnt = 0; intr_cnt < (vdev->no_of_vpath * 2 + 1);
2337                 intr_cnt++) {
2338                 if (vdev->vxge_entries[intr_cnt].in_use) {
2339                         synchronize_irq(vdev->entries[intr_cnt].vector);
2340                         free_irq(vdev->entries[intr_cnt].vector,
2341                                 vdev->vxge_entries[intr_cnt].arg);
2342                         vdev->vxge_entries[intr_cnt].in_use = 0;
2343                 }
2344         }
2345
2346         kfree(vdev->entries);
2347         kfree(vdev->vxge_entries);
2348         vdev->entries = NULL;
2349         vdev->vxge_entries = NULL;
2350
2351         if (vdev->config.intr_type == MSI_X)
2352                 pci_disable_msix(vdev->pdev);
2353 }
2354 #endif
2355
2356 static void vxge_rem_isr(struct vxgedev *vdev)
2357 {
2358         struct __vxge_hw_device  *hldev;
2359         hldev = (struct __vxge_hw_device  *) pci_get_drvdata(vdev->pdev);
2360
2361 #ifdef CONFIG_PCI_MSI
2362         if (vdev->config.intr_type == MSI_X) {
2363                 vxge_rem_msix_isr(vdev);
2364         } else
2365 #endif
2366         if (vdev->config.intr_type == INTA) {
2367                         synchronize_irq(vdev->pdev->irq);
2368                         free_irq(vdev->pdev->irq, vdev);
2369         }
2370 }
2371
2372 static int vxge_add_isr(struct vxgedev *vdev)
2373 {
2374         int ret = 0;
2375 #ifdef CONFIG_PCI_MSI
2376         int vp_idx = 0, intr_idx = 0, intr_cnt = 0, msix_idx = 0, irq_req = 0;
2377         int pci_fun = PCI_FUNC(vdev->pdev->devfn);
2378
2379         if (vdev->config.intr_type == MSI_X)
2380                 ret = vxge_enable_msix(vdev);
2381
2382         if (ret) {
2383                 vxge_debug_init(VXGE_ERR,
2384                         "%s: Enabling MSI-X Failed", VXGE_DRIVER_NAME);
2385                 vxge_debug_init(VXGE_ERR,
2386                         "%s: Defaulting to INTA", VXGE_DRIVER_NAME);
2387                 vdev->config.intr_type = INTA;
2388         }
2389
2390         if (vdev->config.intr_type == MSI_X) {
2391                 for (intr_idx = 0;
2392                      intr_idx < (vdev->no_of_vpath *
2393                         VXGE_HW_VPATH_MSIX_ACTIVE); intr_idx++) {
2394
2395                         msix_idx = intr_idx % VXGE_HW_VPATH_MSIX_ACTIVE;
2396                         irq_req = 0;
2397
2398                         switch (msix_idx) {
2399                         case 0:
2400                                 snprintf(vdev->desc[intr_cnt], VXGE_INTR_STRLEN,
2401                                 "%s:vxge:MSI-X %d - Tx - fn:%d vpath:%d",
2402                                         vdev->ndev->name,
2403                                         vdev->entries[intr_cnt].entry,
2404                                         pci_fun, vp_idx);
2405                                 ret = request_irq(
2406                                     vdev->entries[intr_cnt].vector,
2407                                         vxge_tx_msix_handle, 0,
2408                                         vdev->desc[intr_cnt],
2409                                         &vdev->vpaths[vp_idx].fifo);
2410                                         vdev->vxge_entries[intr_cnt].arg =
2411                                                 &vdev->vpaths[vp_idx].fifo;
2412                                 irq_req = 1;
2413                                 break;
2414                         case 1:
2415                                 snprintf(vdev->desc[intr_cnt], VXGE_INTR_STRLEN,
2416                                 "%s:vxge:MSI-X %d - Rx - fn:%d vpath:%d",
2417                                         vdev->ndev->name,
2418                                         vdev->entries[intr_cnt].entry,
2419                                         pci_fun, vp_idx);
2420                                 ret = request_irq(
2421                                     vdev->entries[intr_cnt].vector,
2422                                         vxge_rx_msix_napi_handle,
2423                                         0,
2424                                         vdev->desc[intr_cnt],
2425                                         &vdev->vpaths[vp_idx].ring);
2426                                         vdev->vxge_entries[intr_cnt].arg =
2427                                                 &vdev->vpaths[vp_idx].ring;
2428                                 irq_req = 1;
2429                                 break;
2430                         }
2431
2432                         if (ret) {
2433                                 vxge_debug_init(VXGE_ERR,
2434                                         "%s: MSIX - %d  Registration failed",
2435                                         vdev->ndev->name, intr_cnt);
2436                                 vxge_rem_msix_isr(vdev);
2437                                 vdev->config.intr_type = INTA;
2438                                 vxge_debug_init(VXGE_ERR,
2439                                         "%s: Defaulting to INTA"
2440                                         , vdev->ndev->name);
2441                                         goto INTA_MODE;
2442                         }
2443
2444                         if (irq_req) {
2445                                 /* We requested for this msix interrupt */
2446                                 vdev->vxge_entries[intr_cnt].in_use = 1;
2447                                 msix_idx +=  vdev->vpaths[vp_idx].device_id *
2448                                         VXGE_HW_VPATH_MSIX_ACTIVE;
2449                                 vxge_hw_vpath_msix_unmask(
2450                                         vdev->vpaths[vp_idx].handle,
2451                                         msix_idx);
2452                                 intr_cnt++;
2453                         }
2454
2455                         /* Point to next vpath handler */
2456                         if (((intr_idx + 1) % VXGE_HW_VPATH_MSIX_ACTIVE == 0) &&
2457                             (vp_idx < (vdev->no_of_vpath - 1)))
2458                                 vp_idx++;
2459                 }
2460
2461                 intr_cnt = vdev->no_of_vpath * 2;
2462                 snprintf(vdev->desc[intr_cnt], VXGE_INTR_STRLEN,
2463                         "%s:vxge:MSI-X %d - Alarm - fn:%d",
2464                         vdev->ndev->name,
2465                         vdev->entries[intr_cnt].entry,
2466                         pci_fun);
2467                 /* For Alarm interrupts */
2468                 ret = request_irq(vdev->entries[intr_cnt].vector,
2469                                         vxge_alarm_msix_handle, 0,
2470                                         vdev->desc[intr_cnt],
2471                                         &vdev->vpaths[0]);
2472                 if (ret) {
2473                         vxge_debug_init(VXGE_ERR,
2474                                 "%s: MSIX - %d Registration failed",
2475                                 vdev->ndev->name, intr_cnt);
2476                         vxge_rem_msix_isr(vdev);
2477                         vdev->config.intr_type = INTA;
2478                         vxge_debug_init(VXGE_ERR,
2479                                 "%s: Defaulting to INTA",
2480                                 vdev->ndev->name);
2481                                 goto INTA_MODE;
2482                 }
2483
2484                 msix_idx = (vdev->vpaths[0].handle->vpath->vp_id *
2485                         VXGE_HW_VPATH_MSIX_ACTIVE) + VXGE_ALARM_MSIX_ID;
2486                 vxge_hw_vpath_msix_unmask(vdev->vpaths[vp_idx].handle,
2487                                         msix_idx);
2488                 vdev->vxge_entries[intr_cnt].in_use = 1;
2489                 vdev->vxge_entries[intr_cnt].arg = &vdev->vpaths[0];
2490         }
2491 INTA_MODE:
2492 #endif
2493
2494         if (vdev->config.intr_type == INTA) {
2495                 snprintf(vdev->desc[0], VXGE_INTR_STRLEN,
2496                         "%s:vxge:INTA", vdev->ndev->name);
2497                 vxge_hw_device_set_intr_type(vdev->devh,
2498                         VXGE_HW_INTR_MODE_IRQLINE);
2499                 vxge_hw_vpath_tti_ci_set(vdev->devh,
2500                         vdev->vpaths[0].device_id);
2501                 ret = request_irq((int) vdev->pdev->irq,
2502                         vxge_isr_napi,
2503                         IRQF_SHARED, vdev->desc[0], vdev);
2504                 if (ret) {
2505                         vxge_debug_init(VXGE_ERR,
2506                                 "%s %s-%d: ISR registration failed",
2507                                 VXGE_DRIVER_NAME, "IRQ", vdev->pdev->irq);
2508                         return -ENODEV;
2509                 }
2510                 vxge_debug_init(VXGE_TRACE,
2511                         "new %s-%d line allocated",
2512                         "IRQ", vdev->pdev->irq);
2513         }
2514
2515         return VXGE_HW_OK;
2516 }
2517
2518 static void vxge_poll_vp_reset(unsigned long data)
2519 {
2520         struct vxgedev *vdev = (struct vxgedev *)data;
2521         int i, j = 0;
2522
2523         for (i = 0; i < vdev->no_of_vpath; i++) {
2524                 if (test_bit(i, &vdev->vp_reset)) {
2525                         vxge_reset_vpath(vdev, i);
2526                         j++;
2527                 }
2528         }
2529         if (j && (vdev->config.intr_type != MSI_X)) {
2530                 vxge_hw_device_unmask_all(vdev->devh);
2531                 vxge_hw_device_flush_io(vdev->devh);
2532         }
2533
2534         mod_timer(&vdev->vp_reset_timer, jiffies + HZ / 2);
2535 }
2536
2537 static void vxge_poll_vp_lockup(unsigned long data)
2538 {
2539         struct vxgedev *vdev = (struct vxgedev *)data;
2540         int i;
2541         struct vxge_ring *ring;
2542         enum vxge_hw_status status = VXGE_HW_OK;
2543
2544         for (i = 0; i < vdev->no_of_vpath; i++) {
2545                 ring = &vdev->vpaths[i].ring;
2546                 /* Did this vpath received any packets */
2547                 if (ring->stats.prev_rx_frms == ring->stats.rx_frms) {
2548                         status = vxge_hw_vpath_check_leak(ring->handle);
2549
2550                         /* Did it received any packets last time */
2551                         if ((VXGE_HW_FAIL == status) &&
2552                                 (VXGE_HW_FAIL == ring->last_status)) {
2553
2554                                 /* schedule vpath reset */
2555                                 if (!test_and_set_bit(i, &vdev->vp_reset)) {
2556
2557                                         /* disable interrupts for this vpath */
2558                                         vxge_vpath_intr_disable(vdev, i);
2559
2560                                         /* stop the queue for this vpath */
2561                                         vxge_stop_tx_queue(&vdev->vpaths[i].
2562                                                                 fifo);
2563                                         continue;
2564                                 }
2565                         }
2566                 }
2567                 ring->stats.prev_rx_frms = ring->stats.rx_frms;
2568                 ring->last_status = status;
2569         }
2570
2571         /* Check every 1 milli second */
2572         mod_timer(&vdev->vp_lockup_timer, jiffies + HZ / 1000);
2573 }
2574
2575 /**
2576  * vxge_open
2577  * @dev: pointer to the device structure.
2578  *
2579  * This function is the open entry point of the driver. It mainly calls a
2580  * function to allocate Rx buffers and inserts them into the buffer
2581  * descriptors and then enables the Rx part of the NIC.
2582  * Return value: '0' on success and an appropriate (-)ve integer as
2583  * defined in errno.h file on failure.
2584  */
2585 int
2586 vxge_open(struct net_device *dev)
2587 {
2588         enum vxge_hw_status status;
2589         struct vxgedev *vdev;
2590         struct __vxge_hw_device *hldev;
2591         int ret = 0;
2592         int i;
2593         u64 val64, function_mode;
2594         vxge_debug_entryexit(VXGE_TRACE,
2595                 "%s: %s:%d", dev->name, __func__, __LINE__);
2596
2597         vdev = (struct vxgedev *)netdev_priv(dev);
2598         hldev = (struct __vxge_hw_device *) pci_get_drvdata(vdev->pdev);
2599         function_mode = vdev->config.device_hw_info.function_mode;
2600
2601         /* make sure you have link off by default every time Nic is
2602          * initialized */
2603         netif_carrier_off(dev);
2604
2605         /* Open VPATHs */
2606         status = vxge_open_vpaths(vdev);
2607         if (status != VXGE_HW_OK) {
2608                 vxge_debug_init(VXGE_ERR,
2609                         "%s: fatal: Vpath open failed", vdev->ndev->name);
2610                 ret = -EPERM;
2611                 goto out0;
2612         }
2613
2614         vdev->mtu = dev->mtu;
2615
2616         status = vxge_add_isr(vdev);
2617         if (status != VXGE_HW_OK) {
2618                 vxge_debug_init(VXGE_ERR,
2619                         "%s: fatal: ISR add failed", dev->name);
2620                 ret = -EPERM;
2621                 goto out1;
2622         }
2623
2624
2625         if (vdev->config.intr_type != MSI_X) {
2626                 netif_napi_add(dev, &vdev->napi, vxge_poll_inta,
2627                         vdev->config.napi_weight);
2628                 napi_enable(&vdev->napi);
2629                 for (i = 0; i < vdev->no_of_vpath; i++)
2630                         vdev->vpaths[i].ring.napi_p = &vdev->napi;
2631         } else {
2632                 for (i = 0; i < vdev->no_of_vpath; i++) {
2633                         netif_napi_add(dev, &vdev->vpaths[i].ring.napi,
2634                             vxge_poll_msix, vdev->config.napi_weight);
2635                         napi_enable(&vdev->vpaths[i].ring.napi);
2636                         vdev->vpaths[i].ring.napi_p =
2637                                 &vdev->vpaths[i].ring.napi;
2638                 }
2639         }
2640
2641         /* configure RTH */
2642         if (vdev->config.rth_steering) {
2643                 status = vxge_rth_configure(vdev);
2644                 if (status != VXGE_HW_OK) {
2645                         vxge_debug_init(VXGE_ERR,
2646                                 "%s: fatal: RTH configuration failed",
2647                                 dev->name);
2648                         ret = -EPERM;
2649                         goto out2;
2650                 }
2651         }
2652
2653         for (i = 0; i < vdev->no_of_vpath; i++) {
2654                 /* set initial mtu before enabling the device */
2655                 status = vxge_hw_vpath_mtu_set(vdev->vpaths[i].handle,
2656                                                 vdev->mtu);
2657                 if (status != VXGE_HW_OK) {
2658                         vxge_debug_init(VXGE_ERR,
2659                                 "%s: fatal: can not set new MTU", dev->name);
2660                         ret = -EPERM;
2661                         goto out2;
2662                 }
2663         }
2664
2665         VXGE_DEVICE_DEBUG_LEVEL_SET(VXGE_TRACE, VXGE_COMPONENT_LL, vdev);
2666         vxge_debug_init(vdev->level_trace,
2667                 "%s: MTU is %d", vdev->ndev->name, vdev->mtu);
2668         VXGE_DEVICE_DEBUG_LEVEL_SET(VXGE_ERR, VXGE_COMPONENT_LL, vdev);
2669
2670         /* Reprogram the DA table with populated mac addresses */
2671         for (i = 0; i < vdev->no_of_vpath; i++) {
2672                 vxge_restore_vpath_mac_addr(&vdev->vpaths[i]);
2673                 vxge_restore_vpath_vid_table(&vdev->vpaths[i]);
2674         }
2675
2676         /* Enable vpath to sniff all unicast/multicast traffic that not
2677          * addressed to them. We allow promiscous mode for PF only
2678          */
2679
2680         val64 = 0;
2681         for (i = 0; i < VXGE_HW_MAX_VIRTUAL_PATHS; i++)
2682                 val64 |= VXGE_HW_RXMAC_AUTHORIZE_ALL_ADDR_VP(i);
2683
2684         vxge_hw_mgmt_reg_write(vdev->devh,
2685                 vxge_hw_mgmt_reg_type_mrpcim,
2686                 0,
2687                 (ulong)offsetof(struct vxge_hw_mrpcim_reg,
2688                         rxmac_authorize_all_addr),
2689                 val64);
2690
2691         vxge_hw_mgmt_reg_write(vdev->devh,
2692                 vxge_hw_mgmt_reg_type_mrpcim,
2693                 0,
2694                 (ulong)offsetof(struct vxge_hw_mrpcim_reg,
2695                         rxmac_authorize_all_vid),
2696                 val64);
2697
2698         vxge_set_multicast(dev);
2699
2700         /* Enabling Bcast and mcast for all vpath */
2701         for (i = 0; i < vdev->no_of_vpath; i++) {
2702                 status = vxge_hw_vpath_bcast_enable(vdev->vpaths[i].handle);
2703                 if (status != VXGE_HW_OK)
2704                         vxge_debug_init(VXGE_ERR,
2705                                 "%s : Can not enable bcast for vpath "
2706                                 "id %d", dev->name, i);
2707                 if (vdev->config.addr_learn_en) {
2708                         status =
2709                             vxge_hw_vpath_mcast_enable(vdev->vpaths[i].handle);
2710                         if (status != VXGE_HW_OK)
2711                                 vxge_debug_init(VXGE_ERR,
2712                                         "%s : Can not enable mcast for vpath "
2713                                         "id %d", dev->name, i);
2714                 }
2715         }
2716
2717         vxge_hw_device_setpause_data(vdev->devh, 0,
2718                 vdev->config.tx_pause_enable,
2719                 vdev->config.rx_pause_enable);
2720
2721         if (vdev->vp_reset_timer.function == NULL)
2722                 vxge_os_timer(vdev->vp_reset_timer,
2723                         vxge_poll_vp_reset, vdev, (HZ/2));
2724
2725         if (vdev->vp_lockup_timer.function == NULL)
2726                 vxge_os_timer(vdev->vp_lockup_timer,
2727                         vxge_poll_vp_lockup, vdev, (HZ/2));
2728
2729         set_bit(__VXGE_STATE_CARD_UP, &vdev->state);
2730
2731         smp_wmb();
2732
2733         if (vxge_hw_device_link_state_get(vdev->devh) == VXGE_HW_LINK_UP) {
2734                 netif_carrier_on(vdev->ndev);
2735                 printk(KERN_NOTICE "%s: Link Up\n", vdev->ndev->name);
2736                 vdev->stats.link_up++;
2737         }
2738
2739         vxge_hw_device_intr_enable(vdev->devh);
2740
2741         smp_wmb();
2742
2743         for (i = 0; i < vdev->no_of_vpath; i++) {
2744                 vxge_hw_vpath_enable(vdev->vpaths[i].handle);
2745                 smp_wmb();
2746                 vxge_hw_vpath_rx_doorbell_init(vdev->vpaths[i].handle);
2747         }
2748
2749         netif_tx_start_all_queues(vdev->ndev);
2750         goto out0;
2751
2752 out2:
2753         vxge_rem_isr(vdev);
2754
2755         /* Disable napi */
2756         if (vdev->config.intr_type != MSI_X)
2757                 napi_disable(&vdev->napi);
2758         else {
2759                 for (i = 0; i < vdev->no_of_vpath; i++)
2760                         napi_disable(&vdev->vpaths[i].ring.napi);
2761         }
2762
2763 out1:
2764         vxge_close_vpaths(vdev, 0);
2765 out0:
2766         vxge_debug_entryexit(VXGE_TRACE,
2767                                 "%s: %s:%d  Exiting...",
2768                                 dev->name, __func__, __LINE__);
2769         return ret;
2770 }
2771
2772 /* Loop throught the mac address list and delete all the entries */
2773 void vxge_free_mac_add_list(struct vxge_vpath *vpath)
2774 {
2775
2776         struct list_head *entry, *next;
2777         if (list_empty(&vpath->mac_addr_list))
2778                 return;
2779
2780         list_for_each_safe(entry, next, &vpath->mac_addr_list) {
2781                 list_del(entry);
2782                 kfree((struct vxge_mac_addrs *)entry);
2783         }
2784 }
2785
2786 static void vxge_napi_del_all(struct vxgedev *vdev)
2787 {
2788         int i;
2789         if (vdev->config.intr_type != MSI_X)
2790                 netif_napi_del(&vdev->napi);
2791         else {
2792                 for (i = 0; i < vdev->no_of_vpath; i++)
2793                         netif_napi_del(&vdev->vpaths[i].ring.napi);
2794         }
2795 }
2796
2797 int do_vxge_close(struct net_device *dev, int do_io)
2798 {
2799         enum vxge_hw_status status;
2800         struct vxgedev *vdev;
2801         struct __vxge_hw_device *hldev;
2802         int i;
2803         u64 val64, vpath_vector;
2804         vxge_debug_entryexit(VXGE_TRACE, "%s: %s:%d",
2805                 dev->name, __func__, __LINE__);
2806
2807         vdev = (struct vxgedev *)netdev_priv(dev);
2808         hldev = (struct __vxge_hw_device *) pci_get_drvdata(vdev->pdev);
2809
2810         if (unlikely(!is_vxge_card_up(vdev)))
2811                 return 0;
2812
2813         /* If vxge_handle_crit_err task is executing,
2814          * wait till it completes. */
2815         while (test_and_set_bit(__VXGE_STATE_RESET_CARD, &vdev->state))
2816                 msleep(50);
2817
2818         clear_bit(__VXGE_STATE_CARD_UP, &vdev->state);
2819         if (do_io) {
2820                 /* Put the vpath back in normal mode */
2821                 vpath_vector = vxge_mBIT(vdev->vpaths[0].device_id);
2822                 status = vxge_hw_mgmt_reg_read(vdev->devh,
2823                                 vxge_hw_mgmt_reg_type_mrpcim,
2824                                 0,
2825                                 (ulong)offsetof(
2826                                         struct vxge_hw_mrpcim_reg,
2827                                         rts_mgr_cbasin_cfg),
2828                                 &val64);
2829
2830                 if (status == VXGE_HW_OK) {
2831                         val64 &= ~vpath_vector;
2832                         status = vxge_hw_mgmt_reg_write(vdev->devh,
2833                                         vxge_hw_mgmt_reg_type_mrpcim,
2834                                         0,
2835                                         (ulong)offsetof(
2836                                                 struct vxge_hw_mrpcim_reg,
2837                                                 rts_mgr_cbasin_cfg),
2838                                         val64);
2839                 }
2840
2841                 /* Remove the function 0 from promiscous mode */
2842                 vxge_hw_mgmt_reg_write(vdev->devh,
2843                         vxge_hw_mgmt_reg_type_mrpcim,
2844                         0,
2845                         (ulong)offsetof(struct vxge_hw_mrpcim_reg,
2846                                 rxmac_authorize_all_addr),
2847                         0);
2848
2849                 vxge_hw_mgmt_reg_write(vdev->devh,
2850                         vxge_hw_mgmt_reg_type_mrpcim,
2851                         0,
2852                         (ulong)offsetof(struct vxge_hw_mrpcim_reg,
2853                                 rxmac_authorize_all_vid),
2854                         0);
2855
2856                 smp_wmb();
2857         }
2858         del_timer_sync(&vdev->vp_lockup_timer);
2859
2860         del_timer_sync(&vdev->vp_reset_timer);
2861
2862         /* Disable napi */
2863         if (vdev->config.intr_type != MSI_X)
2864                 napi_disable(&vdev->napi);
2865         else {
2866                 for (i = 0; i < vdev->no_of_vpath; i++)
2867                         napi_disable(&vdev->vpaths[i].ring.napi);
2868         }
2869
2870         netif_carrier_off(vdev->ndev);
2871         printk(KERN_NOTICE "%s: Link Down\n", vdev->ndev->name);
2872         netif_tx_stop_all_queues(vdev->ndev);
2873
2874         /* Note that at this point xmit() is stopped by upper layer */
2875         if (do_io)
2876                 vxge_hw_device_intr_disable(vdev->devh);
2877
2878         mdelay(1000);
2879
2880         vxge_rem_isr(vdev);
2881
2882         vxge_napi_del_all(vdev);
2883
2884         if (do_io)
2885                 vxge_reset_all_vpaths(vdev);
2886
2887         vxge_close_vpaths(vdev, 0);
2888
2889         vxge_debug_entryexit(VXGE_TRACE,
2890                 "%s: %s:%d  Exiting...", dev->name, __func__, __LINE__);
2891
2892         clear_bit(__VXGE_STATE_RESET_CARD, &vdev->state);
2893
2894         return 0;
2895 }
2896
2897 /**
2898  * vxge_close
2899  * @dev: device pointer.
2900  *
2901  * This is the stop entry point of the driver. It needs to undo exactly
2902  * whatever was done by the open entry point, thus it's usually referred to
2903  * as the close function.Among other things this function mainly stops the
2904  * Rx side of the NIC and frees all the Rx buffers in the Rx rings.
2905  * Return value: '0' on success and an appropriate (-)ve integer as
2906  * defined in errno.h file on failure.
2907  */
2908 int
2909 vxge_close(struct net_device *dev)
2910 {
2911         do_vxge_close(dev, 1);
2912         return 0;
2913 }
2914
2915 /**
2916  * vxge_change_mtu
2917  * @dev: net device pointer.
2918  * @new_mtu :the new MTU size for the device.
2919  *
2920  * A driver entry point to change MTU size for the device. Before changing
2921  * the MTU the device must be stopped.
2922  */
2923 static int vxge_change_mtu(struct net_device *dev, int new_mtu)
2924 {
2925         struct vxgedev *vdev = netdev_priv(dev);
2926
2927         vxge_debug_entryexit(vdev->level_trace,
2928                 "%s:%d", __func__, __LINE__);
2929         if ((new_mtu < VXGE_HW_MIN_MTU) || (new_mtu > VXGE_HW_MAX_MTU)) {
2930                 vxge_debug_init(vdev->level_err,
2931                         "%s: mtu size is invalid", dev->name);
2932                 return -EPERM;
2933         }
2934
2935         /* check if device is down already */
2936         if (unlikely(!is_vxge_card_up(vdev))) {
2937                 /* just store new value, will use later on open() */
2938                 dev->mtu = new_mtu;
2939                 vxge_debug_init(vdev->level_err,
2940                         "%s", "device is down on MTU change");
2941                 return 0;
2942         }
2943
2944         vxge_debug_init(vdev->level_trace,
2945                 "trying to apply new MTU %d", new_mtu);
2946
2947         if (vxge_close(dev))
2948                 return -EIO;
2949
2950         dev->mtu = new_mtu;
2951         vdev->mtu = new_mtu;
2952
2953         if (vxge_open(dev))
2954                 return -EIO;
2955
2956         vxge_debug_init(vdev->level_trace,
2957                 "%s: MTU changed to %d", vdev->ndev->name, new_mtu);
2958
2959         vxge_debug_entryexit(vdev->level_trace,
2960                 "%s:%d  Exiting...", __func__, __LINE__);
2961
2962         return 0;
2963 }
2964
2965 /**
2966  * vxge_get_stats
2967  * @dev: pointer to the device structure
2968  *
2969  * Updates the device statistics structure. This function updates the device
2970  * statistics structure in the net_device structure and returns a pointer
2971  * to the same.
2972  */
2973 static struct net_device_stats *
2974 vxge_get_stats(struct net_device *dev)
2975 {
2976         struct vxgedev *vdev;
2977         struct net_device_stats *net_stats;
2978         int k;
2979
2980         vdev = netdev_priv(dev);
2981
2982         net_stats = &vdev->stats.net_stats;
2983
2984         memset(net_stats, 0, sizeof(struct net_device_stats));
2985
2986         for (k = 0; k < vdev->no_of_vpath; k++) {
2987                 net_stats->rx_packets += vdev->vpaths[k].ring.stats.rx_frms;
2988                 net_stats->rx_bytes += vdev->vpaths[k].ring.stats.rx_bytes;
2989                 net_stats->rx_errors += vdev->vpaths[k].ring.stats.rx_errors;
2990                 net_stats->multicast += vdev->vpaths[k].ring.stats.rx_mcast;
2991                 net_stats->rx_dropped +=
2992                         vdev->vpaths[k].ring.stats.rx_dropped;
2993
2994                 net_stats->tx_packets += vdev->vpaths[k].fifo.stats.tx_frms;
2995                 net_stats->tx_bytes += vdev->vpaths[k].fifo.stats.tx_bytes;
2996                 net_stats->tx_errors += vdev->vpaths[k].fifo.stats.tx_errors;
2997         }
2998
2999         return net_stats;
3000 }
3001
3002 /**
3003  * vxge_ioctl
3004  * @dev: Device pointer.
3005  * @ifr: An IOCTL specific structure, that can contain a pointer to
3006  *       a proprietary structure used to pass information to the driver.
3007  * @cmd: This is used to distinguish between the different commands that
3008  *       can be passed to the IOCTL functions.
3009  *
3010  * Entry point for the Ioctl.
3011  */
3012 static int vxge_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
3013 {
3014         return -EOPNOTSUPP;
3015 }
3016
3017 /**
3018  * vxge_tx_watchdog
3019  * @dev: pointer to net device structure
3020  *
3021  * Watchdog for transmit side.
3022  * This function is triggered if the Tx Queue is stopped
3023  * for a pre-defined amount of time when the Interface is still up.
3024  */
3025 static void
3026 vxge_tx_watchdog(struct net_device *dev)
3027 {
3028         struct vxgedev *vdev;
3029
3030         vxge_debug_entryexit(VXGE_TRACE, "%s:%d", __func__, __LINE__);
3031
3032         vdev = (struct vxgedev *)netdev_priv(dev);
3033
3034         vdev->cric_err_event = VXGE_HW_EVENT_RESET_START;
3035
3036         vxge_reset(vdev);
3037         vxge_debug_entryexit(VXGE_TRACE,
3038                 "%s:%d  Exiting...", __func__, __LINE__);
3039 }
3040
3041 /**
3042  * vxge_vlan_rx_register
3043  * @dev: net device pointer.
3044  * @grp: vlan group
3045  *
3046  * Vlan group registration
3047  */
3048 static void
3049 vxge_vlan_rx_register(struct net_device *dev, struct vlan_group *grp)
3050 {
3051         struct vxgedev *vdev;
3052         struct vxge_vpath *vpath;
3053         int vp;
3054         u64 vid;
3055         enum vxge_hw_status status;
3056         int i;
3057
3058         vxge_debug_entryexit(VXGE_TRACE, "%s:%d", __func__, __LINE__);
3059
3060         vdev = (struct vxgedev *)netdev_priv(dev);
3061
3062         vpath = &vdev->vpaths[0];
3063         if ((NULL == grp) && (vpath->is_open)) {
3064                 /* Get the first vlan */
3065                 status = vxge_hw_vpath_vid_get(vpath->handle, &vid);
3066
3067                 while (status == VXGE_HW_OK) {
3068
3069                         /* Delete this vlan from the vid table */
3070                         for (vp = 0; vp < vdev->no_of_vpath; vp++) {
3071                                 vpath = &vdev->vpaths[vp];
3072                                 if (!vpath->is_open)
3073                                         continue;
3074
3075                                 vxge_hw_vpath_vid_delete(vpath->handle, vid);
3076                         }
3077
3078                         /* Get the next vlan to be deleted */
3079                         vpath = &vdev->vpaths[0];
3080                         status = vxge_hw_vpath_vid_get(vpath->handle, &vid);
3081                 }
3082         }
3083
3084         vdev->vlgrp = grp;
3085
3086         for (i = 0; i < vdev->no_of_vpath; i++) {
3087                 if (vdev->vpaths[i].is_configured)
3088                         vdev->vpaths[i].ring.vlgrp = grp;
3089         }
3090
3091         vxge_debug_entryexit(VXGE_TRACE,
3092                 "%s:%d  Exiting...", __func__, __LINE__);
3093 }
3094
3095 /**
3096  * vxge_vlan_rx_add_vid
3097  * @dev: net device pointer.
3098  * @vid: vid
3099  *
3100  * Add the vlan id to the devices vlan id table
3101  */
3102 static void
3103 vxge_vlan_rx_add_vid(struct net_device *dev, unsigned short vid)
3104 {
3105         struct vxgedev *vdev;
3106         struct vxge_vpath *vpath;
3107         int vp_id;
3108
3109         vdev = (struct vxgedev *)netdev_priv(dev);
3110
3111         /* Add these vlan to the vid table */
3112         for (vp_id = 0; vp_id < vdev->no_of_vpath; vp_id++) {
3113                 vpath = &vdev->vpaths[vp_id];
3114                 if (!vpath->is_open)
3115                         continue;
3116                 vxge_hw_vpath_vid_add(vpath->handle, vid);
3117         }
3118 }
3119
3120 /**
3121  * vxge_vlan_rx_add_vid
3122  * @dev: net device pointer.
3123  * @vid: vid
3124  *
3125  * Remove the vlan id from the device's vlan id table
3126  */
3127 static void
3128 vxge_vlan_rx_kill_vid(struct net_device *dev, unsigned short vid)
3129 {
3130         struct vxgedev *vdev;
3131         struct vxge_vpath *vpath;
3132         int vp_id;
3133
3134         vxge_debug_entryexit(VXGE_TRACE, "%s:%d", __func__, __LINE__);
3135
3136         vdev = (struct vxgedev *)netdev_priv(dev);
3137
3138         vlan_group_set_device(vdev->vlgrp, vid, NULL);
3139
3140         /* Delete this vlan from the vid table */
3141         for (vp_id = 0; vp_id < vdev->no_of_vpath; vp_id++) {
3142                 vpath = &vdev->vpaths[vp_id];
3143                 if (!vpath->is_open)
3144                         continue;
3145                 vxge_hw_vpath_vid_delete(vpath->handle, vid);
3146         }
3147         vxge_debug_entryexit(VXGE_TRACE,
3148                 "%s:%d  Exiting...", __func__, __LINE__);
3149 }
3150
3151 static const struct net_device_ops vxge_netdev_ops = {
3152         .ndo_open               = vxge_open,
3153         .ndo_stop               = vxge_close,
3154         .ndo_get_stats          = vxge_get_stats,
3155         .ndo_start_xmit         = vxge_xmit,
3156         .ndo_validate_addr      = eth_validate_addr,
3157         .ndo_set_multicast_list = vxge_set_multicast,
3158
3159         .ndo_do_ioctl           = vxge_ioctl,
3160
3161         .ndo_set_mac_address    = vxge_set_mac_addr,
3162         .ndo_change_mtu         = vxge_change_mtu,
3163         .ndo_vlan_rx_register   = vxge_vlan_rx_register,
3164         .ndo_vlan_rx_kill_vid   = vxge_vlan_rx_kill_vid,
3165         .ndo_vlan_rx_add_vid    = vxge_vlan_rx_add_vid,
3166
3167         .ndo_tx_timeout         = vxge_tx_watchdog,
3168 #ifdef CONFIG_NET_POLL_CONTROLLER
3169         .ndo_poll_controller    = vxge_netpoll,
3170 #endif
3171 };
3172
3173 int __devinit vxge_device_register(struct __vxge_hw_device *hldev,
3174                                    struct vxge_config *config,
3175                                    int high_dma, int no_of_vpath,
3176                                    struct vxgedev **vdev_out)
3177 {
3178         struct net_device *ndev;
3179         enum vxge_hw_status status = VXGE_HW_OK;
3180         struct vxgedev *vdev;
3181         int i, ret = 0, no_of_queue = 1;
3182         u64 stat;
3183
3184         *vdev_out = NULL;
3185         if (config->tx_steering_type)
3186                 no_of_queue = no_of_vpath;
3187
3188         ndev = alloc_etherdev_mq(sizeof(struct vxgedev),
3189                         no_of_queue);
3190         if (ndev == NULL) {
3191                 vxge_debug_init(
3192                         vxge_hw_device_trace_level_get(hldev),
3193                 "%s : device allocation failed", __func__);
3194                 ret = -ENODEV;
3195                 goto _out0;
3196         }
3197
3198         vxge_debug_entryexit(
3199                 vxge_hw_device_trace_level_get(hldev),
3200                 "%s: %s:%d  Entering...",
3201                 ndev->name, __func__, __LINE__);
3202
3203         vdev = netdev_priv(ndev);
3204         memset(vdev, 0, sizeof(struct vxgedev));
3205
3206         vdev->ndev = ndev;
3207         vdev->devh = hldev;
3208         vdev->pdev = hldev->pdev;
3209         memcpy(&vdev->config, config, sizeof(struct vxge_config));
3210         vdev->rx_csum = 1;      /* Enable Rx CSUM by default. */
3211
3212         SET_NETDEV_DEV(ndev, &vdev->pdev->dev);
3213
3214         ndev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX |
3215                                 NETIF_F_HW_VLAN_FILTER;
3216         /*  Driver entry points */
3217         ndev->irq = vdev->pdev->irq;
3218         ndev->base_addr = (unsigned long) hldev->bar0;
3219
3220         ndev->netdev_ops = &vxge_netdev_ops;
3221
3222         ndev->watchdog_timeo = VXGE_LL_WATCH_DOG_TIMEOUT;
3223
3224         initialize_ethtool_ops(ndev);
3225
3226         /* Allocate memory for vpath */
3227         vdev->vpaths = kzalloc((sizeof(struct vxge_vpath)) *
3228                                 no_of_vpath, GFP_KERNEL);
3229         if (!vdev->vpaths) {
3230                 vxge_debug_init(VXGE_ERR,
3231                         "%s: vpath memory allocation failed",
3232                         vdev->ndev->name);
3233                 ret = -ENODEV;
3234                 goto _out1;
3235         }
3236
3237         ndev->features |= NETIF_F_SG;
3238
3239         ndev->features |= NETIF_F_HW_CSUM;
3240         vxge_debug_init(vxge_hw_device_trace_level_get(hldev),
3241                 "%s : checksuming enabled", __func__);
3242
3243         if (high_dma) {
3244                 ndev->features |= NETIF_F_HIGHDMA;
3245                 vxge_debug_init(vxge_hw_device_trace_level_get(hldev),
3246                         "%s : using High DMA", __func__);
3247         }
3248
3249         ndev->features |= NETIF_F_TSO | NETIF_F_TSO6;
3250
3251         if (vdev->config.gro_enable)
3252                 ndev->features |= NETIF_F_GRO;
3253
3254 #ifdef NETIF_F_LLTX
3255         ndev->features |= NETIF_F_LLTX;
3256 #endif
3257
3258         for (i = 0; i < no_of_vpath; i++)
3259                 spin_lock_init(&vdev->vpaths[i].fifo.tx_lock);
3260
3261         if (register_netdev(ndev)) {
3262                 vxge_debug_init(vxge_hw_device_trace_level_get(hldev),
3263                         "%s: %s : device registration failed!",
3264                         ndev->name, __func__);
3265                 ret = -ENODEV;
3266                 goto _out2;
3267         }
3268
3269         /*  Set the factory defined MAC address initially */
3270         ndev->addr_len = ETH_ALEN;
3271
3272         /* Make Link state as off at this point, when the Link change
3273          * interrupt comes the state will be automatically changed to
3274          * the right state.
3275          */
3276         netif_carrier_off(ndev);
3277
3278         vxge_debug_init(vxge_hw_device_trace_level_get(hldev),
3279                 "%s: Ethernet device registered",
3280                 ndev->name);
3281
3282         *vdev_out = vdev;
3283
3284         /* Resetting the Device stats */
3285         status = vxge_hw_mrpcim_stats_access(
3286                                 hldev,
3287                                 VXGE_HW_STATS_OP_CLEAR_ALL_STATS,
3288                                 0,
3289                                 0,
3290                                 &stat);
3291
3292         if (status == VXGE_HW_ERR_PRIVILAGED_OPEARATION)
3293                 vxge_debug_init(
3294                         vxge_hw_device_trace_level_get(hldev),
3295                         "%s: device stats clear returns"
3296                         "VXGE_HW_ERR_PRIVILAGED_OPEARATION", ndev->name);
3297
3298         vxge_debug_entryexit(vxge_hw_device_trace_level_get(hldev),
3299                 "%s: %s:%d  Exiting...",
3300                 ndev->name, __func__, __LINE__);
3301
3302         return ret;
3303 _out2:
3304         kfree(vdev->vpaths);
3305 _out1:
3306         free_netdev(ndev);
3307 _out0:
3308         return ret;
3309 }
3310
3311 /*
3312  * vxge_device_unregister
3313  *
3314  * This function will unregister and free network device
3315  */
3316 void
3317 vxge_device_unregister(struct __vxge_hw_device *hldev)
3318 {
3319         struct vxgedev *vdev;
3320         struct net_device *dev;
3321         char buf[IFNAMSIZ];
3322 #if ((VXGE_DEBUG_INIT & VXGE_DEBUG_MASK) || \
3323         (VXGE_DEBUG_ENTRYEXIT & VXGE_DEBUG_MASK))
3324         u32 level_trace;
3325 #endif
3326
3327         dev = hldev->ndev;
3328         vdev = netdev_priv(dev);
3329 #if ((VXGE_DEBUG_INIT & VXGE_DEBUG_MASK) || \
3330         (VXGE_DEBUG_ENTRYEXIT & VXGE_DEBUG_MASK))
3331         level_trace = vdev->level_trace;
3332 #endif
3333         vxge_debug_entryexit(level_trace,
3334                 "%s: %s:%d", vdev->ndev->name, __func__, __LINE__);
3335
3336         memcpy(buf, vdev->ndev->name, IFNAMSIZ);
3337
3338         /* in 2.6 will call stop() if device is up */
3339         unregister_netdev(dev);
3340
3341         flush_scheduled_work();
3342
3343         vxge_debug_init(level_trace, "%s: ethernet device unregistered", buf);
3344         vxge_debug_entryexit(level_trace,
3345                 "%s: %s:%d  Exiting...", buf, __func__, __LINE__);
3346 }
3347
3348 /*
3349  * vxge_callback_crit_err
3350  *
3351  * This function is called by the alarm handler in interrupt context.
3352  * Driver must analyze it based on the event type.
3353  */
3354 static void
3355 vxge_callback_crit_err(struct __vxge_hw_device *hldev,
3356                         enum vxge_hw_event type, u64 vp_id)
3357 {
3358         struct net_device *dev = hldev->ndev;
3359         struct vxgedev *vdev = (struct vxgedev *)netdev_priv(dev);
3360         int vpath_idx;
3361
3362         vxge_debug_entryexit(vdev->level_trace,
3363                 "%s: %s:%d", vdev->ndev->name, __func__, __LINE__);
3364
3365         /* Note: This event type should be used for device wide
3366          * indications only - Serious errors, Slot freeze and critical errors
3367          */
3368         vdev->cric_err_event = type;
3369
3370         for (vpath_idx = 0; vpath_idx < vdev->no_of_vpath; vpath_idx++)
3371                 if (vdev->vpaths[vpath_idx].device_id == vp_id)
3372                         break;
3373
3374         if (!test_bit(__VXGE_STATE_RESET_CARD, &vdev->state)) {
3375                 if (type == VXGE_HW_EVENT_SLOT_FREEZE) {
3376                         vxge_debug_init(VXGE_ERR,
3377                                 "%s: Slot is frozen", vdev->ndev->name);
3378                 } else if (type == VXGE_HW_EVENT_SERR) {
3379                         vxge_debug_init(VXGE_ERR,
3380                                 "%s: Encountered Serious Error",
3381                                 vdev->ndev->name);
3382                 } else if (type == VXGE_HW_EVENT_CRITICAL_ERR)
3383                         vxge_debug_init(VXGE_ERR,
3384                                 "%s: Encountered Critical Error",
3385                                 vdev->ndev->name);
3386         }
3387
3388         if ((type == VXGE_HW_EVENT_SERR) ||
3389                 (type == VXGE_HW_EVENT_SLOT_FREEZE)) {
3390                 if (unlikely(vdev->exec_mode))
3391                         clear_bit(__VXGE_STATE_CARD_UP, &vdev->state);
3392         } else if (type == VXGE_HW_EVENT_CRITICAL_ERR) {
3393                 vxge_hw_device_mask_all(hldev);
3394                 if (unlikely(vdev->exec_mode))
3395                         clear_bit(__VXGE_STATE_CARD_UP, &vdev->state);
3396         } else if ((type == VXGE_HW_EVENT_FIFO_ERR) ||
3397                   (type == VXGE_HW_EVENT_VPATH_ERR)) {
3398
3399                 if (unlikely(vdev->exec_mode))
3400                         clear_bit(__VXGE_STATE_CARD_UP, &vdev->state);
3401                 else {
3402                         /* check if this vpath is already set for reset */
3403                         if (!test_and_set_bit(vpath_idx, &vdev->vp_reset)) {
3404
3405                                 /* disable interrupts for this vpath */
3406                                 vxge_vpath_intr_disable(vdev, vpath_idx);
3407
3408                                 /* stop the queue for this vpath */
3409                                 vxge_stop_tx_queue(&vdev->vpaths[vpath_idx].
3410                                                         fifo);
3411                         }
3412                 }
3413         }
3414
3415         vxge_debug_entryexit(vdev->level_trace,
3416                 "%s: %s:%d  Exiting...",
3417                 vdev->ndev->name, __func__, __LINE__);
3418 }
3419
3420 static void verify_bandwidth(void)
3421 {
3422         int i, band_width, total = 0, equal_priority = 0;
3423
3424         /* 1. If user enters 0 for some fifo, give equal priority to all */
3425         for (i = 0; i < VXGE_HW_MAX_VIRTUAL_PATHS; i++) {
3426                 if (bw_percentage[i] == 0) {
3427                         equal_priority = 1;
3428                         break;
3429                 }
3430         }
3431
3432         if (!equal_priority) {
3433                 /* 2. If sum exceeds 100, give equal priority to all */
3434                 for (i = 0; i < VXGE_HW_MAX_VIRTUAL_PATHS; i++) {
3435                         if (bw_percentage[i] == 0xFF)
3436                                 break;
3437
3438                         total += bw_percentage[i];
3439                         if (total > VXGE_HW_VPATH_BANDWIDTH_MAX) {
3440                                 equal_priority = 1;
3441                                 break;
3442                         }
3443                 }
3444         }
3445
3446         if (!equal_priority) {
3447                 /* Is all the bandwidth consumed? */
3448                 if (total < VXGE_HW_VPATH_BANDWIDTH_MAX) {
3449                         if (i < VXGE_HW_MAX_VIRTUAL_PATHS) {
3450                                 /* Split rest of bw equally among next VPs*/
3451                                 band_width =
3452                                   (VXGE_HW_VPATH_BANDWIDTH_MAX  - total) /
3453                                         (VXGE_HW_MAX_VIRTUAL_PATHS - i);
3454                                 if (band_width < 2) /* min of 2% */
3455                                         equal_priority = 1;
3456                                 else {
3457                                         for (; i < VXGE_HW_MAX_VIRTUAL_PATHS;
3458                                                 i++)
3459                                                 bw_percentage[i] =
3460                                                         band_width;
3461                                 }
3462                         }
3463                 } else if (i < VXGE_HW_MAX_VIRTUAL_PATHS)
3464                         equal_priority = 1;
3465         }
3466
3467         if (equal_priority) {
3468                 vxge_debug_init(VXGE_ERR,
3469                         "%s: Assigning equal bandwidth to all the vpaths",
3470                         VXGE_DRIVER_NAME);
3471                 bw_percentage[0] = VXGE_HW_VPATH_BANDWIDTH_MAX /
3472                                         VXGE_HW_MAX_VIRTUAL_PATHS;
3473                 for (i = 1; i < VXGE_HW_MAX_VIRTUAL_PATHS; i++)
3474                         bw_percentage[i] = bw_percentage[0];
3475         }
3476 }
3477
3478 /*
3479  * Vpath configuration
3480  */
3481 static int __devinit vxge_config_vpaths(
3482                         struct vxge_hw_device_config *device_config,
3483                         u64 vpath_mask, struct vxge_config *config_param)
3484 {
3485         int i, no_of_vpaths = 0, default_no_vpath = 0, temp;
3486         u32 txdl_size, txdl_per_memblock;
3487
3488         temp = driver_config->vpath_per_dev;
3489         if ((driver_config->vpath_per_dev == VXGE_USE_DEFAULT) &&
3490                 (max_config_dev == VXGE_MAX_CONFIG_DEV)) {
3491                 /* No more CPU. Return vpath number as zero.*/
3492                 if (driver_config->g_no_cpus == -1)
3493                         return 0;
3494
3495                 if (!driver_config->g_no_cpus)
3496                         driver_config->g_no_cpus = num_online_cpus();
3497
3498                 driver_config->vpath_per_dev = driver_config->g_no_cpus >> 1;
3499                 if (!driver_config->vpath_per_dev)
3500                         driver_config->vpath_per_dev = 1;
3501
3502                 for (i = 0; i < VXGE_HW_MAX_VIRTUAL_PATHS; i++)
3503                         if (!vxge_bVALn(vpath_mask, i, 1))
3504                                 continue;
3505                         else
3506                                 default_no_vpath++;
3507                 if (default_no_vpath < driver_config->vpath_per_dev)
3508                         driver_config->vpath_per_dev = default_no_vpath;
3509
3510                 driver_config->g_no_cpus = driver_config->g_no_cpus -
3511                                 (driver_config->vpath_per_dev * 2);
3512                 if (driver_config->g_no_cpus <= 0)
3513                         driver_config->g_no_cpus = -1;
3514         }
3515
3516         if (driver_config->vpath_per_dev == 1) {
3517                 vxge_debug_ll_config(VXGE_TRACE,
3518                         "%s: Disable tx and rx steering, "
3519                         "as single vpath is configured", VXGE_DRIVER_NAME);
3520                 config_param->rth_steering = NO_STEERING;
3521                 config_param->tx_steering_type = NO_STEERING;
3522                 device_config->rth_en = 0;
3523         }
3524
3525         /* configure bandwidth */
3526         for (i = 0; i < VXGE_HW_MAX_VIRTUAL_PATHS; i++)
3527                 device_config->vp_config[i].min_bandwidth = bw_percentage[i];
3528
3529         for (i = 0; i < VXGE_HW_MAX_VIRTUAL_PATHS; i++) {
3530                 device_config->vp_config[i].vp_id = i;
3531                 device_config->vp_config[i].mtu = VXGE_HW_DEFAULT_MTU;
3532                 if (no_of_vpaths < driver_config->vpath_per_dev) {
3533                         if (!vxge_bVALn(vpath_mask, i, 1)) {
3534                                 vxge_debug_ll_config(VXGE_TRACE,
3535                                         "%s: vpath: %d is not available",
3536                                         VXGE_DRIVER_NAME, i);
3537                                 continue;
3538                         } else {
3539                                 vxge_debug_ll_config(VXGE_TRACE,
3540                                         "%s: vpath: %d available",
3541                                         VXGE_DRIVER_NAME, i);
3542                                 no_of_vpaths++;
3543                         }
3544                 } else {
3545                         vxge_debug_ll_config(VXGE_TRACE,
3546                                 "%s: vpath: %d is not configured, "
3547                                 "max_config_vpath exceeded",
3548                                 VXGE_DRIVER_NAME, i);
3549                         break;
3550                 }
3551
3552                 /* Configure Tx fifo's */
3553                 device_config->vp_config[i].fifo.enable =
3554                                                 VXGE_HW_FIFO_ENABLE;
3555                 device_config->vp_config[i].fifo.max_frags =
3556                                 MAX_SKB_FRAGS + 1;
3557                 device_config->vp_config[i].fifo.memblock_size =
3558                         VXGE_HW_MIN_FIFO_MEMBLOCK_SIZE;
3559
3560                 txdl_size = device_config->vp_config[i].fifo.max_frags *
3561                                 sizeof(struct vxge_hw_fifo_txd);
3562                 txdl_per_memblock = VXGE_HW_MIN_FIFO_MEMBLOCK_SIZE / txdl_size;
3563
3564                 device_config->vp_config[i].fifo.fifo_blocks =
3565                         ((VXGE_DEF_FIFO_LENGTH - 1) / txdl_per_memblock) + 1;
3566
3567                 device_config->vp_config[i].fifo.intr =
3568                                 VXGE_HW_FIFO_QUEUE_INTR_DISABLE;
3569
3570                 /* Configure tti properties */
3571                 device_config->vp_config[i].tti.intr_enable =
3572                                         VXGE_HW_TIM_INTR_ENABLE;
3573
3574                 device_config->vp_config[i].tti.btimer_val =
3575                         (VXGE_TTI_BTIMER_VAL * 1000) / 272;
3576
3577                 device_config->vp_config[i].tti.timer_ac_en =
3578                                 VXGE_HW_TIM_TIMER_AC_ENABLE;
3579
3580                 /* For msi-x with napi (each vector
3581                 has a handler of its own) -
3582                 Set CI to OFF for all vpaths */
3583                 device_config->vp_config[i].tti.timer_ci_en =
3584                         VXGE_HW_TIM_TIMER_CI_DISABLE;
3585
3586                 device_config->vp_config[i].tti.timer_ri_en =
3587                                 VXGE_HW_TIM_TIMER_RI_DISABLE;
3588
3589                 device_config->vp_config[i].tti.util_sel =
3590                         VXGE_HW_TIM_UTIL_SEL_LEGACY_TX_NET_UTIL;
3591
3592                 device_config->vp_config[i].tti.ltimer_val =
3593                         (VXGE_TTI_LTIMER_VAL * 1000) / 272;
3594
3595                 device_config->vp_config[i].tti.rtimer_val =
3596                         (VXGE_TTI_RTIMER_VAL * 1000) / 272;
3597
3598                 device_config->vp_config[i].tti.urange_a = TTI_TX_URANGE_A;
3599                 device_config->vp_config[i].tti.urange_b = TTI_TX_URANGE_B;
3600                 device_config->vp_config[i].tti.urange_c = TTI_TX_URANGE_C;
3601                 device_config->vp_config[i].tti.uec_a = TTI_TX_UFC_A;
3602                 device_config->vp_config[i].tti.uec_b = TTI_TX_UFC_B;
3603                 device_config->vp_config[i].tti.uec_c = TTI_TX_UFC_C;
3604                 device_config->vp_config[i].tti.uec_d = TTI_TX_UFC_D;
3605
3606                 /* Configure Rx rings */
3607                 device_config->vp_config[i].ring.enable  =
3608                                                 VXGE_HW_RING_ENABLE;
3609
3610                 device_config->vp_config[i].ring.ring_blocks  =
3611                                                 VXGE_HW_DEF_RING_BLOCKS;
3612                 device_config->vp_config[i].ring.buffer_mode =
3613                         VXGE_HW_RING_RXD_BUFFER_MODE_1;
3614                 device_config->vp_config[i].ring.rxds_limit  =
3615                                 VXGE_HW_DEF_RING_RXDS_LIMIT;
3616                 device_config->vp_config[i].ring.scatter_mode =
3617                                         VXGE_HW_RING_SCATTER_MODE_A;
3618
3619                 /* Configure rti properties */
3620                 device_config->vp_config[i].rti.intr_enable =
3621                                         VXGE_HW_TIM_INTR_ENABLE;
3622
3623                 device_config->vp_config[i].rti.btimer_val =
3624                         (VXGE_RTI_BTIMER_VAL * 1000)/272;
3625
3626                 device_config->vp_config[i].rti.timer_ac_en =
3627                                                 VXGE_HW_TIM_TIMER_AC_ENABLE;
3628
3629                 device_config->vp_config[i].rti.timer_ci_en =
3630                                                 VXGE_HW_TIM_TIMER_CI_DISABLE;
3631
3632                 device_config->vp_config[i].rti.timer_ri_en =
3633                                                 VXGE_HW_TIM_TIMER_RI_DISABLE;
3634
3635                 device_config->vp_config[i].rti.util_sel =
3636                                 VXGE_HW_TIM_UTIL_SEL_LEGACY_RX_NET_UTIL;
3637
3638                 device_config->vp_config[i].rti.urange_a =
3639                                                 RTI_RX_URANGE_A;
3640                 device_config->vp_config[i].rti.urange_b =
3641                                                 RTI_RX_URANGE_B;
3642                 device_config->vp_config[i].rti.urange_c =
3643                                                 RTI_RX_URANGE_C;
3644                 device_config->vp_config[i].rti.uec_a = RTI_RX_UFC_A;
3645                 device_config->vp_config[i].rti.uec_b = RTI_RX_UFC_B;
3646                 device_config->vp_config[i].rti.uec_c = RTI_RX_UFC_C;
3647                 device_config->vp_config[i].rti.uec_d = RTI_RX_UFC_D;
3648
3649                 device_config->vp_config[i].rti.rtimer_val =
3650                         (VXGE_RTI_RTIMER_VAL * 1000) / 272;
3651
3652                 device_config->vp_config[i].rti.ltimer_val =
3653                         (VXGE_RTI_LTIMER_VAL * 1000) / 272;
3654
3655                 device_config->vp_config[i].rpa_strip_vlan_tag =
3656                         vlan_tag_strip;
3657         }
3658
3659         driver_config->vpath_per_dev = temp;
3660         return no_of_vpaths;
3661 }
3662
3663 /* initialize device configuratrions */
3664 static void __devinit vxge_device_config_init(
3665                                 struct vxge_hw_device_config *device_config,
3666                                 int *intr_type)
3667 {
3668         /* Used for CQRQ/SRQ. */
3669         device_config->dma_blockpool_initial =
3670                         VXGE_HW_INITIAL_DMA_BLOCK_POOL_SIZE;
3671
3672         device_config->dma_blockpool_max =
3673                         VXGE_HW_MAX_DMA_BLOCK_POOL_SIZE;
3674
3675         if (max_mac_vpath > VXGE_MAX_MAC_ADDR_COUNT)
3676                 max_mac_vpath = VXGE_MAX_MAC_ADDR_COUNT;
3677
3678 #ifndef CONFIG_PCI_MSI
3679         vxge_debug_init(VXGE_ERR,
3680                 "%s: This Kernel does not support "
3681                 "MSI-X. Defaulting to INTA", VXGE_DRIVER_NAME);
3682         *intr_type = INTA;
3683 #endif
3684
3685         /* Configure whether MSI-X or IRQL. */
3686         switch (*intr_type) {
3687         case INTA:
3688                 device_config->intr_mode = VXGE_HW_INTR_MODE_IRQLINE;
3689                 break;
3690
3691         case MSI_X:
3692                 device_config->intr_mode = VXGE_HW_INTR_MODE_MSIX;
3693                 break;
3694         }
3695         /* Timer period between device poll */
3696         device_config->device_poll_millis = VXGE_TIMER_DELAY;
3697
3698         /* Configure mac based steering. */
3699         device_config->rts_mac_en = addr_learn_en;
3700
3701         /* Configure Vpaths */
3702         device_config->rth_it_type = VXGE_HW_RTH_IT_TYPE_MULTI_IT;
3703
3704         vxge_debug_ll_config(VXGE_TRACE, "%s : Device Config Params ",
3705                         __func__);
3706         vxge_debug_ll_config(VXGE_TRACE, "dma_blockpool_initial : %d",
3707                         device_config->dma_blockpool_initial);
3708         vxge_debug_ll_config(VXGE_TRACE, "dma_blockpool_max : %d",
3709                         device_config->dma_blockpool_max);
3710         vxge_debug_ll_config(VXGE_TRACE, "intr_mode : %d",
3711                         device_config->intr_mode);
3712         vxge_debug_ll_config(VXGE_TRACE, "device_poll_millis : %d",
3713                         device_config->device_poll_millis);
3714         vxge_debug_ll_config(VXGE_TRACE, "rts_mac_en : %d",
3715                         device_config->rts_mac_en);
3716         vxge_debug_ll_config(VXGE_TRACE, "rth_en : %d",
3717                         device_config->rth_en);
3718         vxge_debug_ll_config(VXGE_TRACE, "rth_it_type : %d",
3719                         device_config->rth_it_type);
3720 }
3721
3722 static void __devinit vxge_print_parm(struct vxgedev *vdev, u64 vpath_mask)
3723 {
3724         int i;
3725
3726         vxge_debug_init(VXGE_TRACE,
3727                 "%s: %d Vpath(s) opened",
3728                 vdev->ndev->name, vdev->no_of_vpath);
3729
3730         switch (vdev->config.intr_type) {
3731         case INTA:
3732                 vxge_debug_init(VXGE_TRACE,
3733                         "%s: Interrupt type INTA", vdev->ndev->name);
3734                 break;
3735
3736         case MSI_X:
3737                 vxge_debug_init(VXGE_TRACE,
3738                         "%s: Interrupt type MSI-X", vdev->ndev->name);
3739                 break;
3740         }
3741
3742         if (vdev->config.rth_steering) {
3743                 vxge_debug_init(VXGE_TRACE,
3744                         "%s: RTH steering enabled for TCP_IPV4",
3745                         vdev->ndev->name);
3746         } else {
3747                 vxge_debug_init(VXGE_TRACE,
3748                         "%s: RTH steering disabled", vdev->ndev->name);
3749         }
3750
3751         switch (vdev->config.tx_steering_type) {
3752         case NO_STEERING:
3753                 vxge_debug_init(VXGE_TRACE,
3754                         "%s: Tx steering disabled", vdev->ndev->name);
3755                 break;
3756         case TX_PRIORITY_STEERING:
3757                 vxge_debug_init(VXGE_TRACE,
3758                         "%s: Unsupported tx steering option",
3759                         vdev->ndev->name);
3760                 vxge_debug_init(VXGE_TRACE,
3761                         "%s: Tx steering disabled", vdev->ndev->name);
3762                 vdev->config.tx_steering_type = 0;
3763                 break;
3764         case TX_VLAN_STEERING:
3765                 vxge_debug_init(VXGE_TRACE,
3766                         "%s: Unsupported tx steering option",
3767                         vdev->ndev->name);
3768                 vxge_debug_init(VXGE_TRACE,
3769                         "%s: Tx steering disabled", vdev->ndev->name);
3770                 vdev->config.tx_steering_type = 0;
3771                 break;
3772         case TX_MULTIQ_STEERING:
3773                 vxge_debug_init(VXGE_TRACE,
3774                         "%s: Tx multiqueue steering enabled",
3775                         vdev->ndev->name);
3776                 break;
3777         case TX_PORT_STEERING:
3778                 vxge_debug_init(VXGE_TRACE,
3779                         "%s: Tx port steering enabled",
3780                         vdev->ndev->name);
3781                 break;
3782         default:
3783                 vxge_debug_init(VXGE_ERR,
3784                         "%s: Unsupported tx steering type",
3785                         vdev->ndev->name);
3786                 vxge_debug_init(VXGE_TRACE,
3787                         "%s: Tx steering disabled", vdev->ndev->name);
3788                 vdev->config.tx_steering_type = 0;
3789         }
3790
3791         if (vdev->config.gro_enable) {
3792                 vxge_debug_init(VXGE_ERR,
3793                         "%s: Generic receive offload enabled",
3794                         vdev->ndev->name);
3795         } else
3796                 vxge_debug_init(VXGE_TRACE,
3797                         "%s: Generic receive offload disabled",
3798                         vdev->ndev->name);
3799
3800         if (vdev->config.addr_learn_en)
3801                 vxge_debug_init(VXGE_TRACE,
3802                         "%s: MAC Address learning enabled", vdev->ndev->name);
3803
3804         vxge_debug_init(VXGE_TRACE,
3805                 "%s: Rx doorbell mode enabled", vdev->ndev->name);
3806
3807         for (i = 0; i < VXGE_HW_MAX_VIRTUAL_PATHS; i++) {
3808                 if (!vxge_bVALn(vpath_mask, i, 1))
3809                         continue;
3810                 vxge_debug_ll_config(VXGE_TRACE,
3811                         "%s: MTU size - %d", vdev->ndev->name,
3812                         ((struct __vxge_hw_device  *)(vdev->devh))->
3813                                 config.vp_config[i].mtu);
3814                 vxge_debug_init(VXGE_TRACE,
3815                         "%s: VLAN tag stripping %s", vdev->ndev->name,
3816                         ((struct __vxge_hw_device  *)(vdev->devh))->
3817                                 config.vp_config[i].rpa_strip_vlan_tag
3818                         ? "Enabled" : "Disabled");
3819                 vxge_debug_init(VXGE_TRACE,
3820                         "%s: Ring blocks : %d", vdev->ndev->name,
3821                         ((struct __vxge_hw_device  *)(vdev->devh))->
3822                                 config.vp_config[i].ring.ring_blocks);
3823                 vxge_debug_init(VXGE_TRACE,
3824                         "%s: Fifo blocks : %d", vdev->ndev->name,
3825                         ((struct __vxge_hw_device  *)(vdev->devh))->
3826                                 config.vp_config[i].fifo.fifo_blocks);
3827                 vxge_debug_ll_config(VXGE_TRACE,
3828                         "%s: Max frags : %d", vdev->ndev->name,
3829                         ((struct __vxge_hw_device  *)(vdev->devh))->
3830                                 config.vp_config[i].fifo.max_frags);
3831                 break;
3832         }
3833 }
3834
3835 #ifdef CONFIG_PM
3836 /**
3837  * vxge_pm_suspend - vxge power management suspend entry point
3838  *
3839  */
3840 static int vxge_pm_suspend(struct pci_dev *pdev, pm_message_t state)
3841 {
3842         return -ENOSYS;
3843 }
3844 /**
3845  * vxge_pm_resume - vxge power management resume entry point
3846  *
3847  */
3848 static int vxge_pm_resume(struct pci_dev *pdev)
3849 {
3850         return -ENOSYS;
3851 }
3852
3853 #endif
3854
3855 /**
3856  * vxge_io_error_detected - called when PCI error is detected
3857  * @pdev: Pointer to PCI device
3858  * @state: The current pci connection state
3859  *
3860  * This function is called after a PCI bus error affecting
3861  * this device has been detected.
3862  */
3863 static pci_ers_result_t vxge_io_error_detected(struct pci_dev *pdev,
3864                                                 pci_channel_state_t state)
3865 {
3866         struct __vxge_hw_device  *hldev =
3867                 (struct __vxge_hw_device  *) pci_get_drvdata(pdev);
3868         struct net_device *netdev = hldev->ndev;
3869
3870         netif_device_detach(netdev);
3871
3872         if (state == pci_channel_io_perm_failure)
3873                 return PCI_ERS_RESULT_DISCONNECT;
3874
3875         if (netif_running(netdev)) {
3876                 /* Bring down the card, while avoiding PCI I/O */
3877                 do_vxge_close(netdev, 0);
3878         }
3879
3880         pci_disable_device(pdev);
3881
3882         return PCI_ERS_RESULT_NEED_RESET;
3883 }
3884
3885 /**
3886  * vxge_io_slot_reset - called after the pci bus has been reset.
3887  * @pdev: Pointer to PCI device
3888  *
3889  * Restart the card from scratch, as if from a cold-boot.
3890  * At this point, the card has exprienced a hard reset,
3891  * followed by fixups by BIOS, and has its config space
3892  * set up identically to what it was at cold boot.
3893  */
3894 static pci_ers_result_t vxge_io_slot_reset(struct pci_dev *pdev)
3895 {
3896         struct __vxge_hw_device  *hldev =
3897                 (struct __vxge_hw_device  *) pci_get_drvdata(pdev);
3898         struct net_device *netdev = hldev->ndev;
3899
3900         struct vxgedev *vdev = netdev_priv(netdev);
3901
3902         if (pci_enable_device(pdev)) {
3903                 printk(KERN_ERR "%s: "
3904                         "Cannot re-enable device after reset\n",
3905                         VXGE_DRIVER_NAME);
3906                 return PCI_ERS_RESULT_DISCONNECT;
3907         }
3908
3909         pci_set_master(pdev);
3910         vxge_reset(vdev);
3911
3912         return PCI_ERS_RESULT_RECOVERED;
3913 }
3914
3915 /**
3916  * vxge_io_resume - called when traffic can start flowing again.
3917  * @pdev: Pointer to PCI device
3918  *
3919  * This callback is called when the error recovery driver tells
3920  * us that its OK to resume normal operation.
3921  */
3922 static void vxge_io_resume(struct pci_dev *pdev)
3923 {
3924         struct __vxge_hw_device  *hldev =
3925                 (struct __vxge_hw_device  *) pci_get_drvdata(pdev);
3926         struct net_device *netdev = hldev->ndev;
3927
3928         if (netif_running(netdev)) {
3929                 if (vxge_open(netdev)) {
3930                         printk(KERN_ERR "%s: "
3931                                 "Can't bring device back up after reset\n",
3932                                 VXGE_DRIVER_NAME);
3933                         return;
3934                 }
3935         }
3936
3937         netif_device_attach(netdev);
3938 }
3939
3940 static inline u32 vxge_get_num_vfs(u64 function_mode)
3941 {
3942         u32 num_functions = 0;
3943
3944         switch (function_mode) {
3945         case VXGE_HW_FUNCTION_MODE_MULTI_FUNCTION:
3946         case VXGE_HW_FUNCTION_MODE_SRIOV_8:
3947                 num_functions = 8;
3948                 break;
3949         case VXGE_HW_FUNCTION_MODE_SINGLE_FUNCTION:
3950                 num_functions = 1;
3951                 break;
3952         case VXGE_HW_FUNCTION_MODE_SRIOV:
3953         case VXGE_HW_FUNCTION_MODE_MRIOV:
3954         case VXGE_HW_FUNCTION_MODE_MULTI_FUNCTION_17:
3955                 num_functions = 17;
3956                 break;
3957         case VXGE_HW_FUNCTION_MODE_SRIOV_4:
3958                 num_functions = 4;
3959                 break;
3960         case VXGE_HW_FUNCTION_MODE_MULTI_FUNCTION_2:
3961                 num_functions = 2;
3962                 break;
3963         case VXGE_HW_FUNCTION_MODE_MRIOV_8:
3964                 num_functions = 8; /* TODO */
3965                 break;
3966         }
3967         return num_functions;
3968 }
3969
3970 /**
3971  * vxge_probe
3972  * @pdev : structure containing the PCI related information of the device.
3973  * @pre: List of PCI devices supported by the driver listed in vxge_id_table.
3974  * Description:
3975  * This function is called when a new PCI device gets detected and initializes
3976  * it.
3977  * Return value:
3978  * returns 0 on success and negative on failure.
3979  *
3980  */
3981 static int __devinit
3982 vxge_probe(struct pci_dev *pdev, const struct pci_device_id *pre)
3983 {
3984         struct __vxge_hw_device  *hldev;
3985         enum vxge_hw_status status;
3986         int ret;
3987         int high_dma = 0;
3988         u64 vpath_mask = 0;
3989         struct vxgedev *vdev;
3990         struct vxge_config *ll_config = NULL;
3991         struct vxge_hw_device_config *device_config = NULL;
3992         struct vxge_hw_device_attr attr;
3993         int i, j, no_of_vpath = 0, max_vpath_supported = 0;
3994         u8 *macaddr;
3995         struct vxge_mac_addrs *entry;
3996         static int bus = -1, device = -1;
3997         u32 host_type;
3998         u8 new_device = 0;
3999         enum vxge_hw_status is_privileged;
4000         u32 function_mode;
4001         u32 num_vfs = 0;
4002
4003         vxge_debug_entryexit(VXGE_TRACE, "%s:%d", __func__, __LINE__);
4004         attr.pdev = pdev;
4005
4006         /* In SRIOV-17 mode, functions of the same adapter
4007          * can be deployed on different buses */
4008         if ((!pdev->is_virtfn) && ((bus != pdev->bus->number) ||
4009                 (device != PCI_SLOT(pdev->devfn))))
4010                 new_device = 1;
4011
4012         bus = pdev->bus->number;
4013         device = PCI_SLOT(pdev->devfn);
4014
4015         if (new_device) {
4016                 if (driver_config->config_dev_cnt &&
4017                    (driver_config->config_dev_cnt !=
4018                         driver_config->total_dev_cnt))
4019                         vxge_debug_init(VXGE_ERR,
4020                                 "%s: Configured %d of %d devices",
4021                                 VXGE_DRIVER_NAME,
4022                                 driver_config->config_dev_cnt,
4023                                 driver_config->total_dev_cnt);
4024                 driver_config->config_dev_cnt = 0;
4025                 driver_config->total_dev_cnt = 0;
4026         }
4027         /* Now making the CPU based no of vpath calculation
4028          * applicable for individual functions as well.
4029          */
4030         driver_config->g_no_cpus = 0;
4031         driver_config->vpath_per_dev = max_config_vpath;
4032
4033         driver_config->total_dev_cnt++;
4034         if (++driver_config->config_dev_cnt > max_config_dev) {
4035                 ret = 0;
4036                 goto _exit0;
4037         }
4038
4039         device_config = kzalloc(sizeof(struct vxge_hw_device_config),
4040                 GFP_KERNEL);
4041         if (!device_config) {
4042                 ret = -ENOMEM;
4043                 vxge_debug_init(VXGE_ERR,
4044                         "device_config : malloc failed %s %d",
4045                         __FILE__, __LINE__);
4046                 goto _exit0;
4047         }
4048
4049         ll_config = kzalloc(sizeof(*ll_config), GFP_KERNEL);
4050         if (!ll_config) {
4051                 ret = -ENOMEM;
4052                 vxge_debug_init(VXGE_ERR,
4053                         "ll_config : malloc failed %s %d",
4054                         __FILE__, __LINE__);
4055                 goto _exit0;
4056         }
4057         ll_config->tx_steering_type = TX_MULTIQ_STEERING;
4058         ll_config->intr_type = MSI_X;
4059         ll_config->napi_weight = NEW_NAPI_WEIGHT;
4060         ll_config->rth_steering = RTH_STEERING;
4061
4062         /* get the default configuration parameters */
4063         vxge_hw_device_config_default_get(device_config);
4064
4065         /* initialize configuration parameters */
4066         vxge_device_config_init(device_config, &ll_config->intr_type);
4067
4068         ret = pci_enable_device(pdev);
4069         if (ret) {
4070                 vxge_debug_init(VXGE_ERR,
4071                         "%s : can not enable PCI device", __func__);
4072                 goto _exit0;
4073         }
4074
4075         if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
4076                 vxge_debug_ll_config(VXGE_TRACE,
4077                         "%s : using 64bit DMA", __func__);
4078
4079                 high_dma = 1;
4080
4081                 if (pci_set_consistent_dma_mask(pdev,
4082                                                 DMA_BIT_MASK(64))) {
4083                         vxge_debug_init(VXGE_ERR,
4084                                 "%s : unable to obtain 64bit DMA for "
4085                                 "consistent allocations", __func__);
4086                         ret = -ENOMEM;
4087                         goto _exit1;
4088                 }
4089         } else if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) {
4090                 vxge_debug_ll_config(VXGE_TRACE,
4091                         "%s : using 32bit DMA", __func__);
4092         } else {
4093                 ret = -ENOMEM;
4094                 goto _exit1;
4095         }
4096
4097         if (pci_request_regions(pdev, VXGE_DRIVER_NAME)) {
4098                 vxge_debug_init(VXGE_ERR,
4099                         "%s : request regions failed", __func__);
4100                 ret = -ENODEV;
4101                 goto _exit1;
4102         }
4103
4104         pci_set_master(pdev);
4105
4106         attr.bar0 = pci_ioremap_bar(pdev, 0);
4107         if (!attr.bar0) {
4108                 vxge_debug_init(VXGE_ERR,
4109                         "%s : cannot remap io memory bar0", __func__);
4110                 ret = -ENODEV;
4111                 goto _exit2;
4112         }
4113         vxge_debug_ll_config(VXGE_TRACE,
4114                 "pci ioremap bar0: %p:0x%llx",
4115                 attr.bar0,
4116                 (unsigned long long)pci_resource_start(pdev, 0));
4117
4118         status = vxge_hw_device_hw_info_get(attr.bar0,
4119                         &ll_config->device_hw_info);
4120         if (status != VXGE_HW_OK) {
4121                 vxge_debug_init(VXGE_ERR,
4122                         "%s: Reading of hardware info failed."
4123                         "Please try upgrading the firmware.", VXGE_DRIVER_NAME);
4124                 ret = -EINVAL;
4125                 goto _exit3;
4126         }
4127
4128         if (ll_config->device_hw_info.fw_version.major !=
4129                 VXGE_DRIVER_FW_VERSION_MAJOR) {
4130                 vxge_debug_init(VXGE_ERR,
4131                         "%s: Incorrect firmware version."
4132                         "Please upgrade the firmware to version 1.x.x",
4133                         VXGE_DRIVER_NAME);
4134                 ret = -EINVAL;
4135                 goto _exit3;
4136         }
4137
4138         vpath_mask = ll_config->device_hw_info.vpath_mask;
4139         if (vpath_mask == 0) {
4140                 vxge_debug_ll_config(VXGE_TRACE,
4141                         "%s: No vpaths available in device", VXGE_DRIVER_NAME);
4142                 ret = -EINVAL;
4143                 goto _exit3;
4144         }
4145
4146         vxge_debug_ll_config(VXGE_TRACE,
4147                 "%s:%d  Vpath mask = %llx", __func__, __LINE__,
4148                 (unsigned long long)vpath_mask);
4149
4150         function_mode = ll_config->device_hw_info.function_mode;
4151         host_type = ll_config->device_hw_info.host_type;
4152         is_privileged = __vxge_hw_device_is_privilaged(host_type,
4153                 ll_config->device_hw_info.func_id);
4154
4155         /* Check how many vpaths are available */
4156         for (i = 0; i < VXGE_HW_MAX_VIRTUAL_PATHS; i++) {
4157                 if (!((vpath_mask) & vxge_mBIT(i)))
4158                         continue;
4159                 max_vpath_supported++;
4160         }
4161
4162         if (new_device)
4163                 num_vfs = vxge_get_num_vfs(function_mode) - 1;
4164
4165         /* Enable SRIOV mode, if firmware has SRIOV support and if it is a PF */
4166         if (is_sriov(function_mode) && (max_config_dev > 1) &&
4167                 (ll_config->intr_type != INTA) &&
4168                 (is_privileged == VXGE_HW_OK)) {
4169                 ret = pci_enable_sriov(pdev, ((max_config_dev - 1) < num_vfs)
4170                         ? (max_config_dev - 1) : num_vfs);
4171                 if (ret)
4172                         vxge_debug_ll_config(VXGE_ERR,
4173                                 "Failed in enabling SRIOV mode: %d\n", ret);
4174         }
4175
4176         /*
4177          * Configure vpaths and get driver configured number of vpaths
4178          * which is less than or equal to the maximum vpaths per function.
4179          */
4180         no_of_vpath = vxge_config_vpaths(device_config, vpath_mask, ll_config);
4181         if (!no_of_vpath) {
4182                 vxge_debug_ll_config(VXGE_ERR,
4183                         "%s: No more vpaths to configure", VXGE_DRIVER_NAME);
4184                 ret = 0;
4185                 goto _exit3;
4186         }
4187
4188         /* Setting driver callbacks */
4189         attr.uld_callbacks.link_up = vxge_callback_link_up;
4190         attr.uld_callbacks.link_down = vxge_callback_link_down;
4191         attr.uld_callbacks.crit_err = vxge_callback_crit_err;
4192
4193         status = vxge_hw_device_initialize(&hldev, &attr, device_config);
4194         if (status != VXGE_HW_OK) {
4195                 vxge_debug_init(VXGE_ERR,
4196                         "Failed to initialize device (%d)", status);
4197                         ret = -EINVAL;
4198                         goto _exit3;
4199         }
4200
4201         /* if FCS stripping is not disabled in MAC fail driver load */
4202         if (vxge_hw_vpath_strip_fcs_check(hldev, vpath_mask) != VXGE_HW_OK) {
4203                 vxge_debug_init(VXGE_ERR,
4204                         "%s: FCS stripping is not disabled in MAC"
4205                         " failing driver load", VXGE_DRIVER_NAME);
4206                 ret = -EINVAL;
4207                 goto _exit4;
4208         }
4209
4210         vxge_hw_device_debug_set(hldev, VXGE_ERR, VXGE_COMPONENT_LL);
4211
4212         /* set private device info */
4213         pci_set_drvdata(pdev, hldev);
4214
4215         ll_config->gro_enable = VXGE_GRO_ALWAYS_AGGREGATE;
4216         ll_config->fifo_indicate_max_pkts = VXGE_FIFO_INDICATE_MAX_PKTS;
4217         ll_config->addr_learn_en = addr_learn_en;
4218         ll_config->rth_algorithm = RTH_ALG_JENKINS;
4219         ll_config->rth_hash_type_tcpipv4 = VXGE_HW_RING_HASH_TYPE_TCP_IPV4;
4220         ll_config->rth_hash_type_ipv4 = VXGE_HW_RING_HASH_TYPE_NONE;
4221         ll_config->rth_hash_type_tcpipv6 = VXGE_HW_RING_HASH_TYPE_NONE;
4222         ll_config->rth_hash_type_ipv6 = VXGE_HW_RING_HASH_TYPE_NONE;
4223         ll_config->rth_hash_type_tcpipv6ex = VXGE_HW_RING_HASH_TYPE_NONE;
4224         ll_config->rth_hash_type_ipv6ex = VXGE_HW_RING_HASH_TYPE_NONE;
4225         ll_config->rth_bkt_sz = RTH_BUCKET_SIZE;
4226         ll_config->tx_pause_enable = VXGE_PAUSE_CTRL_ENABLE;
4227         ll_config->rx_pause_enable = VXGE_PAUSE_CTRL_ENABLE;
4228
4229         if (vxge_device_register(hldev, ll_config, high_dma, no_of_vpath,
4230                 &vdev)) {
4231                 ret = -EINVAL;
4232                 goto _exit4;
4233         }
4234
4235         vxge_hw_device_debug_set(hldev, VXGE_TRACE, VXGE_COMPONENT_LL);
4236         VXGE_COPY_DEBUG_INFO_TO_LL(vdev, vxge_hw_device_error_level_get(hldev),
4237                 vxge_hw_device_trace_level_get(hldev));
4238
4239         /* set private HW device info */
4240         hldev->ndev = vdev->ndev;
4241         vdev->mtu = VXGE_HW_DEFAULT_MTU;
4242         vdev->bar0 = attr.bar0;
4243         vdev->max_vpath_supported = max_vpath_supported;
4244         vdev->no_of_vpath = no_of_vpath;
4245
4246         /* Virtual Path count */
4247         for (i = 0, j = 0; i < VXGE_HW_MAX_VIRTUAL_PATHS; i++) {
4248                 if (!vxge_bVALn(vpath_mask, i, 1))
4249                         continue;
4250                 if (j >= vdev->no_of_vpath)
4251                         break;
4252
4253                 vdev->vpaths[j].is_configured = 1;
4254                 vdev->vpaths[j].device_id = i;
4255                 vdev->vpaths[j].fifo.driver_id = j;
4256                 vdev->vpaths[j].ring.driver_id = j;
4257                 vdev->vpaths[j].vdev = vdev;
4258                 vdev->vpaths[j].max_mac_addr_cnt = max_mac_vpath;
4259                 memcpy((u8 *)vdev->vpaths[j].macaddr,
4260                                 ll_config->device_hw_info.mac_addrs[i],
4261                                 ETH_ALEN);
4262
4263                 /* Initialize the mac address list header */
4264                 INIT_LIST_HEAD(&vdev->vpaths[j].mac_addr_list);
4265
4266                 vdev->vpaths[j].mac_addr_cnt = 0;
4267                 vdev->vpaths[j].mcast_addr_cnt = 0;
4268                 j++;
4269         }
4270         vdev->exec_mode = VXGE_EXEC_MODE_DISABLE;
4271         vdev->max_config_port = max_config_port;
4272
4273         vdev->vlan_tag_strip = vlan_tag_strip;
4274
4275         /* map the hashing selector table to the configured vpaths */
4276         for (i = 0; i < vdev->no_of_vpath; i++)
4277                 vdev->vpath_selector[i] = vpath_selector[i];
4278
4279         macaddr = (u8 *)vdev->vpaths[0].macaddr;
4280
4281         ll_config->device_hw_info.serial_number[VXGE_HW_INFO_LEN - 1] = '\0';
4282         ll_config->device_hw_info.product_desc[VXGE_HW_INFO_LEN - 1] = '\0';
4283         ll_config->device_hw_info.part_number[VXGE_HW_INFO_LEN - 1] = '\0';
4284
4285         vxge_debug_init(VXGE_TRACE, "%s: SERIAL NUMBER: %s",
4286                 vdev->ndev->name, ll_config->device_hw_info.serial_number);
4287
4288         vxge_debug_init(VXGE_TRACE, "%s: PART NUMBER: %s",
4289                 vdev->ndev->name, ll_config->device_hw_info.part_number);
4290
4291         vxge_debug_init(VXGE_TRACE, "%s: Neterion %s Server Adapter",
4292                 vdev->ndev->name, ll_config->device_hw_info.product_desc);
4293
4294         vxge_debug_init(VXGE_TRACE, "%s: MAC ADDR: %pM",
4295                 vdev->ndev->name, macaddr);
4296
4297         vxge_debug_init(VXGE_TRACE, "%s: Link Width x%d",
4298                 vdev->ndev->name, vxge_hw_device_link_width_get(hldev));
4299
4300         vxge_debug_init(VXGE_TRACE,
4301                 "%s: Firmware version : %s Date : %s", vdev->ndev->name,
4302                 ll_config->device_hw_info.fw_version.version,
4303                 ll_config->device_hw_info.fw_date.date);
4304
4305         if (new_device) {
4306                 switch (ll_config->device_hw_info.function_mode) {
4307                 case VXGE_HW_FUNCTION_MODE_SINGLE_FUNCTION:
4308                         vxge_debug_init(VXGE_TRACE,
4309                         "%s: Single Function Mode Enabled", vdev->ndev->name);
4310                 break;
4311                 case VXGE_HW_FUNCTION_MODE_MULTI_FUNCTION:
4312                         vxge_debug_init(VXGE_TRACE,
4313                         "%s: Multi Function Mode Enabled", vdev->ndev->name);
4314                 break;
4315                 case VXGE_HW_FUNCTION_MODE_SRIOV:
4316                         vxge_debug_init(VXGE_TRACE,
4317                         "%s: Single Root IOV Mode Enabled", vdev->ndev->name);
4318                 break;
4319                 case VXGE_HW_FUNCTION_MODE_MRIOV:
4320                         vxge_debug_init(VXGE_TRACE,
4321                         "%s: Multi Root IOV Mode Enabled", vdev->ndev->name);
4322                 break;
4323                 }
4324         }
4325
4326         vxge_print_parm(vdev, vpath_mask);
4327
4328         /* Store the fw version for ethttool option */
4329         strcpy(vdev->fw_version, ll_config->device_hw_info.fw_version.version);
4330         memcpy(vdev->ndev->dev_addr, (u8 *)vdev->vpaths[0].macaddr, ETH_ALEN);
4331         memcpy(vdev->ndev->perm_addr, vdev->ndev->dev_addr, ETH_ALEN);
4332
4333         /* Copy the station mac address to the list */
4334         for (i = 0; i < vdev->no_of_vpath; i++) {
4335                 entry = (struct vxge_mac_addrs *)
4336                                 kzalloc(sizeof(struct vxge_mac_addrs),
4337                                         GFP_KERNEL);
4338                 if (NULL == entry) {
4339                         vxge_debug_init(VXGE_ERR,
4340                                 "%s: mac_addr_list : memory allocation failed",
4341                                 vdev->ndev->name);
4342                         ret = -EPERM;
4343                         goto _exit5;
4344                 }
4345                 macaddr = (u8 *)&entry->macaddr;
4346                 memcpy(macaddr, vdev->ndev->dev_addr, ETH_ALEN);
4347                 list_add(&entry->item, &vdev->vpaths[i].mac_addr_list);
4348                 vdev->vpaths[i].mac_addr_cnt = 1;
4349         }
4350
4351         kfree(device_config);
4352
4353         /*
4354          * INTA is shared in multi-function mode. This is unlike the INTA
4355          * implementation in MR mode, where each VH has its own INTA message.
4356          * - INTA is masked (disabled) as long as at least one function sets
4357          * its TITAN_MASK_ALL_INT.ALARM bit.
4358          * - INTA is unmasked (enabled) when all enabled functions have cleared
4359          * their own TITAN_MASK_ALL_INT.ALARM bit.
4360          * The TITAN_MASK_ALL_INT ALARM & TRAFFIC bits are cleared on power up.
4361          * Though this driver leaves the top level interrupts unmasked while
4362          * leaving the required module interrupt bits masked on exit, there
4363          * could be a rougue driver around that does not follow this procedure
4364          * resulting in a failure to generate interrupts. The following code is
4365          * present to prevent such a failure.
4366          */
4367
4368         if (ll_config->device_hw_info.function_mode ==
4369                 VXGE_HW_FUNCTION_MODE_MULTI_FUNCTION)
4370                 if (vdev->config.intr_type == INTA)
4371                         vxge_hw_device_unmask_all(hldev);
4372
4373         vxge_debug_entryexit(VXGE_TRACE, "%s: %s:%d  Exiting...",
4374                 vdev->ndev->name, __func__, __LINE__);
4375
4376         vxge_hw_device_debug_set(hldev, VXGE_ERR, VXGE_COMPONENT_LL);
4377         VXGE_COPY_DEBUG_INFO_TO_LL(vdev, vxge_hw_device_error_level_get(hldev),
4378                 vxge_hw_device_trace_level_get(hldev));
4379
4380         kfree(ll_config);
4381         return 0;
4382
4383 _exit5:
4384         for (i = 0; i < vdev->no_of_vpath; i++)
4385                 vxge_free_mac_add_list(&vdev->vpaths[i]);
4386
4387         vxge_device_unregister(hldev);
4388 _exit4:
4389         pci_disable_sriov(pdev);
4390         vxge_hw_device_terminate(hldev);
4391 _exit3:
4392         iounmap(attr.bar0);
4393 _exit2:
4394         pci_release_regions(pdev);
4395 _exit1:
4396         pci_disable_device(pdev);
4397 _exit0:
4398         kfree(ll_config);
4399         kfree(device_config);
4400         driver_config->config_dev_cnt--;
4401         pci_set_drvdata(pdev, NULL);
4402         return ret;
4403 }
4404
4405 /**
4406  * vxge_rem_nic - Free the PCI device
4407  * @pdev: structure containing the PCI related information of the device.
4408  * Description: This function is called by the Pci subsystem to release a
4409  * PCI device and free up all resource held up by the device.
4410  */
4411 static void __devexit
4412 vxge_remove(struct pci_dev *pdev)
4413 {
4414         struct __vxge_hw_device  *hldev;
4415         struct vxgedev *vdev = NULL;
4416         struct net_device *dev;
4417         int i = 0;
4418 #if ((VXGE_DEBUG_INIT & VXGE_DEBUG_MASK) || \
4419         (VXGE_DEBUG_ENTRYEXIT & VXGE_DEBUG_MASK))
4420         u32 level_trace;
4421 #endif
4422
4423         hldev = (struct __vxge_hw_device  *) pci_get_drvdata(pdev);
4424
4425         if (hldev == NULL)
4426                 return;
4427         dev = hldev->ndev;
4428         vdev = netdev_priv(dev);
4429
4430 #if ((VXGE_DEBUG_INIT & VXGE_DEBUG_MASK) || \
4431         (VXGE_DEBUG_ENTRYEXIT & VXGE_DEBUG_MASK))
4432         level_trace = vdev->level_trace;
4433 #endif
4434         vxge_debug_entryexit(level_trace,
4435                 "%s:%d", __func__, __LINE__);
4436
4437         vxge_debug_init(level_trace,
4438                 "%s : removing PCI device...", __func__);
4439         vxge_device_unregister(hldev);
4440
4441         for (i = 0; i < vdev->no_of_vpath; i++) {
4442                 vxge_free_mac_add_list(&vdev->vpaths[i]);
4443                 vdev->vpaths[i].mcast_addr_cnt = 0;
4444                 vdev->vpaths[i].mac_addr_cnt = 0;
4445         }
4446
4447         kfree(vdev->vpaths);
4448
4449         iounmap(vdev->bar0);
4450
4451         pci_disable_sriov(pdev);
4452
4453         /* we are safe to free it now */
4454         free_netdev(dev);
4455
4456         vxge_debug_init(level_trace,
4457                 "%s:%d  Device unregistered", __func__, __LINE__);
4458
4459         vxge_hw_device_terminate(hldev);
4460
4461         pci_disable_device(pdev);
4462         pci_release_regions(pdev);
4463         pci_set_drvdata(pdev, NULL);
4464         vxge_debug_entryexit(level_trace,
4465                 "%s:%d  Exiting...", __func__, __LINE__);
4466 }
4467
4468 static struct pci_error_handlers vxge_err_handler = {
4469         .error_detected = vxge_io_error_detected,
4470         .slot_reset = vxge_io_slot_reset,
4471         .resume = vxge_io_resume,
4472 };
4473
4474 static struct pci_driver vxge_driver = {
4475         .name = VXGE_DRIVER_NAME,
4476         .id_table = vxge_id_table,
4477         .probe = vxge_probe,
4478         .remove = __devexit_p(vxge_remove),
4479 #ifdef CONFIG_PM
4480         .suspend = vxge_pm_suspend,
4481         .resume = vxge_pm_resume,
4482 #endif
4483         .err_handler = &vxge_err_handler,
4484 };
4485
4486 static int __init
4487 vxge_starter(void)
4488 {
4489         int ret = 0;
4490         char version[32];
4491         snprintf(version, 32, "%s", DRV_VERSION);
4492
4493         printk(KERN_INFO "%s: Copyright(c) 2002-2009 Neterion Inc\n",
4494                 VXGE_DRIVER_NAME);
4495         printk(KERN_INFO "%s: Driver version: %s\n",
4496                         VXGE_DRIVER_NAME, version);
4497
4498         verify_bandwidth();
4499
4500         driver_config = kzalloc(sizeof(struct vxge_drv_config), GFP_KERNEL);
4501         if (!driver_config)
4502                 return -ENOMEM;
4503
4504         ret = pci_register_driver(&vxge_driver);
4505
4506         if (driver_config->config_dev_cnt &&
4507            (driver_config->config_dev_cnt != driver_config->total_dev_cnt))
4508                 vxge_debug_init(VXGE_ERR,
4509                         "%s: Configured %d of %d devices",
4510                         VXGE_DRIVER_NAME, driver_config->config_dev_cnt,
4511                         driver_config->total_dev_cnt);
4512
4513         if (ret)
4514                 kfree(driver_config);
4515
4516         return ret;
4517 }
4518
4519 static void __exit
4520 vxge_closer(void)
4521 {
4522         pci_unregister_driver(&vxge_driver);
4523         kfree(driver_config);
4524 }
4525 module_init(vxge_starter);
4526 module_exit(vxge_closer);