fm10k_pf.c 58 KB

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