fm10k_vf.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright(c) 2013 - 2018 Intel Corporation. */
  3. #include "fm10k_vf.h"
  4. /**
  5. * fm10k_stop_hw_vf - Stop Tx/Rx units
  6. * @hw: pointer to hardware structure
  7. *
  8. **/
  9. static s32 fm10k_stop_hw_vf(struct fm10k_hw *hw)
  10. {
  11. u8 *perm_addr = hw->mac.perm_addr;
  12. u32 bal = 0, bah = 0, tdlen;
  13. s32 err;
  14. u16 i;
  15. /* we need to disable the queues before taking further steps */
  16. err = fm10k_stop_hw_generic(hw);
  17. if (err && err != FM10K_ERR_REQUESTS_PENDING)
  18. return err;
  19. /* If permanent address is set then we need to restore it */
  20. if (is_valid_ether_addr(perm_addr)) {
  21. bal = (((u32)perm_addr[3]) << 24) |
  22. (((u32)perm_addr[4]) << 16) |
  23. (((u32)perm_addr[5]) << 8);
  24. bah = (((u32)0xFF) << 24) |
  25. (((u32)perm_addr[0]) << 16) |
  26. (((u32)perm_addr[1]) << 8) |
  27. ((u32)perm_addr[2]);
  28. }
  29. /* restore default itr_scale for next VF initialization */
  30. tdlen = hw->mac.itr_scale << FM10K_TDLEN_ITR_SCALE_SHIFT;
  31. /* The queues have already been disabled so we just need to
  32. * update their base address registers
  33. */
  34. for (i = 0; i < hw->mac.max_queues; i++) {
  35. fm10k_write_reg(hw, FM10K_TDBAL(i), bal);
  36. fm10k_write_reg(hw, FM10K_TDBAH(i), bah);
  37. fm10k_write_reg(hw, FM10K_RDBAL(i), bal);
  38. fm10k_write_reg(hw, FM10K_RDBAH(i), bah);
  39. /* Restore ITR scale in software-defined mechanism in TDLEN
  40. * for next VF initialization. See definition of
  41. * FM10K_TDLEN_ITR_SCALE_SHIFT for more details on the use of
  42. * TDLEN here.
  43. */
  44. fm10k_write_reg(hw, FM10K_TDLEN(i), tdlen);
  45. }
  46. return err;
  47. }
  48. /**
  49. * fm10k_reset_hw_vf - VF hardware reset
  50. * @hw: pointer to hardware structure
  51. *
  52. * This function should return the hardware to a state similar to the
  53. * one it is in after just being initialized.
  54. **/
  55. static s32 fm10k_reset_hw_vf(struct fm10k_hw *hw)
  56. {
  57. s32 err;
  58. /* shut down queues we own and reset DMA configuration */
  59. err = fm10k_stop_hw_vf(hw);
  60. if (err == FM10K_ERR_REQUESTS_PENDING)
  61. hw->mac.reset_while_pending++;
  62. else if (err)
  63. return err;
  64. /* Inititate VF reset */
  65. fm10k_write_reg(hw, FM10K_VFCTRL, FM10K_VFCTRL_RST);
  66. /* Flush write and allow 100us for reset to complete */
  67. fm10k_write_flush(hw);
  68. udelay(FM10K_RESET_TIMEOUT);
  69. /* Clear reset bit and verify it was cleared */
  70. fm10k_write_reg(hw, FM10K_VFCTRL, 0);
  71. if (fm10k_read_reg(hw, FM10K_VFCTRL) & FM10K_VFCTRL_RST)
  72. return FM10K_ERR_RESET_FAILED;
  73. return 0;
  74. }
  75. /**
  76. * fm10k_init_hw_vf - VF hardware initialization
  77. * @hw: pointer to hardware structure
  78. *
  79. **/
  80. static s32 fm10k_init_hw_vf(struct fm10k_hw *hw)
  81. {
  82. u32 tqdloc, tqdloc0 = ~fm10k_read_reg(hw, FM10K_TQDLOC(0));
  83. s32 err;
  84. u16 i;
  85. /* verify we have at least 1 queue */
  86. if (!~fm10k_read_reg(hw, FM10K_TXQCTL(0)) ||
  87. !~fm10k_read_reg(hw, FM10K_RXQCTL(0))) {
  88. err = FM10K_ERR_NO_RESOURCES;
  89. goto reset_max_queues;
  90. }
  91. /* determine how many queues we have */
  92. for (i = 1; tqdloc0 && (i < FM10K_MAX_QUEUES_POOL); i++) {
  93. /* verify the Descriptor cache offsets are increasing */
  94. tqdloc = ~fm10k_read_reg(hw, FM10K_TQDLOC(i));
  95. if (!tqdloc || (tqdloc == tqdloc0))
  96. break;
  97. /* check to verify the PF doesn't own any of our queues */
  98. if (!~fm10k_read_reg(hw, FM10K_TXQCTL(i)) ||
  99. !~fm10k_read_reg(hw, FM10K_RXQCTL(i)))
  100. break;
  101. }
  102. /* shut down queues we own and reset DMA configuration */
  103. err = fm10k_disable_queues_generic(hw, i);
  104. if (err)
  105. goto reset_max_queues;
  106. /* record maximum queue count */
  107. hw->mac.max_queues = i;
  108. /* fetch default VLAN and ITR scale */
  109. hw->mac.default_vid = (fm10k_read_reg(hw, FM10K_TXQCTL(0)) &
  110. FM10K_TXQCTL_VID_MASK) >> FM10K_TXQCTL_VID_SHIFT;
  111. /* Read the ITR scale from TDLEN. See the definition of
  112. * FM10K_TDLEN_ITR_SCALE_SHIFT for more information about how TDLEN is
  113. * used here.
  114. */
  115. hw->mac.itr_scale = (fm10k_read_reg(hw, FM10K_TDLEN(0)) &
  116. FM10K_TDLEN_ITR_SCALE_MASK) >>
  117. FM10K_TDLEN_ITR_SCALE_SHIFT;
  118. return 0;
  119. reset_max_queues:
  120. hw->mac.max_queues = 0;
  121. return err;
  122. }
  123. /* This structure defines the attibutes to be parsed below */
  124. const struct fm10k_tlv_attr fm10k_mac_vlan_msg_attr[] = {
  125. FM10K_TLV_ATTR_U32(FM10K_MAC_VLAN_MSG_VLAN),
  126. FM10K_TLV_ATTR_BOOL(FM10K_MAC_VLAN_MSG_SET),
  127. FM10K_TLV_ATTR_MAC_ADDR(FM10K_MAC_VLAN_MSG_MAC),
  128. FM10K_TLV_ATTR_MAC_ADDR(FM10K_MAC_VLAN_MSG_DEFAULT_MAC),
  129. FM10K_TLV_ATTR_MAC_ADDR(FM10K_MAC_VLAN_MSG_MULTICAST),
  130. FM10K_TLV_ATTR_LAST
  131. };
  132. /**
  133. * fm10k_update_vlan_vf - Update status of VLAN ID in VLAN filter table
  134. * @hw: pointer to hardware structure
  135. * @vid: VLAN ID to add to table
  136. * @vsi: Reserved, should always be 0
  137. * @set: Indicates if this is a set or clear operation
  138. *
  139. * This function adds or removes the corresponding VLAN ID from the VLAN
  140. * filter table for this VF.
  141. **/
  142. static s32 fm10k_update_vlan_vf(struct fm10k_hw *hw, u32 vid, u8 vsi, bool set)
  143. {
  144. struct fm10k_mbx_info *mbx = &hw->mbx;
  145. u32 msg[4];
  146. /* verify the index is not set */
  147. if (vsi)
  148. return FM10K_ERR_PARAM;
  149. /* clever trick to verify reserved bits in both vid and length */
  150. if ((vid << 16 | vid) >> 28)
  151. return FM10K_ERR_PARAM;
  152. /* encode set bit into the VLAN ID */
  153. if (!set)
  154. vid |= FM10K_VLAN_CLEAR;
  155. /* generate VLAN request */
  156. fm10k_tlv_msg_init(msg, FM10K_VF_MSG_ID_MAC_VLAN);
  157. fm10k_tlv_attr_put_u32(msg, FM10K_MAC_VLAN_MSG_VLAN, vid);
  158. /* load onto outgoing mailbox */
  159. return mbx->ops.enqueue_tx(hw, mbx, msg);
  160. }
  161. /**
  162. * fm10k_msg_mac_vlan_vf - Read device MAC address from mailbox message
  163. * @hw: pointer to the HW structure
  164. * @results: Attributes for message
  165. * @mbx: unused mailbox data
  166. *
  167. * This function should determine the MAC address for the VF
  168. **/
  169. s32 fm10k_msg_mac_vlan_vf(struct fm10k_hw *hw, u32 **results,
  170. struct fm10k_mbx_info *mbx)
  171. {
  172. u8 perm_addr[ETH_ALEN];
  173. u16 vid;
  174. s32 err;
  175. /* record MAC address requested */
  176. err = fm10k_tlv_attr_get_mac_vlan(
  177. results[FM10K_MAC_VLAN_MSG_DEFAULT_MAC],
  178. perm_addr, &vid);
  179. if (err)
  180. return err;
  181. ether_addr_copy(hw->mac.perm_addr, perm_addr);
  182. hw->mac.default_vid = vid & (FM10K_VLAN_TABLE_VID_MAX - 1);
  183. hw->mac.vlan_override = !!(vid & FM10K_VLAN_OVERRIDE);
  184. return 0;
  185. }
  186. /**
  187. * fm10k_read_mac_addr_vf - Read device MAC address
  188. * @hw: pointer to the HW structure
  189. *
  190. * This function should determine the MAC address for the VF
  191. **/
  192. static s32 fm10k_read_mac_addr_vf(struct fm10k_hw *hw)
  193. {
  194. u8 perm_addr[ETH_ALEN];
  195. u32 base_addr;
  196. base_addr = fm10k_read_reg(hw, FM10K_TDBAL(0));
  197. /* last byte should be 0 */
  198. if (base_addr << 24)
  199. return FM10K_ERR_INVALID_MAC_ADDR;
  200. perm_addr[3] = (u8)(base_addr >> 24);
  201. perm_addr[4] = (u8)(base_addr >> 16);
  202. perm_addr[5] = (u8)(base_addr >> 8);
  203. base_addr = fm10k_read_reg(hw, FM10K_TDBAH(0));
  204. /* first byte should be all 1's */
  205. if ((~base_addr) >> 24)
  206. return FM10K_ERR_INVALID_MAC_ADDR;
  207. perm_addr[0] = (u8)(base_addr >> 16);
  208. perm_addr[1] = (u8)(base_addr >> 8);
  209. perm_addr[2] = (u8)(base_addr);
  210. ether_addr_copy(hw->mac.perm_addr, perm_addr);
  211. ether_addr_copy(hw->mac.addr, perm_addr);
  212. return 0;
  213. }
  214. /**
  215. * fm10k_update_uc_addr_vf - Update device unicast addresses
  216. * @hw: pointer to the HW structure
  217. * @glort: unused
  218. * @mac: MAC address to add/remove from table
  219. * @vid: VLAN ID to add/remove from table
  220. * @add: Indicates if this is an add or remove operation
  221. * @flags: flags field to indicate add and secure - unused
  222. *
  223. * This function is used to add or remove unicast MAC addresses for
  224. * the VF.
  225. **/
  226. static s32 fm10k_update_uc_addr_vf(struct fm10k_hw *hw, u16 glort,
  227. const u8 *mac, u16 vid, bool add, u8 flags)
  228. {
  229. struct fm10k_mbx_info *mbx = &hw->mbx;
  230. u32 msg[7];
  231. /* verify VLAN ID is valid */
  232. if (vid >= FM10K_VLAN_TABLE_VID_MAX)
  233. return FM10K_ERR_PARAM;
  234. /* verify MAC address is valid */
  235. if (!is_valid_ether_addr(mac))
  236. return FM10K_ERR_PARAM;
  237. /* verify we are not locked down on the MAC address */
  238. if (is_valid_ether_addr(hw->mac.perm_addr) &&
  239. !ether_addr_equal(hw->mac.perm_addr, mac))
  240. return FM10K_ERR_PARAM;
  241. /* add bit to notify us if this is a set or clear operation */
  242. if (!add)
  243. vid |= FM10K_VLAN_CLEAR;
  244. /* generate VLAN request */
  245. fm10k_tlv_msg_init(msg, FM10K_VF_MSG_ID_MAC_VLAN);
  246. fm10k_tlv_attr_put_mac_vlan(msg, FM10K_MAC_VLAN_MSG_MAC, mac, vid);
  247. /* load onto outgoing mailbox */
  248. return mbx->ops.enqueue_tx(hw, mbx, msg);
  249. }
  250. /**
  251. * fm10k_update_mc_addr_vf - Update device multicast addresses
  252. * @hw: pointer to the HW structure
  253. * @glort: unused
  254. * @mac: MAC address to add/remove from table
  255. * @vid: VLAN ID to add/remove from table
  256. * @add: Indicates if this is an add or remove operation
  257. *
  258. * This function is used to add or remove multicast MAC addresses for
  259. * the VF.
  260. **/
  261. static s32 fm10k_update_mc_addr_vf(struct fm10k_hw *hw, u16 glort,
  262. const u8 *mac, u16 vid, bool add)
  263. {
  264. struct fm10k_mbx_info *mbx = &hw->mbx;
  265. u32 msg[7];
  266. /* verify VLAN ID is valid */
  267. if (vid >= FM10K_VLAN_TABLE_VID_MAX)
  268. return FM10K_ERR_PARAM;
  269. /* verify multicast address is valid */
  270. if (!is_multicast_ether_addr(mac))
  271. return FM10K_ERR_PARAM;
  272. /* add bit to notify us if this is a set or clear operation */
  273. if (!add)
  274. vid |= FM10K_VLAN_CLEAR;
  275. /* generate VLAN request */
  276. fm10k_tlv_msg_init(msg, FM10K_VF_MSG_ID_MAC_VLAN);
  277. fm10k_tlv_attr_put_mac_vlan(msg, FM10K_MAC_VLAN_MSG_MULTICAST,
  278. mac, vid);
  279. /* load onto outgoing mailbox */
  280. return mbx->ops.enqueue_tx(hw, mbx, msg);
  281. }
  282. /**
  283. * fm10k_update_int_moderator_vf - Request update of interrupt moderator list
  284. * @hw: pointer to hardware structure
  285. *
  286. * This function will issue a request to the PF to rescan our MSI-X table
  287. * and to update the interrupt moderator linked list.
  288. **/
  289. static void fm10k_update_int_moderator_vf(struct fm10k_hw *hw)
  290. {
  291. struct fm10k_mbx_info *mbx = &hw->mbx;
  292. u32 msg[1];
  293. /* generate MSI-X request */
  294. fm10k_tlv_msg_init(msg, FM10K_VF_MSG_ID_MSIX);
  295. /* load onto outgoing mailbox */
  296. mbx->ops.enqueue_tx(hw, mbx, msg);
  297. }
  298. /* This structure defines the attibutes to be parsed below */
  299. const struct fm10k_tlv_attr fm10k_lport_state_msg_attr[] = {
  300. FM10K_TLV_ATTR_BOOL(FM10K_LPORT_STATE_MSG_DISABLE),
  301. FM10K_TLV_ATTR_U8(FM10K_LPORT_STATE_MSG_XCAST_MODE),
  302. FM10K_TLV_ATTR_BOOL(FM10K_LPORT_STATE_MSG_READY),
  303. FM10K_TLV_ATTR_LAST
  304. };
  305. /**
  306. * fm10k_msg_lport_state_vf - Message handler for lport_state message from PF
  307. * @hw: Pointer to hardware structure
  308. * @results: pointer array containing parsed data
  309. * @mbx: Pointer to mailbox information structure
  310. *
  311. * This handler is meant to capture the indication from the PF that we
  312. * are ready to bring up the interface.
  313. **/
  314. s32 fm10k_msg_lport_state_vf(struct fm10k_hw *hw, u32 **results,
  315. struct fm10k_mbx_info *mbx)
  316. {
  317. hw->mac.dglort_map = !results[FM10K_LPORT_STATE_MSG_READY] ?
  318. FM10K_DGLORTMAP_NONE : FM10K_DGLORTMAP_ZERO;
  319. return 0;
  320. }
  321. /**
  322. * fm10k_update_lport_state_vf - Update device state in lower device
  323. * @hw: pointer to the HW structure
  324. * @glort: unused
  325. * @count: number of logical ports to enable - unused (always 1)
  326. * @enable: boolean value indicating if this is an enable or disable request
  327. *
  328. * Notify the lower device of a state change. If the lower device is
  329. * enabled we can add filters, if it is disabled all filters for this
  330. * logical port are flushed.
  331. **/
  332. static s32 fm10k_update_lport_state_vf(struct fm10k_hw *hw, u16 glort,
  333. u16 count, bool enable)
  334. {
  335. struct fm10k_mbx_info *mbx = &hw->mbx;
  336. u32 msg[2];
  337. /* reset glort mask 0 as we have to wait to be enabled */
  338. hw->mac.dglort_map = FM10K_DGLORTMAP_NONE;
  339. /* generate port state request */
  340. fm10k_tlv_msg_init(msg, FM10K_VF_MSG_ID_LPORT_STATE);
  341. if (!enable)
  342. fm10k_tlv_attr_put_bool(msg, FM10K_LPORT_STATE_MSG_DISABLE);
  343. /* load onto outgoing mailbox */
  344. return mbx->ops.enqueue_tx(hw, mbx, msg);
  345. }
  346. /**
  347. * fm10k_update_xcast_mode_vf - Request update of multicast mode
  348. * @hw: pointer to hardware structure
  349. * @glort: unused
  350. * @mode: integer value indicating mode being requested
  351. *
  352. * This function will attempt to request a higher mode for the port
  353. * so that it can enable either multicast, multicast promiscuous, or
  354. * promiscuous mode of operation.
  355. **/
  356. static s32 fm10k_update_xcast_mode_vf(struct fm10k_hw *hw, u16 glort, u8 mode)
  357. {
  358. struct fm10k_mbx_info *mbx = &hw->mbx;
  359. u32 msg[3];
  360. if (mode > FM10K_XCAST_MODE_NONE)
  361. return FM10K_ERR_PARAM;
  362. /* generate message requesting to change xcast mode */
  363. fm10k_tlv_msg_init(msg, FM10K_VF_MSG_ID_LPORT_STATE);
  364. fm10k_tlv_attr_put_u8(msg, FM10K_LPORT_STATE_MSG_XCAST_MODE, mode);
  365. /* load onto outgoing mailbox */
  366. return mbx->ops.enqueue_tx(hw, mbx, msg);
  367. }
  368. /**
  369. * fm10k_update_hw_stats_vf - Updates hardware related statistics of VF
  370. * @hw: pointer to hardware structure
  371. * @stats: pointer to statistics structure
  372. *
  373. * This function collects and aggregates per queue hardware statistics.
  374. **/
  375. static void fm10k_update_hw_stats_vf(struct fm10k_hw *hw,
  376. struct fm10k_hw_stats *stats)
  377. {
  378. fm10k_update_hw_stats_q(hw, stats->q, 0, hw->mac.max_queues);
  379. }
  380. /**
  381. * fm10k_rebind_hw_stats_vf - Resets base for hardware statistics of VF
  382. * @hw: pointer to hardware structure
  383. * @stats: pointer to the stats structure to update
  384. *
  385. * This function resets the base for queue hardware statistics.
  386. **/
  387. static void fm10k_rebind_hw_stats_vf(struct fm10k_hw *hw,
  388. struct fm10k_hw_stats *stats)
  389. {
  390. /* Unbind Queue Statistics */
  391. fm10k_unbind_hw_stats_q(stats->q, 0, hw->mac.max_queues);
  392. /* Reinitialize bases for all stats */
  393. fm10k_update_hw_stats_vf(hw, stats);
  394. }
  395. /**
  396. * fm10k_configure_dglort_map_vf - Configures GLORT entry and queues
  397. * @hw: pointer to hardware structure
  398. * @dglort: pointer to dglort configuration structure
  399. *
  400. * Reads the configuration structure contained in dglort_cfg and uses
  401. * that information to then populate a DGLORTMAP/DEC entry and the queues
  402. * to which it has been assigned.
  403. **/
  404. static s32 fm10k_configure_dglort_map_vf(struct fm10k_hw *hw,
  405. struct fm10k_dglort_cfg *dglort)
  406. {
  407. /* verify the dglort pointer */
  408. if (!dglort)
  409. return FM10K_ERR_PARAM;
  410. /* stub for now until we determine correct message for this */
  411. return 0;
  412. }
  413. static const struct fm10k_msg_data fm10k_msg_data_vf[] = {
  414. FM10K_TLV_MSG_TEST_HANDLER(fm10k_tlv_msg_test),
  415. FM10K_VF_MSG_MAC_VLAN_HANDLER(fm10k_msg_mac_vlan_vf),
  416. FM10K_VF_MSG_LPORT_STATE_HANDLER(fm10k_msg_lport_state_vf),
  417. FM10K_TLV_MSG_ERROR_HANDLER(fm10k_tlv_msg_error),
  418. };
  419. static const struct fm10k_mac_ops mac_ops_vf = {
  420. .get_bus_info = fm10k_get_bus_info_generic,
  421. .reset_hw = fm10k_reset_hw_vf,
  422. .init_hw = fm10k_init_hw_vf,
  423. .start_hw = fm10k_start_hw_generic,
  424. .stop_hw = fm10k_stop_hw_vf,
  425. .update_vlan = fm10k_update_vlan_vf,
  426. .read_mac_addr = fm10k_read_mac_addr_vf,
  427. .update_uc_addr = fm10k_update_uc_addr_vf,
  428. .update_mc_addr = fm10k_update_mc_addr_vf,
  429. .update_xcast_mode = fm10k_update_xcast_mode_vf,
  430. .update_int_moderator = fm10k_update_int_moderator_vf,
  431. .update_lport_state = fm10k_update_lport_state_vf,
  432. .update_hw_stats = fm10k_update_hw_stats_vf,
  433. .rebind_hw_stats = fm10k_rebind_hw_stats_vf,
  434. .configure_dglort_map = fm10k_configure_dglort_map_vf,
  435. .get_host_state = fm10k_get_host_state_generic,
  436. };
  437. static s32 fm10k_get_invariants_vf(struct fm10k_hw *hw)
  438. {
  439. fm10k_get_invariants_generic(hw);
  440. return fm10k_pfvf_mbx_init(hw, &hw->mbx, fm10k_msg_data_vf, 0);
  441. }
  442. const struct fm10k_info fm10k_vf_info = {
  443. .mac = fm10k_mac_vf,
  444. .get_invariants = fm10k_get_invariants_vf,
  445. .mac_ops = &mac_ops_vf,
  446. };