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