fm10k_pf.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright(c) 2013 - 2018 Intel Corporation. */
  3. #include "fm10k_pf.h"
  4. #include "fm10k_vf.h"
  5. /**
  6. * fm10k_reset_hw_pf - PF hardware reset
  7. * @hw: pointer to hardware structure
  8. *
  9. * This function should return the hardware to a state similar to the
  10. * one it is in after being powered on.
  11. **/
  12. static s32 fm10k_reset_hw_pf(struct fm10k_hw *hw)
  13. {
  14. s32 err;
  15. u32 reg;
  16. u16 i;
  17. /* Disable interrupts */
  18. fm10k_write_reg(hw, FM10K_EIMR, FM10K_EIMR_DISABLE(ALL));
  19. /* Lock ITR2 reg 0 into itself and disable interrupt moderation */
  20. fm10k_write_reg(hw, FM10K_ITR2(0), 0);
  21. fm10k_write_reg(hw, FM10K_INT_CTRL, 0);
  22. /* We assume here Tx and Rx queue 0 are owned by the PF */
  23. /* Shut off VF access to their queues forcing them to queue 0 */
  24. for (i = 0; i < FM10K_TQMAP_TABLE_SIZE; i++) {
  25. fm10k_write_reg(hw, FM10K_TQMAP(i), 0);
  26. fm10k_write_reg(hw, FM10K_RQMAP(i), 0);
  27. }
  28. /* shut down all rings */
  29. err = fm10k_disable_queues_generic(hw, FM10K_MAX_QUEUES);
  30. if (err == FM10K_ERR_REQUESTS_PENDING) {
  31. hw->mac.reset_while_pending++;
  32. goto force_reset;
  33. } else if (err) {
  34. return err;
  35. }
  36. /* Verify that DMA is no longer active */
  37. reg = fm10k_read_reg(hw, FM10K_DMA_CTRL);
  38. if (reg & (FM10K_DMA_CTRL_TX_ACTIVE | FM10K_DMA_CTRL_RX_ACTIVE))
  39. return FM10K_ERR_DMA_PENDING;
  40. force_reset:
  41. /* Inititate data path reset */
  42. reg = FM10K_DMA_CTRL_DATAPATH_RESET;
  43. fm10k_write_reg(hw, FM10K_DMA_CTRL, reg);
  44. /* Flush write and allow 100us for reset to complete */
  45. fm10k_write_flush(hw);
  46. udelay(FM10K_RESET_TIMEOUT);
  47. /* Verify we made it out of reset */
  48. reg = fm10k_read_reg(hw, FM10K_IP);
  49. if (!(reg & FM10K_IP_NOTINRESET))
  50. return FM10K_ERR_RESET_FAILED;
  51. return 0;
  52. }
  53. /**
  54. * fm10k_is_ari_hierarchy_pf - Indicate ARI hierarchy support
  55. * @hw: pointer to hardware structure
  56. *
  57. * Looks at the ARI hierarchy bit to determine whether ARI is supported or not.
  58. **/
  59. static bool fm10k_is_ari_hierarchy_pf(struct fm10k_hw *hw)
  60. {
  61. u16 sriov_ctrl = fm10k_read_pci_cfg_word(hw, FM10K_PCIE_SRIOV_CTRL);
  62. return !!(sriov_ctrl & FM10K_PCIE_SRIOV_CTRL_VFARI);
  63. }
  64. /**
  65. * fm10k_init_hw_pf - PF hardware initialization
  66. * @hw: pointer to hardware structure
  67. *
  68. **/
  69. static s32 fm10k_init_hw_pf(struct fm10k_hw *hw)
  70. {
  71. u32 dma_ctrl, txqctl;
  72. u16 i;
  73. /* Establish default VSI as valid */
  74. fm10k_write_reg(hw, FM10K_DGLORTDEC(fm10k_dglort_default), 0);
  75. fm10k_write_reg(hw, FM10K_DGLORTMAP(fm10k_dglort_default),
  76. FM10K_DGLORTMAP_ANY);
  77. /* Invalidate all other GLORT entries */
  78. for (i = 1; i < FM10K_DGLORT_COUNT; i++)
  79. fm10k_write_reg(hw, FM10K_DGLORTMAP(i), FM10K_DGLORTMAP_NONE);
  80. /* reset ITR2(0) to point to itself */
  81. fm10k_write_reg(hw, FM10K_ITR2(0), 0);
  82. /* reset VF ITR2(0) to point to 0 avoid PF registers */
  83. fm10k_write_reg(hw, FM10K_ITR2(FM10K_ITR_REG_COUNT_PF), 0);
  84. /* loop through all PF ITR2 registers pointing them to the previous */
  85. for (i = 1; i < FM10K_ITR_REG_COUNT_PF; i++)
  86. fm10k_write_reg(hw, FM10K_ITR2(i), i - 1);
  87. /* Enable interrupt moderator if not already enabled */
  88. fm10k_write_reg(hw, FM10K_INT_CTRL, FM10K_INT_CTRL_ENABLEMODERATOR);
  89. /* compute the default txqctl configuration */
  90. txqctl = FM10K_TXQCTL_PF | FM10K_TXQCTL_UNLIMITED_BW |
  91. (hw->mac.default_vid << FM10K_TXQCTL_VID_SHIFT);
  92. for (i = 0; i < FM10K_MAX_QUEUES; i++) {
  93. /* configure rings for 256 Queue / 32 Descriptor cache mode */
  94. fm10k_write_reg(hw, FM10K_TQDLOC(i),
  95. (i * FM10K_TQDLOC_BASE_32_DESC) |
  96. FM10K_TQDLOC_SIZE_32_DESC);
  97. fm10k_write_reg(hw, FM10K_TXQCTL(i), txqctl);
  98. /* configure rings to provide TPH processing hints */
  99. fm10k_write_reg(hw, FM10K_TPH_TXCTRL(i),
  100. FM10K_TPH_TXCTRL_DESC_TPHEN |
  101. FM10K_TPH_TXCTRL_DESC_RROEN |
  102. FM10K_TPH_TXCTRL_DESC_WROEN |
  103. FM10K_TPH_TXCTRL_DATA_RROEN);
  104. fm10k_write_reg(hw, FM10K_TPH_RXCTRL(i),
  105. FM10K_TPH_RXCTRL_DESC_TPHEN |
  106. FM10K_TPH_RXCTRL_DESC_RROEN |
  107. FM10K_TPH_RXCTRL_DATA_WROEN |
  108. FM10K_TPH_RXCTRL_HDR_WROEN);
  109. }
  110. /* set max hold interval to align with 1.024 usec in all modes and
  111. * store ITR scale
  112. */
  113. switch (hw->bus.speed) {
  114. case fm10k_bus_speed_2500:
  115. dma_ctrl = FM10K_DMA_CTRL_MAX_HOLD_1US_GEN1;
  116. hw->mac.itr_scale = FM10K_TDLEN_ITR_SCALE_GEN1;
  117. break;
  118. case fm10k_bus_speed_5000:
  119. dma_ctrl = FM10K_DMA_CTRL_MAX_HOLD_1US_GEN2;
  120. hw->mac.itr_scale = FM10K_TDLEN_ITR_SCALE_GEN2;
  121. break;
  122. case fm10k_bus_speed_8000:
  123. dma_ctrl = FM10K_DMA_CTRL_MAX_HOLD_1US_GEN3;
  124. hw->mac.itr_scale = FM10K_TDLEN_ITR_SCALE_GEN3;
  125. break;
  126. default:
  127. dma_ctrl = 0;
  128. /* just in case, assume Gen3 ITR scale */
  129. hw->mac.itr_scale = FM10K_TDLEN_ITR_SCALE_GEN3;
  130. break;
  131. }
  132. /* Configure TSO flags */
  133. fm10k_write_reg(hw, FM10K_DTXTCPFLGL, FM10K_TSO_FLAGS_LOW);
  134. fm10k_write_reg(hw, FM10K_DTXTCPFLGH, FM10K_TSO_FLAGS_HI);
  135. /* Enable DMA engine
  136. * Set Rx Descriptor size to 32
  137. * Set Minimum MSS to 64
  138. * Set Maximum number of Rx queues to 256 / 32 Descriptor
  139. */
  140. dma_ctrl |= FM10K_DMA_CTRL_TX_ENABLE | FM10K_DMA_CTRL_RX_ENABLE |
  141. FM10K_DMA_CTRL_RX_DESC_SIZE | FM10K_DMA_CTRL_MINMSS_64 |
  142. FM10K_DMA_CTRL_32_DESC;
  143. fm10k_write_reg(hw, FM10K_DMA_CTRL, dma_ctrl);
  144. /* record maximum queue count, we limit ourselves to 128 */
  145. hw->mac.max_queues = FM10K_MAX_QUEUES_PF;
  146. /* We support either 64 VFs or 7 VFs depending on if we have ARI */
  147. hw->iov.total_vfs = fm10k_is_ari_hierarchy_pf(hw) ? 64 : 7;
  148. return 0;
  149. }
  150. /**
  151. * fm10k_update_vlan_pf - Update status of VLAN ID in VLAN filter table
  152. * @hw: pointer to hardware structure
  153. * @vid: VLAN ID to add to table
  154. * @vsi: Index indicating VF ID or PF ID in table
  155. * @set: Indicates if this is a set or clear operation
  156. *
  157. * This function adds or removes the corresponding VLAN ID from the VLAN
  158. * filter table for the corresponding function. In addition to the
  159. * standard set/clear that supports one bit a multi-bit write is
  160. * supported to set 64 bits at a time.
  161. **/
  162. static s32 fm10k_update_vlan_pf(struct fm10k_hw *hw, u32 vid, u8 vsi, bool set)
  163. {
  164. u32 vlan_table, reg, mask, bit, len;
  165. /* verify the VSI index is valid */
  166. if (vsi > FM10K_VLAN_TABLE_VSI_MAX)
  167. return FM10K_ERR_PARAM;
  168. /* VLAN multi-bit write:
  169. * The multi-bit write has several parts to it.
  170. * 24 16 8 0
  171. * 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0
  172. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  173. * | RSVD0 | Length |C|RSVD0| VLAN ID |
  174. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  175. *
  176. * VLAN ID: Vlan Starting value
  177. * RSVD0: Reserved section, must be 0
  178. * C: Flag field, 0 is set, 1 is clear (Used in VF VLAN message)
  179. * Length: Number of times to repeat the bit being set
  180. */
  181. len = vid >> 16;
  182. vid = (vid << 17) >> 17;
  183. /* verify the reserved 0 fields are 0 */
  184. if (len >= FM10K_VLAN_TABLE_VID_MAX || vid >= FM10K_VLAN_TABLE_VID_MAX)
  185. return FM10K_ERR_PARAM;
  186. /* Loop through the table updating all required VLANs */
  187. for (reg = FM10K_VLAN_TABLE(vsi, vid / 32), bit = vid % 32;
  188. len < FM10K_VLAN_TABLE_VID_MAX;
  189. len -= 32 - bit, reg++, bit = 0) {
  190. /* record the initial state of the register */
  191. vlan_table = fm10k_read_reg(hw, reg);
  192. /* truncate mask if we are at the start or end of the run */
  193. mask = (~(u32)0 >> ((len < 31) ? 31 - len : 0)) << bit;
  194. /* make necessary modifications to the register */
  195. mask &= set ? ~vlan_table : vlan_table;
  196. if (mask)
  197. fm10k_write_reg(hw, reg, vlan_table ^ mask);
  198. }
  199. return 0;
  200. }
  201. /**
  202. * fm10k_read_mac_addr_pf - Read device MAC address
  203. * @hw: pointer to the HW structure
  204. *
  205. * Reads the device MAC address from the SM_AREA and stores the value.
  206. **/
  207. static s32 fm10k_read_mac_addr_pf(struct fm10k_hw *hw)
  208. {
  209. u8 perm_addr[ETH_ALEN];
  210. u32 serial_num;
  211. serial_num = fm10k_read_reg(hw, FM10K_SM_AREA(1));
  212. /* last byte should be all 1's */
  213. if ((~serial_num) << 24)
  214. return FM10K_ERR_INVALID_MAC_ADDR;
  215. perm_addr[0] = (u8)(serial_num >> 24);
  216. perm_addr[1] = (u8)(serial_num >> 16);
  217. perm_addr[2] = (u8)(serial_num >> 8);
  218. serial_num = fm10k_read_reg(hw, FM10K_SM_AREA(0));
  219. /* first byte should be all 1's */
  220. if ((~serial_num) >> 24)
  221. return FM10K_ERR_INVALID_MAC_ADDR;
  222. perm_addr[3] = (u8)(serial_num >> 16);
  223. perm_addr[4] = (u8)(serial_num >> 8);
  224. perm_addr[5] = (u8)(serial_num);
  225. ether_addr_copy(hw->mac.perm_addr, perm_addr);
  226. ether_addr_copy(hw->mac.addr, perm_addr);
  227. return 0;
  228. }
  229. /**
  230. * fm10k_glort_valid_pf - Validate that the provided glort is valid
  231. * @hw: pointer to the HW structure
  232. * @glort: base glort to be validated
  233. *
  234. * This function will return an error if the provided glort is invalid
  235. **/
  236. bool fm10k_glort_valid_pf(struct fm10k_hw *hw, u16 glort)
  237. {
  238. glort &= hw->mac.dglort_map >> FM10K_DGLORTMAP_MASK_SHIFT;
  239. return glort == (hw->mac.dglort_map & FM10K_DGLORTMAP_NONE);
  240. }
  241. /**
  242. * fm10k_update_xc_addr_pf - Update device addresses
  243. * @hw: pointer to the HW structure
  244. * @glort: base resource tag for this request
  245. * @mac: MAC address to add/remove from table
  246. * @vid: VLAN ID to add/remove from table
  247. * @add: Indicates if this is an add or remove operation
  248. * @flags: flags field to indicate add and secure
  249. *
  250. * This function generates a message to the Switch API requesting
  251. * that the given logical port add/remove the given L2 MAC/VLAN address.
  252. **/
  253. static s32 fm10k_update_xc_addr_pf(struct fm10k_hw *hw, u16 glort,
  254. const u8 *mac, u16 vid, bool add, u8 flags)
  255. {
  256. struct fm10k_mbx_info *mbx = &hw->mbx;
  257. struct fm10k_mac_update mac_update;
  258. u32 msg[5];
  259. /* clear set bit from VLAN ID */
  260. vid &= ~FM10K_VLAN_CLEAR;
  261. /* if glort or VLAN are not valid return error */
  262. if (!fm10k_glort_valid_pf(hw, glort) || vid >= FM10K_VLAN_TABLE_VID_MAX)
  263. return FM10K_ERR_PARAM;
  264. /* record fields */
  265. mac_update.mac_lower = cpu_to_le32(((u32)mac[2] << 24) |
  266. ((u32)mac[3] << 16) |
  267. ((u32)mac[4] << 8) |
  268. ((u32)mac[5]));
  269. mac_update.mac_upper = cpu_to_le16(((u16)mac[0] << 8) |
  270. ((u16)mac[1]));
  271. mac_update.vlan = cpu_to_le16(vid);
  272. mac_update.glort = cpu_to_le16(glort);
  273. mac_update.action = add ? 0 : 1;
  274. mac_update.flags = flags;
  275. /* populate mac_update fields */
  276. fm10k_tlv_msg_init(msg, FM10K_PF_MSG_ID_UPDATE_MAC_FWD_RULE);
  277. fm10k_tlv_attr_put_le_struct(msg, FM10K_PF_ATTR_ID_MAC_UPDATE,
  278. &mac_update, sizeof(mac_update));
  279. /* load onto outgoing mailbox */
  280. return mbx->ops.enqueue_tx(hw, mbx, msg);
  281. }
  282. /**
  283. * fm10k_update_uc_addr_pf - Update device unicast addresses
  284. * @hw: pointer to the HW structure
  285. * @glort: base resource tag for this request
  286. * @mac: MAC address to add/remove from table
  287. * @vid: VLAN ID to add/remove from table
  288. * @add: Indicates if this is an add or remove operation
  289. * @flags: flags field to indicate add and secure
  290. *
  291. * This function is used to add or remove unicast addresses for
  292. * the PF.
  293. **/
  294. static s32 fm10k_update_uc_addr_pf(struct fm10k_hw *hw, u16 glort,
  295. const u8 *mac, u16 vid, bool add, u8 flags)
  296. {
  297. /* verify MAC address is valid */
  298. if (!is_valid_ether_addr(mac))
  299. return FM10K_ERR_PARAM;
  300. return fm10k_update_xc_addr_pf(hw, glort, mac, vid, add, flags);
  301. }
  302. /**
  303. * fm10k_update_mc_addr_pf - Update device multicast addresses
  304. * @hw: pointer to the HW structure
  305. * @glort: base resource tag for this request
  306. * @mac: MAC address to add/remove from table
  307. * @vid: VLAN ID to add/remove from table
  308. * @add: Indicates if this is an add or remove operation
  309. *
  310. * This function is used to add or remove multicast MAC addresses for
  311. * the PF.
  312. **/
  313. static s32 fm10k_update_mc_addr_pf(struct fm10k_hw *hw, u16 glort,
  314. const u8 *mac, u16 vid, bool add)
  315. {
  316. /* verify multicast address is valid */
  317. if (!is_multicast_ether_addr(mac))
  318. return FM10K_ERR_PARAM;
  319. return fm10k_update_xc_addr_pf(hw, glort, mac, vid, add, 0);
  320. }
  321. /**
  322. * fm10k_update_xcast_mode_pf - Request update of multicast mode
  323. * @hw: pointer to hardware structure
  324. * @glort: base resource tag for this request
  325. * @mode: integer value indicating mode being requested
  326. *
  327. * This function will attempt to request a higher mode for the port
  328. * so that it can enable either multicast, multicast promiscuous, or
  329. * promiscuous mode of operation.
  330. **/
  331. static s32 fm10k_update_xcast_mode_pf(struct fm10k_hw *hw, u16 glort, u8 mode)
  332. {
  333. struct fm10k_mbx_info *mbx = &hw->mbx;
  334. u32 msg[3], xcast_mode;
  335. if (mode > FM10K_XCAST_MODE_NONE)
  336. return FM10K_ERR_PARAM;
  337. /* if glort is not valid return error */
  338. if (!fm10k_glort_valid_pf(hw, glort))
  339. return FM10K_ERR_PARAM;
  340. /* write xcast mode as a single u32 value,
  341. * lower 16 bits: glort
  342. * upper 16 bits: mode
  343. */
  344. xcast_mode = ((u32)mode << 16) | glort;
  345. /* generate message requesting to change xcast mode */
  346. fm10k_tlv_msg_init(msg, FM10K_PF_MSG_ID_XCAST_MODES);
  347. fm10k_tlv_attr_put_u32(msg, FM10K_PF_ATTR_ID_XCAST_MODE, xcast_mode);
  348. /* load onto outgoing mailbox */
  349. return mbx->ops.enqueue_tx(hw, mbx, msg);
  350. }
  351. /**
  352. * fm10k_update_int_moderator_pf - Update interrupt moderator linked list
  353. * @hw: pointer to hardware structure
  354. *
  355. * This function walks through the MSI-X vector table to determine the
  356. * number of active interrupts and based on that information updates the
  357. * interrupt moderator linked list.
  358. **/
  359. static void fm10k_update_int_moderator_pf(struct fm10k_hw *hw)
  360. {
  361. u32 i;
  362. /* Disable interrupt moderator */
  363. fm10k_write_reg(hw, FM10K_INT_CTRL, 0);
  364. /* loop through PF from last to first looking enabled vectors */
  365. for (i = FM10K_ITR_REG_COUNT_PF - 1; i; i--) {
  366. if (!fm10k_read_reg(hw, FM10K_MSIX_VECTOR_MASK(i)))
  367. break;
  368. }
  369. /* always reset VFITR2[0] to point to last enabled PF vector */
  370. fm10k_write_reg(hw, FM10K_ITR2(FM10K_ITR_REG_COUNT_PF), i);
  371. /* reset ITR2[0] to point to last enabled PF vector */
  372. if (!hw->iov.num_vfs)
  373. fm10k_write_reg(hw, FM10K_ITR2(0), i);
  374. /* Enable interrupt moderator */
  375. fm10k_write_reg(hw, FM10K_INT_CTRL, FM10K_INT_CTRL_ENABLEMODERATOR);
  376. }
  377. /**
  378. * fm10k_update_lport_state_pf - Notify the switch of a change in port state
  379. * @hw: pointer to the HW structure
  380. * @glort: base resource tag for this request
  381. * @count: number of logical ports being updated
  382. * @enable: boolean value indicating enable or disable
  383. *
  384. * This function is used to add/remove a logical port from the switch.
  385. **/
  386. static s32 fm10k_update_lport_state_pf(struct fm10k_hw *hw, u16 glort,
  387. u16 count, bool enable)
  388. {
  389. struct fm10k_mbx_info *mbx = &hw->mbx;
  390. u32 msg[3], lport_msg;
  391. /* do nothing if we are being asked to create or destroy 0 ports */
  392. if (!count)
  393. return 0;
  394. /* if glort is not valid return error */
  395. if (!fm10k_glort_valid_pf(hw, glort))
  396. return FM10K_ERR_PARAM;
  397. /* reset multicast mode if deleting lport */
  398. if (!enable)
  399. fm10k_update_xcast_mode_pf(hw, glort, FM10K_XCAST_MODE_NONE);
  400. /* construct the lport message from the 2 pieces of data we have */
  401. lport_msg = ((u32)count << 16) | glort;
  402. /* generate lport create/delete message */
  403. fm10k_tlv_msg_init(msg, enable ? FM10K_PF_MSG_ID_LPORT_CREATE :
  404. FM10K_PF_MSG_ID_LPORT_DELETE);
  405. fm10k_tlv_attr_put_u32(msg, FM10K_PF_ATTR_ID_PORT, lport_msg);
  406. /* load onto outgoing mailbox */
  407. return mbx->ops.enqueue_tx(hw, mbx, msg);
  408. }
  409. /**
  410. * fm10k_configure_dglort_map_pf - Configures GLORT entry and queues
  411. * @hw: pointer to hardware structure
  412. * @dglort: pointer to dglort configuration structure
  413. *
  414. * Reads the configuration structure contained in dglort_cfg and uses
  415. * that information to then populate a DGLORTMAP/DEC entry and the queues
  416. * to which it has been assigned.
  417. **/
  418. static s32 fm10k_configure_dglort_map_pf(struct fm10k_hw *hw,
  419. struct fm10k_dglort_cfg *dglort)
  420. {
  421. u16 glort, queue_count, vsi_count, pc_count;
  422. u16 vsi, queue, pc, q_idx;
  423. u32 txqctl, dglortdec, dglortmap;
  424. /* verify the dglort pointer */
  425. if (!dglort)
  426. return FM10K_ERR_PARAM;
  427. /* verify the dglort values */
  428. if ((dglort->idx > 7) || (dglort->rss_l > 7) || (dglort->pc_l > 3) ||
  429. (dglort->vsi_l > 6) || (dglort->vsi_b > 64) ||
  430. (dglort->queue_l > 8) || (dglort->queue_b >= 256))
  431. return FM10K_ERR_PARAM;
  432. /* determine count of VSIs and queues */
  433. queue_count = BIT(dglort->rss_l + dglort->pc_l);
  434. vsi_count = BIT(dglort->vsi_l + dglort->queue_l);
  435. glort = dglort->glort;
  436. q_idx = dglort->queue_b;
  437. /* configure SGLORT for queues */
  438. for (vsi = 0; vsi < vsi_count; vsi++, glort++) {
  439. for (queue = 0; queue < queue_count; queue++, q_idx++) {
  440. if (q_idx >= FM10K_MAX_QUEUES)
  441. break;
  442. fm10k_write_reg(hw, FM10K_TX_SGLORT(q_idx), glort);
  443. fm10k_write_reg(hw, FM10K_RX_SGLORT(q_idx), glort);
  444. }
  445. }
  446. /* determine count of PCs and queues */
  447. queue_count = BIT(dglort->queue_l + dglort->rss_l + dglort->vsi_l);
  448. pc_count = BIT(dglort->pc_l);
  449. /* configure PC for Tx queues */
  450. for (pc = 0; pc < pc_count; pc++) {
  451. q_idx = pc + dglort->queue_b;
  452. for (queue = 0; queue < queue_count; queue++) {
  453. if (q_idx >= FM10K_MAX_QUEUES)
  454. break;
  455. txqctl = fm10k_read_reg(hw, FM10K_TXQCTL(q_idx));
  456. txqctl &= ~FM10K_TXQCTL_PC_MASK;
  457. txqctl |= pc << FM10K_TXQCTL_PC_SHIFT;
  458. fm10k_write_reg(hw, FM10K_TXQCTL(q_idx), txqctl);
  459. q_idx += pc_count;
  460. }
  461. }
  462. /* configure DGLORTDEC */
  463. dglortdec = ((u32)(dglort->rss_l) << FM10K_DGLORTDEC_RSSLENGTH_SHIFT) |
  464. ((u32)(dglort->queue_b) << FM10K_DGLORTDEC_QBASE_SHIFT) |
  465. ((u32)(dglort->pc_l) << FM10K_DGLORTDEC_PCLENGTH_SHIFT) |
  466. ((u32)(dglort->vsi_b) << FM10K_DGLORTDEC_VSIBASE_SHIFT) |
  467. ((u32)(dglort->vsi_l) << FM10K_DGLORTDEC_VSILENGTH_SHIFT) |
  468. ((u32)(dglort->queue_l));
  469. if (dglort->inner_rss)
  470. dglortdec |= FM10K_DGLORTDEC_INNERRSS_ENABLE;
  471. /* configure DGLORTMAP */
  472. dglortmap = (dglort->idx == fm10k_dglort_default) ?
  473. FM10K_DGLORTMAP_ANY : FM10K_DGLORTMAP_ZERO;
  474. dglortmap <<= dglort->vsi_l + dglort->queue_l + dglort->shared_l;
  475. dglortmap |= dglort->glort;
  476. /* write values to hardware */
  477. fm10k_write_reg(hw, FM10K_DGLORTDEC(dglort->idx), dglortdec);
  478. fm10k_write_reg(hw, FM10K_DGLORTMAP(dglort->idx), dglortmap);
  479. return 0;
  480. }
  481. u16 fm10k_queues_per_pool(struct fm10k_hw *hw)
  482. {
  483. u16 num_pools = hw->iov.num_pools;
  484. return (num_pools > 32) ? 2 : (num_pools > 16) ? 4 : (num_pools > 8) ?
  485. 8 : FM10K_MAX_QUEUES_POOL;
  486. }
  487. u16 fm10k_vf_queue_index(struct fm10k_hw *hw, u16 vf_idx)
  488. {
  489. u16 num_vfs = hw->iov.num_vfs;
  490. u16 vf_q_idx = FM10K_MAX_QUEUES;
  491. vf_q_idx -= fm10k_queues_per_pool(hw) * (num_vfs - vf_idx);
  492. return vf_q_idx;
  493. }
  494. static u16 fm10k_vectors_per_pool(struct fm10k_hw *hw)
  495. {
  496. u16 num_pools = hw->iov.num_pools;
  497. return (num_pools > 32) ? 8 : (num_pools > 16) ? 16 :
  498. FM10K_MAX_VECTORS_POOL;
  499. }
  500. static u16 fm10k_vf_vector_index(struct fm10k_hw *hw, u16 vf_idx)
  501. {
  502. u16 vf_v_idx = FM10K_MAX_VECTORS_PF;
  503. vf_v_idx += fm10k_vectors_per_pool(hw) * vf_idx;
  504. return vf_v_idx;
  505. }
  506. /**
  507. * fm10k_iov_assign_resources_pf - Assign pool resources for virtualization
  508. * @hw: pointer to the HW structure
  509. * @num_vfs: number of VFs to be allocated
  510. * @num_pools: number of virtualization pools to be allocated
  511. *
  512. * Allocates queues and traffic classes to virtualization entities to prepare
  513. * the PF for SR-IOV and VMDq
  514. **/
  515. static s32 fm10k_iov_assign_resources_pf(struct fm10k_hw *hw, u16 num_vfs,
  516. u16 num_pools)
  517. {
  518. u16 qmap_stride, qpp, vpp, vf_q_idx, vf_q_idx0, qmap_idx;
  519. u32 vid = hw->mac.default_vid << FM10K_TXQCTL_VID_SHIFT;
  520. int i, j;
  521. /* hardware only supports up to 64 pools */
  522. if (num_pools > 64)
  523. return FM10K_ERR_PARAM;
  524. /* the number of VFs cannot exceed the number of pools */
  525. if ((num_vfs > num_pools) || (num_vfs > hw->iov.total_vfs))
  526. return FM10K_ERR_PARAM;
  527. /* record number of virtualization entities */
  528. hw->iov.num_vfs = num_vfs;
  529. hw->iov.num_pools = num_pools;
  530. /* determine qmap offsets and counts */
  531. qmap_stride = (num_vfs > 8) ? 32 : 256;
  532. qpp = fm10k_queues_per_pool(hw);
  533. vpp = fm10k_vectors_per_pool(hw);
  534. /* calculate starting index for queues */
  535. vf_q_idx = fm10k_vf_queue_index(hw, 0);
  536. qmap_idx = 0;
  537. /* establish TCs with -1 credits and no quanta to prevent transmit */
  538. for (i = 0; i < num_vfs; i++) {
  539. fm10k_write_reg(hw, FM10K_TC_MAXCREDIT(i), 0);
  540. fm10k_write_reg(hw, FM10K_TC_RATE(i), 0);
  541. fm10k_write_reg(hw, FM10K_TC_CREDIT(i),
  542. FM10K_TC_CREDIT_CREDIT_MASK);
  543. }
  544. /* zero out all mbmem registers */
  545. for (i = FM10K_VFMBMEM_LEN * num_vfs; i--;)
  546. fm10k_write_reg(hw, FM10K_MBMEM(i), 0);
  547. /* clear event notification of VF FLR */
  548. fm10k_write_reg(hw, FM10K_PFVFLREC(0), ~0);
  549. fm10k_write_reg(hw, FM10K_PFVFLREC(1), ~0);
  550. /* loop through unallocated rings assigning them back to PF */
  551. for (i = FM10K_MAX_QUEUES_PF; i < vf_q_idx; i++) {
  552. fm10k_write_reg(hw, FM10K_TXDCTL(i), 0);
  553. fm10k_write_reg(hw, FM10K_TXQCTL(i), FM10K_TXQCTL_PF |
  554. FM10K_TXQCTL_UNLIMITED_BW | vid);
  555. fm10k_write_reg(hw, FM10K_RXQCTL(i), FM10K_RXQCTL_PF);
  556. }
  557. /* PF should have already updated VFITR2[0] */
  558. /* update all ITR registers to flow to VFITR2[0] */
  559. for (i = FM10K_ITR_REG_COUNT_PF + 1; i < FM10K_ITR_REG_COUNT; i++) {
  560. if (!(i & (vpp - 1)))
  561. fm10k_write_reg(hw, FM10K_ITR2(i), i - vpp);
  562. else
  563. fm10k_write_reg(hw, FM10K_ITR2(i), i - 1);
  564. }
  565. /* update PF ITR2[0] to reference the last vector */
  566. fm10k_write_reg(hw, FM10K_ITR2(0),
  567. fm10k_vf_vector_index(hw, num_vfs - 1));
  568. /* loop through rings populating rings and TCs */
  569. for (i = 0; i < num_vfs; i++) {
  570. /* record index for VF queue 0 for use in end of loop */
  571. vf_q_idx0 = vf_q_idx;
  572. for (j = 0; j < qpp; j++, qmap_idx++, vf_q_idx++) {
  573. /* assign VF and locked TC to queues */
  574. fm10k_write_reg(hw, FM10K_TXDCTL(vf_q_idx), 0);
  575. fm10k_write_reg(hw, FM10K_TXQCTL(vf_q_idx),
  576. (i << FM10K_TXQCTL_TC_SHIFT) | i |
  577. FM10K_TXQCTL_VF | vid);
  578. fm10k_write_reg(hw, FM10K_RXDCTL(vf_q_idx),
  579. FM10K_RXDCTL_WRITE_BACK_MIN_DELAY |
  580. FM10K_RXDCTL_DROP_ON_EMPTY);
  581. fm10k_write_reg(hw, FM10K_RXQCTL(vf_q_idx),
  582. (i << FM10K_RXQCTL_VF_SHIFT) |
  583. FM10K_RXQCTL_VF);
  584. /* map queue pair to VF */
  585. fm10k_write_reg(hw, FM10K_TQMAP(qmap_idx), vf_q_idx);
  586. fm10k_write_reg(hw, FM10K_RQMAP(qmap_idx), vf_q_idx);
  587. }
  588. /* repeat the first ring for all of the remaining VF rings */
  589. for (; j < qmap_stride; j++, qmap_idx++) {
  590. fm10k_write_reg(hw, FM10K_TQMAP(qmap_idx), vf_q_idx0);
  591. fm10k_write_reg(hw, FM10K_RQMAP(qmap_idx), vf_q_idx0);
  592. }
  593. }
  594. /* loop through remaining indexes assigning all to queue 0 */
  595. while (qmap_idx < FM10K_TQMAP_TABLE_SIZE) {
  596. fm10k_write_reg(hw, FM10K_TQMAP(qmap_idx), 0);
  597. fm10k_write_reg(hw, FM10K_RQMAP(qmap_idx), 0);
  598. qmap_idx++;
  599. }
  600. return 0;
  601. }
  602. /**
  603. * fm10k_iov_configure_tc_pf - Configure the shaping group for VF
  604. * @hw: pointer to the HW structure
  605. * @vf_idx: index of VF receiving GLORT
  606. * @rate: Rate indicated in Mb/s
  607. *
  608. * Configured the TC for a given VF to allow only up to a given number
  609. * of Mb/s of outgoing Tx throughput.
  610. **/
  611. static s32 fm10k_iov_configure_tc_pf(struct fm10k_hw *hw, u16 vf_idx, int rate)
  612. {
  613. /* configure defaults */
  614. u32 interval = FM10K_TC_RATE_INTERVAL_4US_GEN3;
  615. u32 tc_rate = FM10K_TC_RATE_QUANTA_MASK;
  616. /* verify vf is in range */
  617. if (vf_idx >= hw->iov.num_vfs)
  618. return FM10K_ERR_PARAM;
  619. /* set interval to align with 4.096 usec in all modes */
  620. switch (hw->bus.speed) {
  621. case fm10k_bus_speed_2500:
  622. interval = FM10K_TC_RATE_INTERVAL_4US_GEN1;
  623. break;
  624. case fm10k_bus_speed_5000:
  625. interval = FM10K_TC_RATE_INTERVAL_4US_GEN2;
  626. break;
  627. default:
  628. break;
  629. }
  630. if (rate) {
  631. if (rate > FM10K_VF_TC_MAX || rate < FM10K_VF_TC_MIN)
  632. return FM10K_ERR_PARAM;
  633. /* The quanta is measured in Bytes per 4.096 or 8.192 usec
  634. * The rate is provided in Mbits per second
  635. * To tralslate from rate to quanta we need to multiply the
  636. * rate by 8.192 usec and divide by 8 bits/byte. To avoid
  637. * dealing with floating point we can round the values up
  638. * to the nearest whole number ratio which gives us 128 / 125.
  639. */
  640. tc_rate = (rate * 128) / 125;
  641. /* try to keep the rate limiting accurate by increasing
  642. * the number of credits and interval for rates less than 4Gb/s
  643. */
  644. if (rate < 4000)
  645. interval <<= 1;
  646. else
  647. tc_rate >>= 1;
  648. }
  649. /* update rate limiter with new values */
  650. fm10k_write_reg(hw, FM10K_TC_RATE(vf_idx), tc_rate | interval);
  651. fm10k_write_reg(hw, FM10K_TC_MAXCREDIT(vf_idx), FM10K_TC_MAXCREDIT_64K);
  652. fm10k_write_reg(hw, FM10K_TC_CREDIT(vf_idx), FM10K_TC_MAXCREDIT_64K);
  653. return 0;
  654. }
  655. /**
  656. * fm10k_iov_assign_int_moderator_pf - Add VF interrupts to moderator list
  657. * @hw: pointer to the HW structure
  658. * @vf_idx: index of VF receiving GLORT
  659. *
  660. * Update the interrupt moderator linked list to include any MSI-X
  661. * interrupts which the VF has enabled in the MSI-X vector table.
  662. **/
  663. static s32 fm10k_iov_assign_int_moderator_pf(struct fm10k_hw *hw, u16 vf_idx)
  664. {
  665. u16 vf_v_idx, vf_v_limit, i;
  666. /* verify vf is in range */
  667. if (vf_idx >= hw->iov.num_vfs)
  668. return FM10K_ERR_PARAM;
  669. /* determine vector offset and count */
  670. vf_v_idx = fm10k_vf_vector_index(hw, vf_idx);
  671. vf_v_limit = vf_v_idx + fm10k_vectors_per_pool(hw);
  672. /* search for first vector that is not masked */
  673. for (i = vf_v_limit - 1; i > vf_v_idx; i--) {
  674. if (!fm10k_read_reg(hw, FM10K_MSIX_VECTOR_MASK(i)))
  675. break;
  676. }
  677. /* reset linked list so it now includes our active vectors */
  678. if (vf_idx == (hw->iov.num_vfs - 1))
  679. fm10k_write_reg(hw, FM10K_ITR2(0), i);
  680. else
  681. fm10k_write_reg(hw, FM10K_ITR2(vf_v_limit), i);
  682. return 0;
  683. }
  684. /**
  685. * fm10k_iov_assign_default_mac_vlan_pf - Assign a MAC and VLAN to VF
  686. * @hw: pointer to the HW structure
  687. * @vf_info: pointer to VF information structure
  688. *
  689. * Assign a MAC address and default VLAN to a VF and notify it of the update
  690. **/
  691. static s32 fm10k_iov_assign_default_mac_vlan_pf(struct fm10k_hw *hw,
  692. struct fm10k_vf_info *vf_info)
  693. {
  694. u16 qmap_stride, queues_per_pool, vf_q_idx, timeout, qmap_idx, i;
  695. u32 msg[4], txdctl, txqctl, tdbal = 0, tdbah = 0;
  696. s32 err = 0;
  697. u16 vf_idx, vf_vid;
  698. /* verify vf is in range */
  699. if (!vf_info || vf_info->vf_idx >= hw->iov.num_vfs)
  700. return FM10K_ERR_PARAM;
  701. /* determine qmap offsets and counts */
  702. qmap_stride = (hw->iov.num_vfs > 8) ? 32 : 256;
  703. queues_per_pool = fm10k_queues_per_pool(hw);
  704. /* calculate starting index for queues */
  705. vf_idx = vf_info->vf_idx;
  706. vf_q_idx = fm10k_vf_queue_index(hw, vf_idx);
  707. qmap_idx = qmap_stride * vf_idx;
  708. /* Determine correct default VLAN ID. The FM10K_VLAN_OVERRIDE bit is
  709. * used here to indicate to the VF that it will not have privilege to
  710. * write VLAN_TABLE. All policy is enforced on the PF but this allows
  711. * the VF to correctly report errors to userspace requests.
  712. */
  713. if (vf_info->pf_vid)
  714. vf_vid = vf_info->pf_vid | FM10K_VLAN_OVERRIDE;
  715. else
  716. vf_vid = vf_info->sw_vid;
  717. /* generate MAC_ADDR request */
  718. fm10k_tlv_msg_init(msg, FM10K_VF_MSG_ID_MAC_VLAN);
  719. fm10k_tlv_attr_put_mac_vlan(msg, FM10K_MAC_VLAN_MSG_DEFAULT_MAC,
  720. vf_info->mac, vf_vid);
  721. /* Configure Queue control register with new VLAN ID. The TXQCTL
  722. * register is RO from the VF, so the PF must do this even in the
  723. * case of notifying the VF of a new VID via the mailbox.
  724. */
  725. txqctl = ((u32)vf_vid << FM10K_TXQCTL_VID_SHIFT) &
  726. FM10K_TXQCTL_VID_MASK;
  727. txqctl |= (vf_idx << FM10K_TXQCTL_TC_SHIFT) |
  728. FM10K_TXQCTL_VF | vf_idx;
  729. for (i = 0; i < queues_per_pool; i++)
  730. fm10k_write_reg(hw, FM10K_TXQCTL(vf_q_idx + i), txqctl);
  731. /* try loading a message onto outgoing mailbox first */
  732. if (vf_info->mbx.ops.enqueue_tx) {
  733. err = vf_info->mbx.ops.enqueue_tx(hw, &vf_info->mbx, msg);
  734. if (err != FM10K_MBX_ERR_NO_MBX)
  735. return err;
  736. err = 0;
  737. }
  738. /* If we aren't connected to a mailbox, this is most likely because
  739. * the VF driver is not running. It should thus be safe to re-map
  740. * queues and use the registers to pass the MAC address so that the VF
  741. * driver gets correct information during its initialization.
  742. */
  743. /* MAP Tx queue back to 0 temporarily, and disable it */
  744. fm10k_write_reg(hw, FM10K_TQMAP(qmap_idx), 0);
  745. fm10k_write_reg(hw, FM10K_TXDCTL(vf_q_idx), 0);
  746. /* verify ring has disabled before modifying base address registers */
  747. txdctl = fm10k_read_reg(hw, FM10K_TXDCTL(vf_q_idx));
  748. for (timeout = 0; txdctl & FM10K_TXDCTL_ENABLE; timeout++) {
  749. /* limit ourselves to a 1ms timeout */
  750. if (timeout == 10) {
  751. err = FM10K_ERR_DMA_PENDING;
  752. goto err_out;
  753. }
  754. usleep_range(100, 200);
  755. txdctl = fm10k_read_reg(hw, FM10K_TXDCTL(vf_q_idx));
  756. }
  757. /* Update base address registers to contain MAC address */
  758. if (is_valid_ether_addr(vf_info->mac)) {
  759. tdbal = (((u32)vf_info->mac[3]) << 24) |
  760. (((u32)vf_info->mac[4]) << 16) |
  761. (((u32)vf_info->mac[5]) << 8);
  762. tdbah = (((u32)0xFF) << 24) |
  763. (((u32)vf_info->mac[0]) << 16) |
  764. (((u32)vf_info->mac[1]) << 8) |
  765. ((u32)vf_info->mac[2]);
  766. }
  767. /* Record the base address into queue 0 */
  768. fm10k_write_reg(hw, FM10K_TDBAL(vf_q_idx), tdbal);
  769. fm10k_write_reg(hw, FM10K_TDBAH(vf_q_idx), tdbah);
  770. /* Provide the VF the ITR scale, using software-defined fields in TDLEN
  771. * to pass the information during VF initialization. See definition of
  772. * FM10K_TDLEN_ITR_SCALE_SHIFT for more details.
  773. */
  774. fm10k_write_reg(hw, FM10K_TDLEN(vf_q_idx), hw->mac.itr_scale <<
  775. FM10K_TDLEN_ITR_SCALE_SHIFT);
  776. err_out:
  777. /* restore the queue back to VF ownership */
  778. fm10k_write_reg(hw, FM10K_TQMAP(qmap_idx), vf_q_idx);
  779. return err;
  780. }
  781. /**
  782. * fm10k_iov_reset_resources_pf - Reassign queues and interrupts to a VF
  783. * @hw: pointer to the HW structure
  784. * @vf_info: pointer to VF information structure
  785. *
  786. * Reassign the interrupts and queues to a VF following an FLR
  787. **/
  788. static s32 fm10k_iov_reset_resources_pf(struct fm10k_hw *hw,
  789. struct fm10k_vf_info *vf_info)
  790. {
  791. u16 qmap_stride, queues_per_pool, vf_q_idx, qmap_idx;
  792. u32 tdbal = 0, tdbah = 0, txqctl, rxqctl;
  793. u16 vf_v_idx, vf_v_limit, vf_vid;
  794. u8 vf_idx = vf_info->vf_idx;
  795. int i;
  796. /* verify vf is in range */
  797. if (vf_idx >= hw->iov.num_vfs)
  798. return FM10K_ERR_PARAM;
  799. /* clear event notification of VF FLR */
  800. fm10k_write_reg(hw, FM10K_PFVFLREC(vf_idx / 32), BIT(vf_idx % 32));
  801. /* force timeout and then disconnect the mailbox */
  802. vf_info->mbx.timeout = 0;
  803. if (vf_info->mbx.ops.disconnect)
  804. vf_info->mbx.ops.disconnect(hw, &vf_info->mbx);
  805. /* determine vector offset and count */
  806. vf_v_idx = fm10k_vf_vector_index(hw, vf_idx);
  807. vf_v_limit = vf_v_idx + fm10k_vectors_per_pool(hw);
  808. /* determine qmap offsets and counts */
  809. qmap_stride = (hw->iov.num_vfs > 8) ? 32 : 256;
  810. queues_per_pool = fm10k_queues_per_pool(hw);
  811. qmap_idx = qmap_stride * vf_idx;
  812. /* make all the queues inaccessible to the VF */
  813. for (i = qmap_idx; i < (qmap_idx + qmap_stride); i++) {
  814. fm10k_write_reg(hw, FM10K_TQMAP(i), 0);
  815. fm10k_write_reg(hw, FM10K_RQMAP(i), 0);
  816. }
  817. /* calculate starting index for queues */
  818. vf_q_idx = fm10k_vf_queue_index(hw, vf_idx);
  819. /* determine correct default VLAN ID */
  820. if (vf_info->pf_vid)
  821. vf_vid = vf_info->pf_vid;
  822. else
  823. vf_vid = vf_info->sw_vid;
  824. /* configure Queue control register */
  825. txqctl = ((u32)vf_vid << FM10K_TXQCTL_VID_SHIFT) |
  826. (vf_idx << FM10K_TXQCTL_TC_SHIFT) |
  827. FM10K_TXQCTL_VF | vf_idx;
  828. rxqctl = (vf_idx << FM10K_RXQCTL_VF_SHIFT) | FM10K_RXQCTL_VF;
  829. /* stop further DMA and reset queue ownership back to VF */
  830. for (i = vf_q_idx; i < (queues_per_pool + vf_q_idx); i++) {
  831. fm10k_write_reg(hw, FM10K_TXDCTL(i), 0);
  832. fm10k_write_reg(hw, FM10K_TXQCTL(i), txqctl);
  833. fm10k_write_reg(hw, FM10K_RXDCTL(i),
  834. FM10K_RXDCTL_WRITE_BACK_MIN_DELAY |
  835. FM10K_RXDCTL_DROP_ON_EMPTY);
  836. fm10k_write_reg(hw, FM10K_RXQCTL(i), rxqctl);
  837. }
  838. /* reset TC with -1 credits and no quanta to prevent transmit */
  839. fm10k_write_reg(hw, FM10K_TC_MAXCREDIT(vf_idx), 0);
  840. fm10k_write_reg(hw, FM10K_TC_RATE(vf_idx), 0);
  841. fm10k_write_reg(hw, FM10K_TC_CREDIT(vf_idx),
  842. FM10K_TC_CREDIT_CREDIT_MASK);
  843. /* update our first entry in the table based on previous VF */
  844. if (!vf_idx)
  845. hw->mac.ops.update_int_moderator(hw);
  846. else
  847. hw->iov.ops.assign_int_moderator(hw, vf_idx - 1);
  848. /* reset linked list so it now includes our active vectors */
  849. if (vf_idx == (hw->iov.num_vfs - 1))
  850. fm10k_write_reg(hw, FM10K_ITR2(0), vf_v_idx);
  851. else
  852. fm10k_write_reg(hw, FM10K_ITR2(vf_v_limit), vf_v_idx);
  853. /* link remaining vectors so that next points to previous */
  854. for (vf_v_idx++; vf_v_idx < vf_v_limit; vf_v_idx++)
  855. fm10k_write_reg(hw, FM10K_ITR2(vf_v_idx), vf_v_idx - 1);
  856. /* zero out MBMEM, VLAN_TABLE, RETA, RSSRK, and MRQC registers */
  857. for (i = FM10K_VFMBMEM_LEN; i--;)
  858. fm10k_write_reg(hw, FM10K_MBMEM_VF(vf_idx, i), 0);
  859. for (i = FM10K_VLAN_TABLE_SIZE; i--;)
  860. fm10k_write_reg(hw, FM10K_VLAN_TABLE(vf_info->vsi, i), 0);
  861. for (i = FM10K_RETA_SIZE; i--;)
  862. fm10k_write_reg(hw, FM10K_RETA(vf_info->vsi, i), 0);
  863. for (i = FM10K_RSSRK_SIZE; i--;)
  864. fm10k_write_reg(hw, FM10K_RSSRK(vf_info->vsi, i), 0);
  865. fm10k_write_reg(hw, FM10K_MRQC(vf_info->vsi), 0);
  866. /* Update base address registers to contain MAC address */
  867. if (is_valid_ether_addr(vf_info->mac)) {
  868. tdbal = (((u32)vf_info->mac[3]) << 24) |
  869. (((u32)vf_info->mac[4]) << 16) |
  870. (((u32)vf_info->mac[5]) << 8);
  871. tdbah = (((u32)0xFF) << 24) |
  872. (((u32)vf_info->mac[0]) << 16) |
  873. (((u32)vf_info->mac[1]) << 8) |
  874. ((u32)vf_info->mac[2]);
  875. }
  876. /* map queue pairs back to VF from last to first */
  877. for (i = queues_per_pool; i--;) {
  878. fm10k_write_reg(hw, FM10K_TDBAL(vf_q_idx + i), tdbal);
  879. fm10k_write_reg(hw, FM10K_TDBAH(vf_q_idx + i), tdbah);
  880. /* See definition of FM10K_TDLEN_ITR_SCALE_SHIFT for an
  881. * explanation of how TDLEN is used.
  882. */
  883. fm10k_write_reg(hw, FM10K_TDLEN(vf_q_idx + i),
  884. hw->mac.itr_scale <<
  885. FM10K_TDLEN_ITR_SCALE_SHIFT);
  886. fm10k_write_reg(hw, FM10K_TQMAP(qmap_idx + i), vf_q_idx + i);
  887. fm10k_write_reg(hw, FM10K_RQMAP(qmap_idx + i), vf_q_idx + i);
  888. }
  889. /* repeat the first ring for all the remaining VF rings */
  890. for (i = queues_per_pool; i < qmap_stride; i++) {
  891. fm10k_write_reg(hw, FM10K_TQMAP(qmap_idx + i), vf_q_idx);
  892. fm10k_write_reg(hw, FM10K_RQMAP(qmap_idx + i), vf_q_idx);
  893. }
  894. return 0;
  895. }
  896. /**
  897. * fm10k_iov_set_lport_pf - Assign and enable a logical port for a given VF
  898. * @hw: pointer to hardware structure
  899. * @vf_info: pointer to VF information structure
  900. * @lport_idx: Logical port offset from the hardware glort
  901. * @flags: Set of capability flags to extend port beyond basic functionality
  902. *
  903. * This function allows enabling a VF port by assigning it a GLORT and
  904. * setting the flags so that it can enable an Rx mode.
  905. **/
  906. static s32 fm10k_iov_set_lport_pf(struct fm10k_hw *hw,
  907. struct fm10k_vf_info *vf_info,
  908. u16 lport_idx, u8 flags)
  909. {
  910. u16 glort = (hw->mac.dglort_map + lport_idx) & FM10K_DGLORTMAP_NONE;
  911. /* if glort is not valid return error */
  912. if (!fm10k_glort_valid_pf(hw, glort))
  913. return FM10K_ERR_PARAM;
  914. vf_info->vf_flags = flags | FM10K_VF_FLAG_NONE_CAPABLE;
  915. vf_info->glort = glort;
  916. return 0;
  917. }
  918. /**
  919. * fm10k_iov_reset_lport_pf - Disable a logical port for a given VF
  920. * @hw: pointer to hardware structure
  921. * @vf_info: pointer to VF information structure
  922. *
  923. * This function disables a VF port by stripping it of a GLORT and
  924. * setting the flags so that it cannot enable any Rx mode.
  925. **/
  926. static void fm10k_iov_reset_lport_pf(struct fm10k_hw *hw,
  927. struct fm10k_vf_info *vf_info)
  928. {
  929. u32 msg[1];
  930. /* need to disable the port if it is already enabled */
  931. if (FM10K_VF_FLAG_ENABLED(vf_info)) {
  932. /* notify switch that this port has been disabled */
  933. fm10k_update_lport_state_pf(hw, vf_info->glort, 1, false);
  934. /* generate port state response to notify VF it is not ready */
  935. fm10k_tlv_msg_init(msg, FM10K_VF_MSG_ID_LPORT_STATE);
  936. vf_info->mbx.ops.enqueue_tx(hw, &vf_info->mbx, msg);
  937. }
  938. /* clear flags and glort if it exists */
  939. vf_info->vf_flags = 0;
  940. vf_info->glort = 0;
  941. }
  942. /**
  943. * fm10k_iov_update_stats_pf - Updates hardware related statistics for VFs
  944. * @hw: pointer to hardware structure
  945. * @q: stats for all queues of a VF
  946. * @vf_idx: index of VF
  947. *
  948. * This function collects queue stats for VFs.
  949. **/
  950. static void fm10k_iov_update_stats_pf(struct fm10k_hw *hw,
  951. struct fm10k_hw_stats_q *q,
  952. u16 vf_idx)
  953. {
  954. u32 idx, qpp;
  955. /* get stats for all of the queues */
  956. qpp = fm10k_queues_per_pool(hw);
  957. idx = fm10k_vf_queue_index(hw, vf_idx);
  958. fm10k_update_hw_stats_q(hw, q, idx, qpp);
  959. }
  960. /**
  961. * fm10k_iov_msg_msix_pf - Message handler for MSI-X request from VF
  962. * @hw: Pointer to hardware structure
  963. * @results: Pointer array to message, results[0] is pointer to message
  964. * @mbx: Pointer to mailbox information structure
  965. *
  966. * This function is a default handler for MSI-X requests from the VF. The
  967. * assumption is that in this case it is acceptable to just directly
  968. * hand off the message from the VF to the underlying shared code.
  969. **/
  970. s32 fm10k_iov_msg_msix_pf(struct fm10k_hw *hw, u32 **results,
  971. struct fm10k_mbx_info *mbx)
  972. {
  973. struct fm10k_vf_info *vf_info = (struct fm10k_vf_info *)mbx;
  974. u8 vf_idx = vf_info->vf_idx;
  975. return hw->iov.ops.assign_int_moderator(hw, vf_idx);
  976. }
  977. /**
  978. * fm10k_iov_select_vid - Select correct default VLAN ID
  979. * @vf_info: pointer to VF information structure
  980. * @vid: VLAN ID to correct
  981. *
  982. * Will report an error if the VLAN ID is out of range. For VID = 0, it will
  983. * return either the pf_vid or sw_vid depending on which one is set.
  984. */
  985. s32 fm10k_iov_select_vid(struct fm10k_vf_info *vf_info, u16 vid)
  986. {
  987. if (!vid)
  988. return vf_info->pf_vid ? vf_info->pf_vid : vf_info->sw_vid;
  989. else if (vf_info->pf_vid && vid != vf_info->pf_vid)
  990. return FM10K_ERR_PARAM;
  991. else
  992. return vid;
  993. }
  994. /**
  995. * fm10k_iov_msg_mac_vlan_pf - Message handler for MAC/VLAN request from VF
  996. * @hw: Pointer to hardware structure
  997. * @results: Pointer array to message, results[0] is pointer to message
  998. * @mbx: Pointer to mailbox information structure
  999. *
  1000. * This function is a default handler for MAC/VLAN requests from the VF.
  1001. * The assumption is that in this case it is acceptable to just directly
  1002. * hand off the message from the VF to the underlying shared code.
  1003. **/
  1004. s32 fm10k_iov_msg_mac_vlan_pf(struct fm10k_hw *hw, u32 **results,
  1005. struct fm10k_mbx_info *mbx)
  1006. {
  1007. struct fm10k_vf_info *vf_info = (struct fm10k_vf_info *)mbx;
  1008. u8 mac[ETH_ALEN];
  1009. u32 *result;
  1010. int err = 0;
  1011. bool set;
  1012. u16 vlan;
  1013. u32 vid;
  1014. /* we shouldn't be updating rules on a disabled interface */
  1015. if (!FM10K_VF_FLAG_ENABLED(vf_info))
  1016. err = FM10K_ERR_PARAM;
  1017. if (!err && !!results[FM10K_MAC_VLAN_MSG_VLAN]) {
  1018. result = results[FM10K_MAC_VLAN_MSG_VLAN];
  1019. /* record VLAN id requested */
  1020. err = fm10k_tlv_attr_get_u32(result, &vid);
  1021. if (err)
  1022. return err;
  1023. set = !(vid & FM10K_VLAN_CLEAR);
  1024. vid &= ~FM10K_VLAN_CLEAR;
  1025. /* if the length field has been set, this is a multi-bit
  1026. * update request. For multi-bit requests, simply disallow
  1027. * them when the pf_vid has been set. In this case, the PF
  1028. * should have already cleared the VLAN_TABLE, and if we
  1029. * allowed them, it could allow a rogue VF to receive traffic
  1030. * on a VLAN it was not assigned. In the single-bit case, we
  1031. * need to modify requests for VLAN 0 to use the default PF or
  1032. * SW vid when assigned.
  1033. */
  1034. if (vid >> 16) {
  1035. /* prevent multi-bit requests when PF has
  1036. * administratively set the VLAN for this VF
  1037. */
  1038. if (vf_info->pf_vid)
  1039. return FM10K_ERR_PARAM;
  1040. } else {
  1041. err = fm10k_iov_select_vid(vf_info, (u16)vid);
  1042. if (err < 0)
  1043. return err;
  1044. vid = err;
  1045. }
  1046. /* update VSI info for VF in regards to VLAN table */
  1047. err = hw->mac.ops.update_vlan(hw, vid, vf_info->vsi, set);
  1048. }
  1049. if (!err && !!results[FM10K_MAC_VLAN_MSG_MAC]) {
  1050. result = results[FM10K_MAC_VLAN_MSG_MAC];
  1051. /* record unicast MAC address requested */
  1052. err = fm10k_tlv_attr_get_mac_vlan(result, mac, &vlan);
  1053. if (err)
  1054. return err;
  1055. /* block attempts to set MAC for a locked device */
  1056. if (is_valid_ether_addr(vf_info->mac) &&
  1057. !ether_addr_equal(mac, vf_info->mac))
  1058. return FM10K_ERR_PARAM;
  1059. set = !(vlan & FM10K_VLAN_CLEAR);
  1060. vlan &= ~FM10K_VLAN_CLEAR;
  1061. err = fm10k_iov_select_vid(vf_info, vlan);
  1062. if (err < 0)
  1063. return err;
  1064. vlan = (u16)err;
  1065. /* notify switch of request for new unicast address */
  1066. err = hw->mac.ops.update_uc_addr(hw, vf_info->glort,
  1067. mac, vlan, set, 0);
  1068. }
  1069. if (!err && !!results[FM10K_MAC_VLAN_MSG_MULTICAST]) {
  1070. result = results[FM10K_MAC_VLAN_MSG_MULTICAST];
  1071. /* record multicast MAC address requested */
  1072. err = fm10k_tlv_attr_get_mac_vlan(result, mac, &vlan);
  1073. if (err)
  1074. return err;
  1075. /* verify that the VF is allowed to request multicast */
  1076. if (!(vf_info->vf_flags & FM10K_VF_FLAG_MULTI_ENABLED))
  1077. return FM10K_ERR_PARAM;
  1078. set = !(vlan & FM10K_VLAN_CLEAR);
  1079. vlan &= ~FM10K_VLAN_CLEAR;
  1080. err = fm10k_iov_select_vid(vf_info, vlan);
  1081. if (err < 0)
  1082. return err;
  1083. vlan = (u16)err;
  1084. /* notify switch of request for new multicast address */
  1085. err = hw->mac.ops.update_mc_addr(hw, vf_info->glort,
  1086. mac, vlan, set);
  1087. }
  1088. return err;
  1089. }
  1090. /**
  1091. * fm10k_iov_supported_xcast_mode_pf - Determine best match for xcast mode
  1092. * @vf_info: VF info structure containing capability flags
  1093. * @mode: Requested xcast mode
  1094. *
  1095. * This function outputs the mode that most closely matches the requested
  1096. * mode. If not modes match it will request we disable the port
  1097. **/
  1098. static u8 fm10k_iov_supported_xcast_mode_pf(struct fm10k_vf_info *vf_info,
  1099. u8 mode)
  1100. {
  1101. u8 vf_flags = vf_info->vf_flags;
  1102. /* match up mode to capabilities as best as possible */
  1103. switch (mode) {
  1104. case FM10K_XCAST_MODE_PROMISC:
  1105. if (vf_flags & FM10K_VF_FLAG_PROMISC_CAPABLE)
  1106. return FM10K_XCAST_MODE_PROMISC;
  1107. /* fall through */
  1108. case FM10K_XCAST_MODE_ALLMULTI:
  1109. if (vf_flags & FM10K_VF_FLAG_ALLMULTI_CAPABLE)
  1110. return FM10K_XCAST_MODE_ALLMULTI;
  1111. /* fall through */
  1112. case FM10K_XCAST_MODE_MULTI:
  1113. if (vf_flags & FM10K_VF_FLAG_MULTI_CAPABLE)
  1114. return FM10K_XCAST_MODE_MULTI;
  1115. /* fall through */
  1116. case FM10K_XCAST_MODE_NONE:
  1117. if (vf_flags & FM10K_VF_FLAG_NONE_CAPABLE)
  1118. return FM10K_XCAST_MODE_NONE;
  1119. /* fall through */
  1120. default:
  1121. break;
  1122. }
  1123. /* disable interface as it should not be able to request any */
  1124. return FM10K_XCAST_MODE_DISABLE;
  1125. }
  1126. /**
  1127. * fm10k_iov_msg_lport_state_pf - Message handler for port state requests
  1128. * @hw: Pointer to hardware structure
  1129. * @results: Pointer array to message, results[0] is pointer to message
  1130. * @mbx: Pointer to mailbox information structure
  1131. *
  1132. * This function is a default handler for port state requests. The port
  1133. * state requests for now are basic and consist of enabling or disabling
  1134. * the port.
  1135. **/
  1136. s32 fm10k_iov_msg_lport_state_pf(struct fm10k_hw *hw, u32 **results,
  1137. struct fm10k_mbx_info *mbx)
  1138. {
  1139. struct fm10k_vf_info *vf_info = (struct fm10k_vf_info *)mbx;
  1140. u32 *result;
  1141. s32 err = 0;
  1142. u32 msg[2];
  1143. u8 mode = 0;
  1144. /* verify VF is allowed to enable even minimal mode */
  1145. if (!(vf_info->vf_flags & FM10K_VF_FLAG_NONE_CAPABLE))
  1146. return FM10K_ERR_PARAM;
  1147. if (!!results[FM10K_LPORT_STATE_MSG_XCAST_MODE]) {
  1148. result = results[FM10K_LPORT_STATE_MSG_XCAST_MODE];
  1149. /* XCAST mode update requested */
  1150. err = fm10k_tlv_attr_get_u8(result, &mode);
  1151. if (err)
  1152. return FM10K_ERR_PARAM;
  1153. /* prep for possible demotion depending on capabilities */
  1154. mode = fm10k_iov_supported_xcast_mode_pf(vf_info, mode);
  1155. /* if mode is not currently enabled, enable it */
  1156. if (!(FM10K_VF_FLAG_ENABLED(vf_info) & BIT(mode)))
  1157. fm10k_update_xcast_mode_pf(hw, vf_info->glort, mode);
  1158. /* swap mode back to a bit flag */
  1159. mode = FM10K_VF_FLAG_SET_MODE(mode);
  1160. } else if (!results[FM10K_LPORT_STATE_MSG_DISABLE]) {
  1161. /* need to disable the port if it is already enabled */
  1162. if (FM10K_VF_FLAG_ENABLED(vf_info))
  1163. err = fm10k_update_lport_state_pf(hw, vf_info->glort,
  1164. 1, false);
  1165. /* we need to clear VF_FLAG_ENABLED flags in order to ensure
  1166. * that we actually re-enable the LPORT state below. Note that
  1167. * this has no impact if the VF is already disabled, as the
  1168. * flags are already cleared.
  1169. */
  1170. if (!err)
  1171. vf_info->vf_flags = FM10K_VF_FLAG_CAPABLE(vf_info);
  1172. /* when enabling the port we should reset the rate limiters */
  1173. hw->iov.ops.configure_tc(hw, vf_info->vf_idx, vf_info->rate);
  1174. /* set mode for minimal functionality */
  1175. mode = FM10K_VF_FLAG_SET_MODE_NONE;
  1176. /* generate port state response to notify VF it is ready */
  1177. fm10k_tlv_msg_init(msg, FM10K_VF_MSG_ID_LPORT_STATE);
  1178. fm10k_tlv_attr_put_bool(msg, FM10K_LPORT_STATE_MSG_READY);
  1179. mbx->ops.enqueue_tx(hw, mbx, msg);
  1180. }
  1181. /* if enable state toggled note the update */
  1182. if (!err && (!FM10K_VF_FLAG_ENABLED(vf_info) != !mode))
  1183. err = fm10k_update_lport_state_pf(hw, vf_info->glort, 1,
  1184. !!mode);
  1185. /* if state change succeeded, then update our stored state */
  1186. mode |= FM10K_VF_FLAG_CAPABLE(vf_info);
  1187. if (!err)
  1188. vf_info->vf_flags = mode;
  1189. return err;
  1190. }
  1191. /**
  1192. * fm10k_update_stats_hw_pf - Updates hardware related statistics of PF
  1193. * @hw: pointer to hardware structure
  1194. * @stats: pointer to the stats structure to update
  1195. *
  1196. * This function collects and aggregates global and per queue hardware
  1197. * statistics.
  1198. **/
  1199. static void fm10k_update_hw_stats_pf(struct fm10k_hw *hw,
  1200. struct fm10k_hw_stats *stats)
  1201. {
  1202. u32 timeout, ur, ca, um, xec, vlan_drop, loopback_drop, nodesc_drop;
  1203. u32 id, id_prev;
  1204. /* Use Tx queue 0 as a canary to detect a reset */
  1205. id = fm10k_read_reg(hw, FM10K_TXQCTL(0));
  1206. /* Read Global Statistics */
  1207. do {
  1208. timeout = fm10k_read_hw_stats_32b(hw, FM10K_STATS_TIMEOUT,
  1209. &stats->timeout);
  1210. ur = fm10k_read_hw_stats_32b(hw, FM10K_STATS_UR, &stats->ur);
  1211. ca = fm10k_read_hw_stats_32b(hw, FM10K_STATS_CA, &stats->ca);
  1212. um = fm10k_read_hw_stats_32b(hw, FM10K_STATS_UM, &stats->um);
  1213. xec = fm10k_read_hw_stats_32b(hw, FM10K_STATS_XEC, &stats->xec);
  1214. vlan_drop = fm10k_read_hw_stats_32b(hw, FM10K_STATS_VLAN_DROP,
  1215. &stats->vlan_drop);
  1216. loopback_drop =
  1217. fm10k_read_hw_stats_32b(hw,
  1218. FM10K_STATS_LOOPBACK_DROP,
  1219. &stats->loopback_drop);
  1220. nodesc_drop = fm10k_read_hw_stats_32b(hw,
  1221. FM10K_STATS_NODESC_DROP,
  1222. &stats->nodesc_drop);
  1223. /* if value has not changed then we have consistent data */
  1224. id_prev = id;
  1225. id = fm10k_read_reg(hw, FM10K_TXQCTL(0));
  1226. } while ((id ^ id_prev) & FM10K_TXQCTL_ID_MASK);
  1227. /* drop non-ID bits and set VALID ID bit */
  1228. id &= FM10K_TXQCTL_ID_MASK;
  1229. id |= FM10K_STAT_VALID;
  1230. /* Update Global Statistics */
  1231. if (stats->stats_idx == id) {
  1232. stats->timeout.count += timeout;
  1233. stats->ur.count += ur;
  1234. stats->ca.count += ca;
  1235. stats->um.count += um;
  1236. stats->xec.count += xec;
  1237. stats->vlan_drop.count += vlan_drop;
  1238. stats->loopback_drop.count += loopback_drop;
  1239. stats->nodesc_drop.count += nodesc_drop;
  1240. }
  1241. /* Update bases and record current PF id */
  1242. fm10k_update_hw_base_32b(&stats->timeout, timeout);
  1243. fm10k_update_hw_base_32b(&stats->ur, ur);
  1244. fm10k_update_hw_base_32b(&stats->ca, ca);
  1245. fm10k_update_hw_base_32b(&stats->um, um);
  1246. fm10k_update_hw_base_32b(&stats->xec, xec);
  1247. fm10k_update_hw_base_32b(&stats->vlan_drop, vlan_drop);
  1248. fm10k_update_hw_base_32b(&stats->loopback_drop, loopback_drop);
  1249. fm10k_update_hw_base_32b(&stats->nodesc_drop, nodesc_drop);
  1250. stats->stats_idx = id;
  1251. /* Update Queue Statistics */
  1252. fm10k_update_hw_stats_q(hw, stats->q, 0, hw->mac.max_queues);
  1253. }
  1254. /**
  1255. * fm10k_rebind_hw_stats_pf - Resets base for hardware statistics of PF
  1256. * @hw: pointer to hardware structure
  1257. * @stats: pointer to the stats structure to update
  1258. *
  1259. * This function resets the base for global and per queue hardware
  1260. * statistics.
  1261. **/
  1262. static void fm10k_rebind_hw_stats_pf(struct fm10k_hw *hw,
  1263. struct fm10k_hw_stats *stats)
  1264. {
  1265. /* Unbind Global Statistics */
  1266. fm10k_unbind_hw_stats_32b(&stats->timeout);
  1267. fm10k_unbind_hw_stats_32b(&stats->ur);
  1268. fm10k_unbind_hw_stats_32b(&stats->ca);
  1269. fm10k_unbind_hw_stats_32b(&stats->um);
  1270. fm10k_unbind_hw_stats_32b(&stats->xec);
  1271. fm10k_unbind_hw_stats_32b(&stats->vlan_drop);
  1272. fm10k_unbind_hw_stats_32b(&stats->loopback_drop);
  1273. fm10k_unbind_hw_stats_32b(&stats->nodesc_drop);
  1274. /* Unbind Queue Statistics */
  1275. fm10k_unbind_hw_stats_q(stats->q, 0, hw->mac.max_queues);
  1276. /* Reinitialize bases for all stats */
  1277. fm10k_update_hw_stats_pf(hw, stats);
  1278. }
  1279. /**
  1280. * fm10k_set_dma_mask_pf - Configures PhyAddrSpace to limit DMA to system
  1281. * @hw: pointer to hardware structure
  1282. * @dma_mask: 64 bit DMA mask required for platform
  1283. *
  1284. * This function sets the PHYADDR.PhyAddrSpace bits for the endpoint in order
  1285. * to limit the access to memory beyond what is physically in the system.
  1286. **/
  1287. static void fm10k_set_dma_mask_pf(struct fm10k_hw *hw, u64 dma_mask)
  1288. {
  1289. /* we need to write the upper 32 bits of DMA mask to PhyAddrSpace */
  1290. u32 phyaddr = (u32)(dma_mask >> 32);
  1291. fm10k_write_reg(hw, FM10K_PHYADDR, phyaddr);
  1292. }
  1293. /**
  1294. * fm10k_get_fault_pf - Record a fault in one of the interface units
  1295. * @hw: pointer to hardware structure
  1296. * @type: pointer to fault type register offset
  1297. * @fault: pointer to memory location to record the fault
  1298. *
  1299. * Record the fault register contents to the fault data structure and
  1300. * clear the entry from the register.
  1301. *
  1302. * Returns ERR_PARAM if invalid register is specified or no error is present.
  1303. **/
  1304. static s32 fm10k_get_fault_pf(struct fm10k_hw *hw, int type,
  1305. struct fm10k_fault *fault)
  1306. {
  1307. u32 func;
  1308. /* verify the fault register is in range and is aligned */
  1309. switch (type) {
  1310. case FM10K_PCA_FAULT:
  1311. case FM10K_THI_FAULT:
  1312. case FM10K_FUM_FAULT:
  1313. break;
  1314. default:
  1315. return FM10K_ERR_PARAM;
  1316. }
  1317. /* only service faults that are valid */
  1318. func = fm10k_read_reg(hw, type + FM10K_FAULT_FUNC);
  1319. if (!(func & FM10K_FAULT_FUNC_VALID))
  1320. return FM10K_ERR_PARAM;
  1321. /* read remaining fields */
  1322. fault->address = fm10k_read_reg(hw, type + FM10K_FAULT_ADDR_HI);
  1323. fault->address <<= 32;
  1324. fault->address = fm10k_read_reg(hw, type + FM10K_FAULT_ADDR_LO);
  1325. fault->specinfo = fm10k_read_reg(hw, type + FM10K_FAULT_SPECINFO);
  1326. /* clear valid bit to allow for next error */
  1327. fm10k_write_reg(hw, type + FM10K_FAULT_FUNC, FM10K_FAULT_FUNC_VALID);
  1328. /* Record which function triggered the error */
  1329. if (func & FM10K_FAULT_FUNC_PF)
  1330. fault->func = 0;
  1331. else
  1332. fault->func = 1 + ((func & FM10K_FAULT_FUNC_VF_MASK) >>
  1333. FM10K_FAULT_FUNC_VF_SHIFT);
  1334. /* record fault type */
  1335. fault->type = func & FM10K_FAULT_FUNC_TYPE_MASK;
  1336. return 0;
  1337. }
  1338. /**
  1339. * fm10k_request_lport_map_pf - Request LPORT map from the switch API
  1340. * @hw: pointer to hardware structure
  1341. *
  1342. **/
  1343. static s32 fm10k_request_lport_map_pf(struct fm10k_hw *hw)
  1344. {
  1345. struct fm10k_mbx_info *mbx = &hw->mbx;
  1346. u32 msg[1];
  1347. /* issue request asking for LPORT map */
  1348. fm10k_tlv_msg_init(msg, FM10K_PF_MSG_ID_LPORT_MAP);
  1349. /* load onto outgoing mailbox */
  1350. return mbx->ops.enqueue_tx(hw, mbx, msg);
  1351. }
  1352. /**
  1353. * fm10k_get_host_state_pf - Returns the state of the switch and mailbox
  1354. * @hw: pointer to hardware structure
  1355. * @switch_ready: pointer to boolean value that will record switch state
  1356. *
  1357. * This function will check the DMA_CTRL2 register and mailbox in order
  1358. * to determine if the switch is ready for the PF to begin requesting
  1359. * addresses and mapping traffic to the local interface.
  1360. **/
  1361. static s32 fm10k_get_host_state_pf(struct fm10k_hw *hw, bool *switch_ready)
  1362. {
  1363. u32 dma_ctrl2;
  1364. /* verify the switch is ready for interaction */
  1365. dma_ctrl2 = fm10k_read_reg(hw, FM10K_DMA_CTRL2);
  1366. if (!(dma_ctrl2 & FM10K_DMA_CTRL2_SWITCH_READY))
  1367. return 0;
  1368. /* retrieve generic host state info */
  1369. return fm10k_get_host_state_generic(hw, switch_ready);
  1370. }
  1371. /* This structure defines the attibutes to be parsed below */
  1372. const struct fm10k_tlv_attr fm10k_lport_map_msg_attr[] = {
  1373. FM10K_TLV_ATTR_LE_STRUCT(FM10K_PF_ATTR_ID_ERR,
  1374. sizeof(struct fm10k_swapi_error)),
  1375. FM10K_TLV_ATTR_U32(FM10K_PF_ATTR_ID_LPORT_MAP),
  1376. FM10K_TLV_ATTR_LAST
  1377. };
  1378. /**
  1379. * fm10k_msg_lport_map_pf - Message handler for lport_map message from SM
  1380. * @hw: Pointer to hardware structure
  1381. * @results: pointer array containing parsed data
  1382. * @mbx: Pointer to mailbox information structure
  1383. *
  1384. * This handler configures the lport mapping based on the reply from the
  1385. * switch API.
  1386. **/
  1387. s32 fm10k_msg_lport_map_pf(struct fm10k_hw *hw, u32 **results,
  1388. struct fm10k_mbx_info *mbx)
  1389. {
  1390. u16 glort, mask;
  1391. u32 dglort_map;
  1392. s32 err;
  1393. err = fm10k_tlv_attr_get_u32(results[FM10K_PF_ATTR_ID_LPORT_MAP],
  1394. &dglort_map);
  1395. if (err)
  1396. return err;
  1397. /* extract values out of the header */
  1398. glort = FM10K_MSG_HDR_FIELD_GET(dglort_map, LPORT_MAP_GLORT);
  1399. mask = FM10K_MSG_HDR_FIELD_GET(dglort_map, LPORT_MAP_MASK);
  1400. /* verify mask is set and none of the masked bits in glort are set */
  1401. if (!mask || (glort & ~mask))
  1402. return FM10K_ERR_PARAM;
  1403. /* verify the mask is contiguous, and that it is 1's followed by 0's */
  1404. if (((~(mask - 1) & mask) + mask) & FM10K_DGLORTMAP_NONE)
  1405. return FM10K_ERR_PARAM;
  1406. /* record the glort, mask, and port count */
  1407. hw->mac.dglort_map = dglort_map;
  1408. return 0;
  1409. }
  1410. const struct fm10k_tlv_attr fm10k_update_pvid_msg_attr[] = {
  1411. FM10K_TLV_ATTR_U32(FM10K_PF_ATTR_ID_UPDATE_PVID),
  1412. FM10K_TLV_ATTR_LAST
  1413. };
  1414. /**
  1415. * fm10k_msg_update_pvid_pf - Message handler for port VLAN message from SM
  1416. * @hw: Pointer to hardware structure
  1417. * @results: pointer array containing parsed data
  1418. * @mbx: Pointer to mailbox information structure
  1419. *
  1420. * This handler configures the default VLAN for the PF
  1421. **/
  1422. static s32 fm10k_msg_update_pvid_pf(struct fm10k_hw *hw, u32 **results,
  1423. struct fm10k_mbx_info *mbx)
  1424. {
  1425. u16 glort, pvid;
  1426. u32 pvid_update;
  1427. s32 err;
  1428. err = fm10k_tlv_attr_get_u32(results[FM10K_PF_ATTR_ID_UPDATE_PVID],
  1429. &pvid_update);
  1430. if (err)
  1431. return err;
  1432. /* extract values from the pvid update */
  1433. glort = FM10K_MSG_HDR_FIELD_GET(pvid_update, UPDATE_PVID_GLORT);
  1434. pvid = FM10K_MSG_HDR_FIELD_GET(pvid_update, UPDATE_PVID_PVID);
  1435. /* if glort is not valid return error */
  1436. if (!fm10k_glort_valid_pf(hw, glort))
  1437. return FM10K_ERR_PARAM;
  1438. /* verify VLAN ID is valid */
  1439. if (pvid >= FM10K_VLAN_TABLE_VID_MAX)
  1440. return FM10K_ERR_PARAM;
  1441. /* record the port VLAN ID value */
  1442. hw->mac.default_vid = pvid;
  1443. return 0;
  1444. }
  1445. /**
  1446. * fm10k_record_global_table_data - Move global table data to swapi table info
  1447. * @from: pointer to source table data structure
  1448. * @to: pointer to destination table info structure
  1449. *
  1450. * This function is will copy table_data to the table_info contained in
  1451. * the hw struct.
  1452. **/
  1453. static void fm10k_record_global_table_data(struct fm10k_global_table_data *from,
  1454. struct fm10k_swapi_table_info *to)
  1455. {
  1456. /* convert from le32 struct to CPU byte ordered values */
  1457. to->used = le32_to_cpu(from->used);
  1458. to->avail = le32_to_cpu(from->avail);
  1459. }
  1460. const struct fm10k_tlv_attr fm10k_err_msg_attr[] = {
  1461. FM10K_TLV_ATTR_LE_STRUCT(FM10K_PF_ATTR_ID_ERR,
  1462. sizeof(struct fm10k_swapi_error)),
  1463. FM10K_TLV_ATTR_LAST
  1464. };
  1465. /**
  1466. * fm10k_msg_err_pf - Message handler for error reply
  1467. * @hw: Pointer to hardware structure
  1468. * @results: pointer array containing parsed data
  1469. * @mbx: Pointer to mailbox information structure
  1470. *
  1471. * This handler will capture the data for any error replies to previous
  1472. * messages that the PF has sent.
  1473. **/
  1474. s32 fm10k_msg_err_pf(struct fm10k_hw *hw, u32 **results,
  1475. struct fm10k_mbx_info *mbx)
  1476. {
  1477. struct fm10k_swapi_error err_msg;
  1478. s32 err;
  1479. /* extract structure from message */
  1480. err = fm10k_tlv_attr_get_le_struct(results[FM10K_PF_ATTR_ID_ERR],
  1481. &err_msg, sizeof(err_msg));
  1482. if (err)
  1483. return err;
  1484. /* record table status */
  1485. fm10k_record_global_table_data(&err_msg.mac, &hw->swapi.mac);
  1486. fm10k_record_global_table_data(&err_msg.nexthop, &hw->swapi.nexthop);
  1487. fm10k_record_global_table_data(&err_msg.ffu, &hw->swapi.ffu);
  1488. /* record SW API status value */
  1489. hw->swapi.status = le32_to_cpu(err_msg.status);
  1490. return 0;
  1491. }
  1492. static const struct fm10k_msg_data fm10k_msg_data_pf[] = {
  1493. FM10K_PF_MSG_ERR_HANDLER(XCAST_MODES, fm10k_msg_err_pf),
  1494. FM10K_PF_MSG_ERR_HANDLER(UPDATE_MAC_FWD_RULE, fm10k_msg_err_pf),
  1495. FM10K_PF_MSG_LPORT_MAP_HANDLER(fm10k_msg_lport_map_pf),
  1496. FM10K_PF_MSG_ERR_HANDLER(LPORT_CREATE, fm10k_msg_err_pf),
  1497. FM10K_PF_MSG_ERR_HANDLER(LPORT_DELETE, fm10k_msg_err_pf),
  1498. FM10K_PF_MSG_UPDATE_PVID_HANDLER(fm10k_msg_update_pvid_pf),
  1499. FM10K_TLV_MSG_ERROR_HANDLER(fm10k_tlv_msg_error),
  1500. };
  1501. static const struct fm10k_mac_ops mac_ops_pf = {
  1502. .get_bus_info = fm10k_get_bus_info_generic,
  1503. .reset_hw = fm10k_reset_hw_pf,
  1504. .init_hw = fm10k_init_hw_pf,
  1505. .start_hw = fm10k_start_hw_generic,
  1506. .stop_hw = fm10k_stop_hw_generic,
  1507. .update_vlan = fm10k_update_vlan_pf,
  1508. .read_mac_addr = fm10k_read_mac_addr_pf,
  1509. .update_uc_addr = fm10k_update_uc_addr_pf,
  1510. .update_mc_addr = fm10k_update_mc_addr_pf,
  1511. .update_xcast_mode = fm10k_update_xcast_mode_pf,
  1512. .update_int_moderator = fm10k_update_int_moderator_pf,
  1513. .update_lport_state = fm10k_update_lport_state_pf,
  1514. .update_hw_stats = fm10k_update_hw_stats_pf,
  1515. .rebind_hw_stats = fm10k_rebind_hw_stats_pf,
  1516. .configure_dglort_map = fm10k_configure_dglort_map_pf,
  1517. .set_dma_mask = fm10k_set_dma_mask_pf,
  1518. .get_fault = fm10k_get_fault_pf,
  1519. .get_host_state = fm10k_get_host_state_pf,
  1520. .request_lport_map = fm10k_request_lport_map_pf,
  1521. };
  1522. static const struct fm10k_iov_ops iov_ops_pf = {
  1523. .assign_resources = fm10k_iov_assign_resources_pf,
  1524. .configure_tc = fm10k_iov_configure_tc_pf,
  1525. .assign_int_moderator = fm10k_iov_assign_int_moderator_pf,
  1526. .assign_default_mac_vlan = fm10k_iov_assign_default_mac_vlan_pf,
  1527. .reset_resources = fm10k_iov_reset_resources_pf,
  1528. .set_lport = fm10k_iov_set_lport_pf,
  1529. .reset_lport = fm10k_iov_reset_lport_pf,
  1530. .update_stats = fm10k_iov_update_stats_pf,
  1531. };
  1532. static s32 fm10k_get_invariants_pf(struct fm10k_hw *hw)
  1533. {
  1534. fm10k_get_invariants_generic(hw);
  1535. return fm10k_sm_mbx_init(hw, &hw->mbx, fm10k_msg_data_pf);
  1536. }
  1537. const struct fm10k_info fm10k_pf_info = {
  1538. .mac = fm10k_mac_pf,
  1539. .get_invariants = fm10k_get_invariants_pf,
  1540. .mac_ops = &mac_ops_pf,
  1541. .iov_ops = &iov_ops_pf,
  1542. };