fm10k_pf.c 57 KB

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