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