blob: cb8f2a040a18c85c1c66579169fe2e94c271d8a2 [file] [log] [blame]
Dmitry Kravkov9f6c9252010-07-27 12:34:34 +00001/* 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
Dmitry Kravkovd6214d72010-10-06 03:32:10 +000026extern int num_queues;
Dmitry Kravkov9f6c9252010-07-27 12:34:34 +000027
28/*********************** Interfaces ****************************
29 * Functions that need to be implemented by each driver version
30 */
31
32/**
33 * Initialize link parameters structure variables.
34 *
35 * @param bp
36 * @param load_mode
37 *
38 * @return u8
39 */
40u8 bnx2x_initial_phy_init(struct bnx2x *bp, int load_mode);
41
42/**
43 * Configure hw according to link parameters structure.
44 *
45 * @param bp
46 */
47void bnx2x_link_set(struct bnx2x *bp);
48
49/**
50 * Query link status
51 *
52 * @param bp
Yaniv Rosnera22f0782010-09-07 11:41:20 +000053 * @param is_serdes
Dmitry Kravkov9f6c9252010-07-27 12:34:34 +000054 *
55 * @return 0 - link is UP
56 */
Yaniv Rosnera22f0782010-09-07 11:41:20 +000057u8 bnx2x_link_test(struct bnx2x *bp, u8 is_serdes);
Dmitry Kravkov9f6c9252010-07-27 12:34:34 +000058
59/**
60 * Handles link status change
61 *
62 * @param bp
63 */
64void bnx2x__link_status_update(struct bnx2x *bp);
65
66/**
Dmitry Kravkovf85582f2010-10-06 03:34:21 +000067 * Report link status to upper layer
68 *
69 * @param bp
70 *
71 * @return int
72 */
73void bnx2x_link_report(struct bnx2x *bp);
74
75/**
Dmitry Kravkov0793f83f2010-12-01 12:39:28 -080076 * calculates MF speed according to current linespeed and MF
77 * configuration
78 *
79 * @param bp
80 *
81 * @return u16
82 */
83u16 bnx2x_get_mf_speed(struct bnx2x *bp);
84
85/**
Dmitry Kravkov9f6c9252010-07-27 12:34:34 +000086 * MSI-X slowpath interrupt handler
87 *
88 * @param irq
89 * @param dev_instance
90 *
91 * @return irqreturn_t
92 */
93irqreturn_t bnx2x_msix_sp_int(int irq, void *dev_instance);
94
95/**
96 * non MSI-X interrupt handler
97 *
98 * @param irq
99 * @param dev_instance
100 *
101 * @return irqreturn_t
102 */
103irqreturn_t bnx2x_interrupt(int irq, void *dev_instance);
104#ifdef BCM_CNIC
105
106/**
107 * Send command to cnic driver
108 *
109 * @param bp
110 * @param cmd
111 */
112int bnx2x_cnic_notify(struct bnx2x *bp, int cmd);
113
114/**
115 * Provides cnic information for proper interrupt handling
116 *
117 * @param bp
118 */
119void bnx2x_setup_cnic_irq_info(struct bnx2x *bp);
120#endif
121
122/**
123 * Enable HW interrupts.
124 *
125 * @param bp
126 */
127void bnx2x_int_enable(struct bnx2x *bp);
128
129/**
130 * Disable interrupts. This function ensures that there are no
131 * ISRs or SP DPCs (sp_task) are running after it returns.
132 *
133 * @param bp
134 * @param disable_hw if true, disable HW interrupts.
135 */
136void bnx2x_int_disable_sync(struct bnx2x *bp, int disable_hw);
137
138/**
Dmitry Kravkov6891dd22010-08-03 21:49:40 +0000139 * Loads device firmware
140 *
141 * @param bp
142 *
143 * @return int
144 */
145int bnx2x_init_firmware(struct bnx2x *bp);
146
147/**
Dmitry Kravkov9f6c9252010-07-27 12:34:34 +0000148 * Init HW blocks according to current initialization stage:
149 * COMMON, PORT or FUNCTION.
150 *
151 * @param bp
152 * @param load_code: COMMON, PORT or FUNCTION
153 *
154 * @return int
155 */
156int bnx2x_init_hw(struct bnx2x *bp, u32 load_code);
157
158/**
159 * Init driver internals:
160 * - rings
161 * - status blocks
162 * - etc.
163 *
164 * @param bp
165 * @param load_code COMMON, PORT or FUNCTION
166 */
167void bnx2x_nic_init(struct bnx2x *bp, u32 load_code);
168
169/**
170 * Allocate driver's memory.
171 *
172 * @param bp
173 *
174 * @return int
175 */
176int bnx2x_alloc_mem(struct bnx2x *bp);
177
178/**
179 * Release driver's memory.
180 *
181 * @param bp
182 */
183void bnx2x_free_mem(struct bnx2x *bp);
184
185/**
Dmitry Kravkov523224a2010-10-06 03:23:26 +0000186 * Setup eth Client.
Dmitry Kravkov9f6c9252010-07-27 12:34:34 +0000187 *
188 * @param bp
189 * @param fp
Dmitry Kravkov523224a2010-10-06 03:23:26 +0000190 * @param is_leading
Dmitry Kravkov9f6c9252010-07-27 12:34:34 +0000191 *
192 * @return int
193 */
Dmitry Kravkov523224a2010-10-06 03:23:26 +0000194int bnx2x_setup_client(struct bnx2x *bp, struct bnx2x_fastpath *fp,
195 int is_leading);
Dmitry Kravkov9f6c9252010-07-27 12:34:34 +0000196
197/**
Dmitry Kravkovd6214d72010-10-06 03:32:10 +0000198 * Set number of queues according to mode
Dmitry Kravkov9f6c9252010-07-27 12:34:34 +0000199 *
200 * @param bp
201 *
202 */
Dmitry Kravkovd6214d72010-10-06 03:32:10 +0000203void bnx2x_set_num_queues(struct bnx2x *bp);
Dmitry Kravkov9f6c9252010-07-27 12:34:34 +0000204
205/**
206 * Cleanup chip internals:
207 * - Cleanup MAC configuration.
208 * - Close clients.
209 * - etc.
210 *
211 * @param bp
212 * @param unload_mode
213 */
214void bnx2x_chip_cleanup(struct bnx2x *bp, int unload_mode);
215
216/**
217 * Acquire HW lock.
218 *
219 * @param bp
220 * @param resource Resource bit which was locked
221 *
222 * @return int
223 */
224int bnx2x_acquire_hw_lock(struct bnx2x *bp, u32 resource);
225
226/**
227 * Release HW lock.
228 *
229 * @param bp driver handle
230 * @param resource Resource bit which was locked
231 *
232 * @return int
233 */
234int bnx2x_release_hw_lock(struct bnx2x *bp, u32 resource);
235
236/**
237 * Configure eth MAC address in the HW according to the value in
Dmitry Kravkovf85582f2010-10-06 03:34:21 +0000238 * netdev->dev_addr.
Dmitry Kravkov9f6c9252010-07-27 12:34:34 +0000239 *
240 * @param bp driver handle
241 * @param set
242 */
Dmitry Kravkov523224a2010-10-06 03:23:26 +0000243void bnx2x_set_eth_mac(struct bnx2x *bp, int set);
Dmitry Kravkov9f6c9252010-07-27 12:34:34 +0000244
Dmitry Kravkov9f6c9252010-07-27 12:34:34 +0000245/**
Dmitry Kravkovf85582f2010-10-06 03:34:21 +0000246 * Set MAC filtering configurations.
247 *
248 * @remarks called with netif_tx_lock from dev_mcast.c
Dmitry Kravkov9f6c9252010-07-27 12:34:34 +0000249 *
250 * @param dev net_device
Dmitry Kravkov9f6c9252010-07-27 12:34:34 +0000251 */
252void bnx2x_set_rx_mode(struct net_device *dev);
253
254/**
255 * Configure MAC filtering rules in a FW.
256 *
257 * @param bp driver handle
258 */
259void bnx2x_set_storm_rx_mode(struct bnx2x *bp);
260
261/* Parity errors related */
262void bnx2x_inc_load_cnt(struct bnx2x *bp);
263u32 bnx2x_dec_load_cnt(struct bnx2x *bp);
264bool bnx2x_chk_parity_attn(struct bnx2x *bp);
265bool bnx2x_reset_is_done(struct bnx2x *bp);
266void bnx2x_disable_close_the_gate(struct bnx2x *bp);
267
268/**
269 * Perform statistics handling according to event
270 *
271 * @param bp driver handle
Dmitry Kravkovf85582f2010-10-06 03:34:21 +0000272 * @param event bnx2x_stats_event
Dmitry Kravkov9f6c9252010-07-27 12:34:34 +0000273 */
274void bnx2x_stats_handle(struct bnx2x *bp, enum bnx2x_stats_event event);
275
276/**
Dmitry Kravkovf85582f2010-10-06 03:34:21 +0000277 * Handle ramrods completion
Dmitry Kravkov9f6c9252010-07-27 12:34:34 +0000278 *
279 * @param fp fastpath handle for the event
280 * @param rr_cqe eth_rx_cqe
281 */
Dmitry Kravkovf85582f2010-10-06 03:34:21 +0000282void bnx2x_sp_event(struct bnx2x_fastpath *fp, union eth_rx_cqe *rr_cqe);
Dmitry Kravkov9f6c9252010-07-27 12:34:34 +0000283
Dmitry Kravkov523224a2010-10-06 03:23:26 +0000284/**
285 * Init/halt function before/after sending
286 * CLIENT_SETUP/CFC_DEL for the first/last client.
287 *
288 * @param bp
289 *
290 * @return int
291 */
292int bnx2x_func_start(struct bnx2x *bp);
Dmitry Kravkov523224a2010-10-06 03:23:26 +0000293
294/**
295 * Prepare ILT configurations according to current driver
296 * parameters.
297 *
298 * @param bp
299 */
300void bnx2x_ilt_set_info(struct bnx2x *bp);
Dmitry Kravkov9f6c9252010-07-27 12:34:34 +0000301
Dmitry Kravkovd6214d72010-10-06 03:32:10 +0000302/**
Dmitry Kravkovf85582f2010-10-06 03:34:21 +0000303 * Set power state to the requested value. Currently only D0 and
304 * D3hot are supported.
305 *
306 * @param bp
307 * @param state D0 or D3hot
308 *
309 * @return int
310 */
311int bnx2x_set_power_state(struct bnx2x *bp, pci_power_t state);
312
313/* dev_close main block */
314int bnx2x_nic_unload(struct bnx2x *bp, int unload_mode);
315
316/* dev_open main block */
317int bnx2x_nic_load(struct bnx2x *bp, int load_mode);
318
319/* hard_xmit callback */
320netdev_tx_t bnx2x_start_xmit(struct sk_buff *skb, struct net_device *dev);
321
322int bnx2x_change_mac_addr(struct net_device *dev, void *p);
323
324/* NAPI poll Rx part */
325int bnx2x_rx_int(struct bnx2x_fastpath *fp, int budget);
326
327/* NAPI poll Tx part */
328int bnx2x_tx_int(struct bnx2x_fastpath *fp);
329
330/* suspend/resume callbacks */
331int bnx2x_suspend(struct pci_dev *pdev, pm_message_t state);
332int bnx2x_resume(struct pci_dev *pdev);
333
334/* Release IRQ vectors */
335void bnx2x_free_irq(struct bnx2x *bp);
336
337void bnx2x_init_rx_rings(struct bnx2x *bp);
338void bnx2x_free_skbs(struct bnx2x *bp);
339void bnx2x_netif_stop(struct bnx2x *bp, int disable_hw);
340void bnx2x_netif_start(struct bnx2x *bp);
341
342/**
Dmitry Kravkovd6214d72010-10-06 03:32:10 +0000343 * Fill msix_table, request vectors, update num_queues according
344 * to number of available vectors
345 *
346 * @param bp
347 *
348 * @return int
349 */
350int bnx2x_enable_msix(struct bnx2x *bp);
351
352/**
353 * Request msi mode from OS, updated internals accordingly
354 *
355 * @param bp
356 *
357 * @return int
358 */
359int bnx2x_enable_msi(struct bnx2x *bp);
360
361/**
Dmitry Kravkovd6214d72010-10-06 03:32:10 +0000362 * NAPI callback
363 *
364 * @param napi
365 * @param budget
366 *
367 * @return int
368 */
369int bnx2x_poll(struct napi_struct *napi, int budget);
Dmitry Kravkovf85582f2010-10-06 03:34:21 +0000370
371/**
372 * Allocate/release memories outsize main driver structure
373 *
374 * @param bp
375 *
376 * @return int
377 */
378int __devinit bnx2x_alloc_mem_bp(struct bnx2x *bp);
379void bnx2x_free_mem_bp(struct bnx2x *bp);
380
381/**
382 * Change mtu netdev callback
383 *
384 * @param dev
385 * @param new_mtu
386 *
387 * @return int
388 */
389int bnx2x_change_mtu(struct net_device *dev, int new_mtu);
390
391/**
392 * tx timeout netdev callback
393 *
394 * @param dev
395 * @param new_mtu
396 *
397 * @return int
398 */
399void bnx2x_tx_timeout(struct net_device *dev);
400
401#ifdef BCM_VLAN
402/**
403 * vlan rx register netdev callback
404 *
405 * @param dev
406 * @param new_mtu
407 *
408 * @return int
409 */
410void bnx2x_vlan_rx_register(struct net_device *dev,
411 struct vlan_group *vlgrp);
412
413#endif
414
Dmitry Kravkov9f6c9252010-07-27 12:34:34 +0000415static inline void bnx2x_update_fpsb_idx(struct bnx2x_fastpath *fp)
416{
Dmitry Kravkov9f6c9252010-07-27 12:34:34 +0000417 barrier(); /* status block is written to by the chip */
Dmitry Kravkov523224a2010-10-06 03:23:26 +0000418 fp->fp_hc_idx = fp->sb_running_index[SM_RX_ID];
Dmitry Kravkov9f6c9252010-07-27 12:34:34 +0000419}
420
421static inline void bnx2x_update_rx_prod(struct bnx2x *bp,
422 struct bnx2x_fastpath *fp,
423 u16 bd_prod, u16 rx_comp_prod,
424 u16 rx_sge_prod)
425{
426 struct ustorm_eth_rx_producers rx_prods = {0};
427 int i;
428
429 /* Update producers */
430 rx_prods.bd_prod = bd_prod;
431 rx_prods.cqe_prod = rx_comp_prod;
432 rx_prods.sge_prod = rx_sge_prod;
433
434 /*
435 * Make sure that the BD and SGE data is updated before updating the
436 * producers since FW might read the BD/SGE right after the producer
437 * is updated.
438 * This is only applicable for weak-ordered memory model archs such
439 * as IA-64. The following barrier is also mandatory since FW will
440 * assumes BDs must have buffers.
441 */
442 wmb();
443
444 for (i = 0; i < sizeof(struct ustorm_eth_rx_producers)/4; i++)
Dmitry Kravkov523224a2010-10-06 03:23:26 +0000445 REG_WR(bp,
446 BAR_USTRORM_INTMEM + fp->ustorm_rx_prods_offset + i*4,
Dmitry Kravkov9f6c9252010-07-27 12:34:34 +0000447 ((u32 *)&rx_prods)[i]);
448
449 mmiowb(); /* keep prod updates ordered */
450
451 DP(NETIF_MSG_RX_STATUS,
452 "queue[%d]: wrote bd_prod %u cqe_prod %u sge_prod %u\n",
453 fp->index, bd_prod, rx_comp_prod, rx_sge_prod);
454}
455
Dmitry Kravkovf2e08992010-10-06 03:28:26 +0000456static inline void bnx2x_igu_ack_sb_gen(struct bnx2x *bp, u8 igu_sb_id,
457 u8 segment, u16 index, u8 op,
458 u8 update, u32 igu_addr)
459{
460 struct igu_regular cmd_data = {0};
461
462 cmd_data.sb_id_and_flags =
463 ((index << IGU_REGULAR_SB_INDEX_SHIFT) |
464 (segment << IGU_REGULAR_SEGMENT_ACCESS_SHIFT) |
465 (update << IGU_REGULAR_BUPDATE_SHIFT) |
466 (op << IGU_REGULAR_ENABLE_INT_SHIFT));
467
468 DP(NETIF_MSG_HW, "write 0x%08x to IGU addr 0x%x\n",
469 cmd_data.sb_id_and_flags, igu_addr);
470 REG_WR(bp, igu_addr, cmd_data.sb_id_and_flags);
471
472 /* Make sure that ACK is written */
473 mmiowb();
474 barrier();
475}
476
477static inline void bnx2x_igu_clear_sb_gen(struct bnx2x *bp,
478 u8 idu_sb_id, bool is_Pf)
479{
480 u32 data, ctl, cnt = 100;
481 u32 igu_addr_data = IGU_REG_COMMAND_REG_32LSB_DATA;
482 u32 igu_addr_ctl = IGU_REG_COMMAND_REG_CTRL;
483 u32 igu_addr_ack = IGU_REG_CSTORM_TYPE_0_SB_CLEANUP + (idu_sb_id/32)*4;
484 u32 sb_bit = 1 << (idu_sb_id%32);
485 u32 func_encode = BP_FUNC(bp) |
486 ((is_Pf == true ? 1 : 0) << IGU_FID_ENCODE_IS_PF_SHIFT);
487 u32 addr_encode = IGU_CMD_E2_PROD_UPD_BASE + idu_sb_id;
488
489 /* Not supported in BC mode */
490 if (CHIP_INT_MODE_IS_BC(bp))
491 return;
492
493 data = (IGU_USE_REGISTER_cstorm_type_0_sb_cleanup
494 << IGU_REGULAR_CLEANUP_TYPE_SHIFT) |
495 IGU_REGULAR_CLEANUP_SET |
496 IGU_REGULAR_BCLEANUP;
497
498 ctl = addr_encode << IGU_CTRL_REG_ADDRESS_SHIFT |
499 func_encode << IGU_CTRL_REG_FID_SHIFT |
500 IGU_CTRL_CMD_TYPE_WR << IGU_CTRL_REG_TYPE_SHIFT;
501
502 DP(NETIF_MSG_HW, "write 0x%08x to IGU(via GRC) addr 0x%x\n",
503 data, igu_addr_data);
504 REG_WR(bp, igu_addr_data, data);
505 mmiowb();
506 barrier();
507 DP(NETIF_MSG_HW, "write 0x%08x to IGU(via GRC) addr 0x%x\n",
508 ctl, igu_addr_ctl);
509 REG_WR(bp, igu_addr_ctl, ctl);
510 mmiowb();
511 barrier();
512
513 /* wait for clean up to finish */
514 while (!(REG_RD(bp, igu_addr_ack) & sb_bit) && --cnt)
515 msleep(20);
Dmitry Kravkov9f6c9252010-07-27 12:34:34 +0000516
517
Dmitry Kravkovf2e08992010-10-06 03:28:26 +0000518 if (!(REG_RD(bp, igu_addr_ack) & sb_bit)) {
519 DP(NETIF_MSG_HW, "Unable to finish IGU cleanup: "
520 "idu_sb_id %d offset %d bit %d (cnt %d)\n",
521 idu_sb_id, idu_sb_id/32, idu_sb_id%32, cnt);
522 }
523}
524
525static inline void bnx2x_hc_ack_sb(struct bnx2x *bp, u8 sb_id,
526 u8 storm, u16 index, u8 op, u8 update)
Dmitry Kravkov9f6c9252010-07-27 12:34:34 +0000527{
528 u32 hc_addr = (HC_REG_COMMAND_REG + BP_PORT(bp)*32 +
529 COMMAND_REG_INT_ACK);
530 struct igu_ack_register igu_ack;
531
532 igu_ack.status_block_index = index;
533 igu_ack.sb_id_and_flags =
534 ((sb_id << IGU_ACK_REGISTER_STATUS_BLOCK_ID_SHIFT) |
535 (storm << IGU_ACK_REGISTER_STORM_ID_SHIFT) |
536 (update << IGU_ACK_REGISTER_UPDATE_INDEX_SHIFT) |
537 (op << IGU_ACK_REGISTER_INTERRUPT_MODE_SHIFT));
538
539 DP(BNX2X_MSG_OFF, "write 0x%08x to HC addr 0x%x\n",
540 (*(u32 *)&igu_ack), hc_addr);
541 REG_WR(bp, hc_addr, (*(u32 *)&igu_ack));
542
543 /* Make sure that ACK is written */
544 mmiowb();
545 barrier();
546}
Dmitry Kravkovf2e08992010-10-06 03:28:26 +0000547
548static inline void bnx2x_igu_ack_sb(struct bnx2x *bp, u8 igu_sb_id, u8 segment,
549 u16 index, u8 op, u8 update)
550{
551 u32 igu_addr = BAR_IGU_INTMEM + (IGU_CMD_INT_ACK_BASE + igu_sb_id)*8;
552
553 bnx2x_igu_ack_sb_gen(bp, igu_sb_id, segment, index, op, update,
554 igu_addr);
555}
556
557static inline void bnx2x_ack_sb(struct bnx2x *bp, u8 igu_sb_id, u8 storm,
558 u16 index, u8 op, u8 update)
559{
560 if (bp->common.int_block == INT_BLOCK_HC)
561 bnx2x_hc_ack_sb(bp, igu_sb_id, storm, index, op, update);
562 else {
563 u8 segment;
564
565 if (CHIP_INT_MODE_IS_BC(bp))
566 segment = storm;
567 else if (igu_sb_id != bp->igu_dsb_id)
568 segment = IGU_SEG_ACCESS_DEF;
569 else if (storm == ATTENTION_ID)
570 segment = IGU_SEG_ACCESS_ATTN;
571 else
572 segment = IGU_SEG_ACCESS_DEF;
573 bnx2x_igu_ack_sb(bp, igu_sb_id, segment, index, op, update);
574 }
575}
576
577static inline u16 bnx2x_hc_ack_int(struct bnx2x *bp)
Dmitry Kravkov9f6c9252010-07-27 12:34:34 +0000578{
579 u32 hc_addr = (HC_REG_COMMAND_REG + BP_PORT(bp)*32 +
580 COMMAND_REG_SIMD_MASK);
581 u32 result = REG_RD(bp, hc_addr);
582
583 DP(BNX2X_MSG_OFF, "read 0x%08x from HC addr 0x%x\n",
584 result, hc_addr);
585
Dmitry Kravkovf2e08992010-10-06 03:28:26 +0000586 barrier();
Dmitry Kravkov9f6c9252010-07-27 12:34:34 +0000587 return result;
588}
589
Dmitry Kravkovf2e08992010-10-06 03:28:26 +0000590static inline u16 bnx2x_igu_ack_int(struct bnx2x *bp)
591{
592 u32 igu_addr = (BAR_IGU_INTMEM + IGU_REG_SISR_MDPC_WMASK_LSB_UPPER*8);
593 u32 result = REG_RD(bp, igu_addr);
594
595 DP(NETIF_MSG_HW, "read 0x%08x from IGU addr 0x%x\n",
596 result, igu_addr);
597
598 barrier();
599 return result;
600}
601
602static inline u16 bnx2x_ack_int(struct bnx2x *bp)
603{
604 barrier();
605 if (bp->common.int_block == INT_BLOCK_HC)
606 return bnx2x_hc_ack_int(bp);
607 else
608 return bnx2x_igu_ack_int(bp);
609}
610
Dmitry Kravkov9f6c9252010-07-27 12:34:34 +0000611static inline int bnx2x_has_tx_work_unload(struct bnx2x_fastpath *fp)
612{
613 /* Tell compiler that consumer and producer can change */
614 barrier();
Eric Dumazet807540b2010-09-23 05:40:09 +0000615 return fp->tx_pkt_prod != fp->tx_pkt_cons;
Dmitry Kravkov9f6c9252010-07-27 12:34:34 +0000616}
617
618static inline u16 bnx2x_tx_avail(struct bnx2x_fastpath *fp)
619{
620 s16 used;
621 u16 prod;
622 u16 cons;
623
624 prod = fp->tx_bd_prod;
625 cons = fp->tx_bd_cons;
626
627 /* NUM_TX_RINGS = number of "next-page" entries
628 It will be used as a threshold */
629 used = SUB_S16(prod, cons) + (s16)NUM_TX_RINGS;
630
631#ifdef BNX2X_STOP_ON_ERROR
632 WARN_ON(used < 0);
633 WARN_ON(used > fp->bp->tx_ring_size);
634 WARN_ON((fp->bp->tx_ring_size - used) > MAX_TX_AVAIL);
635#endif
636
637 return (s16)(fp->bp->tx_ring_size) - used;
638}
639
640static inline int bnx2x_has_tx_work(struct bnx2x_fastpath *fp)
641{
642 u16 hw_cons;
643
644 /* Tell compiler that status block fields can change */
645 barrier();
646 hw_cons = le16_to_cpu(*fp->tx_cons_sb);
647 return hw_cons != fp->tx_pkt_cons;
648}
649
Dmitry Kravkov523224a2010-10-06 03:23:26 +0000650static inline int bnx2x_has_rx_work(struct bnx2x_fastpath *fp)
651{
652 u16 rx_cons_sb;
653
654 /* Tell compiler that status block fields can change */
655 barrier();
656 rx_cons_sb = le16_to_cpu(*fp->rx_cons_sb);
657 if ((rx_cons_sb & MAX_RCQ_DESC_CNT) == MAX_RCQ_DESC_CNT)
658 rx_cons_sb++;
659 return (fp->rx_comp_cons != rx_cons_sb);
660}
Dmitry Kravkovf85582f2010-10-06 03:34:21 +0000661
Dmitry Kravkovf2e08992010-10-06 03:28:26 +0000662/**
663 * disables tx from stack point of view
664 *
665 * @param bp
666 */
667static inline void bnx2x_tx_disable(struct bnx2x *bp)
668{
669 netif_tx_disable(bp->dev);
670 netif_carrier_off(bp->dev);
671}
672
Dmitry Kravkov9f6c9252010-07-27 12:34:34 +0000673static inline void bnx2x_free_rx_sge(struct bnx2x *bp,
674 struct bnx2x_fastpath *fp, u16 index)
675{
676 struct sw_rx_page *sw_buf = &fp->rx_page_ring[index];
677 struct page *page = sw_buf->page;
678 struct eth_rx_sge *sge = &fp->rx_sge_ring[index];
679
680 /* Skip "next page" elements */
681 if (!page)
682 return;
683
684 dma_unmap_page(&bp->pdev->dev, dma_unmap_addr(sw_buf, mapping),
Dmitry Kravkov4bca60f2010-10-06 03:30:27 +0000685 SGE_PAGE_SIZE*PAGES_PER_SGE, DMA_FROM_DEVICE);
Dmitry Kravkov9f6c9252010-07-27 12:34:34 +0000686 __free_pages(page, PAGES_PER_SGE_SHIFT);
687
688 sw_buf->page = NULL;
689 sge->addr_hi = 0;
690 sge->addr_lo = 0;
691}
692
Dmitry Kravkovd6214d72010-10-06 03:32:10 +0000693static inline void bnx2x_add_all_napi(struct bnx2x *bp)
694{
695 int i;
Dmitry Kravkov9f6c9252010-07-27 12:34:34 +0000696
Dmitry Kravkovd6214d72010-10-06 03:32:10 +0000697 /* Add NAPI objects */
698 for_each_queue(bp, i)
699 netif_napi_add(bp->dev, &bnx2x_fp(bp, i, napi),
700 bnx2x_poll, BNX2X_NAPI_WEIGHT);
701}
Dmitry Kravkov523224a2010-10-06 03:23:26 +0000702
Dmitry Kravkovd6214d72010-10-06 03:32:10 +0000703static inline void bnx2x_del_all_napi(struct bnx2x *bp)
704{
705 int i;
Dmitry Kravkov523224a2010-10-06 03:23:26 +0000706
Dmitry Kravkovd6214d72010-10-06 03:32:10 +0000707 for_each_queue(bp, i)
708 netif_napi_del(&bnx2x_fp(bp, i, napi));
709}
710
711static inline void bnx2x_disable_msi(struct bnx2x *bp)
712{
713 if (bp->flags & USING_MSIX_FLAG) {
714 pci_disable_msix(bp->pdev);
715 bp->flags &= ~USING_MSIX_FLAG;
716 } else if (bp->flags & USING_MSI_FLAG) {
717 pci_disable_msi(bp->pdev);
718 bp->flags &= ~USING_MSI_FLAG;
719 }
720}
721
722static inline int bnx2x_calc_num_queues(struct bnx2x *bp)
723{
724 return num_queues ?
725 min_t(int, num_queues, BNX2X_MAX_QUEUES(bp)) :
726 min_t(int, num_online_cpus(), BNX2X_MAX_QUEUES(bp));
727}
Dmitry Kravkov523224a2010-10-06 03:23:26 +0000728
729static inline void bnx2x_clear_sge_mask_next_elems(struct bnx2x_fastpath *fp)
730{
731 int i, j;
732
733 for (i = 1; i <= NUM_RX_SGE_PAGES; i++) {
734 int idx = RX_SGE_CNT * i - 1;
735
736 for (j = 0; j < 2; j++) {
737 SGE_MASK_CLEAR_BIT(fp, idx);
738 idx--;
739 }
740 }
741}
742
743static inline void bnx2x_init_sge_ring_bit_mask(struct bnx2x_fastpath *fp)
744{
745 /* Set the mask to all 1-s: it's faster to compare to 0 than to 0xf-s */
746 memset(fp->sge_mask, 0xff,
747 (NUM_RX_SGE >> RX_SGE_MASK_ELEM_SHIFT)*sizeof(u64));
748
749 /* Clear the two last indices in the page to 1:
750 these are the indices that correspond to the "next" element,
751 hence will never be indicated and should be removed from
752 the calculations. */
753 bnx2x_clear_sge_mask_next_elems(fp);
Dmitry Kravkov9f6c9252010-07-27 12:34:34 +0000754}
755
756static inline int bnx2x_alloc_rx_sge(struct bnx2x *bp,
757 struct bnx2x_fastpath *fp, u16 index)
758{
759 struct page *page = alloc_pages(GFP_ATOMIC, PAGES_PER_SGE_SHIFT);
760 struct sw_rx_page *sw_buf = &fp->rx_page_ring[index];
761 struct eth_rx_sge *sge = &fp->rx_sge_ring[index];
762 dma_addr_t mapping;
763
764 if (unlikely(page == NULL))
765 return -ENOMEM;
766
767 mapping = dma_map_page(&bp->pdev->dev, page, 0,
768 SGE_PAGE_SIZE*PAGES_PER_SGE, DMA_FROM_DEVICE);
769 if (unlikely(dma_mapping_error(&bp->pdev->dev, mapping))) {
770 __free_pages(page, PAGES_PER_SGE_SHIFT);
771 return -ENOMEM;
772 }
773
774 sw_buf->page = page;
775 dma_unmap_addr_set(sw_buf, mapping, mapping);
776
777 sge->addr_hi = cpu_to_le32(U64_HI(mapping));
778 sge->addr_lo = cpu_to_le32(U64_LO(mapping));
779
780 return 0;
781}
Dmitry Kravkovf85582f2010-10-06 03:34:21 +0000782
Dmitry Kravkov9f6c9252010-07-27 12:34:34 +0000783static inline int bnx2x_alloc_rx_skb(struct bnx2x *bp,
784 struct bnx2x_fastpath *fp, u16 index)
785{
786 struct sk_buff *skb;
787 struct sw_rx_bd *rx_buf = &fp->rx_buf_ring[index];
788 struct eth_rx_bd *rx_bd = &fp->rx_desc_ring[index];
789 dma_addr_t mapping;
790
791 skb = netdev_alloc_skb(bp->dev, bp->rx_buf_size);
792 if (unlikely(skb == NULL))
793 return -ENOMEM;
794
795 mapping = dma_map_single(&bp->pdev->dev, skb->data, bp->rx_buf_size,
796 DMA_FROM_DEVICE);
797 if (unlikely(dma_mapping_error(&bp->pdev->dev, mapping))) {
798 dev_kfree_skb(skb);
799 return -ENOMEM;
800 }
801
802 rx_buf->skb = skb;
803 dma_unmap_addr_set(rx_buf, mapping, mapping);
804
805 rx_bd->addr_hi = cpu_to_le32(U64_HI(mapping));
806 rx_bd->addr_lo = cpu_to_le32(U64_LO(mapping));
807
808 return 0;
809}
810
811/* note that we are not allocating a new skb,
812 * we are just moving one from cons to prod
813 * we are not creating a new mapping,
814 * so there is no need to check for dma_mapping_error().
815 */
816static inline void bnx2x_reuse_rx_skb(struct bnx2x_fastpath *fp,
Dmitry Kravkov749a8502010-10-06 03:29:05 +0000817 u16 cons, u16 prod)
Dmitry Kravkov9f6c9252010-07-27 12:34:34 +0000818{
819 struct bnx2x *bp = fp->bp;
820 struct sw_rx_bd *cons_rx_buf = &fp->rx_buf_ring[cons];
821 struct sw_rx_bd *prod_rx_buf = &fp->rx_buf_ring[prod];
822 struct eth_rx_bd *cons_bd = &fp->rx_desc_ring[cons];
823 struct eth_rx_bd *prod_bd = &fp->rx_desc_ring[prod];
824
825 dma_sync_single_for_device(&bp->pdev->dev,
826 dma_unmap_addr(cons_rx_buf, mapping),
827 RX_COPY_THRESH, DMA_FROM_DEVICE);
828
829 prod_rx_buf->skb = cons_rx_buf->skb;
830 dma_unmap_addr_set(prod_rx_buf, mapping,
831 dma_unmap_addr(cons_rx_buf, mapping));
832 *prod_bd = *cons_bd;
833}
Dmitry Kravkovf85582f2010-10-06 03:34:21 +0000834
Dmitry Kravkov523224a2010-10-06 03:23:26 +0000835static inline void bnx2x_free_rx_sge_range(struct bnx2x *bp,
836 struct bnx2x_fastpath *fp, int last)
Dmitry Kravkov9f6c9252010-07-27 12:34:34 +0000837{
Dmitry Kravkov523224a2010-10-06 03:23:26 +0000838 int i;
Dmitry Kravkov9f6c9252010-07-27 12:34:34 +0000839
Dmitry Kravkov523224a2010-10-06 03:23:26 +0000840 for (i = 0; i < last; i++)
841 bnx2x_free_rx_sge(bp, fp, i);
Dmitry Kravkov9f6c9252010-07-27 12:34:34 +0000842}
843
Dmitry Kravkov9f6c9252010-07-27 12:34:34 +0000844static inline void bnx2x_free_tpa_pool(struct bnx2x *bp,
845 struct bnx2x_fastpath *fp, int last)
846{
847 int i;
848
849 for (i = 0; i < last; i++) {
850 struct sw_rx_bd *rx_buf = &(fp->tpa_pool[i]);
851 struct sk_buff *skb = rx_buf->skb;
852
853 if (skb == NULL) {
854 DP(NETIF_MSG_IFDOWN, "tpa bin %d empty on free\n", i);
855 continue;
856 }
857
858 if (fp->tpa_state[i] == BNX2X_TPA_START)
859 dma_unmap_single(&bp->pdev->dev,
860 dma_unmap_addr(rx_buf, mapping),
861 bp->rx_buf_size, DMA_FROM_DEVICE);
862
863 dev_kfree_skb(skb);
864 rx_buf->skb = NULL;
865 }
866}
867
868
Dmitry Kravkov523224a2010-10-06 03:23:26 +0000869static inline void bnx2x_init_tx_rings(struct bnx2x *bp)
Dmitry Kravkov9f6c9252010-07-27 12:34:34 +0000870{
871 int i, j;
872
873 for_each_queue(bp, j) {
874 struct bnx2x_fastpath *fp = &bp->fp[j];
875
876 for (i = 1; i <= NUM_TX_RINGS; i++) {
877 struct eth_tx_next_bd *tx_next_bd =
878 &fp->tx_desc_ring[TX_DESC_CNT * i - 1].next_bd;
879
880 tx_next_bd->addr_hi =
881 cpu_to_le32(U64_HI(fp->tx_desc_mapping +
882 BCM_PAGE_SIZE*(i % NUM_TX_RINGS)));
883 tx_next_bd->addr_lo =
884 cpu_to_le32(U64_LO(fp->tx_desc_mapping +
885 BCM_PAGE_SIZE*(i % NUM_TX_RINGS)));
886 }
887
Dmitry Kravkov523224a2010-10-06 03:23:26 +0000888 SET_FLAG(fp->tx_db.data.header.header, DOORBELL_HDR_DB_TYPE, 1);
Dmitry Kravkov9f6c9252010-07-27 12:34:34 +0000889 fp->tx_db.data.zero_fill1 = 0;
890 fp->tx_db.data.prod = 0;
891
892 fp->tx_pkt_prod = 0;
893 fp->tx_pkt_cons = 0;
894 fp->tx_bd_prod = 0;
895 fp->tx_bd_cons = 0;
Dmitry Kravkov9f6c9252010-07-27 12:34:34 +0000896 fp->tx_pkt = 0;
897 }
898}
Dmitry Kravkovf85582f2010-10-06 03:34:21 +0000899
Dmitry Kravkov523224a2010-10-06 03:23:26 +0000900static inline void bnx2x_set_next_page_rx_bd(struct bnx2x_fastpath *fp)
Dmitry Kravkov9f6c9252010-07-27 12:34:34 +0000901{
Dmitry Kravkov523224a2010-10-06 03:23:26 +0000902 int i;
Dmitry Kravkov9f6c9252010-07-27 12:34:34 +0000903
Dmitry Kravkov523224a2010-10-06 03:23:26 +0000904 for (i = 1; i <= NUM_RX_RINGS; i++) {
905 struct eth_rx_bd *rx_bd;
906
907 rx_bd = &fp->rx_desc_ring[RX_DESC_CNT * i - 2];
908 rx_bd->addr_hi =
909 cpu_to_le32(U64_HI(fp->rx_desc_mapping +
910 BCM_PAGE_SIZE*(i % NUM_RX_RINGS)));
911 rx_bd->addr_lo =
912 cpu_to_le32(U64_LO(fp->rx_desc_mapping +
913 BCM_PAGE_SIZE*(i % NUM_RX_RINGS)));
914 }
Dmitry Kravkov9f6c9252010-07-27 12:34:34 +0000915}
916
Dmitry Kravkov523224a2010-10-06 03:23:26 +0000917static inline void bnx2x_set_next_page_sgl(struct bnx2x_fastpath *fp)
918{
919 int i;
920
921 for (i = 1; i <= NUM_RX_SGE_PAGES; i++) {
922 struct eth_rx_sge *sge;
923
924 sge = &fp->rx_sge_ring[RX_SGE_CNT * i - 2];
925 sge->addr_hi =
926 cpu_to_le32(U64_HI(fp->rx_sge_mapping +
927 BCM_PAGE_SIZE*(i % NUM_RX_SGE_PAGES)));
928
929 sge->addr_lo =
930 cpu_to_le32(U64_LO(fp->rx_sge_mapping +
931 BCM_PAGE_SIZE*(i % NUM_RX_SGE_PAGES)));
932 }
933}
934
935static inline void bnx2x_set_next_page_rx_cq(struct bnx2x_fastpath *fp)
936{
937 int i;
938 for (i = 1; i <= NUM_RCQ_RINGS; i++) {
939 struct eth_rx_cqe_next_page *nextpg;
940
941 nextpg = (struct eth_rx_cqe_next_page *)
942 &fp->rx_comp_ring[RCQ_DESC_CNT * i - 1];
943 nextpg->addr_hi =
944 cpu_to_le32(U64_HI(fp->rx_comp_mapping +
945 BCM_PAGE_SIZE*(i % NUM_RCQ_RINGS)));
946 nextpg->addr_lo =
947 cpu_to_le32(U64_LO(fp->rx_comp_mapping +
948 BCM_PAGE_SIZE*(i % NUM_RCQ_RINGS)));
949 }
950}
951
952
953
954static inline void __storm_memset_struct(struct bnx2x *bp,
955 u32 addr, size_t size, u32 *data)
956{
957 int i;
958 for (i = 0; i < size/4; i++)
959 REG_WR(bp, addr + (i * 4), data[i]);
960}
961
962static inline void storm_memset_mac_filters(struct bnx2x *bp,
963 struct tstorm_eth_mac_filter_config *mac_filters,
964 u16 abs_fid)
965{
966 size_t size = sizeof(struct tstorm_eth_mac_filter_config);
967
968 u32 addr = BAR_TSTRORM_INTMEM +
969 TSTORM_MAC_FILTER_CONFIG_OFFSET(abs_fid);
970
971 __storm_memset_struct(bp, addr, size, (u32 *)mac_filters);
972}
973
974static inline void storm_memset_cmng(struct bnx2x *bp,
975 struct cmng_struct_per_port *cmng,
976 u8 port)
977{
978 size_t size = sizeof(struct cmng_struct_per_port);
979
980 u32 addr = BAR_XSTRORM_INTMEM +
981 XSTORM_CMNG_PER_PORT_VARS_OFFSET(port);
982
983 __storm_memset_struct(bp, addr, size, (u32 *)cmng);
984}
Dmitry Kravkovf85582f2010-10-06 03:34:21 +0000985
Dmitry Kravkov9f6c9252010-07-27 12:34:34 +0000986/* HW Lock for shared dual port PHYs */
987void bnx2x_acquire_phy_lock(struct bnx2x *bp);
988void bnx2x_release_phy_lock(struct bnx2x *bp);
989
Dmitry Kravkov9f6c9252010-07-27 12:34:34 +0000990#endif /* BNX2X_CMN_H */