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