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