fm10k_common.c 15 KB

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