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