fm10k_common.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. /* Intel Ethernet Switch Host Interface Driver
  2. * Copyright(c) 2013 - 2014 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. /* clear the enable bit for all rings */
  184. for (i = 0; i < q_cnt; i++) {
  185. reg = fm10k_read_reg(hw, FM10K_TXDCTL(i));
  186. fm10k_write_reg(hw, FM10K_TXDCTL(i),
  187. reg & ~FM10K_TXDCTL_ENABLE);
  188. reg = fm10k_read_reg(hw, FM10K_RXQCTL(i));
  189. fm10k_write_reg(hw, FM10K_RXQCTL(i),
  190. reg & ~FM10K_RXQCTL_ENABLE);
  191. }
  192. fm10k_write_flush(hw);
  193. udelay(1);
  194. /* loop through all queues to verify that they are all disabled */
  195. for (i = 0, time = FM10K_QUEUE_DISABLE_TIMEOUT; time;) {
  196. /* if we are at end of rings all rings are disabled */
  197. if (i == q_cnt)
  198. return 0;
  199. /* if queue enables cleared, then move to next ring pair */
  200. reg = fm10k_read_reg(hw, FM10K_TXDCTL(i));
  201. if (!~reg || !(reg & FM10K_TXDCTL_ENABLE)) {
  202. reg = fm10k_read_reg(hw, FM10K_RXQCTL(i));
  203. if (!~reg || !(reg & FM10K_RXQCTL_ENABLE)) {
  204. i++;
  205. continue;
  206. }
  207. }
  208. /* decrement time and wait 1 usec */
  209. time--;
  210. if (time)
  211. udelay(1);
  212. }
  213. return FM10K_ERR_REQUESTS_PENDING;
  214. }
  215. /**
  216. * fm10k_stop_hw_generic - Stop Tx/Rx units
  217. * @hw: pointer to hardware structure
  218. *
  219. **/
  220. s32 fm10k_stop_hw_generic(struct fm10k_hw *hw)
  221. {
  222. return fm10k_disable_queues_generic(hw, hw->mac.max_queues);
  223. }
  224. /**
  225. * fm10k_read_hw_stats_32b - Reads value of 32-bit registers
  226. * @hw: pointer to the hardware structure
  227. * @addr: address of register containing a 32-bit value
  228. *
  229. * Function reads the content of the register and returns the delta
  230. * between the base and the current value.
  231. * **/
  232. u32 fm10k_read_hw_stats_32b(struct fm10k_hw *hw, u32 addr,
  233. struct fm10k_hw_stat *stat)
  234. {
  235. u32 delta = fm10k_read_reg(hw, addr) - stat->base_l;
  236. if (FM10K_REMOVED(hw->hw_addr))
  237. stat->base_h = 0;
  238. return delta;
  239. }
  240. /**
  241. * fm10k_read_hw_stats_48b - Reads value of 48-bit registers
  242. * @hw: pointer to the hardware structure
  243. * @addr: address of register containing the lower 32-bit value
  244. *
  245. * Function reads the content of 2 registers, combined to represent a 48-bit
  246. * statistical value. Extra processing is required to handle overflowing.
  247. * Finally, a delta value is returned representing the difference between the
  248. * values stored in registers and values stored in the statistic counters.
  249. * **/
  250. static u64 fm10k_read_hw_stats_48b(struct fm10k_hw *hw, u32 addr,
  251. struct fm10k_hw_stat *stat)
  252. {
  253. u32 count_l;
  254. u32 count_h;
  255. u32 count_tmp;
  256. u64 delta;
  257. count_h = fm10k_read_reg(hw, addr + 1);
  258. /* Check for overflow */
  259. do {
  260. count_tmp = count_h;
  261. count_l = fm10k_read_reg(hw, addr);
  262. count_h = fm10k_read_reg(hw, addr + 1);
  263. } while (count_h != count_tmp);
  264. delta = ((u64)(count_h - stat->base_h) << 32) + count_l;
  265. delta -= stat->base_l;
  266. return delta & FM10K_48_BIT_MASK;
  267. }
  268. /**
  269. * fm10k_update_hw_base_48b - Updates 48-bit statistic base value
  270. * @stat: pointer to the hardware statistic structure
  271. * @delta: value to be updated into the hardware statistic structure
  272. *
  273. * Function receives a value and determines if an update is required based on
  274. * a delta calculation. Only the base value will be updated.
  275. **/
  276. static void fm10k_update_hw_base_48b(struct fm10k_hw_stat *stat, u64 delta)
  277. {
  278. if (!delta)
  279. return;
  280. /* update lower 32 bits */
  281. delta += stat->base_l;
  282. stat->base_l = (u32)delta;
  283. /* update upper 32 bits */
  284. stat->base_h += (u32)(delta >> 32);
  285. }
  286. /**
  287. * fm10k_update_hw_stats_tx_q - Updates TX queue statistics counters
  288. * @hw: pointer to the hardware structure
  289. * @q: pointer to the ring of hardware statistics queue
  290. * @idx: index pointing to the start of the ring iteration
  291. *
  292. * Function updates the TX queue statistics counters that are related to the
  293. * hardware.
  294. **/
  295. static void fm10k_update_hw_stats_tx_q(struct fm10k_hw *hw,
  296. struct fm10k_hw_stats_q *q,
  297. u32 idx)
  298. {
  299. u32 id_tx, id_tx_prev, tx_packets;
  300. u64 tx_bytes = 0;
  301. /* Retrieve TX Owner Data */
  302. id_tx = fm10k_read_reg(hw, FM10K_TXQCTL(idx));
  303. /* Process TX Ring */
  304. do {
  305. tx_packets = fm10k_read_hw_stats_32b(hw, FM10K_QPTC(idx),
  306. &q->tx_packets);
  307. if (tx_packets)
  308. tx_bytes = fm10k_read_hw_stats_48b(hw,
  309. FM10K_QBTC_L(idx),
  310. &q->tx_bytes);
  311. /* Re-Check Owner Data */
  312. id_tx_prev = id_tx;
  313. id_tx = fm10k_read_reg(hw, FM10K_TXQCTL(idx));
  314. } while ((id_tx ^ id_tx_prev) & FM10K_TXQCTL_ID_MASK);
  315. /* drop non-ID bits and set VALID ID bit */
  316. id_tx &= FM10K_TXQCTL_ID_MASK;
  317. id_tx |= FM10K_STAT_VALID;
  318. /* update packet counts */
  319. if (q->tx_stats_idx == id_tx) {
  320. q->tx_packets.count += tx_packets;
  321. q->tx_bytes.count += tx_bytes;
  322. }
  323. /* update bases and record ID */
  324. fm10k_update_hw_base_32b(&q->tx_packets, tx_packets);
  325. fm10k_update_hw_base_48b(&q->tx_bytes, tx_bytes);
  326. q->tx_stats_idx = id_tx;
  327. }
  328. /**
  329. * fm10k_update_hw_stats_rx_q - Updates RX queue statistics counters
  330. * @hw: pointer to the hardware structure
  331. * @q: pointer to the ring of hardware statistics queue
  332. * @idx: index pointing to the start of the ring iteration
  333. *
  334. * Function updates the RX queue statistics counters that are related to the
  335. * hardware.
  336. **/
  337. static void fm10k_update_hw_stats_rx_q(struct fm10k_hw *hw,
  338. struct fm10k_hw_stats_q *q,
  339. u32 idx)
  340. {
  341. u32 id_rx, id_rx_prev, rx_packets, rx_drops;
  342. u64 rx_bytes = 0;
  343. /* Retrieve RX Owner Data */
  344. id_rx = fm10k_read_reg(hw, FM10K_RXQCTL(idx));
  345. /* Process RX Ring */
  346. do {
  347. rx_drops = fm10k_read_hw_stats_32b(hw, FM10K_QPRDC(idx),
  348. &q->rx_drops);
  349. rx_packets = fm10k_read_hw_stats_32b(hw, FM10K_QPRC(idx),
  350. &q->rx_packets);
  351. if (rx_packets)
  352. rx_bytes = fm10k_read_hw_stats_48b(hw,
  353. FM10K_QBRC_L(idx),
  354. &q->rx_bytes);
  355. /* Re-Check Owner Data */
  356. id_rx_prev = id_rx;
  357. id_rx = fm10k_read_reg(hw, FM10K_RXQCTL(idx));
  358. } while ((id_rx ^ id_rx_prev) & FM10K_RXQCTL_ID_MASK);
  359. /* drop non-ID bits and set VALID ID bit */
  360. id_rx &= FM10K_RXQCTL_ID_MASK;
  361. id_rx |= FM10K_STAT_VALID;
  362. /* update packet counts */
  363. if (q->rx_stats_idx == id_rx) {
  364. q->rx_drops.count += rx_drops;
  365. q->rx_packets.count += rx_packets;
  366. q->rx_bytes.count += rx_bytes;
  367. }
  368. /* update bases and record ID */
  369. fm10k_update_hw_base_32b(&q->rx_drops, rx_drops);
  370. fm10k_update_hw_base_32b(&q->rx_packets, rx_packets);
  371. fm10k_update_hw_base_48b(&q->rx_bytes, rx_bytes);
  372. q->rx_stats_idx = id_rx;
  373. }
  374. /**
  375. * fm10k_update_hw_stats_q - Updates queue statistics counters
  376. * @hw: pointer to the hardware structure
  377. * @q: pointer to the ring of hardware statistics queue
  378. * @idx: index pointing to the start of the ring iteration
  379. * @count: number of queues to iterate over
  380. *
  381. * Function updates the queue statistics counters that are related to the
  382. * hardware.
  383. **/
  384. void fm10k_update_hw_stats_q(struct fm10k_hw *hw, struct fm10k_hw_stats_q *q,
  385. u32 idx, u32 count)
  386. {
  387. u32 i;
  388. for (i = 0; i < count; i++, idx++, q++) {
  389. fm10k_update_hw_stats_tx_q(hw, q, idx);
  390. fm10k_update_hw_stats_rx_q(hw, q, idx);
  391. }
  392. }
  393. /**
  394. * fm10k_unbind_hw_stats_q - Unbind the queue counters from their queues
  395. * @hw: pointer to the hardware structure
  396. * @q: pointer to the ring of hardware statistics queue
  397. * @idx: index pointing to the start of the ring iteration
  398. * @count: number of queues to iterate over
  399. *
  400. * Function invalidates the index values for the queues so any updates that
  401. * may have happened are ignored and the base for the queue stats is reset.
  402. **/
  403. void fm10k_unbind_hw_stats_q(struct fm10k_hw_stats_q *q, u32 idx, u32 count)
  404. {
  405. u32 i;
  406. for (i = 0; i < count; i++, idx++, q++) {
  407. q->rx_stats_idx = 0;
  408. q->tx_stats_idx = 0;
  409. }
  410. }
  411. /**
  412. * fm10k_get_host_state_generic - Returns the state of the host
  413. * @hw: pointer to hardware structure
  414. * @host_ready: pointer to boolean value that will record host state
  415. *
  416. * This function will check the health of the mailbox and Tx queue 0
  417. * in order to determine if we should report that the link is up or not.
  418. **/
  419. s32 fm10k_get_host_state_generic(struct fm10k_hw *hw, bool *host_ready)
  420. {
  421. struct fm10k_mbx_info *mbx = &hw->mbx;
  422. struct fm10k_mac_info *mac = &hw->mac;
  423. s32 ret_val = 0;
  424. u32 txdctl = fm10k_read_reg(hw, FM10K_TXDCTL(0));
  425. /* process upstream mailbox in case interrupts were disabled */
  426. mbx->ops.process(hw, mbx);
  427. /* If Tx is no longer enabled link should come down */
  428. if (!(~txdctl) || !(txdctl & FM10K_TXDCTL_ENABLE))
  429. mac->get_host_state = true;
  430. /* exit if not checking for link, or link cannot be changed */
  431. if (!mac->get_host_state || !(~txdctl))
  432. goto out;
  433. /* if we somehow dropped the Tx enable we should reset */
  434. if (hw->mac.tx_ready && !(txdctl & FM10K_TXDCTL_ENABLE)) {
  435. ret_val = FM10K_ERR_RESET_REQUESTED;
  436. goto out;
  437. }
  438. /* if Mailbox timed out we should request reset */
  439. if (!mbx->timeout) {
  440. ret_val = FM10K_ERR_RESET_REQUESTED;
  441. goto out;
  442. }
  443. /* verify Mailbox is still valid */
  444. if (!mbx->ops.tx_ready(mbx, FM10K_VFMBX_MSG_MTU))
  445. goto out;
  446. /* interface cannot receive traffic without logical ports */
  447. if (mac->dglort_map == FM10K_DGLORTMAP_NONE)
  448. goto out;
  449. /* if we passed all the tests above then the switch is ready and we no
  450. * longer need to check for link
  451. */
  452. mac->get_host_state = false;
  453. out:
  454. *host_ready = !mac->get_host_state;
  455. return ret_val;
  456. }