bnx2x_cmn.h 28 KB

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