drivers/net: return operator cleanup
[cascardo/linux.git] / drivers / net / bnx2x / bnx2x_cmn.h
1 /* bnx2x_cmn.h: Broadcom Everest network driver.
2  *
3  * Copyright (c) 2007-2010 Broadcom Corporation
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation.
8  *
9  * Maintained by: Eilon Greenstein <eilong@broadcom.com>
10  * Written by: Eliezer Tamir
11  * Based on code from Michael Chan's bnx2 driver
12  * UDP CSUM errata workaround by Arik Gendelman
13  * Slowpath and fastpath rework by Vladislav Zolotarov
14  * Statistics and Link management by Yitchak Gertner
15  *
16  */
17 #ifndef BNX2X_CMN_H
18 #define BNX2X_CMN_H
19
20 #include <linux/types.h>
21 #include <linux/netdevice.h>
22
23
24 #include "bnx2x.h"
25
26
27 /*********************** Interfaces ****************************
28  *  Functions that need to be implemented by each driver version
29  */
30
31 /**
32  * Initialize link parameters structure variables.
33  *
34  * @param bp
35  * @param load_mode
36  *
37  * @return u8
38  */
39 u8 bnx2x_initial_phy_init(struct bnx2x *bp, int load_mode);
40
41 /**
42  * Configure hw according to link parameters structure.
43  *
44  * @param bp
45  */
46 void bnx2x_link_set(struct bnx2x *bp);
47
48 /**
49  * Query link status
50  *
51  * @param bp
52  * @param is_serdes
53  *
54  * @return 0 - link is UP
55  */
56 u8 bnx2x_link_test(struct bnx2x *bp, u8 is_serdes);
57
58 /**
59  * Handles link status change
60  *
61  * @param bp
62  */
63 void bnx2x__link_status_update(struct bnx2x *bp);
64
65 /**
66  * MSI-X slowpath interrupt handler
67  *
68  * @param irq
69  * @param dev_instance
70  *
71  * @return irqreturn_t
72  */
73 irqreturn_t bnx2x_msix_sp_int(int irq, void *dev_instance);
74
75 /**
76  * non MSI-X interrupt handler
77  *
78  * @param irq
79  * @param dev_instance
80  *
81  * @return irqreturn_t
82  */
83 irqreturn_t bnx2x_interrupt(int irq, void *dev_instance);
84 #ifdef BCM_CNIC
85
86 /**
87  * Send command to cnic driver
88  *
89  * @param bp
90  * @param cmd
91  */
92 int bnx2x_cnic_notify(struct bnx2x *bp, int cmd);
93
94 /**
95  * Provides cnic information for proper interrupt handling
96  *
97  * @param bp
98  */
99 void bnx2x_setup_cnic_irq_info(struct bnx2x *bp);
100 #endif
101
102 /**
103  * Enable HW interrupts.
104  *
105  * @param bp
106  */
107 void bnx2x_int_enable(struct bnx2x *bp);
108
109 /**
110  * Disable interrupts. This function ensures that there are no
111  * ISRs or SP DPCs (sp_task) are running after it returns.
112  *
113  * @param bp
114  * @param disable_hw if true, disable HW interrupts.
115  */
116 void bnx2x_int_disable_sync(struct bnx2x *bp, int disable_hw);
117
118 /**
119  * Loads device firmware
120  *
121  * @param bp
122  *
123  * @return int
124  */
125 int bnx2x_init_firmware(struct bnx2x *bp);
126
127 /**
128  * Init HW blocks according to current initialization stage:
129  * COMMON, PORT or FUNCTION.
130  *
131  * @param bp
132  * @param load_code: COMMON, PORT or FUNCTION
133  *
134  * @return int
135  */
136 int bnx2x_init_hw(struct bnx2x *bp, u32 load_code);
137
138 /**
139  * Init driver internals:
140  *  - rings
141  *  - status blocks
142  *  - etc.
143  *
144  * @param bp
145  * @param load_code COMMON, PORT or FUNCTION
146  */
147 void bnx2x_nic_init(struct bnx2x *bp, u32 load_code);
148
149 /**
150  * Allocate driver's memory.
151  *
152  * @param bp
153  *
154  * @return int
155  */
156 int bnx2x_alloc_mem(struct bnx2x *bp);
157
158 /**
159  * Release driver's memory.
160  *
161  * @param bp
162  */
163 void bnx2x_free_mem(struct bnx2x *bp);
164
165 /**
166  * Bring up a leading (the first) eth Client.
167  *
168  * @param bp
169  *
170  * @return int
171  */
172 int bnx2x_setup_leading(struct bnx2x *bp);
173
174 /**
175  * Setup non-leading eth Client.
176  *
177  * @param bp
178  * @param fp
179  *
180  * @return int
181  */
182 int bnx2x_setup_multi(struct bnx2x *bp, int index);
183
184 /**
185  * Set number of quueus according to mode and number of available
186  * msi-x vectors
187  *
188  * @param bp
189  *
190  */
191 void bnx2x_set_num_queues_msix(struct bnx2x *bp);
192
193 /**
194  * Cleanup chip internals:
195  * - Cleanup MAC configuration.
196  * - Close clients.
197  * - etc.
198  *
199  * @param bp
200  * @param unload_mode
201  */
202 void bnx2x_chip_cleanup(struct bnx2x *bp, int unload_mode);
203
204 /**
205  * Acquire HW lock.
206  *
207  * @param bp
208  * @param resource Resource bit which was locked
209  *
210  * @return int
211  */
212 int bnx2x_acquire_hw_lock(struct bnx2x *bp, u32 resource);
213
214 /**
215  * Release HW lock.
216  *
217  * @param bp driver handle
218  * @param resource Resource bit which was locked
219  *
220  * @return int
221  */
222 int bnx2x_release_hw_lock(struct bnx2x *bp, u32 resource);
223
224 /**
225  * Configure eth MAC address in the HW according to the value in
226  * netdev->dev_addr for 57711
227  *
228  * @param bp driver handle
229  * @param set
230  */
231 void bnx2x_set_eth_mac_addr_e1h(struct bnx2x *bp, int set);
232
233 /**
234  * Configure eth MAC address in the HW according to the value in
235  * netdev->dev_addr for 57710
236  *
237  * @param bp driver handle
238  * @param set
239  */
240 void bnx2x_set_eth_mac_addr_e1(struct bnx2x *bp, int set);
241
242 #ifdef BCM_CNIC
243 /**
244  * Set iSCSI MAC(s) at the next enties in the CAM after the ETH
245  * MAC(s). The function will wait until the ramrod completion
246  * returns.
247  *
248  * @param bp driver handle
249  * @param set set or clear the CAM entry
250  *
251  * @return 0 if cussess, -ENODEV if ramrod doesn't return.
252  */
253 int bnx2x_set_iscsi_eth_mac_addr(struct bnx2x *bp, int set);
254 #endif
255
256 /**
257  * Initialize status block in FW and HW
258  *
259  * @param bp driver handle
260  * @param sb host_status_block
261  * @param dma_addr_t mapping
262  * @param int sb_id
263  */
264 void bnx2x_init_sb(struct bnx2x *bp, struct host_status_block *sb,
265                           dma_addr_t mapping, int sb_id);
266
267 /**
268  * Reconfigure FW/HW according to dev->flags rx mode
269  *
270  * @param dev net_device
271  *
272  */
273 void bnx2x_set_rx_mode(struct net_device *dev);
274
275 /**
276  * Configure MAC filtering rules in a FW.
277  *
278  * @param bp driver handle
279  */
280 void bnx2x_set_storm_rx_mode(struct bnx2x *bp);
281
282 /* Parity errors related */
283 void bnx2x_inc_load_cnt(struct bnx2x *bp);
284 u32 bnx2x_dec_load_cnt(struct bnx2x *bp);
285 bool bnx2x_chk_parity_attn(struct bnx2x *bp);
286 bool bnx2x_reset_is_done(struct bnx2x *bp);
287 void bnx2x_disable_close_the_gate(struct bnx2x *bp);
288
289 /**
290  * Perform statistics handling according to event
291  *
292  * @param bp driver handle
293  * @param even tbnx2x_stats_event
294  */
295 void bnx2x_stats_handle(struct bnx2x *bp, enum bnx2x_stats_event event);
296
297 /**
298  * Configures FW with client paramteres (like HW VLAN removal)
299  * for each active client.
300  *
301  * @param bp
302  */
303 void bnx2x_set_client_config(struct bnx2x *bp);
304
305 /**
306  * Handle sp events
307  *
308  * @param fp fastpath handle for the event
309  * @param rr_cqe eth_rx_cqe
310  */
311 void bnx2x_sp_event(struct bnx2x_fastpath *fp,  union eth_rx_cqe *rr_cqe);
312
313
314 static inline void bnx2x_update_fpsb_idx(struct bnx2x_fastpath *fp)
315 {
316         struct host_status_block *fpsb = fp->status_blk;
317
318         barrier(); /* status block is written to by the chip */
319         fp->fp_c_idx = fpsb->c_status_block.status_block_index;
320         fp->fp_u_idx = fpsb->u_status_block.status_block_index;
321 }
322
323 static inline void bnx2x_update_rx_prod(struct bnx2x *bp,
324                                         struct bnx2x_fastpath *fp,
325                                         u16 bd_prod, u16 rx_comp_prod,
326                                         u16 rx_sge_prod)
327 {
328         struct ustorm_eth_rx_producers rx_prods = {0};
329         int i;
330
331         /* Update producers */
332         rx_prods.bd_prod = bd_prod;
333         rx_prods.cqe_prod = rx_comp_prod;
334         rx_prods.sge_prod = rx_sge_prod;
335
336         /*
337          * Make sure that the BD and SGE data is updated before updating the
338          * producers since FW might read the BD/SGE right after the producer
339          * is updated.
340          * This is only applicable for weak-ordered memory model archs such
341          * as IA-64. The following barrier is also mandatory since FW will
342          * assumes BDs must have buffers.
343          */
344         wmb();
345
346         for (i = 0; i < sizeof(struct ustorm_eth_rx_producers)/4; i++)
347                 REG_WR(bp, BAR_USTRORM_INTMEM +
348                        USTORM_RX_PRODS_OFFSET(BP_PORT(bp), fp->cl_id) + i*4,
349                        ((u32 *)&rx_prods)[i]);
350
351         mmiowb(); /* keep prod updates ordered */
352
353         DP(NETIF_MSG_RX_STATUS,
354            "queue[%d]:  wrote  bd_prod %u  cqe_prod %u  sge_prod %u\n",
355            fp->index, bd_prod, rx_comp_prod, rx_sge_prod);
356 }
357
358
359
360 static inline void bnx2x_ack_sb(struct bnx2x *bp, u8 sb_id,
361                                 u8 storm, u16 index, u8 op, u8 update)
362 {
363         u32 hc_addr = (HC_REG_COMMAND_REG + BP_PORT(bp)*32 +
364                        COMMAND_REG_INT_ACK);
365         struct igu_ack_register igu_ack;
366
367         igu_ack.status_block_index = index;
368         igu_ack.sb_id_and_flags =
369                         ((sb_id << IGU_ACK_REGISTER_STATUS_BLOCK_ID_SHIFT) |
370                          (storm << IGU_ACK_REGISTER_STORM_ID_SHIFT) |
371                          (update << IGU_ACK_REGISTER_UPDATE_INDEX_SHIFT) |
372                          (op << IGU_ACK_REGISTER_INTERRUPT_MODE_SHIFT));
373
374         DP(BNX2X_MSG_OFF, "write 0x%08x to HC addr 0x%x\n",
375            (*(u32 *)&igu_ack), hc_addr);
376         REG_WR(bp, hc_addr, (*(u32 *)&igu_ack));
377
378         /* Make sure that ACK is written */
379         mmiowb();
380         barrier();
381 }
382 static inline u16 bnx2x_ack_int(struct bnx2x *bp)
383 {
384         u32 hc_addr = (HC_REG_COMMAND_REG + BP_PORT(bp)*32 +
385                        COMMAND_REG_SIMD_MASK);
386         u32 result = REG_RD(bp, hc_addr);
387
388         DP(BNX2X_MSG_OFF, "read 0x%08x from HC addr 0x%x\n",
389            result, hc_addr);
390
391         return result;
392 }
393
394 /*
395  * fast path service functions
396  */
397
398 static inline int bnx2x_has_tx_work_unload(struct bnx2x_fastpath *fp)
399 {
400         /* Tell compiler that consumer and producer can change */
401         barrier();
402         return fp->tx_pkt_prod != fp->tx_pkt_cons;
403 }
404
405 static inline u16 bnx2x_tx_avail(struct bnx2x_fastpath *fp)
406 {
407         s16 used;
408         u16 prod;
409         u16 cons;
410
411         prod = fp->tx_bd_prod;
412         cons = fp->tx_bd_cons;
413
414         /* NUM_TX_RINGS = number of "next-page" entries
415            It will be used as a threshold */
416         used = SUB_S16(prod, cons) + (s16)NUM_TX_RINGS;
417
418 #ifdef BNX2X_STOP_ON_ERROR
419         WARN_ON(used < 0);
420         WARN_ON(used > fp->bp->tx_ring_size);
421         WARN_ON((fp->bp->tx_ring_size - used) > MAX_TX_AVAIL);
422 #endif
423
424         return (s16)(fp->bp->tx_ring_size) - used;
425 }
426
427 static inline int bnx2x_has_tx_work(struct bnx2x_fastpath *fp)
428 {
429         u16 hw_cons;
430
431         /* Tell compiler that status block fields can change */
432         barrier();
433         hw_cons = le16_to_cpu(*fp->tx_cons_sb);
434         return hw_cons != fp->tx_pkt_cons;
435 }
436
437 static inline void bnx2x_free_rx_sge(struct bnx2x *bp,
438                                      struct bnx2x_fastpath *fp, u16 index)
439 {
440         struct sw_rx_page *sw_buf = &fp->rx_page_ring[index];
441         struct page *page = sw_buf->page;
442         struct eth_rx_sge *sge = &fp->rx_sge_ring[index];
443
444         /* Skip "next page" elements */
445         if (!page)
446                 return;
447
448         dma_unmap_page(&bp->pdev->dev, dma_unmap_addr(sw_buf, mapping),
449                        SGE_PAGE_SIZE*PAGES_PER_SGE, PCI_DMA_FROMDEVICE);
450         __free_pages(page, PAGES_PER_SGE_SHIFT);
451
452         sw_buf->page = NULL;
453         sge->addr_hi = 0;
454         sge->addr_lo = 0;
455 }
456
457 static inline void bnx2x_free_rx_sge_range(struct bnx2x *bp,
458                                            struct bnx2x_fastpath *fp, int last)
459 {
460         int i;
461
462         for (i = 0; i < last; i++)
463                 bnx2x_free_rx_sge(bp, fp, i);
464 }
465
466 static inline int bnx2x_alloc_rx_sge(struct bnx2x *bp,
467                                      struct bnx2x_fastpath *fp, u16 index)
468 {
469         struct page *page = alloc_pages(GFP_ATOMIC, PAGES_PER_SGE_SHIFT);
470         struct sw_rx_page *sw_buf = &fp->rx_page_ring[index];
471         struct eth_rx_sge *sge = &fp->rx_sge_ring[index];
472         dma_addr_t mapping;
473
474         if (unlikely(page == NULL))
475                 return -ENOMEM;
476
477         mapping = dma_map_page(&bp->pdev->dev, page, 0,
478                                SGE_PAGE_SIZE*PAGES_PER_SGE, DMA_FROM_DEVICE);
479         if (unlikely(dma_mapping_error(&bp->pdev->dev, mapping))) {
480                 __free_pages(page, PAGES_PER_SGE_SHIFT);
481                 return -ENOMEM;
482         }
483
484         sw_buf->page = page;
485         dma_unmap_addr_set(sw_buf, mapping, mapping);
486
487         sge->addr_hi = cpu_to_le32(U64_HI(mapping));
488         sge->addr_lo = cpu_to_le32(U64_LO(mapping));
489
490         return 0;
491 }
492 static inline int bnx2x_alloc_rx_skb(struct bnx2x *bp,
493                                      struct bnx2x_fastpath *fp, u16 index)
494 {
495         struct sk_buff *skb;
496         struct sw_rx_bd *rx_buf = &fp->rx_buf_ring[index];
497         struct eth_rx_bd *rx_bd = &fp->rx_desc_ring[index];
498         dma_addr_t mapping;
499
500         skb = netdev_alloc_skb(bp->dev, bp->rx_buf_size);
501         if (unlikely(skb == NULL))
502                 return -ENOMEM;
503
504         mapping = dma_map_single(&bp->pdev->dev, skb->data, bp->rx_buf_size,
505                                  DMA_FROM_DEVICE);
506         if (unlikely(dma_mapping_error(&bp->pdev->dev, mapping))) {
507                 dev_kfree_skb(skb);
508                 return -ENOMEM;
509         }
510
511         rx_buf->skb = skb;
512         dma_unmap_addr_set(rx_buf, mapping, mapping);
513
514         rx_bd->addr_hi = cpu_to_le32(U64_HI(mapping));
515         rx_bd->addr_lo = cpu_to_le32(U64_LO(mapping));
516
517         return 0;
518 }
519
520 /* note that we are not allocating a new skb,
521  * we are just moving one from cons to prod
522  * we are not creating a new mapping,
523  * so there is no need to check for dma_mapping_error().
524  */
525 static inline void bnx2x_reuse_rx_skb(struct bnx2x_fastpath *fp,
526                                struct sk_buff *skb, u16 cons, u16 prod)
527 {
528         struct bnx2x *bp = fp->bp;
529         struct sw_rx_bd *cons_rx_buf = &fp->rx_buf_ring[cons];
530         struct sw_rx_bd *prod_rx_buf = &fp->rx_buf_ring[prod];
531         struct eth_rx_bd *cons_bd = &fp->rx_desc_ring[cons];
532         struct eth_rx_bd *prod_bd = &fp->rx_desc_ring[prod];
533
534         dma_sync_single_for_device(&bp->pdev->dev,
535                                    dma_unmap_addr(cons_rx_buf, mapping),
536                                    RX_COPY_THRESH, DMA_FROM_DEVICE);
537
538         prod_rx_buf->skb = cons_rx_buf->skb;
539         dma_unmap_addr_set(prod_rx_buf, mapping,
540                            dma_unmap_addr(cons_rx_buf, mapping));
541         *prod_bd = *cons_bd;
542 }
543
544 static inline void bnx2x_clear_sge_mask_next_elems(struct bnx2x_fastpath *fp)
545 {
546         int i, j;
547
548         for (i = 1; i <= NUM_RX_SGE_PAGES; i++) {
549                 int idx = RX_SGE_CNT * i - 1;
550
551                 for (j = 0; j < 2; j++) {
552                         SGE_MASK_CLEAR_BIT(fp, idx);
553                         idx--;
554                 }
555         }
556 }
557
558 static inline void bnx2x_init_sge_ring_bit_mask(struct bnx2x_fastpath *fp)
559 {
560         /* Set the mask to all 1-s: it's faster to compare to 0 than to 0xf-s */
561         memset(fp->sge_mask, 0xff,
562                (NUM_RX_SGE >> RX_SGE_MASK_ELEM_SHIFT)*sizeof(u64));
563
564         /* Clear the two last indices in the page to 1:
565            these are the indices that correspond to the "next" element,
566            hence will never be indicated and should be removed from
567            the calculations. */
568         bnx2x_clear_sge_mask_next_elems(fp);
569 }
570 static inline void bnx2x_free_tpa_pool(struct bnx2x *bp,
571                                        struct bnx2x_fastpath *fp, int last)
572 {
573         int i;
574
575         for (i = 0; i < last; i++) {
576                 struct sw_rx_bd *rx_buf = &(fp->tpa_pool[i]);
577                 struct sk_buff *skb = rx_buf->skb;
578
579                 if (skb == NULL) {
580                         DP(NETIF_MSG_IFDOWN, "tpa bin %d empty on free\n", i);
581                         continue;
582                 }
583
584                 if (fp->tpa_state[i] == BNX2X_TPA_START)
585                         dma_unmap_single(&bp->pdev->dev,
586                                          dma_unmap_addr(rx_buf, mapping),
587                                          bp->rx_buf_size, DMA_FROM_DEVICE);
588
589                 dev_kfree_skb(skb);
590                 rx_buf->skb = NULL;
591         }
592 }
593
594
595 static inline void bnx2x_init_tx_ring(struct bnx2x *bp)
596 {
597         int i, j;
598
599         for_each_queue(bp, j) {
600                 struct bnx2x_fastpath *fp = &bp->fp[j];
601
602                 for (i = 1; i <= NUM_TX_RINGS; i++) {
603                         struct eth_tx_next_bd *tx_next_bd =
604                                 &fp->tx_desc_ring[TX_DESC_CNT * i - 1].next_bd;
605
606                         tx_next_bd->addr_hi =
607                                 cpu_to_le32(U64_HI(fp->tx_desc_mapping +
608                                             BCM_PAGE_SIZE*(i % NUM_TX_RINGS)));
609                         tx_next_bd->addr_lo =
610                                 cpu_to_le32(U64_LO(fp->tx_desc_mapping +
611                                             BCM_PAGE_SIZE*(i % NUM_TX_RINGS)));
612                 }
613
614                 fp->tx_db.data.header.header = DOORBELL_HDR_DB_TYPE;
615                 fp->tx_db.data.zero_fill1 = 0;
616                 fp->tx_db.data.prod = 0;
617
618                 fp->tx_pkt_prod = 0;
619                 fp->tx_pkt_cons = 0;
620                 fp->tx_bd_prod = 0;
621                 fp->tx_bd_cons = 0;
622                 fp->tx_cons_sb = BNX2X_TX_SB_INDEX;
623                 fp->tx_pkt = 0;
624         }
625 }
626 static inline int bnx2x_has_rx_work(struct bnx2x_fastpath *fp)
627 {
628         u16 rx_cons_sb;
629
630         /* Tell compiler that status block fields can change */
631         barrier();
632         rx_cons_sb = le16_to_cpu(*fp->rx_cons_sb);
633         if ((rx_cons_sb & MAX_RCQ_DESC_CNT) == MAX_RCQ_DESC_CNT)
634                 rx_cons_sb++;
635         return fp->rx_comp_cons != rx_cons_sb;
636 }
637
638 /* HW Lock for shared dual port PHYs */
639 void bnx2x_acquire_phy_lock(struct bnx2x *bp);
640 void bnx2x_release_phy_lock(struct bnx2x *bp);
641
642 void bnx2x_link_report(struct bnx2x *bp);
643 int bnx2x_rx_int(struct bnx2x_fastpath *fp, int budget);
644 int bnx2x_tx_int(struct bnx2x_fastpath *fp);
645 void bnx2x_init_rx_rings(struct bnx2x *bp);
646 netdev_tx_t bnx2x_start_xmit(struct sk_buff *skb, struct net_device *dev);
647
648 int bnx2x_change_mac_addr(struct net_device *dev, void *p);
649 void bnx2x_tx_timeout(struct net_device *dev);
650 void bnx2x_vlan_rx_register(struct net_device *dev, struct vlan_group *vlgrp);
651 void bnx2x_netif_start(struct bnx2x *bp);
652 void bnx2x_netif_stop(struct bnx2x *bp, int disable_hw);
653 void bnx2x_free_irq(struct bnx2x *bp, bool disable_only);
654 int bnx2x_suspend(struct pci_dev *pdev, pm_message_t state);
655 int bnx2x_resume(struct pci_dev *pdev);
656 void bnx2x_free_skbs(struct bnx2x *bp);
657 int bnx2x_change_mtu(struct net_device *dev, int new_mtu);
658 int bnx2x_nic_unload(struct bnx2x *bp, int unload_mode);
659 int bnx2x_nic_load(struct bnx2x *bp, int load_mode);
660 int bnx2x_set_power_state(struct bnx2x *bp, pci_power_t state);
661
662 #endif /* BNX2X_CMN_H */