fm10k_pf.c 58 KB

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