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