fm10k_pf.c 58 KB

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