bnx2x_cmn.h 24 KB

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