fm10k_pf.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894
  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. /* 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(((u32)mac[0] << 8) |
  290. ((u32)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. /* construct the lport message from the 2 pieces of data we have */
  418. lport_msg = ((u32)count << 16) | glort;
  419. /* generate lport create/delete message */
  420. fm10k_tlv_msg_init(msg, enable ? FM10K_PF_MSG_ID_LPORT_CREATE :
  421. FM10K_PF_MSG_ID_LPORT_DELETE);
  422. fm10k_tlv_attr_put_u32(msg, FM10K_PF_ATTR_ID_PORT, lport_msg);
  423. /* load onto outgoing mailbox */
  424. return mbx->ops.enqueue_tx(hw, mbx, msg);
  425. }
  426. /**
  427. * fm10k_configure_dglort_map_pf - Configures GLORT entry and queues
  428. * @hw: pointer to hardware structure
  429. * @dglort: pointer to dglort configuration structure
  430. *
  431. * Reads the configuration structure contained in dglort_cfg and uses
  432. * that information to then populate a DGLORTMAP/DEC entry and the queues
  433. * to which it has been assigned.
  434. **/
  435. static s32 fm10k_configure_dglort_map_pf(struct fm10k_hw *hw,
  436. struct fm10k_dglort_cfg *dglort)
  437. {
  438. u16 glort, queue_count, vsi_count, pc_count;
  439. u16 vsi, queue, pc, q_idx;
  440. u32 txqctl, dglortdec, dglortmap;
  441. /* verify the dglort pointer */
  442. if (!dglort)
  443. return FM10K_ERR_PARAM;
  444. /* verify the dglort values */
  445. if ((dglort->idx > 7) || (dglort->rss_l > 7) || (dglort->pc_l > 3) ||
  446. (dglort->vsi_l > 6) || (dglort->vsi_b > 64) ||
  447. (dglort->queue_l > 8) || (dglort->queue_b >= 256))
  448. return FM10K_ERR_PARAM;
  449. /* determine count of VSIs and queues */
  450. queue_count = 1 << (dglort->rss_l + dglort->pc_l);
  451. vsi_count = 1 << (dglort->vsi_l + dglort->queue_l);
  452. glort = dglort->glort;
  453. q_idx = dglort->queue_b;
  454. /* configure SGLORT for queues */
  455. for (vsi = 0; vsi < vsi_count; vsi++, glort++) {
  456. for (queue = 0; queue < queue_count; queue++, q_idx++) {
  457. if (q_idx >= FM10K_MAX_QUEUES)
  458. break;
  459. fm10k_write_reg(hw, FM10K_TX_SGLORT(q_idx), glort);
  460. fm10k_write_reg(hw, FM10K_RX_SGLORT(q_idx), glort);
  461. }
  462. }
  463. /* determine count of PCs and queues */
  464. queue_count = 1 << (dglort->queue_l + dglort->rss_l + dglort->vsi_l);
  465. pc_count = 1 << dglort->pc_l;
  466. /* configure PC for Tx queues */
  467. for (pc = 0; pc < pc_count; pc++) {
  468. q_idx = pc + dglort->queue_b;
  469. for (queue = 0; queue < queue_count; queue++) {
  470. if (q_idx >= FM10K_MAX_QUEUES)
  471. break;
  472. txqctl = fm10k_read_reg(hw, FM10K_TXQCTL(q_idx));
  473. txqctl &= ~FM10K_TXQCTL_PC_MASK;
  474. txqctl |= pc << FM10K_TXQCTL_PC_SHIFT;
  475. fm10k_write_reg(hw, FM10K_TXQCTL(q_idx), txqctl);
  476. q_idx += pc_count;
  477. }
  478. }
  479. /* configure DGLORTDEC */
  480. dglortdec = ((u32)(dglort->rss_l) << FM10K_DGLORTDEC_RSSLENGTH_SHIFT) |
  481. ((u32)(dglort->queue_b) << FM10K_DGLORTDEC_QBASE_SHIFT) |
  482. ((u32)(dglort->pc_l) << FM10K_DGLORTDEC_PCLENGTH_SHIFT) |
  483. ((u32)(dglort->vsi_b) << FM10K_DGLORTDEC_VSIBASE_SHIFT) |
  484. ((u32)(dglort->vsi_l) << FM10K_DGLORTDEC_VSILENGTH_SHIFT) |
  485. ((u32)(dglort->queue_l));
  486. if (dglort->inner_rss)
  487. dglortdec |= FM10K_DGLORTDEC_INNERRSS_ENABLE;
  488. /* configure DGLORTMAP */
  489. dglortmap = (dglort->idx == fm10k_dglort_default) ?
  490. FM10K_DGLORTMAP_ANY : FM10K_DGLORTMAP_ZERO;
  491. dglortmap <<= dglort->vsi_l + dglort->queue_l + dglort->shared_l;
  492. dglortmap |= dglort->glort;
  493. /* write values to hardware */
  494. fm10k_write_reg(hw, FM10K_DGLORTDEC(dglort->idx), dglortdec);
  495. fm10k_write_reg(hw, FM10K_DGLORTMAP(dglort->idx), dglortmap);
  496. return 0;
  497. }
  498. u16 fm10k_queues_per_pool(struct fm10k_hw *hw)
  499. {
  500. u16 num_pools = hw->iov.num_pools;
  501. return (num_pools > 32) ? 2 : (num_pools > 16) ? 4 : (num_pools > 8) ?
  502. 8 : FM10K_MAX_QUEUES_POOL;
  503. }
  504. u16 fm10k_vf_queue_index(struct fm10k_hw *hw, u16 vf_idx)
  505. {
  506. u16 num_vfs = hw->iov.num_vfs;
  507. u16 vf_q_idx = FM10K_MAX_QUEUES;
  508. vf_q_idx -= fm10k_queues_per_pool(hw) * (num_vfs - vf_idx);
  509. return vf_q_idx;
  510. }
  511. static u16 fm10k_vectors_per_pool(struct fm10k_hw *hw)
  512. {
  513. u16 num_pools = hw->iov.num_pools;
  514. return (num_pools > 32) ? 8 : (num_pools > 16) ? 16 :
  515. FM10K_MAX_VECTORS_POOL;
  516. }
  517. static u16 fm10k_vf_vector_index(struct fm10k_hw *hw, u16 vf_idx)
  518. {
  519. u16 vf_v_idx = FM10K_MAX_VECTORS_PF;
  520. vf_v_idx += fm10k_vectors_per_pool(hw) * vf_idx;
  521. return vf_v_idx;
  522. }
  523. /**
  524. * fm10k_iov_assign_resources_pf - Assign pool resources for virtualization
  525. * @hw: pointer to the HW structure
  526. * @num_vfs: number of VFs to be allocated
  527. * @num_pools: number of virtualization pools to be allocated
  528. *
  529. * Allocates queues and traffic classes to virtualization entities to prepare
  530. * the PF for SR-IOV and VMDq
  531. **/
  532. static s32 fm10k_iov_assign_resources_pf(struct fm10k_hw *hw, u16 num_vfs,
  533. u16 num_pools)
  534. {
  535. u16 qmap_stride, qpp, vpp, vf_q_idx, vf_q_idx0, qmap_idx;
  536. u32 vid = hw->mac.default_vid << FM10K_TXQCTL_VID_SHIFT;
  537. int i, j;
  538. /* hardware only supports up to 64 pools */
  539. if (num_pools > 64)
  540. return FM10K_ERR_PARAM;
  541. /* the number of VFs cannot exceed the number of pools */
  542. if ((num_vfs > num_pools) || (num_vfs > hw->iov.total_vfs))
  543. return FM10K_ERR_PARAM;
  544. /* record number of virtualization entities */
  545. hw->iov.num_vfs = num_vfs;
  546. hw->iov.num_pools = num_pools;
  547. /* determine qmap offsets and counts */
  548. qmap_stride = (num_vfs > 8) ? 32 : 256;
  549. qpp = fm10k_queues_per_pool(hw);
  550. vpp = fm10k_vectors_per_pool(hw);
  551. /* calculate starting index for queues */
  552. vf_q_idx = fm10k_vf_queue_index(hw, 0);
  553. qmap_idx = 0;
  554. /* establish TCs with -1 credits and no quanta to prevent transmit */
  555. for (i = 0; i < num_vfs; i++) {
  556. fm10k_write_reg(hw, FM10K_TC_MAXCREDIT(i), 0);
  557. fm10k_write_reg(hw, FM10K_TC_RATE(i), 0);
  558. fm10k_write_reg(hw, FM10K_TC_CREDIT(i),
  559. FM10K_TC_CREDIT_CREDIT_MASK);
  560. }
  561. /* zero out all mbmem registers */
  562. for (i = FM10K_VFMBMEM_LEN * num_vfs; i--;)
  563. fm10k_write_reg(hw, FM10K_MBMEM(i), 0);
  564. /* clear event notification of VF FLR */
  565. fm10k_write_reg(hw, FM10K_PFVFLREC(0), ~0);
  566. fm10k_write_reg(hw, FM10K_PFVFLREC(1), ~0);
  567. /* loop through unallocated rings assigning them back to PF */
  568. for (i = FM10K_MAX_QUEUES_PF; i < vf_q_idx; i++) {
  569. fm10k_write_reg(hw, FM10K_TXDCTL(i), 0);
  570. fm10k_write_reg(hw, FM10K_TXQCTL(i), FM10K_TXQCTL_PF |
  571. FM10K_TXQCTL_UNLIMITED_BW | vid);
  572. fm10k_write_reg(hw, FM10K_RXQCTL(i), FM10K_RXQCTL_PF);
  573. }
  574. /* PF should have already updated VFITR2[0] */
  575. /* update all ITR registers to flow to VFITR2[0] */
  576. for (i = FM10K_ITR_REG_COUNT_PF + 1; i < FM10K_ITR_REG_COUNT; i++) {
  577. if (!(i & (vpp - 1)))
  578. fm10k_write_reg(hw, FM10K_ITR2(i), i - vpp);
  579. else
  580. fm10k_write_reg(hw, FM10K_ITR2(i), i - 1);
  581. }
  582. /* update PF ITR2[0] to reference the last vector */
  583. fm10k_write_reg(hw, FM10K_ITR2(0),
  584. fm10k_vf_vector_index(hw, num_vfs - 1));
  585. /* loop through rings populating rings and TCs */
  586. for (i = 0; i < num_vfs; i++) {
  587. /* record index for VF queue 0 for use in end of loop */
  588. vf_q_idx0 = vf_q_idx;
  589. for (j = 0; j < qpp; j++, qmap_idx++, vf_q_idx++) {
  590. /* assign VF and locked TC to queues */
  591. fm10k_write_reg(hw, FM10K_TXDCTL(vf_q_idx), 0);
  592. fm10k_write_reg(hw, FM10K_TXQCTL(vf_q_idx),
  593. (i << FM10K_TXQCTL_TC_SHIFT) | i |
  594. FM10K_TXQCTL_VF | vid);
  595. fm10k_write_reg(hw, FM10K_RXDCTL(vf_q_idx),
  596. FM10K_RXDCTL_WRITE_BACK_MIN_DELAY |
  597. FM10K_RXDCTL_DROP_ON_EMPTY);
  598. fm10k_write_reg(hw, FM10K_RXQCTL(vf_q_idx),
  599. FM10K_RXQCTL_VF |
  600. (i << FM10K_RXQCTL_VF_SHIFT));
  601. /* map queue pair to VF */
  602. fm10k_write_reg(hw, FM10K_TQMAP(qmap_idx), vf_q_idx);
  603. fm10k_write_reg(hw, FM10K_RQMAP(qmap_idx), vf_q_idx);
  604. }
  605. /* repeat the first ring for all of the remaining VF rings */
  606. for (; j < qmap_stride; j++, qmap_idx++) {
  607. fm10k_write_reg(hw, FM10K_TQMAP(qmap_idx), vf_q_idx0);
  608. fm10k_write_reg(hw, FM10K_RQMAP(qmap_idx), vf_q_idx0);
  609. }
  610. }
  611. /* loop through remaining indexes assigning all to queue 0 */
  612. while (qmap_idx < FM10K_TQMAP_TABLE_SIZE) {
  613. fm10k_write_reg(hw, FM10K_TQMAP(qmap_idx), 0);
  614. fm10k_write_reg(hw, FM10K_RQMAP(qmap_idx), 0);
  615. qmap_idx++;
  616. }
  617. return 0;
  618. }
  619. /**
  620. * fm10k_iov_configure_tc_pf - Configure the shaping group for VF
  621. * @hw: pointer to the HW structure
  622. * @vf_idx: index of VF receiving GLORT
  623. * @rate: Rate indicated in Mb/s
  624. *
  625. * Configured the TC for a given VF to allow only up to a given number
  626. * of Mb/s of outgoing Tx throughput.
  627. **/
  628. static s32 fm10k_iov_configure_tc_pf(struct fm10k_hw *hw, u16 vf_idx, int rate)
  629. {
  630. /* configure defaults */
  631. u32 interval = FM10K_TC_RATE_INTERVAL_4US_GEN3;
  632. u32 tc_rate = FM10K_TC_RATE_QUANTA_MASK;
  633. /* verify vf is in range */
  634. if (vf_idx >= hw->iov.num_vfs)
  635. return FM10K_ERR_PARAM;
  636. /* set interval to align with 4.096 usec in all modes */
  637. switch (hw->bus.speed) {
  638. case fm10k_bus_speed_2500:
  639. interval = FM10K_TC_RATE_INTERVAL_4US_GEN1;
  640. break;
  641. case fm10k_bus_speed_5000:
  642. interval = FM10K_TC_RATE_INTERVAL_4US_GEN2;
  643. break;
  644. default:
  645. break;
  646. }
  647. if (rate) {
  648. if (rate > FM10K_VF_TC_MAX || rate < FM10K_VF_TC_MIN)
  649. return FM10K_ERR_PARAM;
  650. /* The quanta is measured in Bytes per 4.096 or 8.192 usec
  651. * The rate is provided in Mbits per second
  652. * To tralslate from rate to quanta we need to multiply the
  653. * rate by 8.192 usec and divide by 8 bits/byte. To avoid
  654. * dealing with floating point we can round the values up
  655. * to the nearest whole number ratio which gives us 128 / 125.
  656. */
  657. tc_rate = (rate * 128) / 125;
  658. /* try to keep the rate limiting accurate by increasing
  659. * the number of credits and interval for rates less than 4Gb/s
  660. */
  661. if (rate < 4000)
  662. interval <<= 1;
  663. else
  664. tc_rate >>= 1;
  665. }
  666. /* update rate limiter with new values */
  667. fm10k_write_reg(hw, FM10K_TC_RATE(vf_idx), tc_rate | interval);
  668. fm10k_write_reg(hw, FM10K_TC_MAXCREDIT(vf_idx), FM10K_TC_MAXCREDIT_64K);
  669. fm10k_write_reg(hw, FM10K_TC_CREDIT(vf_idx), FM10K_TC_MAXCREDIT_64K);
  670. return 0;
  671. }
  672. /**
  673. * fm10k_iov_assign_int_moderator_pf - Add VF interrupts to moderator list
  674. * @hw: pointer to the HW structure
  675. * @vf_idx: index of VF receiving GLORT
  676. *
  677. * Update the interrupt moderator linked list to include any MSI-X
  678. * interrupts which the VF has enabled in the MSI-X vector table.
  679. **/
  680. static s32 fm10k_iov_assign_int_moderator_pf(struct fm10k_hw *hw, u16 vf_idx)
  681. {
  682. u16 vf_v_idx, vf_v_limit, i;
  683. /* verify vf is in range */
  684. if (vf_idx >= hw->iov.num_vfs)
  685. return FM10K_ERR_PARAM;
  686. /* determine vector offset and count */
  687. vf_v_idx = fm10k_vf_vector_index(hw, vf_idx);
  688. vf_v_limit = vf_v_idx + fm10k_vectors_per_pool(hw);
  689. /* search for first vector that is not masked */
  690. for (i = vf_v_limit - 1; i > vf_v_idx; i--) {
  691. if (!fm10k_read_reg(hw, FM10K_MSIX_VECTOR_MASK(i)))
  692. break;
  693. }
  694. /* reset linked list so it now includes our active vectors */
  695. if (vf_idx == (hw->iov.num_vfs - 1))
  696. fm10k_write_reg(hw, FM10K_ITR2(0), i);
  697. else
  698. fm10k_write_reg(hw, FM10K_ITR2(vf_v_limit), i);
  699. return 0;
  700. }
  701. /**
  702. * fm10k_iov_assign_default_mac_vlan_pf - Assign a MAC and VLAN to VF
  703. * @hw: pointer to the HW structure
  704. * @vf_info: pointer to VF information structure
  705. *
  706. * Assign a MAC address and default VLAN to a VF and notify it of the update
  707. **/
  708. static s32 fm10k_iov_assign_default_mac_vlan_pf(struct fm10k_hw *hw,
  709. struct fm10k_vf_info *vf_info)
  710. {
  711. u16 qmap_stride, queues_per_pool, vf_q_idx, timeout, qmap_idx, i;
  712. u32 msg[4], txdctl, txqctl, tdbal = 0, tdbah = 0;
  713. s32 err = 0;
  714. u16 vf_idx, vf_vid;
  715. /* verify vf is in range */
  716. if (!vf_info || vf_info->vf_idx >= hw->iov.num_vfs)
  717. return FM10K_ERR_PARAM;
  718. /* determine qmap offsets and counts */
  719. qmap_stride = (hw->iov.num_vfs > 8) ? 32 : 256;
  720. queues_per_pool = fm10k_queues_per_pool(hw);
  721. /* calculate starting index for queues */
  722. vf_idx = vf_info->vf_idx;
  723. vf_q_idx = fm10k_vf_queue_index(hw, vf_idx);
  724. qmap_idx = qmap_stride * vf_idx;
  725. /* MAP Tx queue back to 0 temporarily, and disable it */
  726. fm10k_write_reg(hw, FM10K_TQMAP(qmap_idx), 0);
  727. fm10k_write_reg(hw, FM10K_TXDCTL(vf_q_idx), 0);
  728. /* determine correct default VLAN ID */
  729. if (vf_info->pf_vid)
  730. vf_vid = vf_info->pf_vid | FM10K_VLAN_CLEAR;
  731. else
  732. vf_vid = vf_info->sw_vid;
  733. /* generate MAC_ADDR request */
  734. fm10k_tlv_msg_init(msg, FM10K_VF_MSG_ID_MAC_VLAN);
  735. fm10k_tlv_attr_put_mac_vlan(msg, FM10K_MAC_VLAN_MSG_DEFAULT_MAC,
  736. vf_info->mac, vf_vid);
  737. /* load onto outgoing mailbox, ignore any errors on enqueue */
  738. if (vf_info->mbx.ops.enqueue_tx)
  739. vf_info->mbx.ops.enqueue_tx(hw, &vf_info->mbx, msg);
  740. /* verify ring has disabled before modifying base address registers */
  741. txdctl = fm10k_read_reg(hw, FM10K_TXDCTL(vf_q_idx));
  742. for (timeout = 0; txdctl & FM10K_TXDCTL_ENABLE; timeout++) {
  743. /* limit ourselves to a 1ms timeout */
  744. if (timeout == 10) {
  745. err = FM10K_ERR_DMA_PENDING;
  746. goto err_out;
  747. }
  748. usleep_range(100, 200);
  749. txdctl = fm10k_read_reg(hw, FM10K_TXDCTL(vf_q_idx));
  750. }
  751. /* Update base address registers to contain MAC address */
  752. if (is_valid_ether_addr(vf_info->mac)) {
  753. tdbal = (((u32)vf_info->mac[3]) << 24) |
  754. (((u32)vf_info->mac[4]) << 16) |
  755. (((u32)vf_info->mac[5]) << 8);
  756. tdbah = (((u32)0xFF) << 24) |
  757. (((u32)vf_info->mac[0]) << 16) |
  758. (((u32)vf_info->mac[1]) << 8) |
  759. ((u32)vf_info->mac[2]);
  760. }
  761. /* Record the base address into queue 0 */
  762. fm10k_write_reg(hw, FM10K_TDBAL(vf_q_idx), tdbal);
  763. fm10k_write_reg(hw, FM10K_TDBAH(vf_q_idx), tdbah);
  764. err_out:
  765. /* configure Queue control register */
  766. txqctl = ((u32)vf_vid << FM10K_TXQCTL_VID_SHIFT) &
  767. FM10K_TXQCTL_VID_MASK;
  768. txqctl |= (vf_idx << FM10K_TXQCTL_TC_SHIFT) |
  769. FM10K_TXQCTL_VF | vf_idx;
  770. /* assign VID */
  771. for (i = 0; i < queues_per_pool; i++)
  772. fm10k_write_reg(hw, FM10K_TXQCTL(vf_q_idx + i), txqctl);
  773. /* restore the queue back to VF ownership */
  774. fm10k_write_reg(hw, FM10K_TQMAP(qmap_idx), vf_q_idx);
  775. return err;
  776. }
  777. /**
  778. * fm10k_iov_reset_resources_pf - Reassign queues and interrupts to a VF
  779. * @hw: pointer to the HW structure
  780. * @vf_info: pointer to VF information structure
  781. *
  782. * Reassign the interrupts and queues to a VF following an FLR
  783. **/
  784. static s32 fm10k_iov_reset_resources_pf(struct fm10k_hw *hw,
  785. struct fm10k_vf_info *vf_info)
  786. {
  787. u16 qmap_stride, queues_per_pool, vf_q_idx, qmap_idx;
  788. u32 tdbal = 0, tdbah = 0, txqctl, rxqctl;
  789. u16 vf_v_idx, vf_v_limit, vf_vid;
  790. u8 vf_idx = vf_info->vf_idx;
  791. int i;
  792. /* verify vf is in range */
  793. if (vf_idx >= hw->iov.num_vfs)
  794. return FM10K_ERR_PARAM;
  795. /* clear event notification of VF FLR */
  796. fm10k_write_reg(hw, FM10K_PFVFLREC(vf_idx / 32), 1 << (vf_idx % 32));
  797. /* force timeout and then disconnect the mailbox */
  798. vf_info->mbx.timeout = 0;
  799. if (vf_info->mbx.ops.disconnect)
  800. vf_info->mbx.ops.disconnect(hw, &vf_info->mbx);
  801. /* determine vector offset and count */
  802. vf_v_idx = fm10k_vf_vector_index(hw, vf_idx);
  803. vf_v_limit = vf_v_idx + fm10k_vectors_per_pool(hw);
  804. /* determine qmap offsets and counts */
  805. qmap_stride = (hw->iov.num_vfs > 8) ? 32 : 256;
  806. queues_per_pool = fm10k_queues_per_pool(hw);
  807. qmap_idx = qmap_stride * vf_idx;
  808. /* make all the queues inaccessible to the VF */
  809. for (i = qmap_idx; i < (qmap_idx + qmap_stride); i++) {
  810. fm10k_write_reg(hw, FM10K_TQMAP(i), 0);
  811. fm10k_write_reg(hw, FM10K_RQMAP(i), 0);
  812. }
  813. /* calculate starting index for queues */
  814. vf_q_idx = fm10k_vf_queue_index(hw, vf_idx);
  815. /* determine correct default VLAN ID */
  816. if (vf_info->pf_vid)
  817. vf_vid = vf_info->pf_vid;
  818. else
  819. vf_vid = vf_info->sw_vid;
  820. /* configure Queue control register */
  821. txqctl = ((u32)vf_vid << FM10K_TXQCTL_VID_SHIFT) |
  822. (vf_idx << FM10K_TXQCTL_TC_SHIFT) |
  823. FM10K_TXQCTL_VF | vf_idx;
  824. rxqctl = FM10K_RXQCTL_VF | (vf_idx << FM10K_RXQCTL_VF_SHIFT);
  825. /* stop further DMA and reset queue ownership back to VF */
  826. for (i = vf_q_idx; i < (queues_per_pool + vf_q_idx); i++) {
  827. fm10k_write_reg(hw, FM10K_TXDCTL(i), 0);
  828. fm10k_write_reg(hw, FM10K_TXQCTL(i), txqctl);
  829. fm10k_write_reg(hw, FM10K_RXDCTL(i),
  830. FM10K_RXDCTL_WRITE_BACK_MIN_DELAY |
  831. FM10K_RXDCTL_DROP_ON_EMPTY);
  832. fm10k_write_reg(hw, FM10K_RXQCTL(i), rxqctl);
  833. }
  834. /* reset TC with -1 credits and no quanta to prevent transmit */
  835. fm10k_write_reg(hw, FM10K_TC_MAXCREDIT(vf_idx), 0);
  836. fm10k_write_reg(hw, FM10K_TC_RATE(vf_idx), 0);
  837. fm10k_write_reg(hw, FM10K_TC_CREDIT(vf_idx),
  838. FM10K_TC_CREDIT_CREDIT_MASK);
  839. /* update our first entry in the table based on previous VF */
  840. if (!vf_idx)
  841. hw->mac.ops.update_int_moderator(hw);
  842. else
  843. hw->iov.ops.assign_int_moderator(hw, vf_idx - 1);
  844. /* reset linked list so it now includes our active vectors */
  845. if (vf_idx == (hw->iov.num_vfs - 1))
  846. fm10k_write_reg(hw, FM10K_ITR2(0), vf_v_idx);
  847. else
  848. fm10k_write_reg(hw, FM10K_ITR2(vf_v_limit), vf_v_idx);
  849. /* link remaining vectors so that next points to previous */
  850. for (vf_v_idx++; vf_v_idx < vf_v_limit; vf_v_idx++)
  851. fm10k_write_reg(hw, FM10K_ITR2(vf_v_idx), vf_v_idx - 1);
  852. /* zero out MBMEM, VLAN_TABLE, RETA, RSSRK, and MRQC registers */
  853. for (i = FM10K_VFMBMEM_LEN; i--;)
  854. fm10k_write_reg(hw, FM10K_MBMEM_VF(vf_idx, i), 0);
  855. for (i = FM10K_VLAN_TABLE_SIZE; i--;)
  856. fm10k_write_reg(hw, FM10K_VLAN_TABLE(vf_info->vsi, i), 0);
  857. for (i = FM10K_RETA_SIZE; i--;)
  858. fm10k_write_reg(hw, FM10K_RETA(vf_info->vsi, i), 0);
  859. for (i = FM10K_RSSRK_SIZE; i--;)
  860. fm10k_write_reg(hw, FM10K_RSSRK(vf_info->vsi, i), 0);
  861. fm10k_write_reg(hw, FM10K_MRQC(vf_info->vsi), 0);
  862. /* Update base address registers to contain MAC address */
  863. if (is_valid_ether_addr(vf_info->mac)) {
  864. tdbal = (((u32)vf_info->mac[3]) << 24) |
  865. (((u32)vf_info->mac[4]) << 16) |
  866. (((u32)vf_info->mac[5]) << 8);
  867. tdbah = (((u32)0xFF) << 24) |
  868. (((u32)vf_info->mac[0]) << 16) |
  869. (((u32)vf_info->mac[1]) << 8) |
  870. ((u32)vf_info->mac[2]);
  871. }
  872. /* map queue pairs back to VF from last to first */
  873. for (i = queues_per_pool; i--;) {
  874. fm10k_write_reg(hw, FM10K_TDBAL(vf_q_idx + i), tdbal);
  875. fm10k_write_reg(hw, FM10K_TDBAH(vf_q_idx + i), tdbah);
  876. fm10k_write_reg(hw, FM10K_TQMAP(qmap_idx + i), vf_q_idx + i);
  877. fm10k_write_reg(hw, FM10K_RQMAP(qmap_idx + i), vf_q_idx + i);
  878. }
  879. /* repeat the first ring for all the remaining VF rings */
  880. for (i = queues_per_pool; i < qmap_stride; i++) {
  881. fm10k_write_reg(hw, FM10K_TQMAP(qmap_idx + i), vf_q_idx);
  882. fm10k_write_reg(hw, FM10K_RQMAP(qmap_idx + i), vf_q_idx);
  883. }
  884. return 0;
  885. }
  886. /**
  887. * fm10k_iov_set_lport_pf - Assign and enable a logical port for a given VF
  888. * @hw: pointer to hardware structure
  889. * @vf_info: pointer to VF information structure
  890. * @lport_idx: Logical port offset from the hardware glort
  891. * @flags: Set of capability flags to extend port beyond basic functionality
  892. *
  893. * This function allows enabling a VF port by assigning it a GLORT and
  894. * setting the flags so that it can enable an Rx mode.
  895. **/
  896. static s32 fm10k_iov_set_lport_pf(struct fm10k_hw *hw,
  897. struct fm10k_vf_info *vf_info,
  898. u16 lport_idx, u8 flags)
  899. {
  900. u16 glort = (hw->mac.dglort_map + lport_idx) & FM10K_DGLORTMAP_NONE;
  901. /* if glort is not valid return error */
  902. if (!fm10k_glort_valid_pf(hw, glort))
  903. return FM10K_ERR_PARAM;
  904. vf_info->vf_flags = flags | FM10K_VF_FLAG_NONE_CAPABLE;
  905. vf_info->glort = glort;
  906. return 0;
  907. }
  908. /**
  909. * fm10k_iov_reset_lport_pf - Disable a logical port for a given VF
  910. * @hw: pointer to hardware structure
  911. * @vf_info: pointer to VF information structure
  912. *
  913. * This function disables a VF port by stripping it of a GLORT and
  914. * setting the flags so that it cannot enable any Rx mode.
  915. **/
  916. static void fm10k_iov_reset_lport_pf(struct fm10k_hw *hw,
  917. struct fm10k_vf_info *vf_info)
  918. {
  919. u32 msg[1];
  920. /* need to disable the port if it is already enabled */
  921. if (FM10K_VF_FLAG_ENABLED(vf_info)) {
  922. /* notify switch that this port has been disabled */
  923. fm10k_update_lport_state_pf(hw, vf_info->glort, 1, false);
  924. /* generate port state response to notify VF it is not ready */
  925. fm10k_tlv_msg_init(msg, FM10K_VF_MSG_ID_LPORT_STATE);
  926. vf_info->mbx.ops.enqueue_tx(hw, &vf_info->mbx, msg);
  927. }
  928. /* clear flags and glort if it exists */
  929. vf_info->vf_flags = 0;
  930. vf_info->glort = 0;
  931. }
  932. /**
  933. * fm10k_iov_update_stats_pf - Updates hardware related statistics for VFs
  934. * @hw: pointer to hardware structure
  935. * @q: stats for all queues of a VF
  936. * @vf_idx: index of VF
  937. *
  938. * This function collects queue stats for VFs.
  939. **/
  940. static void fm10k_iov_update_stats_pf(struct fm10k_hw *hw,
  941. struct fm10k_hw_stats_q *q,
  942. u16 vf_idx)
  943. {
  944. u32 idx, qpp;
  945. /* get stats for all of the queues */
  946. qpp = fm10k_queues_per_pool(hw);
  947. idx = fm10k_vf_queue_index(hw, vf_idx);
  948. fm10k_update_hw_stats_q(hw, q, idx, qpp);
  949. }
  950. static s32 fm10k_iov_report_timestamp_pf(struct fm10k_hw *hw,
  951. struct fm10k_vf_info *vf_info,
  952. u64 timestamp)
  953. {
  954. u32 msg[4];
  955. /* generate port state response to notify VF it is not ready */
  956. fm10k_tlv_msg_init(msg, FM10K_VF_MSG_ID_1588);
  957. fm10k_tlv_attr_put_u64(msg, FM10K_1588_MSG_TIMESTAMP, timestamp);
  958. return vf_info->mbx.ops.enqueue_tx(hw, &vf_info->mbx, msg);
  959. }
  960. /**
  961. * fm10k_iov_msg_msix_pf - Message handler for MSI-X request from VF
  962. * @hw: Pointer to hardware structure
  963. * @results: Pointer array to message, results[0] is pointer to message
  964. * @mbx: Pointer to mailbox information structure
  965. *
  966. * This function is a default handler for MSI-X requests from the VF. The
  967. * assumption is that in this case it is acceptable to just directly
  968. * hand off the message from the VF to the underlying shared code.
  969. **/
  970. s32 fm10k_iov_msg_msix_pf(struct fm10k_hw *hw, u32 **results,
  971. struct fm10k_mbx_info *mbx)
  972. {
  973. struct fm10k_vf_info *vf_info = (struct fm10k_vf_info *)mbx;
  974. u8 vf_idx = vf_info->vf_idx;
  975. return hw->iov.ops.assign_int_moderator(hw, vf_idx);
  976. }
  977. /**
  978. * fm10k_iov_msg_mac_vlan_pf - Message handler for MAC/VLAN request from VF
  979. * @hw: Pointer to hardware structure
  980. * @results: Pointer array to message, results[0] is pointer to message
  981. * @mbx: Pointer to mailbox information structure
  982. *
  983. * This function is a default handler for MAC/VLAN requests from the VF.
  984. * The assumption is that in this case it is acceptable to just directly
  985. * hand off the message from the VF to the underlying shared code.
  986. **/
  987. s32 fm10k_iov_msg_mac_vlan_pf(struct fm10k_hw *hw, u32 **results,
  988. struct fm10k_mbx_info *mbx)
  989. {
  990. struct fm10k_vf_info *vf_info = (struct fm10k_vf_info *)mbx;
  991. int err = 0;
  992. u8 mac[ETH_ALEN];
  993. u32 *result;
  994. u16 vlan;
  995. u32 vid;
  996. /* we shouldn't be updating rules on a disabled interface */
  997. if (!FM10K_VF_FLAG_ENABLED(vf_info))
  998. err = FM10K_ERR_PARAM;
  999. if (!err && !!results[FM10K_MAC_VLAN_MSG_VLAN]) {
  1000. result = results[FM10K_MAC_VLAN_MSG_VLAN];
  1001. /* record VLAN id requested */
  1002. err = fm10k_tlv_attr_get_u32(result, &vid);
  1003. if (err)
  1004. return err;
  1005. /* if VLAN ID is 0, set the default VLAN ID instead of 0 */
  1006. if (!vid || (vid == FM10K_VLAN_CLEAR)) {
  1007. if (vf_info->pf_vid)
  1008. vid |= vf_info->pf_vid;
  1009. else
  1010. vid |= vf_info->sw_vid;
  1011. } else if (vid != vf_info->pf_vid) {
  1012. return FM10K_ERR_PARAM;
  1013. }
  1014. /* update VSI info for VF in regards to VLAN table */
  1015. err = hw->mac.ops.update_vlan(hw, vid, vf_info->vsi,
  1016. !(vid & FM10K_VLAN_CLEAR));
  1017. }
  1018. if (!err && !!results[FM10K_MAC_VLAN_MSG_MAC]) {
  1019. result = results[FM10K_MAC_VLAN_MSG_MAC];
  1020. /* record unicast MAC address requested */
  1021. err = fm10k_tlv_attr_get_mac_vlan(result, mac, &vlan);
  1022. if (err)
  1023. return err;
  1024. /* block attempts to set MAC for a locked device */
  1025. if (is_valid_ether_addr(vf_info->mac) &&
  1026. memcmp(mac, vf_info->mac, ETH_ALEN))
  1027. return FM10K_ERR_PARAM;
  1028. /* if VLAN ID is 0, set the default VLAN ID instead of 0 */
  1029. if (!vlan || (vlan == FM10K_VLAN_CLEAR)) {
  1030. if (vf_info->pf_vid)
  1031. vlan |= vf_info->pf_vid;
  1032. else
  1033. vlan |= vf_info->sw_vid;
  1034. } else if (vf_info->pf_vid) {
  1035. return FM10K_ERR_PARAM;
  1036. }
  1037. /* notify switch of request for new unicast address */
  1038. err = hw->mac.ops.update_uc_addr(hw, vf_info->glort, mac, vlan,
  1039. !(vlan & FM10K_VLAN_CLEAR), 0);
  1040. }
  1041. if (!err && !!results[FM10K_MAC_VLAN_MSG_MULTICAST]) {
  1042. result = results[FM10K_MAC_VLAN_MSG_MULTICAST];
  1043. /* record multicast MAC address requested */
  1044. err = fm10k_tlv_attr_get_mac_vlan(result, mac, &vlan);
  1045. if (err)
  1046. return err;
  1047. /* verify that the VF is allowed to request multicast */
  1048. if (!(vf_info->vf_flags & FM10K_VF_FLAG_MULTI_ENABLED))
  1049. return FM10K_ERR_PARAM;
  1050. /* if VLAN ID is 0, set the default VLAN ID instead of 0 */
  1051. if (!vlan || (vlan == FM10K_VLAN_CLEAR)) {
  1052. if (vf_info->pf_vid)
  1053. vlan |= vf_info->pf_vid;
  1054. else
  1055. vlan |= vf_info->sw_vid;
  1056. } else if (vf_info->pf_vid) {
  1057. return FM10K_ERR_PARAM;
  1058. }
  1059. /* notify switch of request for new multicast address */
  1060. err = hw->mac.ops.update_mc_addr(hw, vf_info->glort, mac, vlan,
  1061. !(vlan & FM10K_VLAN_CLEAR));
  1062. }
  1063. return err;
  1064. }
  1065. /**
  1066. * fm10k_iov_supported_xcast_mode_pf - Determine best match for xcast mode
  1067. * @vf_info: VF info structure containing capability flags
  1068. * @mode: Requested xcast mode
  1069. *
  1070. * This function outputs the mode that most closely matches the requested
  1071. * mode. If not modes match it will request we disable the port
  1072. **/
  1073. static u8 fm10k_iov_supported_xcast_mode_pf(struct fm10k_vf_info *vf_info,
  1074. u8 mode)
  1075. {
  1076. u8 vf_flags = vf_info->vf_flags;
  1077. /* match up mode to capabilities as best as possible */
  1078. switch (mode) {
  1079. case FM10K_XCAST_MODE_PROMISC:
  1080. if (vf_flags & FM10K_VF_FLAG_PROMISC_CAPABLE)
  1081. return FM10K_XCAST_MODE_PROMISC;
  1082. /* fallthough */
  1083. case FM10K_XCAST_MODE_ALLMULTI:
  1084. if (vf_flags & FM10K_VF_FLAG_ALLMULTI_CAPABLE)
  1085. return FM10K_XCAST_MODE_ALLMULTI;
  1086. /* fallthough */
  1087. case FM10K_XCAST_MODE_MULTI:
  1088. if (vf_flags & FM10K_VF_FLAG_MULTI_CAPABLE)
  1089. return FM10K_XCAST_MODE_MULTI;
  1090. /* fallthough */
  1091. case FM10K_XCAST_MODE_NONE:
  1092. if (vf_flags & FM10K_VF_FLAG_NONE_CAPABLE)
  1093. return FM10K_XCAST_MODE_NONE;
  1094. /* fallthough */
  1095. default:
  1096. break;
  1097. }
  1098. /* disable interface as it should not be able to request any */
  1099. return FM10K_XCAST_MODE_DISABLE;
  1100. }
  1101. /**
  1102. * fm10k_iov_msg_lport_state_pf - Message handler for port state requests
  1103. * @hw: Pointer to hardware structure
  1104. * @results: Pointer array to message, results[0] is pointer to message
  1105. * @mbx: Pointer to mailbox information structure
  1106. *
  1107. * This function is a default handler for port state requests. The port
  1108. * state requests for now are basic and consist of enabling or disabling
  1109. * the port.
  1110. **/
  1111. s32 fm10k_iov_msg_lport_state_pf(struct fm10k_hw *hw, u32 **results,
  1112. struct fm10k_mbx_info *mbx)
  1113. {
  1114. struct fm10k_vf_info *vf_info = (struct fm10k_vf_info *)mbx;
  1115. u32 *result;
  1116. s32 err = 0;
  1117. u32 msg[2];
  1118. u8 mode = 0;
  1119. /* verify VF is allowed to enable even minimal mode */
  1120. if (!(vf_info->vf_flags & FM10K_VF_FLAG_NONE_CAPABLE))
  1121. return FM10K_ERR_PARAM;
  1122. if (!!results[FM10K_LPORT_STATE_MSG_XCAST_MODE]) {
  1123. result = results[FM10K_LPORT_STATE_MSG_XCAST_MODE];
  1124. /* XCAST mode update requested */
  1125. err = fm10k_tlv_attr_get_u8(result, &mode);
  1126. if (err)
  1127. return FM10K_ERR_PARAM;
  1128. /* prep for possible demotion depending on capabilities */
  1129. mode = fm10k_iov_supported_xcast_mode_pf(vf_info, mode);
  1130. /* if mode is not currently enabled, enable it */
  1131. if (!(FM10K_VF_FLAG_ENABLED(vf_info) & (1 << mode)))
  1132. fm10k_update_xcast_mode_pf(hw, vf_info->glort, mode);
  1133. /* swap mode back to a bit flag */
  1134. mode = FM10K_VF_FLAG_SET_MODE(mode);
  1135. } else if (!results[FM10K_LPORT_STATE_MSG_DISABLE]) {
  1136. /* need to disable the port if it is already enabled */
  1137. if (FM10K_VF_FLAG_ENABLED(vf_info))
  1138. err = fm10k_update_lport_state_pf(hw, vf_info->glort,
  1139. 1, false);
  1140. /* we need to clear VF_FLAG_ENABLED flags in order to ensure
  1141. * that we actually re-enable the LPORT state below. Note that
  1142. * this has no impact if the VF is already disabled, as the
  1143. * flags are already cleared.
  1144. */
  1145. if (!err)
  1146. vf_info->vf_flags = FM10K_VF_FLAG_CAPABLE(vf_info);
  1147. /* when enabling the port we should reset the rate limiters */
  1148. hw->iov.ops.configure_tc(hw, vf_info->vf_idx, vf_info->rate);
  1149. /* set mode for minimal functionality */
  1150. mode = FM10K_VF_FLAG_SET_MODE_NONE;
  1151. /* generate port state response to notify VF it is ready */
  1152. fm10k_tlv_msg_init(msg, FM10K_VF_MSG_ID_LPORT_STATE);
  1153. fm10k_tlv_attr_put_bool(msg, FM10K_LPORT_STATE_MSG_READY);
  1154. mbx->ops.enqueue_tx(hw, mbx, msg);
  1155. }
  1156. /* if enable state toggled note the update */
  1157. if (!err && (!FM10K_VF_FLAG_ENABLED(vf_info) != !mode))
  1158. err = fm10k_update_lport_state_pf(hw, vf_info->glort, 1,
  1159. !!mode);
  1160. /* if state change succeeded, then update our stored state */
  1161. mode |= FM10K_VF_FLAG_CAPABLE(vf_info);
  1162. if (!err)
  1163. vf_info->vf_flags = mode;
  1164. return err;
  1165. }
  1166. const struct fm10k_msg_data fm10k_iov_msg_data_pf[] = {
  1167. FM10K_TLV_MSG_TEST_HANDLER(fm10k_tlv_msg_test),
  1168. FM10K_VF_MSG_MSIX_HANDLER(fm10k_iov_msg_msix_pf),
  1169. FM10K_VF_MSG_MAC_VLAN_HANDLER(fm10k_iov_msg_mac_vlan_pf),
  1170. FM10K_VF_MSG_LPORT_STATE_HANDLER(fm10k_iov_msg_lport_state_pf),
  1171. FM10K_TLV_MSG_ERROR_HANDLER(fm10k_tlv_msg_error),
  1172. };
  1173. /**
  1174. * fm10k_update_stats_hw_pf - Updates hardware related statistics of PF
  1175. * @hw: pointer to hardware structure
  1176. * @stats: pointer to the stats structure to update
  1177. *
  1178. * This function collects and aggregates global and per queue hardware
  1179. * statistics.
  1180. **/
  1181. static void fm10k_update_hw_stats_pf(struct fm10k_hw *hw,
  1182. struct fm10k_hw_stats *stats)
  1183. {
  1184. u32 timeout, ur, ca, um, xec, vlan_drop, loopback_drop, nodesc_drop;
  1185. u32 id, id_prev;
  1186. /* Use Tx queue 0 as a canary to detect a reset */
  1187. id = fm10k_read_reg(hw, FM10K_TXQCTL(0));
  1188. /* Read Global Statistics */
  1189. do {
  1190. timeout = fm10k_read_hw_stats_32b(hw, FM10K_STATS_TIMEOUT,
  1191. &stats->timeout);
  1192. ur = fm10k_read_hw_stats_32b(hw, FM10K_STATS_UR, &stats->ur);
  1193. ca = fm10k_read_hw_stats_32b(hw, FM10K_STATS_CA, &stats->ca);
  1194. um = fm10k_read_hw_stats_32b(hw, FM10K_STATS_UM, &stats->um);
  1195. xec = fm10k_read_hw_stats_32b(hw, FM10K_STATS_XEC, &stats->xec);
  1196. vlan_drop = fm10k_read_hw_stats_32b(hw, FM10K_STATS_VLAN_DROP,
  1197. &stats->vlan_drop);
  1198. loopback_drop = fm10k_read_hw_stats_32b(hw,
  1199. FM10K_STATS_LOOPBACK_DROP,
  1200. &stats->loopback_drop);
  1201. nodesc_drop = fm10k_read_hw_stats_32b(hw,
  1202. FM10K_STATS_NODESC_DROP,
  1203. &stats->nodesc_drop);
  1204. /* if value has not changed then we have consistent data */
  1205. id_prev = id;
  1206. id = fm10k_read_reg(hw, FM10K_TXQCTL(0));
  1207. } while ((id ^ id_prev) & FM10K_TXQCTL_ID_MASK);
  1208. /* drop non-ID bits and set VALID ID bit */
  1209. id &= FM10K_TXQCTL_ID_MASK;
  1210. id |= FM10K_STAT_VALID;
  1211. /* Update Global Statistics */
  1212. if (stats->stats_idx == id) {
  1213. stats->timeout.count += timeout;
  1214. stats->ur.count += ur;
  1215. stats->ca.count += ca;
  1216. stats->um.count += um;
  1217. stats->xec.count += xec;
  1218. stats->vlan_drop.count += vlan_drop;
  1219. stats->loopback_drop.count += loopback_drop;
  1220. stats->nodesc_drop.count += nodesc_drop;
  1221. }
  1222. /* Update bases and record current PF id */
  1223. fm10k_update_hw_base_32b(&stats->timeout, timeout);
  1224. fm10k_update_hw_base_32b(&stats->ur, ur);
  1225. fm10k_update_hw_base_32b(&stats->ca, ca);
  1226. fm10k_update_hw_base_32b(&stats->um, um);
  1227. fm10k_update_hw_base_32b(&stats->xec, xec);
  1228. fm10k_update_hw_base_32b(&stats->vlan_drop, vlan_drop);
  1229. fm10k_update_hw_base_32b(&stats->loopback_drop, loopback_drop);
  1230. fm10k_update_hw_base_32b(&stats->nodesc_drop, nodesc_drop);
  1231. stats->stats_idx = id;
  1232. /* Update Queue Statistics */
  1233. fm10k_update_hw_stats_q(hw, stats->q, 0, hw->mac.max_queues);
  1234. }
  1235. /**
  1236. * fm10k_rebind_hw_stats_pf - Resets base for hardware statistics of PF
  1237. * @hw: pointer to hardware structure
  1238. * @stats: pointer to the stats structure to update
  1239. *
  1240. * This function resets the base for global and per queue hardware
  1241. * statistics.
  1242. **/
  1243. static void fm10k_rebind_hw_stats_pf(struct fm10k_hw *hw,
  1244. struct fm10k_hw_stats *stats)
  1245. {
  1246. /* Unbind Global Statistics */
  1247. fm10k_unbind_hw_stats_32b(&stats->timeout);
  1248. fm10k_unbind_hw_stats_32b(&stats->ur);
  1249. fm10k_unbind_hw_stats_32b(&stats->ca);
  1250. fm10k_unbind_hw_stats_32b(&stats->um);
  1251. fm10k_unbind_hw_stats_32b(&stats->xec);
  1252. fm10k_unbind_hw_stats_32b(&stats->vlan_drop);
  1253. fm10k_unbind_hw_stats_32b(&stats->loopback_drop);
  1254. fm10k_unbind_hw_stats_32b(&stats->nodesc_drop);
  1255. /* Unbind Queue Statistics */
  1256. fm10k_unbind_hw_stats_q(stats->q, 0, hw->mac.max_queues);
  1257. /* Reinitialize bases for all stats */
  1258. fm10k_update_hw_stats_pf(hw, stats);
  1259. }
  1260. /**
  1261. * fm10k_set_dma_mask_pf - Configures PhyAddrSpace to limit DMA to system
  1262. * @hw: pointer to hardware structure
  1263. * @dma_mask: 64 bit DMA mask required for platform
  1264. *
  1265. * This function sets the PHYADDR.PhyAddrSpace bits for the endpoint in order
  1266. * to limit the access to memory beyond what is physically in the system.
  1267. **/
  1268. static void fm10k_set_dma_mask_pf(struct fm10k_hw *hw, u64 dma_mask)
  1269. {
  1270. /* we need to write the upper 32 bits of DMA mask to PhyAddrSpace */
  1271. u32 phyaddr = (u32)(dma_mask >> 32);
  1272. fm10k_write_reg(hw, FM10K_PHYADDR, phyaddr);
  1273. }
  1274. /**
  1275. * fm10k_get_fault_pf - Record a fault in one of the interface units
  1276. * @hw: pointer to hardware structure
  1277. * @type: pointer to fault type register offset
  1278. * @fault: pointer to memory location to record the fault
  1279. *
  1280. * Record the fault register contents to the fault data structure and
  1281. * clear the entry from the register.
  1282. *
  1283. * Returns ERR_PARAM if invalid register is specified or no error is present.
  1284. **/
  1285. static s32 fm10k_get_fault_pf(struct fm10k_hw *hw, int type,
  1286. struct fm10k_fault *fault)
  1287. {
  1288. u32 func;
  1289. /* verify the fault register is in range and is aligned */
  1290. switch (type) {
  1291. case FM10K_PCA_FAULT:
  1292. case FM10K_THI_FAULT:
  1293. case FM10K_FUM_FAULT:
  1294. break;
  1295. default:
  1296. return FM10K_ERR_PARAM;
  1297. }
  1298. /* only service faults that are valid */
  1299. func = fm10k_read_reg(hw, type + FM10K_FAULT_FUNC);
  1300. if (!(func & FM10K_FAULT_FUNC_VALID))
  1301. return FM10K_ERR_PARAM;
  1302. /* read remaining fields */
  1303. fault->address = fm10k_read_reg(hw, type + FM10K_FAULT_ADDR_HI);
  1304. fault->address <<= 32;
  1305. fault->address = fm10k_read_reg(hw, type + FM10K_FAULT_ADDR_LO);
  1306. fault->specinfo = fm10k_read_reg(hw, type + FM10K_FAULT_SPECINFO);
  1307. /* clear valid bit to allow for next error */
  1308. fm10k_write_reg(hw, type + FM10K_FAULT_FUNC, FM10K_FAULT_FUNC_VALID);
  1309. /* Record which function triggered the error */
  1310. if (func & FM10K_FAULT_FUNC_PF)
  1311. fault->func = 0;
  1312. else
  1313. fault->func = 1 + ((func & FM10K_FAULT_FUNC_VF_MASK) >>
  1314. FM10K_FAULT_FUNC_VF_SHIFT);
  1315. /* record fault type */
  1316. fault->type = func & FM10K_FAULT_FUNC_TYPE_MASK;
  1317. return 0;
  1318. }
  1319. /**
  1320. * fm10k_request_lport_map_pf - Request LPORT map from the switch API
  1321. * @hw: pointer to hardware structure
  1322. *
  1323. **/
  1324. static s32 fm10k_request_lport_map_pf(struct fm10k_hw *hw)
  1325. {
  1326. struct fm10k_mbx_info *mbx = &hw->mbx;
  1327. u32 msg[1];
  1328. /* issue request asking for LPORT map */
  1329. fm10k_tlv_msg_init(msg, FM10K_PF_MSG_ID_LPORT_MAP);
  1330. /* load onto outgoing mailbox */
  1331. return mbx->ops.enqueue_tx(hw, mbx, msg);
  1332. }
  1333. /**
  1334. * fm10k_get_host_state_pf - Returns the state of the switch and mailbox
  1335. * @hw: pointer to hardware structure
  1336. * @switch_ready: pointer to boolean value that will record switch state
  1337. *
  1338. * This funciton will check the DMA_CTRL2 register and mailbox in order
  1339. * to determine if the switch is ready for the PF to begin requesting
  1340. * addresses and mapping traffic to the local interface.
  1341. **/
  1342. static s32 fm10k_get_host_state_pf(struct fm10k_hw *hw, bool *switch_ready)
  1343. {
  1344. s32 ret_val = 0;
  1345. u32 dma_ctrl2;
  1346. /* verify the switch is ready for interaction */
  1347. dma_ctrl2 = fm10k_read_reg(hw, FM10K_DMA_CTRL2);
  1348. if (!(dma_ctrl2 & FM10K_DMA_CTRL2_SWITCH_READY))
  1349. goto out;
  1350. /* retrieve generic host state info */
  1351. ret_val = fm10k_get_host_state_generic(hw, switch_ready);
  1352. if (ret_val)
  1353. goto out;
  1354. /* interface cannot receive traffic without logical ports */
  1355. if (hw->mac.dglort_map == FM10K_DGLORTMAP_NONE)
  1356. ret_val = fm10k_request_lport_map_pf(hw);
  1357. out:
  1358. return ret_val;
  1359. }
  1360. /* This structure defines the attibutes to be parsed below */
  1361. const struct fm10k_tlv_attr fm10k_lport_map_msg_attr[] = {
  1362. FM10K_TLV_ATTR_U32(FM10K_PF_ATTR_ID_LPORT_MAP),
  1363. FM10K_TLV_ATTR_LAST
  1364. };
  1365. /**
  1366. * fm10k_msg_lport_map_pf - Message handler for lport_map message from SM
  1367. * @hw: Pointer to hardware structure
  1368. * @results: pointer array containing parsed data
  1369. * @mbx: Pointer to mailbox information structure
  1370. *
  1371. * This handler configures the lport mapping based on the reply from the
  1372. * switch API.
  1373. **/
  1374. s32 fm10k_msg_lport_map_pf(struct fm10k_hw *hw, u32 **results,
  1375. struct fm10k_mbx_info *mbx)
  1376. {
  1377. u16 glort, mask;
  1378. u32 dglort_map;
  1379. s32 err;
  1380. err = fm10k_tlv_attr_get_u32(results[FM10K_PF_ATTR_ID_LPORT_MAP],
  1381. &dglort_map);
  1382. if (err)
  1383. return err;
  1384. /* extract values out of the header */
  1385. glort = FM10K_MSG_HDR_FIELD_GET(dglort_map, LPORT_MAP_GLORT);
  1386. mask = FM10K_MSG_HDR_FIELD_GET(dglort_map, LPORT_MAP_MASK);
  1387. /* verify mask is set and none of the masked bits in glort are set */
  1388. if (!mask || (glort & ~mask))
  1389. return FM10K_ERR_PARAM;
  1390. /* verify the mask is contiguous, and that it is 1's followed by 0's */
  1391. if (((~(mask - 1) & mask) + mask) & FM10K_DGLORTMAP_NONE)
  1392. return FM10K_ERR_PARAM;
  1393. /* record the glort, mask, and port count */
  1394. hw->mac.dglort_map = dglort_map;
  1395. return 0;
  1396. }
  1397. const struct fm10k_tlv_attr fm10k_update_pvid_msg_attr[] = {
  1398. FM10K_TLV_ATTR_U32(FM10K_PF_ATTR_ID_UPDATE_PVID),
  1399. FM10K_TLV_ATTR_LAST
  1400. };
  1401. /**
  1402. * fm10k_msg_update_pvid_pf - Message handler for port VLAN message from SM
  1403. * @hw: Pointer to hardware structure
  1404. * @results: pointer array containing parsed data
  1405. * @mbx: Pointer to mailbox information structure
  1406. *
  1407. * This handler configures the default VLAN for the PF
  1408. **/
  1409. s32 fm10k_msg_update_pvid_pf(struct fm10k_hw *hw, u32 **results,
  1410. struct fm10k_mbx_info *mbx)
  1411. {
  1412. u16 glort, pvid;
  1413. u32 pvid_update;
  1414. s32 err;
  1415. err = fm10k_tlv_attr_get_u32(results[FM10K_PF_ATTR_ID_UPDATE_PVID],
  1416. &pvid_update);
  1417. if (err)
  1418. return err;
  1419. /* extract values from the pvid update */
  1420. glort = FM10K_MSG_HDR_FIELD_GET(pvid_update, UPDATE_PVID_GLORT);
  1421. pvid = FM10K_MSG_HDR_FIELD_GET(pvid_update, UPDATE_PVID_PVID);
  1422. /* if glort is not valid return error */
  1423. if (!fm10k_glort_valid_pf(hw, glort))
  1424. return FM10K_ERR_PARAM;
  1425. /* verify VID is valid */
  1426. if (pvid >= FM10K_VLAN_TABLE_VID_MAX)
  1427. return FM10K_ERR_PARAM;
  1428. /* record the port VLAN ID value */
  1429. hw->mac.default_vid = pvid;
  1430. return 0;
  1431. }
  1432. /**
  1433. * fm10k_record_global_table_data - Move global table data to swapi table info
  1434. * @from: pointer to source table data structure
  1435. * @to: pointer to destination table info structure
  1436. *
  1437. * This function is will copy table_data to the table_info contained in
  1438. * the hw struct.
  1439. **/
  1440. static void fm10k_record_global_table_data(struct fm10k_global_table_data *from,
  1441. struct fm10k_swapi_table_info *to)
  1442. {
  1443. /* convert from le32 struct to CPU byte ordered values */
  1444. to->used = le32_to_cpu(from->used);
  1445. to->avail = le32_to_cpu(from->avail);
  1446. }
  1447. const struct fm10k_tlv_attr fm10k_err_msg_attr[] = {
  1448. FM10K_TLV_ATTR_LE_STRUCT(FM10K_PF_ATTR_ID_ERR,
  1449. sizeof(struct fm10k_swapi_error)),
  1450. FM10K_TLV_ATTR_LAST
  1451. };
  1452. /**
  1453. * fm10k_msg_err_pf - Message handler for error reply
  1454. * @hw: Pointer to hardware structure
  1455. * @results: pointer array containing parsed data
  1456. * @mbx: Pointer to mailbox information structure
  1457. *
  1458. * This handler will capture the data for any error replies to previous
  1459. * messages that the PF has sent.
  1460. **/
  1461. s32 fm10k_msg_err_pf(struct fm10k_hw *hw, u32 **results,
  1462. struct fm10k_mbx_info *mbx)
  1463. {
  1464. struct fm10k_swapi_error err_msg;
  1465. s32 err;
  1466. /* extract structure from message */
  1467. err = fm10k_tlv_attr_get_le_struct(results[FM10K_PF_ATTR_ID_ERR],
  1468. &err_msg, sizeof(err_msg));
  1469. if (err)
  1470. return err;
  1471. /* record table status */
  1472. fm10k_record_global_table_data(&err_msg.mac, &hw->swapi.mac);
  1473. fm10k_record_global_table_data(&err_msg.nexthop, &hw->swapi.nexthop);
  1474. fm10k_record_global_table_data(&err_msg.ffu, &hw->swapi.ffu);
  1475. /* record SW API status value */
  1476. hw->swapi.status = le32_to_cpu(err_msg.status);
  1477. return 0;
  1478. }
  1479. const struct fm10k_tlv_attr fm10k_1588_timestamp_msg_attr[] = {
  1480. FM10K_TLV_ATTR_LE_STRUCT(FM10K_PF_ATTR_ID_1588_TIMESTAMP,
  1481. sizeof(struct fm10k_swapi_1588_timestamp)),
  1482. FM10K_TLV_ATTR_LAST
  1483. };
  1484. /* currently there is no shared 1588 timestamp handler */
  1485. /**
  1486. * fm10k_adjust_systime_pf - Adjust systime frequency
  1487. * @hw: pointer to hardware structure
  1488. * @ppb: adjustment rate in parts per billion
  1489. *
  1490. * This function will adjust the SYSTIME_CFG register contained in BAR 4
  1491. * if this function is supported for BAR 4 access. The adjustment amount
  1492. * is based on the parts per billion value provided and adjusted to a
  1493. * value based on parts per 2^48 clock cycles.
  1494. *
  1495. * If adjustment is not supported or the requested value is too large
  1496. * we will return an error.
  1497. **/
  1498. static s32 fm10k_adjust_systime_pf(struct fm10k_hw *hw, s32 ppb)
  1499. {
  1500. u64 systime_adjust;
  1501. /* if sw_addr is not set we don't have switch register access */
  1502. if (!hw->sw_addr)
  1503. return ppb ? FM10K_ERR_PARAM : 0;
  1504. /* we must convert the value from parts per billion to parts per
  1505. * 2^48 cycles. In addition I have opted to only use the 30 most
  1506. * significant bits of the adjustment value as the 8 least
  1507. * significant bits are located in another register and represent
  1508. * a value significantly less than a part per billion, the result
  1509. * of dropping the 8 least significant bits is that the adjustment
  1510. * value is effectively multiplied by 2^8 when we write it.
  1511. *
  1512. * As a result of all this the math for this breaks down as follows:
  1513. * ppb / 10^9 == adjust * 2^8 / 2^48
  1514. * If we solve this for adjust, and simplify it comes out as:
  1515. * ppb * 2^31 / 5^9 == adjust
  1516. */
  1517. systime_adjust = (ppb < 0) ? -ppb : ppb;
  1518. systime_adjust <<= 31;
  1519. do_div(systime_adjust, 1953125);
  1520. /* verify the requested adjustment value is in range */
  1521. if (systime_adjust > FM10K_SW_SYSTIME_ADJUST_MASK)
  1522. return FM10K_ERR_PARAM;
  1523. if (ppb > 0)
  1524. systime_adjust |= FM10K_SW_SYSTIME_ADJUST_DIR_POSITIVE;
  1525. fm10k_write_sw_reg(hw, FM10K_SW_SYSTIME_ADJUST, (u32)systime_adjust);
  1526. return 0;
  1527. }
  1528. /**
  1529. * fm10k_read_systime_pf - Reads value of systime registers
  1530. * @hw: pointer to the hardware structure
  1531. *
  1532. * Function reads the content of 2 registers, combined to represent a 64 bit
  1533. * value measured in nanosecods. In order to guarantee the value is accurate
  1534. * we check the 32 most significant bits both before and after reading the
  1535. * 32 least significant bits to verify they didn't change as we were reading
  1536. * the registers.
  1537. **/
  1538. static u64 fm10k_read_systime_pf(struct fm10k_hw *hw)
  1539. {
  1540. u32 systime_l, systime_h, systime_tmp;
  1541. systime_h = fm10k_read_reg(hw, FM10K_SYSTIME + 1);
  1542. do {
  1543. systime_tmp = systime_h;
  1544. systime_l = fm10k_read_reg(hw, FM10K_SYSTIME);
  1545. systime_h = fm10k_read_reg(hw, FM10K_SYSTIME + 1);
  1546. } while (systime_tmp != systime_h);
  1547. return ((u64)systime_h << 32) | systime_l;
  1548. }
  1549. static const struct fm10k_msg_data fm10k_msg_data_pf[] = {
  1550. FM10K_PF_MSG_ERR_HANDLER(XCAST_MODES, fm10k_msg_err_pf),
  1551. FM10K_PF_MSG_ERR_HANDLER(UPDATE_MAC_FWD_RULE, fm10k_msg_err_pf),
  1552. FM10K_PF_MSG_LPORT_MAP_HANDLER(fm10k_msg_lport_map_pf),
  1553. FM10K_PF_MSG_ERR_HANDLER(LPORT_CREATE, fm10k_msg_err_pf),
  1554. FM10K_PF_MSG_ERR_HANDLER(LPORT_DELETE, fm10k_msg_err_pf),
  1555. FM10K_PF_MSG_UPDATE_PVID_HANDLER(fm10k_msg_update_pvid_pf),
  1556. FM10K_TLV_MSG_ERROR_HANDLER(fm10k_tlv_msg_error),
  1557. };
  1558. static struct fm10k_mac_ops mac_ops_pf = {
  1559. .get_bus_info = &fm10k_get_bus_info_generic,
  1560. .reset_hw = &fm10k_reset_hw_pf,
  1561. .init_hw = &fm10k_init_hw_pf,
  1562. .start_hw = &fm10k_start_hw_generic,
  1563. .stop_hw = &fm10k_stop_hw_generic,
  1564. .is_slot_appropriate = &fm10k_is_slot_appropriate_pf,
  1565. .update_vlan = &fm10k_update_vlan_pf,
  1566. .read_mac_addr = &fm10k_read_mac_addr_pf,
  1567. .update_uc_addr = &fm10k_update_uc_addr_pf,
  1568. .update_mc_addr = &fm10k_update_mc_addr_pf,
  1569. .update_xcast_mode = &fm10k_update_xcast_mode_pf,
  1570. .update_int_moderator = &fm10k_update_int_moderator_pf,
  1571. .update_lport_state = &fm10k_update_lport_state_pf,
  1572. .update_hw_stats = &fm10k_update_hw_stats_pf,
  1573. .rebind_hw_stats = &fm10k_rebind_hw_stats_pf,
  1574. .configure_dglort_map = &fm10k_configure_dglort_map_pf,
  1575. .set_dma_mask = &fm10k_set_dma_mask_pf,
  1576. .get_fault = &fm10k_get_fault_pf,
  1577. .get_host_state = &fm10k_get_host_state_pf,
  1578. .adjust_systime = &fm10k_adjust_systime_pf,
  1579. .read_systime = &fm10k_read_systime_pf,
  1580. };
  1581. static struct fm10k_iov_ops iov_ops_pf = {
  1582. .assign_resources = &fm10k_iov_assign_resources_pf,
  1583. .configure_tc = &fm10k_iov_configure_tc_pf,
  1584. .assign_int_moderator = &fm10k_iov_assign_int_moderator_pf,
  1585. .assign_default_mac_vlan = fm10k_iov_assign_default_mac_vlan_pf,
  1586. .reset_resources = &fm10k_iov_reset_resources_pf,
  1587. .set_lport = &fm10k_iov_set_lport_pf,
  1588. .reset_lport = &fm10k_iov_reset_lport_pf,
  1589. .update_stats = &fm10k_iov_update_stats_pf,
  1590. .report_timestamp = &fm10k_iov_report_timestamp_pf,
  1591. };
  1592. static s32 fm10k_get_invariants_pf(struct fm10k_hw *hw)
  1593. {
  1594. fm10k_get_invariants_generic(hw);
  1595. return fm10k_sm_mbx_init(hw, &hw->mbx, fm10k_msg_data_pf);
  1596. }
  1597. struct fm10k_info fm10k_pf_info = {
  1598. .mac = fm10k_mac_pf,
  1599. .get_invariants = &fm10k_get_invariants_pf,
  1600. .mac_ops = &mac_ops_pf,
  1601. .iov_ops = &iov_ops_pf,
  1602. };