bnx2x_cmn.h 27 KB

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