fm10k_common.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright(c) 2013 - 2018 Intel Corporation. */
  3. #include "fm10k_common.h"
  4. /**
  5. * fm10k_get_bus_info_generic - Generic set PCI bus info
  6. * @hw: pointer to hardware structure
  7. *
  8. * Gets the PCI bus info (speed, width, type) then calls helper function to
  9. * store this data within the fm10k_hw structure.
  10. **/
  11. s32 fm10k_get_bus_info_generic(struct fm10k_hw *hw)
  12. {
  13. u16 link_cap, link_status, device_cap, device_control;
  14. /* Get the maximum link width and speed from PCIe config space */
  15. link_cap = fm10k_read_pci_cfg_word(hw, FM10K_PCIE_LINK_CAP);
  16. switch (link_cap & FM10K_PCIE_LINK_WIDTH) {
  17. case FM10K_PCIE_LINK_WIDTH_1:
  18. hw->bus_caps.width = fm10k_bus_width_pcie_x1;
  19. break;
  20. case FM10K_PCIE_LINK_WIDTH_2:
  21. hw->bus_caps.width = fm10k_bus_width_pcie_x2;
  22. break;
  23. case FM10K_PCIE_LINK_WIDTH_4:
  24. hw->bus_caps.width = fm10k_bus_width_pcie_x4;
  25. break;
  26. case FM10K_PCIE_LINK_WIDTH_8:
  27. hw->bus_caps.width = fm10k_bus_width_pcie_x8;
  28. break;
  29. default:
  30. hw->bus_caps.width = fm10k_bus_width_unknown;
  31. break;
  32. }
  33. switch (link_cap & FM10K_PCIE_LINK_SPEED) {
  34. case FM10K_PCIE_LINK_SPEED_2500:
  35. hw->bus_caps.speed = fm10k_bus_speed_2500;
  36. break;
  37. case FM10K_PCIE_LINK_SPEED_5000:
  38. hw->bus_caps.speed = fm10k_bus_speed_5000;
  39. break;
  40. case FM10K_PCIE_LINK_SPEED_8000:
  41. hw->bus_caps.speed = fm10k_bus_speed_8000;
  42. break;
  43. default:
  44. hw->bus_caps.speed = fm10k_bus_speed_unknown;
  45. break;
  46. }
  47. /* Get the PCIe maximum payload size for the PCIe function */
  48. device_cap = fm10k_read_pci_cfg_word(hw, FM10K_PCIE_DEV_CAP);
  49. switch (device_cap & FM10K_PCIE_DEV_CAP_PAYLOAD) {
  50. case FM10K_PCIE_DEV_CAP_PAYLOAD_128:
  51. hw->bus_caps.payload = fm10k_bus_payload_128;
  52. break;
  53. case FM10K_PCIE_DEV_CAP_PAYLOAD_256:
  54. hw->bus_caps.payload = fm10k_bus_payload_256;
  55. break;
  56. case FM10K_PCIE_DEV_CAP_PAYLOAD_512:
  57. hw->bus_caps.payload = fm10k_bus_payload_512;
  58. break;
  59. default:
  60. hw->bus_caps.payload = fm10k_bus_payload_unknown;
  61. break;
  62. }
  63. /* Get the negotiated link width and speed from PCIe config space */
  64. link_status = fm10k_read_pci_cfg_word(hw, FM10K_PCIE_LINK_STATUS);
  65. switch (link_status & FM10K_PCIE_LINK_WIDTH) {
  66. case FM10K_PCIE_LINK_WIDTH_1:
  67. hw->bus.width = fm10k_bus_width_pcie_x1;
  68. break;
  69. case FM10K_PCIE_LINK_WIDTH_2:
  70. hw->bus.width = fm10k_bus_width_pcie_x2;
  71. break;
  72. case FM10K_PCIE_LINK_WIDTH_4:
  73. hw->bus.width = fm10k_bus_width_pcie_x4;
  74. break;
  75. case FM10K_PCIE_LINK_WIDTH_8:
  76. hw->bus.width = fm10k_bus_width_pcie_x8;
  77. break;
  78. default:
  79. hw->bus.width = fm10k_bus_width_unknown;
  80. break;
  81. }
  82. switch (link_status & FM10K_PCIE_LINK_SPEED) {
  83. case FM10K_PCIE_LINK_SPEED_2500:
  84. hw->bus.speed = fm10k_bus_speed_2500;
  85. break;
  86. case FM10K_PCIE_LINK_SPEED_5000:
  87. hw->bus.speed = fm10k_bus_speed_5000;
  88. break;
  89. case FM10K_PCIE_LINK_SPEED_8000:
  90. hw->bus.speed = fm10k_bus_speed_8000;
  91. break;
  92. default:
  93. hw->bus.speed = fm10k_bus_speed_unknown;
  94. break;
  95. }
  96. /* Get the negotiated PCIe maximum payload size for the PCIe function */
  97. device_control = fm10k_read_pci_cfg_word(hw, FM10K_PCIE_DEV_CTRL);
  98. switch (device_control & FM10K_PCIE_DEV_CTRL_PAYLOAD) {
  99. case FM10K_PCIE_DEV_CTRL_PAYLOAD_128:
  100. hw->bus.payload = fm10k_bus_payload_128;
  101. break;
  102. case FM10K_PCIE_DEV_CTRL_PAYLOAD_256:
  103. hw->bus.payload = fm10k_bus_payload_256;
  104. break;
  105. case FM10K_PCIE_DEV_CTRL_PAYLOAD_512:
  106. hw->bus.payload = fm10k_bus_payload_512;
  107. break;
  108. default:
  109. hw->bus.payload = fm10k_bus_payload_unknown;
  110. break;
  111. }
  112. return 0;
  113. }
  114. static u16 fm10k_get_pcie_msix_count_generic(struct fm10k_hw *hw)
  115. {
  116. u16 msix_count;
  117. /* read in value from MSI-X capability register */
  118. msix_count = fm10k_read_pci_cfg_word(hw, FM10K_PCI_MSIX_MSG_CTRL);
  119. msix_count &= FM10K_PCI_MSIX_MSG_CTRL_TBL_SZ_MASK;
  120. /* MSI-X count is zero-based in HW */
  121. msix_count++;
  122. if (msix_count > FM10K_MAX_MSIX_VECTORS)
  123. msix_count = FM10K_MAX_MSIX_VECTORS;
  124. return msix_count;
  125. }
  126. /**
  127. * fm10k_get_invariants_generic - Inits constant values
  128. * @hw: pointer to the hardware structure
  129. *
  130. * Initialize the common invariants for the device.
  131. **/
  132. s32 fm10k_get_invariants_generic(struct fm10k_hw *hw)
  133. {
  134. struct fm10k_mac_info *mac = &hw->mac;
  135. /* initialize GLORT state to avoid any false hits */
  136. mac->dglort_map = FM10K_DGLORTMAP_NONE;
  137. /* record maximum number of MSI-X vectors */
  138. mac->max_msix_vectors = fm10k_get_pcie_msix_count_generic(hw);
  139. return 0;
  140. }
  141. /**
  142. * fm10k_start_hw_generic - Prepare hardware for Tx/Rx
  143. * @hw: pointer to hardware structure
  144. *
  145. * This function sets the Tx ready flag to indicate that the Tx path has
  146. * been initialized.
  147. **/
  148. s32 fm10k_start_hw_generic(struct fm10k_hw *hw)
  149. {
  150. /* set flag indicating we are beginning Tx */
  151. hw->mac.tx_ready = true;
  152. return 0;
  153. }
  154. /**
  155. * fm10k_disable_queues_generic - Stop Tx/Rx queues
  156. * @hw: pointer to hardware structure
  157. * @q_cnt: number of queues to be disabled
  158. *
  159. **/
  160. s32 fm10k_disable_queues_generic(struct fm10k_hw *hw, u16 q_cnt)
  161. {
  162. u32 reg;
  163. u16 i, time;
  164. /* clear tx_ready to prevent any false hits for reset */
  165. hw->mac.tx_ready = false;
  166. if (FM10K_REMOVED(hw->hw_addr))
  167. return 0;
  168. /* clear the enable bit for all rings */
  169. for (i = 0; i < q_cnt; i++) {
  170. reg = fm10k_read_reg(hw, FM10K_TXDCTL(i));
  171. fm10k_write_reg(hw, FM10K_TXDCTL(i),
  172. reg & ~FM10K_TXDCTL_ENABLE);
  173. reg = fm10k_read_reg(hw, FM10K_RXQCTL(i));
  174. fm10k_write_reg(hw, FM10K_RXQCTL(i),
  175. reg & ~FM10K_RXQCTL_ENABLE);
  176. }
  177. fm10k_write_flush(hw);
  178. udelay(1);
  179. /* loop through all queues to verify that they are all disabled */
  180. for (i = 0, time = FM10K_QUEUE_DISABLE_TIMEOUT; time;) {
  181. /* if we are at end of rings all rings are disabled */
  182. if (i == q_cnt)
  183. return 0;
  184. /* if queue enables cleared, then move to next ring pair */
  185. reg = fm10k_read_reg(hw, FM10K_TXDCTL(i));
  186. if (!~reg || !(reg & FM10K_TXDCTL_ENABLE)) {
  187. reg = fm10k_read_reg(hw, FM10K_RXQCTL(i));
  188. if (!~reg || !(reg & FM10K_RXQCTL_ENABLE)) {
  189. i++;
  190. continue;
  191. }
  192. }
  193. /* decrement time and wait 1 usec */
  194. time--;
  195. if (time)
  196. udelay(1);
  197. }
  198. return FM10K_ERR_REQUESTS_PENDING;
  199. }
  200. /**
  201. * fm10k_stop_hw_generic - Stop Tx/Rx units
  202. * @hw: pointer to hardware structure
  203. *
  204. **/
  205. s32 fm10k_stop_hw_generic(struct fm10k_hw *hw)
  206. {
  207. return fm10k_disable_queues_generic(hw, hw->mac.max_queues);
  208. }
  209. /**
  210. * fm10k_read_hw_stats_32b - Reads value of 32-bit registers
  211. * @hw: pointer to the hardware structure
  212. * @addr: address of register containing a 32-bit value
  213. * @stat: pointer to structure holding hw stat information
  214. *
  215. * Function reads the content of the register and returns the delta
  216. * between the base and the current value.
  217. * **/
  218. u32 fm10k_read_hw_stats_32b(struct fm10k_hw *hw, u32 addr,
  219. struct fm10k_hw_stat *stat)
  220. {
  221. u32 delta = fm10k_read_reg(hw, addr) - stat->base_l;
  222. if (FM10K_REMOVED(hw->hw_addr))
  223. stat->base_h = 0;
  224. return delta;
  225. }
  226. /**
  227. * fm10k_read_hw_stats_48b - Reads value of 48-bit registers
  228. * @hw: pointer to the hardware structure
  229. * @addr: address of register containing the lower 32-bit value
  230. * @stat: pointer to structure holding hw stat information
  231. *
  232. * Function reads the content of 2 registers, combined to represent a 48-bit
  233. * statistical value. Extra processing is required to handle overflowing.
  234. * Finally, a delta value is returned representing the difference between the
  235. * values stored in registers and values stored in the statistic counters.
  236. * **/
  237. static u64 fm10k_read_hw_stats_48b(struct fm10k_hw *hw, u32 addr,
  238. struct fm10k_hw_stat *stat)
  239. {
  240. u32 count_l;
  241. u32 count_h;
  242. u32 count_tmp;
  243. u64 delta;
  244. count_h = fm10k_read_reg(hw, addr + 1);
  245. /* Check for overflow */
  246. do {
  247. count_tmp = count_h;
  248. count_l = fm10k_read_reg(hw, addr);
  249. count_h = fm10k_read_reg(hw, addr + 1);
  250. } while (count_h != count_tmp);
  251. delta = ((u64)(count_h - stat->base_h) << 32) + count_l;
  252. delta -= stat->base_l;
  253. return delta & FM10K_48_BIT_MASK;
  254. }
  255. /**
  256. * fm10k_update_hw_base_48b - Updates 48-bit statistic base value
  257. * @stat: pointer to the hardware statistic structure
  258. * @delta: value to be updated into the hardware statistic structure
  259. *
  260. * Function receives a value and determines if an update is required based on
  261. * a delta calculation. Only the base value will be updated.
  262. **/
  263. static void fm10k_update_hw_base_48b(struct fm10k_hw_stat *stat, u64 delta)
  264. {
  265. if (!delta)
  266. return;
  267. /* update lower 32 bits */
  268. delta += stat->base_l;
  269. stat->base_l = (u32)delta;
  270. /* update upper 32 bits */
  271. stat->base_h += (u32)(delta >> 32);
  272. }
  273. /**
  274. * fm10k_update_hw_stats_tx_q - Updates TX queue statistics counters
  275. * @hw: pointer to the hardware structure
  276. * @q: pointer to the ring of hardware statistics queue
  277. * @idx: index pointing to the start of the ring iteration
  278. *
  279. * Function updates the TX queue statistics counters that are related to the
  280. * hardware.
  281. **/
  282. static void fm10k_update_hw_stats_tx_q(struct fm10k_hw *hw,
  283. struct fm10k_hw_stats_q *q,
  284. u32 idx)
  285. {
  286. u32 id_tx, id_tx_prev, tx_packets;
  287. u64 tx_bytes = 0;
  288. /* Retrieve TX Owner Data */
  289. id_tx = fm10k_read_reg(hw, FM10K_TXQCTL(idx));
  290. /* Process TX Ring */
  291. do {
  292. tx_packets = fm10k_read_hw_stats_32b(hw, FM10K_QPTC(idx),
  293. &q->tx_packets);
  294. if (tx_packets)
  295. tx_bytes = fm10k_read_hw_stats_48b(hw,
  296. FM10K_QBTC_L(idx),
  297. &q->tx_bytes);
  298. /* Re-Check Owner Data */
  299. id_tx_prev = id_tx;
  300. id_tx = fm10k_read_reg(hw, FM10K_TXQCTL(idx));
  301. } while ((id_tx ^ id_tx_prev) & FM10K_TXQCTL_ID_MASK);
  302. /* drop non-ID bits and set VALID ID bit */
  303. id_tx &= FM10K_TXQCTL_ID_MASK;
  304. id_tx |= FM10K_STAT_VALID;
  305. /* update packet counts */
  306. if (q->tx_stats_idx == id_tx) {
  307. q->tx_packets.count += tx_packets;
  308. q->tx_bytes.count += tx_bytes;
  309. }
  310. /* update bases and record ID */
  311. fm10k_update_hw_base_32b(&q->tx_packets, tx_packets);
  312. fm10k_update_hw_base_48b(&q->tx_bytes, tx_bytes);
  313. q->tx_stats_idx = id_tx;
  314. }
  315. /**
  316. * fm10k_update_hw_stats_rx_q - Updates RX queue statistics counters
  317. * @hw: pointer to the hardware structure
  318. * @q: pointer to the ring of hardware statistics queue
  319. * @idx: index pointing to the start of the ring iteration
  320. *
  321. * Function updates the RX queue statistics counters that are related to the
  322. * hardware.
  323. **/
  324. static void fm10k_update_hw_stats_rx_q(struct fm10k_hw *hw,
  325. struct fm10k_hw_stats_q *q,
  326. u32 idx)
  327. {
  328. u32 id_rx, id_rx_prev, rx_packets, rx_drops;
  329. u64 rx_bytes = 0;
  330. /* Retrieve RX Owner Data */
  331. id_rx = fm10k_read_reg(hw, FM10K_RXQCTL(idx));
  332. /* Process RX Ring */
  333. do {
  334. rx_drops = fm10k_read_hw_stats_32b(hw, FM10K_QPRDC(idx),
  335. &q->rx_drops);
  336. rx_packets = fm10k_read_hw_stats_32b(hw, FM10K_QPRC(idx),
  337. &q->rx_packets);
  338. if (rx_packets)
  339. rx_bytes = fm10k_read_hw_stats_48b(hw,
  340. FM10K_QBRC_L(idx),
  341. &q->rx_bytes);
  342. /* Re-Check Owner Data */
  343. id_rx_prev = id_rx;
  344. id_rx = fm10k_read_reg(hw, FM10K_RXQCTL(idx));
  345. } while ((id_rx ^ id_rx_prev) & FM10K_RXQCTL_ID_MASK);
  346. /* drop non-ID bits and set VALID ID bit */
  347. id_rx &= FM10K_RXQCTL_ID_MASK;
  348. id_rx |= FM10K_STAT_VALID;
  349. /* update packet counts */
  350. if (q->rx_stats_idx == id_rx) {
  351. q->rx_drops.count += rx_drops;
  352. q->rx_packets.count += rx_packets;
  353. q->rx_bytes.count += rx_bytes;
  354. }
  355. /* update bases and record ID */
  356. fm10k_update_hw_base_32b(&q->rx_drops, rx_drops);
  357. fm10k_update_hw_base_32b(&q->rx_packets, rx_packets);
  358. fm10k_update_hw_base_48b(&q->rx_bytes, rx_bytes);
  359. q->rx_stats_idx = id_rx;
  360. }
  361. /**
  362. * fm10k_update_hw_stats_q - Updates queue statistics counters
  363. * @hw: pointer to the hardware structure
  364. * @q: pointer to the ring of hardware statistics queue
  365. * @idx: index pointing to the start of the ring iteration
  366. * @count: number of queues to iterate over
  367. *
  368. * Function updates the queue statistics counters that are related to the
  369. * hardware.
  370. **/
  371. void fm10k_update_hw_stats_q(struct fm10k_hw *hw, struct fm10k_hw_stats_q *q,
  372. u32 idx, u32 count)
  373. {
  374. u32 i;
  375. for (i = 0; i < count; i++, idx++, q++) {
  376. fm10k_update_hw_stats_tx_q(hw, q, idx);
  377. fm10k_update_hw_stats_rx_q(hw, q, idx);
  378. }
  379. }
  380. /**
  381. * fm10k_unbind_hw_stats_q - Unbind the queue counters from their queues
  382. * @q: pointer to the ring of hardware statistics queue
  383. * @idx: index pointing to the start of the ring iteration
  384. * @count: number of queues to iterate over
  385. *
  386. * Function invalidates the index values for the queues so any updates that
  387. * may have happened are ignored and the base for the queue stats is reset.
  388. **/
  389. void fm10k_unbind_hw_stats_q(struct fm10k_hw_stats_q *q, u32 idx, u32 count)
  390. {
  391. u32 i;
  392. for (i = 0; i < count; i++, idx++, q++) {
  393. q->rx_stats_idx = 0;
  394. q->tx_stats_idx = 0;
  395. }
  396. }
  397. /**
  398. * fm10k_get_host_state_generic - Returns the state of the host
  399. * @hw: pointer to hardware structure
  400. * @host_ready: pointer to boolean value that will record host state
  401. *
  402. * This function will check the health of the mailbox and Tx queue 0
  403. * in order to determine if we should report that the link is up or not.
  404. **/
  405. s32 fm10k_get_host_state_generic(struct fm10k_hw *hw, bool *host_ready)
  406. {
  407. struct fm10k_mbx_info *mbx = &hw->mbx;
  408. struct fm10k_mac_info *mac = &hw->mac;
  409. s32 ret_val = 0;
  410. u32 txdctl = fm10k_read_reg(hw, FM10K_TXDCTL(0));
  411. /* process upstream mailbox in case interrupts were disabled */
  412. mbx->ops.process(hw, mbx);
  413. /* If Tx is no longer enabled link should come down */
  414. if (!(~txdctl) || !(txdctl & FM10K_TXDCTL_ENABLE))
  415. mac->get_host_state = true;
  416. /* exit if not checking for link, or link cannot be changed */
  417. if (!mac->get_host_state || !(~txdctl))
  418. goto out;
  419. /* if we somehow dropped the Tx enable we should reset */
  420. if (mac->tx_ready && !(txdctl & FM10K_TXDCTL_ENABLE)) {
  421. ret_val = FM10K_ERR_RESET_REQUESTED;
  422. goto out;
  423. }
  424. /* if Mailbox timed out we should request reset */
  425. if (!mbx->timeout) {
  426. ret_val = FM10K_ERR_RESET_REQUESTED;
  427. goto out;
  428. }
  429. /* verify Mailbox is still open */
  430. if (mbx->state != FM10K_STATE_OPEN)
  431. goto out;
  432. /* interface cannot receive traffic without logical ports */
  433. if (mac->dglort_map == FM10K_DGLORTMAP_NONE) {
  434. if (mac->ops.request_lport_map)
  435. ret_val = mac->ops.request_lport_map(hw);
  436. goto out;
  437. }
  438. /* if we passed all the tests above then the switch is ready and we no
  439. * longer need to check for link
  440. */
  441. mac->get_host_state = false;
  442. out:
  443. *host_ready = !mac->get_host_state;
  444. return ret_val;
  445. }