fm10k_pf.c 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916
  1. /* Intel Ethernet Switch Host Interface Driver
  2. * Copyright(c) 2013 - 2015 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. /* verify the switch is ready for reset */
  54. reg = fm10k_read_reg(hw, FM10K_DMA_CTRL2);
  55. if (!(reg & FM10K_DMA_CTRL2_SWITCH_READY))
  56. goto out;
  57. /* Inititate data path reset */
  58. reg |= FM10K_DMA_CTRL_DATAPATH_RESET;
  59. fm10k_write_reg(hw, FM10K_DMA_CTRL, reg);
  60. /* Flush write and allow 100us for reset to complete */
  61. fm10k_write_flush(hw);
  62. udelay(FM10K_RESET_TIMEOUT);
  63. /* Verify we made it out of reset */
  64. reg = fm10k_read_reg(hw, FM10K_IP);
  65. if (!(reg & FM10K_IP_NOTINRESET))
  66. err = FM10K_ERR_RESET_FAILED;
  67. out:
  68. return err;
  69. }
  70. /**
  71. * fm10k_is_ari_hierarchy_pf - Indicate ARI hierarchy support
  72. * @hw: pointer to hardware structure
  73. *
  74. * Looks at the ARI hierarchy bit to determine whether ARI is supported or not.
  75. **/
  76. static bool fm10k_is_ari_hierarchy_pf(struct fm10k_hw *hw)
  77. {
  78. u16 sriov_ctrl = fm10k_read_pci_cfg_word(hw, FM10K_PCIE_SRIOV_CTRL);
  79. return !!(sriov_ctrl & FM10K_PCIE_SRIOV_CTRL_VFARI);
  80. }
  81. /**
  82. * fm10k_init_hw_pf - PF hardware initialization
  83. * @hw: pointer to hardware structure
  84. *
  85. **/
  86. static s32 fm10k_init_hw_pf(struct fm10k_hw *hw)
  87. {
  88. u32 dma_ctrl, txqctl;
  89. u16 i;
  90. /* Establish default VSI as valid */
  91. fm10k_write_reg(hw, FM10K_DGLORTDEC(fm10k_dglort_default), 0);
  92. fm10k_write_reg(hw, FM10K_DGLORTMAP(fm10k_dglort_default),
  93. FM10K_DGLORTMAP_ANY);
  94. /* Invalidate all other GLORT entries */
  95. for (i = 1; i < FM10K_DGLORT_COUNT; i++)
  96. fm10k_write_reg(hw, FM10K_DGLORTMAP(i), FM10K_DGLORTMAP_NONE);
  97. /* reset ITR2(0) to point to itself */
  98. fm10k_write_reg(hw, FM10K_ITR2(0), 0);
  99. /* reset VF ITR2(0) to point to 0 avoid PF registers */
  100. fm10k_write_reg(hw, FM10K_ITR2(FM10K_ITR_REG_COUNT_PF), 0);
  101. /* loop through all PF ITR2 registers pointing them to the previous */
  102. for (i = 1; i < FM10K_ITR_REG_COUNT_PF; i++)
  103. fm10k_write_reg(hw, FM10K_ITR2(i), i - 1);
  104. /* Enable interrupt moderator if not already enabled */
  105. fm10k_write_reg(hw, FM10K_INT_CTRL, FM10K_INT_CTRL_ENABLEMODERATOR);
  106. /* compute the default txqctl configuration */
  107. txqctl = FM10K_TXQCTL_PF | FM10K_TXQCTL_UNLIMITED_BW |
  108. (hw->mac.default_vid << FM10K_TXQCTL_VID_SHIFT);
  109. for (i = 0; i < FM10K_MAX_QUEUES; i++) {
  110. /* configure rings for 256 Queue / 32 Descriptor cache mode */
  111. fm10k_write_reg(hw, FM10K_TQDLOC(i),
  112. (i * FM10K_TQDLOC_BASE_32_DESC) |
  113. FM10K_TQDLOC_SIZE_32_DESC);
  114. fm10k_write_reg(hw, FM10K_TXQCTL(i), txqctl);
  115. /* configure rings to provide TPH processing hints */
  116. fm10k_write_reg(hw, FM10K_TPH_TXCTRL(i),
  117. FM10K_TPH_TXCTRL_DESC_TPHEN |
  118. FM10K_TPH_TXCTRL_DESC_RROEN |
  119. FM10K_TPH_TXCTRL_DESC_WROEN |
  120. FM10K_TPH_TXCTRL_DATA_RROEN);
  121. fm10k_write_reg(hw, FM10K_TPH_RXCTRL(i),
  122. FM10K_TPH_RXCTRL_DESC_TPHEN |
  123. FM10K_TPH_RXCTRL_DESC_RROEN |
  124. FM10K_TPH_RXCTRL_DATA_WROEN |
  125. FM10K_TPH_RXCTRL_HDR_WROEN);
  126. }
  127. /* set max hold interval to align with 1.024 usec in all modes and
  128. * store ITR scale
  129. */
  130. switch (hw->bus.speed) {
  131. case fm10k_bus_speed_2500:
  132. dma_ctrl = FM10K_DMA_CTRL_MAX_HOLD_1US_GEN1;
  133. hw->mac.itr_scale = FM10K_TDLEN_ITR_SCALE_GEN1;
  134. break;
  135. case fm10k_bus_speed_5000:
  136. dma_ctrl = FM10K_DMA_CTRL_MAX_HOLD_1US_GEN2;
  137. hw->mac.itr_scale = FM10K_TDLEN_ITR_SCALE_GEN2;
  138. break;
  139. case fm10k_bus_speed_8000:
  140. dma_ctrl = FM10K_DMA_CTRL_MAX_HOLD_1US_GEN3;
  141. hw->mac.itr_scale = FM10K_TDLEN_ITR_SCALE_GEN3;
  142. break;
  143. default:
  144. dma_ctrl = 0;
  145. /* just in case, assume Gen3 ITR scale */
  146. hw->mac.itr_scale = FM10K_TDLEN_ITR_SCALE_GEN3;
  147. break;
  148. }
  149. /* Configure TSO flags */
  150. fm10k_write_reg(hw, FM10K_DTXTCPFLGL, FM10K_TSO_FLAGS_LOW);
  151. fm10k_write_reg(hw, FM10K_DTXTCPFLGH, FM10K_TSO_FLAGS_HI);
  152. /* Enable DMA engine
  153. * Set Rx Descriptor size to 32
  154. * Set Minimum MSS to 64
  155. * Set Maximum number of Rx queues to 256 / 32 Descriptor
  156. */
  157. dma_ctrl |= FM10K_DMA_CTRL_TX_ENABLE | FM10K_DMA_CTRL_RX_ENABLE |
  158. FM10K_DMA_CTRL_RX_DESC_SIZE | FM10K_DMA_CTRL_MINMSS_64 |
  159. FM10K_DMA_CTRL_32_DESC;
  160. fm10k_write_reg(hw, FM10K_DMA_CTRL, dma_ctrl);
  161. /* record maximum queue count, we limit ourselves to 128 */
  162. hw->mac.max_queues = FM10K_MAX_QUEUES_PF;
  163. /* We support either 64 VFs or 7 VFs depending on if we have ARI */
  164. hw->iov.total_vfs = fm10k_is_ari_hierarchy_pf(hw) ? 64 : 7;
  165. return 0;
  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. serial_num = fm10k_read_reg(hw, FM10K_SM_AREA(1));
  229. /* last byte should be all 1's */
  230. if ((~serial_num) << 24)
  231. return FM10K_ERR_INVALID_MAC_ADDR;
  232. perm_addr[0] = (u8)(serial_num >> 24);
  233. perm_addr[1] = (u8)(serial_num >> 16);
  234. perm_addr[2] = (u8)(serial_num >> 8);
  235. serial_num = fm10k_read_reg(hw, FM10K_SM_AREA(0));
  236. /* first byte should be all 1's */
  237. if ((~serial_num) >> 24)
  238. return FM10K_ERR_INVALID_MAC_ADDR;
  239. perm_addr[3] = (u8)(serial_num >> 16);
  240. perm_addr[4] = (u8)(serial_num >> 8);
  241. perm_addr[5] = (u8)(serial_num);
  242. ether_addr_copy(hw->mac.perm_addr, perm_addr);
  243. ether_addr_copy(hw->mac.addr, perm_addr);
  244. return 0;
  245. }
  246. /**
  247. * fm10k_glort_valid_pf - Validate that the provided glort is valid
  248. * @hw: pointer to the HW structure
  249. * @glort: base glort to be validated
  250. *
  251. * This function will return an error if the provided glort is invalid
  252. **/
  253. bool fm10k_glort_valid_pf(struct fm10k_hw *hw, u16 glort)
  254. {
  255. glort &= hw->mac.dglort_map >> FM10K_DGLORTMAP_MASK_SHIFT;
  256. return glort == (hw->mac.dglort_map & FM10K_DGLORTMAP_NONE);
  257. }
  258. /**
  259. * fm10k_update_xc_addr_pf - Update device addresses
  260. * @hw: pointer to the HW structure
  261. * @glort: base resource tag for this request
  262. * @mac: MAC address to add/remove from table
  263. * @vid: VLAN ID to add/remove from table
  264. * @add: Indicates if this is an add or remove operation
  265. * @flags: flags field to indicate add and secure
  266. *
  267. * This function generates a message to the Switch API requesting
  268. * that the given logical port add/remove the given L2 MAC/VLAN address.
  269. **/
  270. static s32 fm10k_update_xc_addr_pf(struct fm10k_hw *hw, u16 glort,
  271. const u8 *mac, u16 vid, bool add, u8 flags)
  272. {
  273. struct fm10k_mbx_info *mbx = &hw->mbx;
  274. struct fm10k_mac_update mac_update;
  275. u32 msg[5];
  276. /* clear set bit from VLAN ID */
  277. vid &= ~FM10K_VLAN_CLEAR;
  278. /* if glort or VLAN are not valid return error */
  279. if (!fm10k_glort_valid_pf(hw, glort) || vid >= FM10K_VLAN_TABLE_VID_MAX)
  280. return FM10K_ERR_PARAM;
  281. /* record fields */
  282. mac_update.mac_lower = cpu_to_le32(((u32)mac[2] << 24) |
  283. ((u32)mac[3] << 16) |
  284. ((u32)mac[4] << 8) |
  285. ((u32)mac[5]));
  286. mac_update.mac_upper = cpu_to_le16(((u16)mac[0] << 8) |
  287. ((u16)mac[1]));
  288. mac_update.vlan = cpu_to_le16(vid);
  289. mac_update.glort = cpu_to_le16(glort);
  290. mac_update.action = add ? 0 : 1;
  291. mac_update.flags = flags;
  292. /* populate mac_update fields */
  293. fm10k_tlv_msg_init(msg, FM10K_PF_MSG_ID_UPDATE_MAC_FWD_RULE);
  294. fm10k_tlv_attr_put_le_struct(msg, FM10K_PF_ATTR_ID_MAC_UPDATE,
  295. &mac_update, sizeof(mac_update));
  296. /* load onto outgoing mailbox */
  297. return mbx->ops.enqueue_tx(hw, mbx, msg);
  298. }
  299. /**
  300. * fm10k_update_uc_addr_pf - Update device unicast addresses
  301. * @hw: pointer to the HW structure
  302. * @glort: base resource tag for this request
  303. * @mac: MAC address to add/remove from table
  304. * @vid: VLAN ID to add/remove from table
  305. * @add: Indicates if this is an add or remove operation
  306. * @flags: flags field to indicate add and secure
  307. *
  308. * This function is used to add or remove unicast addresses for
  309. * the PF.
  310. **/
  311. static s32 fm10k_update_uc_addr_pf(struct fm10k_hw *hw, u16 glort,
  312. const u8 *mac, u16 vid, bool add, u8 flags)
  313. {
  314. /* verify MAC address is valid */
  315. if (!is_valid_ether_addr(mac))
  316. return FM10K_ERR_PARAM;
  317. return fm10k_update_xc_addr_pf(hw, glort, mac, vid, add, flags);
  318. }
  319. /**
  320. * fm10k_update_mc_addr_pf - Update device multicast addresses
  321. * @hw: pointer to the HW structure
  322. * @glort: base resource tag for this request
  323. * @mac: MAC address to add/remove from table
  324. * @vid: VLAN ID to add/remove from table
  325. * @add: Indicates if this is an add or remove operation
  326. *
  327. * This function is used to add or remove multicast MAC addresses for
  328. * the PF.
  329. **/
  330. static s32 fm10k_update_mc_addr_pf(struct fm10k_hw *hw, u16 glort,
  331. const u8 *mac, u16 vid, bool add)
  332. {
  333. /* verify multicast address is valid */
  334. if (!is_multicast_ether_addr(mac))
  335. return FM10K_ERR_PARAM;
  336. return fm10k_update_xc_addr_pf(hw, glort, mac, vid, add, 0);
  337. }
  338. /**
  339. * fm10k_update_xcast_mode_pf - Request update of multicast mode
  340. * @hw: pointer to hardware structure
  341. * @glort: base resource tag for this request
  342. * @mode: integer value indicating mode being requested
  343. *
  344. * This function will attempt to request a higher mode for the port
  345. * so that it can enable either multicast, multicast promiscuous, or
  346. * promiscuous mode of operation.
  347. **/
  348. static s32 fm10k_update_xcast_mode_pf(struct fm10k_hw *hw, u16 glort, u8 mode)
  349. {
  350. struct fm10k_mbx_info *mbx = &hw->mbx;
  351. u32 msg[3], xcast_mode;
  352. if (mode > FM10K_XCAST_MODE_NONE)
  353. return FM10K_ERR_PARAM;
  354. /* if glort is not valid return error */
  355. if (!fm10k_glort_valid_pf(hw, glort))
  356. return FM10K_ERR_PARAM;
  357. /* write xcast mode as a single u32 value,
  358. * lower 16 bits: glort
  359. * upper 16 bits: mode
  360. */
  361. xcast_mode = ((u32)mode << 16) | glort;
  362. /* generate message requesting to change xcast mode */
  363. fm10k_tlv_msg_init(msg, FM10K_PF_MSG_ID_XCAST_MODES);
  364. fm10k_tlv_attr_put_u32(msg, FM10K_PF_ATTR_ID_XCAST_MODE, xcast_mode);
  365. /* load onto outgoing mailbox */
  366. return mbx->ops.enqueue_tx(hw, mbx, msg);
  367. }
  368. /**
  369. * fm10k_update_int_moderator_pf - Update interrupt moderator linked list
  370. * @hw: pointer to hardware structure
  371. *
  372. * This function walks through the MSI-X vector table to determine the
  373. * number of active interrupts and based on that information updates the
  374. * interrupt moderator linked list.
  375. **/
  376. static void fm10k_update_int_moderator_pf(struct fm10k_hw *hw)
  377. {
  378. u32 i;
  379. /* Disable interrupt moderator */
  380. fm10k_write_reg(hw, FM10K_INT_CTRL, 0);
  381. /* loop through PF from last to first looking enabled vectors */
  382. for (i = FM10K_ITR_REG_COUNT_PF - 1; i; i--) {
  383. if (!fm10k_read_reg(hw, FM10K_MSIX_VECTOR_MASK(i)))
  384. break;
  385. }
  386. /* always reset VFITR2[0] to point to last enabled PF vector */
  387. fm10k_write_reg(hw, FM10K_ITR2(FM10K_ITR_REG_COUNT_PF), i);
  388. /* reset ITR2[0] to point to last enabled PF vector */
  389. if (!hw->iov.num_vfs)
  390. fm10k_write_reg(hw, FM10K_ITR2(0), i);
  391. /* Enable interrupt moderator */
  392. fm10k_write_reg(hw, FM10K_INT_CTRL, FM10K_INT_CTRL_ENABLEMODERATOR);
  393. }
  394. /**
  395. * fm10k_update_lport_state_pf - Notify the switch of a change in port state
  396. * @hw: pointer to the HW structure
  397. * @glort: base resource tag for this request
  398. * @count: number of logical ports being updated
  399. * @enable: boolean value indicating enable or disable
  400. *
  401. * This function is used to add/remove a logical port from the switch.
  402. **/
  403. static s32 fm10k_update_lport_state_pf(struct fm10k_hw *hw, u16 glort,
  404. u16 count, bool enable)
  405. {
  406. struct fm10k_mbx_info *mbx = &hw->mbx;
  407. u32 msg[3], lport_msg;
  408. /* do nothing if we are being asked to create or destroy 0 ports */
  409. if (!count)
  410. return 0;
  411. /* if glort is not valid return error */
  412. if (!fm10k_glort_valid_pf(hw, glort))
  413. return FM10K_ERR_PARAM;
  414. /* construct the lport message from the 2 pieces of data we have */
  415. lport_msg = ((u32)count << 16) | glort;
  416. /* generate lport create/delete message */
  417. fm10k_tlv_msg_init(msg, enable ? FM10K_PF_MSG_ID_LPORT_CREATE :
  418. FM10K_PF_MSG_ID_LPORT_DELETE);
  419. fm10k_tlv_attr_put_u32(msg, FM10K_PF_ATTR_ID_PORT, lport_msg);
  420. /* load onto outgoing mailbox */
  421. return mbx->ops.enqueue_tx(hw, mbx, msg);
  422. }
  423. /**
  424. * fm10k_configure_dglort_map_pf - Configures GLORT entry and queues
  425. * @hw: pointer to hardware structure
  426. * @dglort: pointer to dglort configuration structure
  427. *
  428. * Reads the configuration structure contained in dglort_cfg and uses
  429. * that information to then populate a DGLORTMAP/DEC entry and the queues
  430. * to which it has been assigned.
  431. **/
  432. static s32 fm10k_configure_dglort_map_pf(struct fm10k_hw *hw,
  433. struct fm10k_dglort_cfg *dglort)
  434. {
  435. u16 glort, queue_count, vsi_count, pc_count;
  436. u16 vsi, queue, pc, q_idx;
  437. u32 txqctl, dglortdec, dglortmap;
  438. /* verify the dglort pointer */
  439. if (!dglort)
  440. return FM10K_ERR_PARAM;
  441. /* verify the dglort values */
  442. if ((dglort->idx > 7) || (dglort->rss_l > 7) || (dglort->pc_l > 3) ||
  443. (dglort->vsi_l > 6) || (dglort->vsi_b > 64) ||
  444. (dglort->queue_l > 8) || (dglort->queue_b >= 256))
  445. return FM10K_ERR_PARAM;
  446. /* determine count of VSIs and queues */
  447. queue_count = 1 << (dglort->rss_l + dglort->pc_l);
  448. vsi_count = 1 << (dglort->vsi_l + dglort->queue_l);
  449. glort = dglort->glort;
  450. q_idx = dglort->queue_b;
  451. /* configure SGLORT for queues */
  452. for (vsi = 0; vsi < vsi_count; vsi++, glort++) {
  453. for (queue = 0; queue < queue_count; queue++, q_idx++) {
  454. if (q_idx >= FM10K_MAX_QUEUES)
  455. break;
  456. fm10k_write_reg(hw, FM10K_TX_SGLORT(q_idx), glort);
  457. fm10k_write_reg(hw, FM10K_RX_SGLORT(q_idx), glort);
  458. }
  459. }
  460. /* determine count of PCs and queues */
  461. queue_count = 1 << (dglort->queue_l + dglort->rss_l + dglort->vsi_l);
  462. pc_count = 1 << dglort->pc_l;
  463. /* configure PC for Tx queues */
  464. for (pc = 0; pc < pc_count; pc++) {
  465. q_idx = pc + dglort->queue_b;
  466. for (queue = 0; queue < queue_count; queue++) {
  467. if (q_idx >= FM10K_MAX_QUEUES)
  468. break;
  469. txqctl = fm10k_read_reg(hw, FM10K_TXQCTL(q_idx));
  470. txqctl &= ~FM10K_TXQCTL_PC_MASK;
  471. txqctl |= pc << FM10K_TXQCTL_PC_SHIFT;
  472. fm10k_write_reg(hw, FM10K_TXQCTL(q_idx), txqctl);
  473. q_idx += pc_count;
  474. }
  475. }
  476. /* configure DGLORTDEC */
  477. dglortdec = ((u32)(dglort->rss_l) << FM10K_DGLORTDEC_RSSLENGTH_SHIFT) |
  478. ((u32)(dglort->queue_b) << FM10K_DGLORTDEC_QBASE_SHIFT) |
  479. ((u32)(dglort->pc_l) << FM10K_DGLORTDEC_PCLENGTH_SHIFT) |
  480. ((u32)(dglort->vsi_b) << FM10K_DGLORTDEC_VSIBASE_SHIFT) |
  481. ((u32)(dglort->vsi_l) << FM10K_DGLORTDEC_VSILENGTH_SHIFT) |
  482. ((u32)(dglort->queue_l));
  483. if (dglort->inner_rss)
  484. dglortdec |= FM10K_DGLORTDEC_INNERRSS_ENABLE;
  485. /* configure DGLORTMAP */
  486. dglortmap = (dglort->idx == fm10k_dglort_default) ?
  487. FM10K_DGLORTMAP_ANY : FM10K_DGLORTMAP_ZERO;
  488. dglortmap <<= dglort->vsi_l + dglort->queue_l + dglort->shared_l;
  489. dglortmap |= dglort->glort;
  490. /* write values to hardware */
  491. fm10k_write_reg(hw, FM10K_DGLORTDEC(dglort->idx), dglortdec);
  492. fm10k_write_reg(hw, FM10K_DGLORTMAP(dglort->idx), dglortmap);
  493. return 0;
  494. }
  495. u16 fm10k_queues_per_pool(struct fm10k_hw *hw)
  496. {
  497. u16 num_pools = hw->iov.num_pools;
  498. return (num_pools > 32) ? 2 : (num_pools > 16) ? 4 : (num_pools > 8) ?
  499. 8 : FM10K_MAX_QUEUES_POOL;
  500. }
  501. u16 fm10k_vf_queue_index(struct fm10k_hw *hw, u16 vf_idx)
  502. {
  503. u16 num_vfs = hw->iov.num_vfs;
  504. u16 vf_q_idx = FM10K_MAX_QUEUES;
  505. vf_q_idx -= fm10k_queues_per_pool(hw) * (num_vfs - vf_idx);
  506. return vf_q_idx;
  507. }
  508. static u16 fm10k_vectors_per_pool(struct fm10k_hw *hw)
  509. {
  510. u16 num_pools = hw->iov.num_pools;
  511. return (num_pools > 32) ? 8 : (num_pools > 16) ? 16 :
  512. FM10K_MAX_VECTORS_POOL;
  513. }
  514. static u16 fm10k_vf_vector_index(struct fm10k_hw *hw, u16 vf_idx)
  515. {
  516. u16 vf_v_idx = FM10K_MAX_VECTORS_PF;
  517. vf_v_idx += fm10k_vectors_per_pool(hw) * vf_idx;
  518. return vf_v_idx;
  519. }
  520. /**
  521. * fm10k_iov_assign_resources_pf - Assign pool resources for virtualization
  522. * @hw: pointer to the HW structure
  523. * @num_vfs: number of VFs to be allocated
  524. * @num_pools: number of virtualization pools to be allocated
  525. *
  526. * Allocates queues and traffic classes to virtualization entities to prepare
  527. * the PF for SR-IOV and VMDq
  528. **/
  529. static s32 fm10k_iov_assign_resources_pf(struct fm10k_hw *hw, u16 num_vfs,
  530. u16 num_pools)
  531. {
  532. u16 qmap_stride, qpp, vpp, vf_q_idx, vf_q_idx0, qmap_idx;
  533. u32 vid = hw->mac.default_vid << FM10K_TXQCTL_VID_SHIFT;
  534. int i, j;
  535. /* hardware only supports up to 64 pools */
  536. if (num_pools > 64)
  537. return FM10K_ERR_PARAM;
  538. /* the number of VFs cannot exceed the number of pools */
  539. if ((num_vfs > num_pools) || (num_vfs > hw->iov.total_vfs))
  540. return FM10K_ERR_PARAM;
  541. /* record number of virtualization entities */
  542. hw->iov.num_vfs = num_vfs;
  543. hw->iov.num_pools = num_pools;
  544. /* determine qmap offsets and counts */
  545. qmap_stride = (num_vfs > 8) ? 32 : 256;
  546. qpp = fm10k_queues_per_pool(hw);
  547. vpp = fm10k_vectors_per_pool(hw);
  548. /* calculate starting index for queues */
  549. vf_q_idx = fm10k_vf_queue_index(hw, 0);
  550. qmap_idx = 0;
  551. /* establish TCs with -1 credits and no quanta to prevent transmit */
  552. for (i = 0; i < num_vfs; i++) {
  553. fm10k_write_reg(hw, FM10K_TC_MAXCREDIT(i), 0);
  554. fm10k_write_reg(hw, FM10K_TC_RATE(i), 0);
  555. fm10k_write_reg(hw, FM10K_TC_CREDIT(i),
  556. FM10K_TC_CREDIT_CREDIT_MASK);
  557. }
  558. /* zero out all mbmem registers */
  559. for (i = FM10K_VFMBMEM_LEN * num_vfs; i--;)
  560. fm10k_write_reg(hw, FM10K_MBMEM(i), 0);
  561. /* clear event notification of VF FLR */
  562. fm10k_write_reg(hw, FM10K_PFVFLREC(0), ~0);
  563. fm10k_write_reg(hw, FM10K_PFVFLREC(1), ~0);
  564. /* loop through unallocated rings assigning them back to PF */
  565. for (i = FM10K_MAX_QUEUES_PF; i < vf_q_idx; i++) {
  566. fm10k_write_reg(hw, FM10K_TXDCTL(i), 0);
  567. fm10k_write_reg(hw, FM10K_TXQCTL(i), FM10K_TXQCTL_PF |
  568. FM10K_TXQCTL_UNLIMITED_BW | 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. /* Provide the VF the ITR scale, using software-defined fields in TDLEN
  762. * to pass the information during VF initialization. See definition of
  763. * FM10K_TDLEN_ITR_SCALE_SHIFT for more details.
  764. */
  765. fm10k_write_reg(hw, FM10K_TDLEN(vf_q_idx), hw->mac.itr_scale <<
  766. FM10K_TDLEN_ITR_SCALE_SHIFT);
  767. err_out:
  768. /* configure Queue control register */
  769. txqctl = ((u32)vf_vid << FM10K_TXQCTL_VID_SHIFT) &
  770. FM10K_TXQCTL_VID_MASK;
  771. txqctl |= (vf_idx << FM10K_TXQCTL_TC_SHIFT) |
  772. FM10K_TXQCTL_VF | vf_idx;
  773. /* assign VLAN ID */
  774. for (i = 0; i < queues_per_pool; i++)
  775. fm10k_write_reg(hw, FM10K_TXQCTL(vf_q_idx + i), txqctl);
  776. /* restore the queue back to VF ownership */
  777. fm10k_write_reg(hw, FM10K_TQMAP(qmap_idx), vf_q_idx);
  778. return err;
  779. }
  780. /**
  781. * fm10k_iov_reset_resources_pf - Reassign queues and interrupts to a VF
  782. * @hw: pointer to the HW structure
  783. * @vf_info: pointer to VF information structure
  784. *
  785. * Reassign the interrupts and queues to a VF following an FLR
  786. **/
  787. static s32 fm10k_iov_reset_resources_pf(struct fm10k_hw *hw,
  788. struct fm10k_vf_info *vf_info)
  789. {
  790. u16 qmap_stride, queues_per_pool, vf_q_idx, qmap_idx;
  791. u32 tdbal = 0, tdbah = 0, txqctl, rxqctl;
  792. u16 vf_v_idx, vf_v_limit, vf_vid;
  793. u8 vf_idx = vf_info->vf_idx;
  794. int i;
  795. /* verify vf is in range */
  796. if (vf_idx >= hw->iov.num_vfs)
  797. return FM10K_ERR_PARAM;
  798. /* clear event notification of VF FLR */
  799. fm10k_write_reg(hw, FM10K_PFVFLREC(vf_idx / 32), 1 << (vf_idx % 32));
  800. /* force timeout and then disconnect the mailbox */
  801. vf_info->mbx.timeout = 0;
  802. if (vf_info->mbx.ops.disconnect)
  803. vf_info->mbx.ops.disconnect(hw, &vf_info->mbx);
  804. /* determine vector offset and count */
  805. vf_v_idx = fm10k_vf_vector_index(hw, vf_idx);
  806. vf_v_limit = vf_v_idx + fm10k_vectors_per_pool(hw);
  807. /* determine qmap offsets and counts */
  808. qmap_stride = (hw->iov.num_vfs > 8) ? 32 : 256;
  809. queues_per_pool = fm10k_queues_per_pool(hw);
  810. qmap_idx = qmap_stride * vf_idx;
  811. /* make all the queues inaccessible to the VF */
  812. for (i = qmap_idx; i < (qmap_idx + qmap_stride); i++) {
  813. fm10k_write_reg(hw, FM10K_TQMAP(i), 0);
  814. fm10k_write_reg(hw, FM10K_RQMAP(i), 0);
  815. }
  816. /* calculate starting index for queues */
  817. vf_q_idx = fm10k_vf_queue_index(hw, vf_idx);
  818. /* determine correct default VLAN ID */
  819. if (vf_info->pf_vid)
  820. vf_vid = vf_info->pf_vid;
  821. else
  822. vf_vid = vf_info->sw_vid;
  823. /* configure Queue control register */
  824. txqctl = ((u32)vf_vid << FM10K_TXQCTL_VID_SHIFT) |
  825. (vf_idx << FM10K_TXQCTL_TC_SHIFT) |
  826. FM10K_TXQCTL_VF | vf_idx;
  827. rxqctl = FM10K_RXQCTL_VF | (vf_idx << FM10K_RXQCTL_VF_SHIFT);
  828. /* stop further DMA and reset queue ownership back to VF */
  829. for (i = vf_q_idx; i < (queues_per_pool + vf_q_idx); i++) {
  830. fm10k_write_reg(hw, FM10K_TXDCTL(i), 0);
  831. fm10k_write_reg(hw, FM10K_TXQCTL(i), txqctl);
  832. fm10k_write_reg(hw, FM10K_RXDCTL(i),
  833. FM10K_RXDCTL_WRITE_BACK_MIN_DELAY |
  834. FM10K_RXDCTL_DROP_ON_EMPTY);
  835. fm10k_write_reg(hw, FM10K_RXQCTL(i), rxqctl);
  836. }
  837. /* reset TC with -1 credits and no quanta to prevent transmit */
  838. fm10k_write_reg(hw, FM10K_TC_MAXCREDIT(vf_idx), 0);
  839. fm10k_write_reg(hw, FM10K_TC_RATE(vf_idx), 0);
  840. fm10k_write_reg(hw, FM10K_TC_CREDIT(vf_idx),
  841. FM10K_TC_CREDIT_CREDIT_MASK);
  842. /* update our first entry in the table based on previous VF */
  843. if (!vf_idx)
  844. hw->mac.ops.update_int_moderator(hw);
  845. else
  846. hw->iov.ops.assign_int_moderator(hw, vf_idx - 1);
  847. /* reset linked list so it now includes our active vectors */
  848. if (vf_idx == (hw->iov.num_vfs - 1))
  849. fm10k_write_reg(hw, FM10K_ITR2(0), vf_v_idx);
  850. else
  851. fm10k_write_reg(hw, FM10K_ITR2(vf_v_limit), vf_v_idx);
  852. /* link remaining vectors so that next points to previous */
  853. for (vf_v_idx++; vf_v_idx < vf_v_limit; vf_v_idx++)
  854. fm10k_write_reg(hw, FM10K_ITR2(vf_v_idx), vf_v_idx - 1);
  855. /* zero out MBMEM, VLAN_TABLE, RETA, RSSRK, and MRQC registers */
  856. for (i = FM10K_VFMBMEM_LEN; i--;)
  857. fm10k_write_reg(hw, FM10K_MBMEM_VF(vf_idx, i), 0);
  858. for (i = FM10K_VLAN_TABLE_SIZE; i--;)
  859. fm10k_write_reg(hw, FM10K_VLAN_TABLE(vf_info->vsi, i), 0);
  860. for (i = FM10K_RETA_SIZE; i--;)
  861. fm10k_write_reg(hw, FM10K_RETA(vf_info->vsi, i), 0);
  862. for (i = FM10K_RSSRK_SIZE; i--;)
  863. fm10k_write_reg(hw, FM10K_RSSRK(vf_info->vsi, i), 0);
  864. fm10k_write_reg(hw, FM10K_MRQC(vf_info->vsi), 0);
  865. /* Update base address registers to contain MAC address */
  866. if (is_valid_ether_addr(vf_info->mac)) {
  867. tdbal = (((u32)vf_info->mac[3]) << 24) |
  868. (((u32)vf_info->mac[4]) << 16) |
  869. (((u32)vf_info->mac[5]) << 8);
  870. tdbah = (((u32)0xFF) << 24) |
  871. (((u32)vf_info->mac[0]) << 16) |
  872. (((u32)vf_info->mac[1]) << 8) |
  873. ((u32)vf_info->mac[2]);
  874. }
  875. /* map queue pairs back to VF from last to first */
  876. for (i = queues_per_pool; i--;) {
  877. fm10k_write_reg(hw, FM10K_TDBAL(vf_q_idx + i), tdbal);
  878. fm10k_write_reg(hw, FM10K_TDBAH(vf_q_idx + i), tdbah);
  879. /* See definition of FM10K_TDLEN_ITR_SCALE_SHIFT for an
  880. * explanation of how TDLEN is used.
  881. */
  882. fm10k_write_reg(hw, FM10K_TDLEN(vf_q_idx + i),
  883. hw->mac.itr_scale <<
  884. FM10K_TDLEN_ITR_SCALE_SHIFT);
  885. fm10k_write_reg(hw, FM10K_TQMAP(qmap_idx + i), vf_q_idx + i);
  886. fm10k_write_reg(hw, FM10K_RQMAP(qmap_idx + i), vf_q_idx + i);
  887. }
  888. /* repeat the first ring for all the remaining VF rings */
  889. for (i = queues_per_pool; i < qmap_stride; i++) {
  890. fm10k_write_reg(hw, FM10K_TQMAP(qmap_idx + i), vf_q_idx);
  891. fm10k_write_reg(hw, FM10K_RQMAP(qmap_idx + i), vf_q_idx);
  892. }
  893. return 0;
  894. }
  895. /**
  896. * fm10k_iov_set_lport_pf - Assign and enable a logical port for a given VF
  897. * @hw: pointer to hardware structure
  898. * @vf_info: pointer to VF information structure
  899. * @lport_idx: Logical port offset from the hardware glort
  900. * @flags: Set of capability flags to extend port beyond basic functionality
  901. *
  902. * This function allows enabling a VF port by assigning it a GLORT and
  903. * setting the flags so that it can enable an Rx mode.
  904. **/
  905. static s32 fm10k_iov_set_lport_pf(struct fm10k_hw *hw,
  906. struct fm10k_vf_info *vf_info,
  907. u16 lport_idx, u8 flags)
  908. {
  909. u16 glort = (hw->mac.dglort_map + lport_idx) & FM10K_DGLORTMAP_NONE;
  910. /* if glort is not valid return error */
  911. if (!fm10k_glort_valid_pf(hw, glort))
  912. return FM10K_ERR_PARAM;
  913. vf_info->vf_flags = flags | FM10K_VF_FLAG_NONE_CAPABLE;
  914. vf_info->glort = glort;
  915. return 0;
  916. }
  917. /**
  918. * fm10k_iov_reset_lport_pf - Disable a logical port for a given VF
  919. * @hw: pointer to hardware structure
  920. * @vf_info: pointer to VF information structure
  921. *
  922. * This function disables a VF port by stripping it of a GLORT and
  923. * setting the flags so that it cannot enable any Rx mode.
  924. **/
  925. static void fm10k_iov_reset_lport_pf(struct fm10k_hw *hw,
  926. struct fm10k_vf_info *vf_info)
  927. {
  928. u32 msg[1];
  929. /* need to disable the port if it is already enabled */
  930. if (FM10K_VF_FLAG_ENABLED(vf_info)) {
  931. /* notify switch that this port has been disabled */
  932. fm10k_update_lport_state_pf(hw, vf_info->glort, 1, false);
  933. /* generate port state response to notify VF it is not ready */
  934. fm10k_tlv_msg_init(msg, FM10K_VF_MSG_ID_LPORT_STATE);
  935. vf_info->mbx.ops.enqueue_tx(hw, &vf_info->mbx, msg);
  936. }
  937. /* clear flags and glort if it exists */
  938. vf_info->vf_flags = 0;
  939. vf_info->glort = 0;
  940. }
  941. /**
  942. * fm10k_iov_update_stats_pf - Updates hardware related statistics for VFs
  943. * @hw: pointer to hardware structure
  944. * @q: stats for all queues of a VF
  945. * @vf_idx: index of VF
  946. *
  947. * This function collects queue stats for VFs.
  948. **/
  949. static void fm10k_iov_update_stats_pf(struct fm10k_hw *hw,
  950. struct fm10k_hw_stats_q *q,
  951. u16 vf_idx)
  952. {
  953. u32 idx, qpp;
  954. /* get stats for all of the queues */
  955. qpp = fm10k_queues_per_pool(hw);
  956. idx = fm10k_vf_queue_index(hw, vf_idx);
  957. fm10k_update_hw_stats_q(hw, q, idx, qpp);
  958. }
  959. static s32 fm10k_iov_report_timestamp_pf(struct fm10k_hw *hw,
  960. struct fm10k_vf_info *vf_info,
  961. u64 timestamp)
  962. {
  963. u32 msg[4];
  964. /* generate port state response to notify VF it is not ready */
  965. fm10k_tlv_msg_init(msg, FM10K_VF_MSG_ID_1588);
  966. fm10k_tlv_attr_put_u64(msg, FM10K_1588_MSG_TIMESTAMP, timestamp);
  967. return vf_info->mbx.ops.enqueue_tx(hw, &vf_info->mbx, msg);
  968. }
  969. /**
  970. * fm10k_iov_msg_msix_pf - Message handler for MSI-X 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 MSI-X requests from the VF. The
  976. * 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_msix_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. u8 vf_idx = vf_info->vf_idx;
  984. return hw->iov.ops.assign_int_moderator(hw, vf_idx);
  985. }
  986. /**
  987. * fm10k_iov_select_vid - Select correct default VLAN ID
  988. * @hw: Pointer to hardware structure
  989. * @vid: VLAN ID to correct
  990. *
  991. * Will report an error if the VLAN ID is out of range. For VID = 0, it will
  992. * return either the pf_vid or sw_vid depending on which one is set.
  993. */
  994. static s32 fm10k_iov_select_vid(struct fm10k_vf_info *vf_info, u16 vid)
  995. {
  996. if (!vid)
  997. return vf_info->pf_vid ? vf_info->pf_vid : vf_info->sw_vid;
  998. else if (vf_info->pf_vid && vid != vf_info->pf_vid)
  999. return FM10K_ERR_PARAM;
  1000. else
  1001. return vid;
  1002. }
  1003. /**
  1004. * fm10k_iov_msg_mac_vlan_pf - Message handler for MAC/VLAN request from VF
  1005. * @hw: Pointer to hardware structure
  1006. * @results: Pointer array to message, results[0] is pointer to message
  1007. * @mbx: Pointer to mailbox information structure
  1008. *
  1009. * This function is a default handler for MAC/VLAN requests from the VF.
  1010. * The assumption is that in this case it is acceptable to just directly
  1011. * hand off the message from the VF to the underlying shared code.
  1012. **/
  1013. s32 fm10k_iov_msg_mac_vlan_pf(struct fm10k_hw *hw, u32 **results,
  1014. struct fm10k_mbx_info *mbx)
  1015. {
  1016. struct fm10k_vf_info *vf_info = (struct fm10k_vf_info *)mbx;
  1017. u8 mac[ETH_ALEN];
  1018. u32 *result;
  1019. int err = 0;
  1020. bool set;
  1021. u16 vlan;
  1022. u32 vid;
  1023. /* we shouldn't be updating rules on a disabled interface */
  1024. if (!FM10K_VF_FLAG_ENABLED(vf_info))
  1025. err = FM10K_ERR_PARAM;
  1026. if (!err && !!results[FM10K_MAC_VLAN_MSG_VLAN]) {
  1027. result = results[FM10K_MAC_VLAN_MSG_VLAN];
  1028. /* record VLAN id requested */
  1029. err = fm10k_tlv_attr_get_u32(result, &vid);
  1030. if (err)
  1031. return err;
  1032. /* verify upper 16 bits are zero */
  1033. if (vid >> 16)
  1034. return FM10K_ERR_PARAM;
  1035. set = !(vid & FM10K_VLAN_CLEAR);
  1036. vid &= ~FM10K_VLAN_CLEAR;
  1037. err = fm10k_iov_select_vid(vf_info, (u16)vid);
  1038. if (err < 0)
  1039. return err;
  1040. vid = err;
  1041. /* update VSI info for VF in regards to VLAN table */
  1042. err = hw->mac.ops.update_vlan(hw, vid, vf_info->vsi, set);
  1043. }
  1044. if (!err && !!results[FM10K_MAC_VLAN_MSG_MAC]) {
  1045. result = results[FM10K_MAC_VLAN_MSG_MAC];
  1046. /* record unicast MAC address requested */
  1047. err = fm10k_tlv_attr_get_mac_vlan(result, mac, &vlan);
  1048. if (err)
  1049. return err;
  1050. /* block attempts to set MAC for a locked device */
  1051. if (is_valid_ether_addr(vf_info->mac) &&
  1052. !ether_addr_equal(mac, vf_info->mac))
  1053. return FM10K_ERR_PARAM;
  1054. set = !(vlan & FM10K_VLAN_CLEAR);
  1055. vlan &= ~FM10K_VLAN_CLEAR;
  1056. err = fm10k_iov_select_vid(vf_info, vlan);
  1057. if (err < 0)
  1058. return err;
  1059. vlan = (u16)err;
  1060. /* notify switch of request for new unicast address */
  1061. err = hw->mac.ops.update_uc_addr(hw, vf_info->glort,
  1062. mac, vlan, set, 0);
  1063. }
  1064. if (!err && !!results[FM10K_MAC_VLAN_MSG_MULTICAST]) {
  1065. result = results[FM10K_MAC_VLAN_MSG_MULTICAST];
  1066. /* record multicast MAC address requested */
  1067. err = fm10k_tlv_attr_get_mac_vlan(result, mac, &vlan);
  1068. if (err)
  1069. return err;
  1070. /* verify that the VF is allowed to request multicast */
  1071. if (!(vf_info->vf_flags & FM10K_VF_FLAG_MULTI_ENABLED))
  1072. return FM10K_ERR_PARAM;
  1073. set = !(vlan & FM10K_VLAN_CLEAR);
  1074. vlan &= ~FM10K_VLAN_CLEAR;
  1075. err = fm10k_iov_select_vid(vf_info, vlan);
  1076. if (err < 0)
  1077. return err;
  1078. vlan = (u16)err;
  1079. /* notify switch of request for new multicast address */
  1080. err = hw->mac.ops.update_mc_addr(hw, vf_info->glort,
  1081. mac, vlan, set);
  1082. }
  1083. return err;
  1084. }
  1085. /**
  1086. * fm10k_iov_supported_xcast_mode_pf - Determine best match for xcast mode
  1087. * @vf_info: VF info structure containing capability flags
  1088. * @mode: Requested xcast mode
  1089. *
  1090. * This function outputs the mode that most closely matches the requested
  1091. * mode. If not modes match it will request we disable the port
  1092. **/
  1093. static u8 fm10k_iov_supported_xcast_mode_pf(struct fm10k_vf_info *vf_info,
  1094. u8 mode)
  1095. {
  1096. u8 vf_flags = vf_info->vf_flags;
  1097. /* match up mode to capabilities as best as possible */
  1098. switch (mode) {
  1099. case FM10K_XCAST_MODE_PROMISC:
  1100. if (vf_flags & FM10K_VF_FLAG_PROMISC_CAPABLE)
  1101. return FM10K_XCAST_MODE_PROMISC;
  1102. /* fallthough */
  1103. case FM10K_XCAST_MODE_ALLMULTI:
  1104. if (vf_flags & FM10K_VF_FLAG_ALLMULTI_CAPABLE)
  1105. return FM10K_XCAST_MODE_ALLMULTI;
  1106. /* fallthough */
  1107. case FM10K_XCAST_MODE_MULTI:
  1108. if (vf_flags & FM10K_VF_FLAG_MULTI_CAPABLE)
  1109. return FM10K_XCAST_MODE_MULTI;
  1110. /* fallthough */
  1111. case FM10K_XCAST_MODE_NONE:
  1112. if (vf_flags & FM10K_VF_FLAG_NONE_CAPABLE)
  1113. return FM10K_XCAST_MODE_NONE;
  1114. /* fallthough */
  1115. default:
  1116. break;
  1117. }
  1118. /* disable interface as it should not be able to request any */
  1119. return FM10K_XCAST_MODE_DISABLE;
  1120. }
  1121. /**
  1122. * fm10k_iov_msg_lport_state_pf - Message handler for port state requests
  1123. * @hw: Pointer to hardware structure
  1124. * @results: Pointer array to message, results[0] is pointer to message
  1125. * @mbx: Pointer to mailbox information structure
  1126. *
  1127. * This function is a default handler for port state requests. The port
  1128. * state requests for now are basic and consist of enabling or disabling
  1129. * the port.
  1130. **/
  1131. s32 fm10k_iov_msg_lport_state_pf(struct fm10k_hw *hw, u32 **results,
  1132. struct fm10k_mbx_info *mbx)
  1133. {
  1134. struct fm10k_vf_info *vf_info = (struct fm10k_vf_info *)mbx;
  1135. u32 *result;
  1136. s32 err = 0;
  1137. u32 msg[2];
  1138. u8 mode = 0;
  1139. /* verify VF is allowed to enable even minimal mode */
  1140. if (!(vf_info->vf_flags & FM10K_VF_FLAG_NONE_CAPABLE))
  1141. return FM10K_ERR_PARAM;
  1142. if (!!results[FM10K_LPORT_STATE_MSG_XCAST_MODE]) {
  1143. result = results[FM10K_LPORT_STATE_MSG_XCAST_MODE];
  1144. /* XCAST mode update requested */
  1145. err = fm10k_tlv_attr_get_u8(result, &mode);
  1146. if (err)
  1147. return FM10K_ERR_PARAM;
  1148. /* prep for possible demotion depending on capabilities */
  1149. mode = fm10k_iov_supported_xcast_mode_pf(vf_info, mode);
  1150. /* if mode is not currently enabled, enable it */
  1151. if (!(FM10K_VF_FLAG_ENABLED(vf_info) & (1 << mode)))
  1152. fm10k_update_xcast_mode_pf(hw, vf_info->glort, mode);
  1153. /* swap mode back to a bit flag */
  1154. mode = FM10K_VF_FLAG_SET_MODE(mode);
  1155. } else if (!results[FM10K_LPORT_STATE_MSG_DISABLE]) {
  1156. /* need to disable the port if it is already enabled */
  1157. if (FM10K_VF_FLAG_ENABLED(vf_info))
  1158. err = fm10k_update_lport_state_pf(hw, vf_info->glort,
  1159. 1, false);
  1160. /* we need to clear VF_FLAG_ENABLED flags in order to ensure
  1161. * that we actually re-enable the LPORT state below. Note that
  1162. * this has no impact if the VF is already disabled, as the
  1163. * flags are already cleared.
  1164. */
  1165. if (!err)
  1166. vf_info->vf_flags = FM10K_VF_FLAG_CAPABLE(vf_info);
  1167. /* when enabling the port we should reset the rate limiters */
  1168. hw->iov.ops.configure_tc(hw, vf_info->vf_idx, vf_info->rate);
  1169. /* set mode for minimal functionality */
  1170. mode = FM10K_VF_FLAG_SET_MODE_NONE;
  1171. /* generate port state response to notify VF it is ready */
  1172. fm10k_tlv_msg_init(msg, FM10K_VF_MSG_ID_LPORT_STATE);
  1173. fm10k_tlv_attr_put_bool(msg, FM10K_LPORT_STATE_MSG_READY);
  1174. mbx->ops.enqueue_tx(hw, mbx, msg);
  1175. }
  1176. /* if enable state toggled note the update */
  1177. if (!err && (!FM10K_VF_FLAG_ENABLED(vf_info) != !mode))
  1178. err = fm10k_update_lport_state_pf(hw, vf_info->glort, 1,
  1179. !!mode);
  1180. /* if state change succeeded, then update our stored state */
  1181. mode |= FM10K_VF_FLAG_CAPABLE(vf_info);
  1182. if (!err)
  1183. vf_info->vf_flags = mode;
  1184. return err;
  1185. }
  1186. /**
  1187. * fm10k_update_stats_hw_pf - Updates hardware related statistics of PF
  1188. * @hw: pointer to hardware structure
  1189. * @stats: pointer to the stats structure to update
  1190. *
  1191. * This function collects and aggregates global and per queue hardware
  1192. * statistics.
  1193. **/
  1194. static void fm10k_update_hw_stats_pf(struct fm10k_hw *hw,
  1195. struct fm10k_hw_stats *stats)
  1196. {
  1197. u32 timeout, ur, ca, um, xec, vlan_drop, loopback_drop, nodesc_drop;
  1198. u32 id, id_prev;
  1199. /* Use Tx queue 0 as a canary to detect a reset */
  1200. id = fm10k_read_reg(hw, FM10K_TXQCTL(0));
  1201. /* Read Global Statistics */
  1202. do {
  1203. timeout = fm10k_read_hw_stats_32b(hw, FM10K_STATS_TIMEOUT,
  1204. &stats->timeout);
  1205. ur = fm10k_read_hw_stats_32b(hw, FM10K_STATS_UR, &stats->ur);
  1206. ca = fm10k_read_hw_stats_32b(hw, FM10K_STATS_CA, &stats->ca);
  1207. um = fm10k_read_hw_stats_32b(hw, FM10K_STATS_UM, &stats->um);
  1208. xec = fm10k_read_hw_stats_32b(hw, FM10K_STATS_XEC, &stats->xec);
  1209. vlan_drop = fm10k_read_hw_stats_32b(hw, FM10K_STATS_VLAN_DROP,
  1210. &stats->vlan_drop);
  1211. loopback_drop =
  1212. fm10k_read_hw_stats_32b(hw,
  1213. FM10K_STATS_LOOPBACK_DROP,
  1214. &stats->loopback_drop);
  1215. nodesc_drop = fm10k_read_hw_stats_32b(hw,
  1216. FM10K_STATS_NODESC_DROP,
  1217. &stats->nodesc_drop);
  1218. /* if value has not changed then we have consistent data */
  1219. id_prev = id;
  1220. id = fm10k_read_reg(hw, FM10K_TXQCTL(0));
  1221. } while ((id ^ id_prev) & FM10K_TXQCTL_ID_MASK);
  1222. /* drop non-ID bits and set VALID ID bit */
  1223. id &= FM10K_TXQCTL_ID_MASK;
  1224. id |= FM10K_STAT_VALID;
  1225. /* Update Global Statistics */
  1226. if (stats->stats_idx == id) {
  1227. stats->timeout.count += timeout;
  1228. stats->ur.count += ur;
  1229. stats->ca.count += ca;
  1230. stats->um.count += um;
  1231. stats->xec.count += xec;
  1232. stats->vlan_drop.count += vlan_drop;
  1233. stats->loopback_drop.count += loopback_drop;
  1234. stats->nodesc_drop.count += nodesc_drop;
  1235. }
  1236. /* Update bases and record current PF id */
  1237. fm10k_update_hw_base_32b(&stats->timeout, timeout);
  1238. fm10k_update_hw_base_32b(&stats->ur, ur);
  1239. fm10k_update_hw_base_32b(&stats->ca, ca);
  1240. fm10k_update_hw_base_32b(&stats->um, um);
  1241. fm10k_update_hw_base_32b(&stats->xec, xec);
  1242. fm10k_update_hw_base_32b(&stats->vlan_drop, vlan_drop);
  1243. fm10k_update_hw_base_32b(&stats->loopback_drop, loopback_drop);
  1244. fm10k_update_hw_base_32b(&stats->nodesc_drop, nodesc_drop);
  1245. stats->stats_idx = id;
  1246. /* Update Queue Statistics */
  1247. fm10k_update_hw_stats_q(hw, stats->q, 0, hw->mac.max_queues);
  1248. }
  1249. /**
  1250. * fm10k_rebind_hw_stats_pf - Resets base for hardware statistics of PF
  1251. * @hw: pointer to hardware structure
  1252. * @stats: pointer to the stats structure to update
  1253. *
  1254. * This function resets the base for global and per queue hardware
  1255. * statistics.
  1256. **/
  1257. static void fm10k_rebind_hw_stats_pf(struct fm10k_hw *hw,
  1258. struct fm10k_hw_stats *stats)
  1259. {
  1260. /* Unbind Global Statistics */
  1261. fm10k_unbind_hw_stats_32b(&stats->timeout);
  1262. fm10k_unbind_hw_stats_32b(&stats->ur);
  1263. fm10k_unbind_hw_stats_32b(&stats->ca);
  1264. fm10k_unbind_hw_stats_32b(&stats->um);
  1265. fm10k_unbind_hw_stats_32b(&stats->xec);
  1266. fm10k_unbind_hw_stats_32b(&stats->vlan_drop);
  1267. fm10k_unbind_hw_stats_32b(&stats->loopback_drop);
  1268. fm10k_unbind_hw_stats_32b(&stats->nodesc_drop);
  1269. /* Unbind Queue Statistics */
  1270. fm10k_unbind_hw_stats_q(stats->q, 0, hw->mac.max_queues);
  1271. /* Reinitialize bases for all stats */
  1272. fm10k_update_hw_stats_pf(hw, stats);
  1273. }
  1274. /**
  1275. * fm10k_set_dma_mask_pf - Configures PhyAddrSpace to limit DMA to system
  1276. * @hw: pointer to hardware structure
  1277. * @dma_mask: 64 bit DMA mask required for platform
  1278. *
  1279. * This function sets the PHYADDR.PhyAddrSpace bits for the endpoint in order
  1280. * to limit the access to memory beyond what is physically in the system.
  1281. **/
  1282. static void fm10k_set_dma_mask_pf(struct fm10k_hw *hw, u64 dma_mask)
  1283. {
  1284. /* we need to write the upper 32 bits of DMA mask to PhyAddrSpace */
  1285. u32 phyaddr = (u32)(dma_mask >> 32);
  1286. fm10k_write_reg(hw, FM10K_PHYADDR, phyaddr);
  1287. }
  1288. /**
  1289. * fm10k_get_fault_pf - Record a fault in one of the interface units
  1290. * @hw: pointer to hardware structure
  1291. * @type: pointer to fault type register offset
  1292. * @fault: pointer to memory location to record the fault
  1293. *
  1294. * Record the fault register contents to the fault data structure and
  1295. * clear the entry from the register.
  1296. *
  1297. * Returns ERR_PARAM if invalid register is specified or no error is present.
  1298. **/
  1299. static s32 fm10k_get_fault_pf(struct fm10k_hw *hw, int type,
  1300. struct fm10k_fault *fault)
  1301. {
  1302. u32 func;
  1303. /* verify the fault register is in range and is aligned */
  1304. switch (type) {
  1305. case FM10K_PCA_FAULT:
  1306. case FM10K_THI_FAULT:
  1307. case FM10K_FUM_FAULT:
  1308. break;
  1309. default:
  1310. return FM10K_ERR_PARAM;
  1311. }
  1312. /* only service faults that are valid */
  1313. func = fm10k_read_reg(hw, type + FM10K_FAULT_FUNC);
  1314. if (!(func & FM10K_FAULT_FUNC_VALID))
  1315. return FM10K_ERR_PARAM;
  1316. /* read remaining fields */
  1317. fault->address = fm10k_read_reg(hw, type + FM10K_FAULT_ADDR_HI);
  1318. fault->address <<= 32;
  1319. fault->address = fm10k_read_reg(hw, type + FM10K_FAULT_ADDR_LO);
  1320. fault->specinfo = fm10k_read_reg(hw, type + FM10K_FAULT_SPECINFO);
  1321. /* clear valid bit to allow for next error */
  1322. fm10k_write_reg(hw, type + FM10K_FAULT_FUNC, FM10K_FAULT_FUNC_VALID);
  1323. /* Record which function triggered the error */
  1324. if (func & FM10K_FAULT_FUNC_PF)
  1325. fault->func = 0;
  1326. else
  1327. fault->func = 1 + ((func & FM10K_FAULT_FUNC_VF_MASK) >>
  1328. FM10K_FAULT_FUNC_VF_SHIFT);
  1329. /* record fault type */
  1330. fault->type = func & FM10K_FAULT_FUNC_TYPE_MASK;
  1331. return 0;
  1332. }
  1333. /**
  1334. * fm10k_request_lport_map_pf - Request LPORT map from the switch API
  1335. * @hw: pointer to hardware structure
  1336. *
  1337. **/
  1338. static s32 fm10k_request_lport_map_pf(struct fm10k_hw *hw)
  1339. {
  1340. struct fm10k_mbx_info *mbx = &hw->mbx;
  1341. u32 msg[1];
  1342. /* issue request asking for LPORT map */
  1343. fm10k_tlv_msg_init(msg, FM10K_PF_MSG_ID_LPORT_MAP);
  1344. /* load onto outgoing mailbox */
  1345. return mbx->ops.enqueue_tx(hw, mbx, msg);
  1346. }
  1347. /**
  1348. * fm10k_get_host_state_pf - Returns the state of the switch and mailbox
  1349. * @hw: pointer to hardware structure
  1350. * @switch_ready: pointer to boolean value that will record switch state
  1351. *
  1352. * This funciton will check the DMA_CTRL2 register and mailbox in order
  1353. * to determine if the switch is ready for the PF to begin requesting
  1354. * addresses and mapping traffic to the local interface.
  1355. **/
  1356. static s32 fm10k_get_host_state_pf(struct fm10k_hw *hw, bool *switch_ready)
  1357. {
  1358. s32 ret_val = 0;
  1359. u32 dma_ctrl2;
  1360. /* verify the switch is ready for interaction */
  1361. dma_ctrl2 = fm10k_read_reg(hw, FM10K_DMA_CTRL2);
  1362. if (!(dma_ctrl2 & FM10K_DMA_CTRL2_SWITCH_READY))
  1363. goto out;
  1364. /* retrieve generic host state info */
  1365. ret_val = fm10k_get_host_state_generic(hw, switch_ready);
  1366. if (ret_val)
  1367. goto out;
  1368. /* interface cannot receive traffic without logical ports */
  1369. if (hw->mac.dglort_map == FM10K_DGLORTMAP_NONE)
  1370. ret_val = fm10k_request_lport_map_pf(hw);
  1371. out:
  1372. return ret_val;
  1373. }
  1374. /* This structure defines the attibutes to be parsed below */
  1375. const struct fm10k_tlv_attr fm10k_lport_map_msg_attr[] = {
  1376. FM10K_TLV_ATTR_U32(FM10K_PF_ATTR_ID_LPORT_MAP),
  1377. FM10K_TLV_ATTR_LAST
  1378. };
  1379. /**
  1380. * fm10k_msg_lport_map_pf - Message handler for lport_map message from SM
  1381. * @hw: Pointer to hardware structure
  1382. * @results: pointer array containing parsed data
  1383. * @mbx: Pointer to mailbox information structure
  1384. *
  1385. * This handler configures the lport mapping based on the reply from the
  1386. * switch API.
  1387. **/
  1388. s32 fm10k_msg_lport_map_pf(struct fm10k_hw *hw, u32 **results,
  1389. struct fm10k_mbx_info *mbx)
  1390. {
  1391. u16 glort, mask;
  1392. u32 dglort_map;
  1393. s32 err;
  1394. err = fm10k_tlv_attr_get_u32(results[FM10K_PF_ATTR_ID_LPORT_MAP],
  1395. &dglort_map);
  1396. if (err)
  1397. return err;
  1398. /* extract values out of the header */
  1399. glort = FM10K_MSG_HDR_FIELD_GET(dglort_map, LPORT_MAP_GLORT);
  1400. mask = FM10K_MSG_HDR_FIELD_GET(dglort_map, LPORT_MAP_MASK);
  1401. /* verify mask is set and none of the masked bits in glort are set */
  1402. if (!mask || (glort & ~mask))
  1403. return FM10K_ERR_PARAM;
  1404. /* verify the mask is contiguous, and that it is 1's followed by 0's */
  1405. if (((~(mask - 1) & mask) + mask) & FM10K_DGLORTMAP_NONE)
  1406. return FM10K_ERR_PARAM;
  1407. /* record the glort, mask, and port count */
  1408. hw->mac.dglort_map = dglort_map;
  1409. return 0;
  1410. }
  1411. const struct fm10k_tlv_attr fm10k_update_pvid_msg_attr[] = {
  1412. FM10K_TLV_ATTR_U32(FM10K_PF_ATTR_ID_UPDATE_PVID),
  1413. FM10K_TLV_ATTR_LAST
  1414. };
  1415. /**
  1416. * fm10k_msg_update_pvid_pf - Message handler for port VLAN message from SM
  1417. * @hw: Pointer to hardware structure
  1418. * @results: pointer array containing parsed data
  1419. * @mbx: Pointer to mailbox information structure
  1420. *
  1421. * This handler configures the default VLAN for the PF
  1422. **/
  1423. static s32 fm10k_msg_update_pvid_pf(struct fm10k_hw *hw, u32 **results,
  1424. struct fm10k_mbx_info *mbx)
  1425. {
  1426. u16 glort, pvid;
  1427. u32 pvid_update;
  1428. s32 err;
  1429. err = fm10k_tlv_attr_get_u32(results[FM10K_PF_ATTR_ID_UPDATE_PVID],
  1430. &pvid_update);
  1431. if (err)
  1432. return err;
  1433. /* extract values from the pvid update */
  1434. glort = FM10K_MSG_HDR_FIELD_GET(pvid_update, UPDATE_PVID_GLORT);
  1435. pvid = FM10K_MSG_HDR_FIELD_GET(pvid_update, UPDATE_PVID_PVID);
  1436. /* if glort is not valid return error */
  1437. if (!fm10k_glort_valid_pf(hw, glort))
  1438. return FM10K_ERR_PARAM;
  1439. /* verify VLAN ID is valid */
  1440. if (pvid >= FM10K_VLAN_TABLE_VID_MAX)
  1441. return FM10K_ERR_PARAM;
  1442. /* record the port VLAN ID value */
  1443. hw->mac.default_vid = pvid;
  1444. return 0;
  1445. }
  1446. /**
  1447. * fm10k_record_global_table_data - Move global table data to swapi table info
  1448. * @from: pointer to source table data structure
  1449. * @to: pointer to destination table info structure
  1450. *
  1451. * This function is will copy table_data to the table_info contained in
  1452. * the hw struct.
  1453. **/
  1454. static void fm10k_record_global_table_data(struct fm10k_global_table_data *from,
  1455. struct fm10k_swapi_table_info *to)
  1456. {
  1457. /* convert from le32 struct to CPU byte ordered values */
  1458. to->used = le32_to_cpu(from->used);
  1459. to->avail = le32_to_cpu(from->avail);
  1460. }
  1461. const struct fm10k_tlv_attr fm10k_err_msg_attr[] = {
  1462. FM10K_TLV_ATTR_LE_STRUCT(FM10K_PF_ATTR_ID_ERR,
  1463. sizeof(struct fm10k_swapi_error)),
  1464. FM10K_TLV_ATTR_LAST
  1465. };
  1466. /**
  1467. * fm10k_msg_err_pf - Message handler for error reply
  1468. * @hw: Pointer to hardware structure
  1469. * @results: pointer array containing parsed data
  1470. * @mbx: Pointer to mailbox information structure
  1471. *
  1472. * This handler will capture the data for any error replies to previous
  1473. * messages that the PF has sent.
  1474. **/
  1475. s32 fm10k_msg_err_pf(struct fm10k_hw *hw, u32 **results,
  1476. struct fm10k_mbx_info *mbx)
  1477. {
  1478. struct fm10k_swapi_error err_msg;
  1479. s32 err;
  1480. /* extract structure from message */
  1481. err = fm10k_tlv_attr_get_le_struct(results[FM10K_PF_ATTR_ID_ERR],
  1482. &err_msg, sizeof(err_msg));
  1483. if (err)
  1484. return err;
  1485. /* record table status */
  1486. fm10k_record_global_table_data(&err_msg.mac, &hw->swapi.mac);
  1487. fm10k_record_global_table_data(&err_msg.nexthop, &hw->swapi.nexthop);
  1488. fm10k_record_global_table_data(&err_msg.ffu, &hw->swapi.ffu);
  1489. /* record SW API status value */
  1490. hw->swapi.status = le32_to_cpu(err_msg.status);
  1491. return 0;
  1492. }
  1493. const struct fm10k_tlv_attr fm10k_1588_timestamp_msg_attr[] = {
  1494. FM10K_TLV_ATTR_LE_STRUCT(FM10K_PF_ATTR_ID_1588_TIMESTAMP,
  1495. sizeof(struct fm10k_swapi_1588_timestamp)),
  1496. FM10K_TLV_ATTR_LAST
  1497. };
  1498. /* currently there is no shared 1588 timestamp handler */
  1499. /**
  1500. * fm10k_adjust_systime_pf - Adjust systime frequency
  1501. * @hw: pointer to hardware structure
  1502. * @ppb: adjustment rate in parts per billion
  1503. *
  1504. * This function will adjust the SYSTIME_CFG register contained in BAR 4
  1505. * if this function is supported for BAR 4 access. The adjustment amount
  1506. * is based on the parts per billion value provided and adjusted to a
  1507. * value based on parts per 2^48 clock cycles.
  1508. *
  1509. * If adjustment is not supported or the requested value is too large
  1510. * we will return an error.
  1511. **/
  1512. static s32 fm10k_adjust_systime_pf(struct fm10k_hw *hw, s32 ppb)
  1513. {
  1514. u64 systime_adjust;
  1515. /* if sw_addr is not set we don't have switch register access */
  1516. if (!hw->sw_addr)
  1517. return ppb ? FM10K_ERR_PARAM : 0;
  1518. /* we must convert the value from parts per billion to parts per
  1519. * 2^48 cycles. In addition I have opted to only use the 30 most
  1520. * significant bits of the adjustment value as the 8 least
  1521. * significant bits are located in another register and represent
  1522. * a value significantly less than a part per billion, the result
  1523. * of dropping the 8 least significant bits is that the adjustment
  1524. * value is effectively multiplied by 2^8 when we write it.
  1525. *
  1526. * As a result of all this the math for this breaks down as follows:
  1527. * ppb / 10^9 == adjust * 2^8 / 2^48
  1528. * If we solve this for adjust, and simplify it comes out as:
  1529. * ppb * 2^31 / 5^9 == adjust
  1530. */
  1531. systime_adjust = (ppb < 0) ? -ppb : ppb;
  1532. systime_adjust <<= 31;
  1533. do_div(systime_adjust, 1953125);
  1534. /* verify the requested adjustment value is in range */
  1535. if (systime_adjust > FM10K_SW_SYSTIME_ADJUST_MASK)
  1536. return FM10K_ERR_PARAM;
  1537. if (ppb > 0)
  1538. systime_adjust |= FM10K_SW_SYSTIME_ADJUST_DIR_POSITIVE;
  1539. fm10k_write_sw_reg(hw, FM10K_SW_SYSTIME_ADJUST, (u32)systime_adjust);
  1540. return 0;
  1541. }
  1542. /**
  1543. * fm10k_read_systime_pf - Reads value of systime registers
  1544. * @hw: pointer to the hardware structure
  1545. *
  1546. * Function reads the content of 2 registers, combined to represent a 64 bit
  1547. * value measured in nanosecods. In order to guarantee the value is accurate
  1548. * we check the 32 most significant bits both before and after reading the
  1549. * 32 least significant bits to verify they didn't change as we were reading
  1550. * the registers.
  1551. **/
  1552. static u64 fm10k_read_systime_pf(struct fm10k_hw *hw)
  1553. {
  1554. u32 systime_l, systime_h, systime_tmp;
  1555. systime_h = fm10k_read_reg(hw, FM10K_SYSTIME + 1);
  1556. do {
  1557. systime_tmp = systime_h;
  1558. systime_l = fm10k_read_reg(hw, FM10K_SYSTIME);
  1559. systime_h = fm10k_read_reg(hw, FM10K_SYSTIME + 1);
  1560. } while (systime_tmp != systime_h);
  1561. return ((u64)systime_h << 32) | systime_l;
  1562. }
  1563. static const struct fm10k_msg_data fm10k_msg_data_pf[] = {
  1564. FM10K_PF_MSG_ERR_HANDLER(XCAST_MODES, fm10k_msg_err_pf),
  1565. FM10K_PF_MSG_ERR_HANDLER(UPDATE_MAC_FWD_RULE, fm10k_msg_err_pf),
  1566. FM10K_PF_MSG_LPORT_MAP_HANDLER(fm10k_msg_lport_map_pf),
  1567. FM10K_PF_MSG_ERR_HANDLER(LPORT_CREATE, fm10k_msg_err_pf),
  1568. FM10K_PF_MSG_ERR_HANDLER(LPORT_DELETE, fm10k_msg_err_pf),
  1569. FM10K_PF_MSG_UPDATE_PVID_HANDLER(fm10k_msg_update_pvid_pf),
  1570. FM10K_TLV_MSG_ERROR_HANDLER(fm10k_tlv_msg_error),
  1571. };
  1572. static const struct fm10k_mac_ops mac_ops_pf = {
  1573. .get_bus_info = fm10k_get_bus_info_generic,
  1574. .reset_hw = fm10k_reset_hw_pf,
  1575. .init_hw = fm10k_init_hw_pf,
  1576. .start_hw = fm10k_start_hw_generic,
  1577. .stop_hw = fm10k_stop_hw_generic,
  1578. .update_vlan = fm10k_update_vlan_pf,
  1579. .read_mac_addr = fm10k_read_mac_addr_pf,
  1580. .update_uc_addr = fm10k_update_uc_addr_pf,
  1581. .update_mc_addr = fm10k_update_mc_addr_pf,
  1582. .update_xcast_mode = fm10k_update_xcast_mode_pf,
  1583. .update_int_moderator = fm10k_update_int_moderator_pf,
  1584. .update_lport_state = fm10k_update_lport_state_pf,
  1585. .update_hw_stats = fm10k_update_hw_stats_pf,
  1586. .rebind_hw_stats = fm10k_rebind_hw_stats_pf,
  1587. .configure_dglort_map = fm10k_configure_dglort_map_pf,
  1588. .set_dma_mask = fm10k_set_dma_mask_pf,
  1589. .get_fault = fm10k_get_fault_pf,
  1590. .get_host_state = fm10k_get_host_state_pf,
  1591. .adjust_systime = fm10k_adjust_systime_pf,
  1592. .read_systime = fm10k_read_systime_pf,
  1593. };
  1594. static const struct fm10k_iov_ops iov_ops_pf = {
  1595. .assign_resources = fm10k_iov_assign_resources_pf,
  1596. .configure_tc = fm10k_iov_configure_tc_pf,
  1597. .assign_int_moderator = fm10k_iov_assign_int_moderator_pf,
  1598. .assign_default_mac_vlan = fm10k_iov_assign_default_mac_vlan_pf,
  1599. .reset_resources = fm10k_iov_reset_resources_pf,
  1600. .set_lport = fm10k_iov_set_lport_pf,
  1601. .reset_lport = fm10k_iov_reset_lport_pf,
  1602. .update_stats = fm10k_iov_update_stats_pf,
  1603. .report_timestamp = fm10k_iov_report_timestamp_pf,
  1604. };
  1605. static s32 fm10k_get_invariants_pf(struct fm10k_hw *hw)
  1606. {
  1607. fm10k_get_invariants_generic(hw);
  1608. return fm10k_sm_mbx_init(hw, &hw->mbx, fm10k_msg_data_pf);
  1609. }
  1610. const struct fm10k_info fm10k_pf_info = {
  1611. .mac = fm10k_mac_pf,
  1612. .get_invariants = fm10k_get_invariants_pf,
  1613. .mac_ops = &mac_ops_pf,
  1614. .iov_ops = &iov_ops_pf,
  1615. };