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