ice_lib.c 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2018, Intel Corporation. */
  3. #include "ice.h"
  4. #include "ice_lib.h"
  5. /**
  6. * ice_setup_rx_ctx - Configure a receive ring context
  7. * @ring: The Rx ring to configure
  8. *
  9. * Configure the Rx descriptor ring in RLAN context.
  10. */
  11. static int ice_setup_rx_ctx(struct ice_ring *ring)
  12. {
  13. struct ice_vsi *vsi = ring->vsi;
  14. struct ice_hw *hw = &vsi->back->hw;
  15. u32 rxdid = ICE_RXDID_FLEX_NIC;
  16. struct ice_rlan_ctx rlan_ctx;
  17. u32 regval;
  18. u16 pf_q;
  19. int err;
  20. /* what is RX queue number in global space of 2K Rx queues */
  21. pf_q = vsi->rxq_map[ring->q_index];
  22. /* clear the context structure first */
  23. memset(&rlan_ctx, 0, sizeof(rlan_ctx));
  24. rlan_ctx.base = ring->dma >> 7;
  25. rlan_ctx.qlen = ring->count;
  26. /* Receive Packet Data Buffer Size.
  27. * The Packet Data Buffer Size is defined in 128 byte units.
  28. */
  29. rlan_ctx.dbuf = vsi->rx_buf_len >> ICE_RLAN_CTX_DBUF_S;
  30. /* use 32 byte descriptors */
  31. rlan_ctx.dsize = 1;
  32. /* Strip the Ethernet CRC bytes before the packet is posted to host
  33. * memory.
  34. */
  35. rlan_ctx.crcstrip = 1;
  36. /* L2TSEL flag defines the reported L2 Tags in the receive descriptor */
  37. rlan_ctx.l2tsel = 1;
  38. rlan_ctx.dtype = ICE_RX_DTYPE_NO_SPLIT;
  39. rlan_ctx.hsplit_0 = ICE_RLAN_RX_HSPLIT_0_NO_SPLIT;
  40. rlan_ctx.hsplit_1 = ICE_RLAN_RX_HSPLIT_1_NO_SPLIT;
  41. /* This controls whether VLAN is stripped from inner headers
  42. * The VLAN in the inner L2 header is stripped to the receive
  43. * descriptor if enabled by this flag.
  44. */
  45. rlan_ctx.showiv = 0;
  46. /* Max packet size for this queue - must not be set to a larger value
  47. * than 5 x DBUF
  48. */
  49. rlan_ctx.rxmax = min_t(u16, vsi->max_frame,
  50. ICE_MAX_CHAINED_RX_BUFS * vsi->rx_buf_len);
  51. /* Rx queue threshold in units of 64 */
  52. rlan_ctx.lrxqthresh = 1;
  53. /* Enable Flexible Descriptors in the queue context which
  54. * allows this driver to select a specific receive descriptor format
  55. */
  56. regval = rd32(hw, QRXFLXP_CNTXT(pf_q));
  57. regval |= (rxdid << QRXFLXP_CNTXT_RXDID_IDX_S) &
  58. QRXFLXP_CNTXT_RXDID_IDX_M;
  59. /* increasing context priority to pick up profile id;
  60. * default is 0x01; setting to 0x03 to ensure profile
  61. * is programming if prev context is of same priority
  62. */
  63. regval |= (0x03 << QRXFLXP_CNTXT_RXDID_PRIO_S) &
  64. QRXFLXP_CNTXT_RXDID_PRIO_M;
  65. wr32(hw, QRXFLXP_CNTXT(pf_q), regval);
  66. /* Absolute queue number out of 2K needs to be passed */
  67. err = ice_write_rxq_ctx(hw, &rlan_ctx, pf_q);
  68. if (err) {
  69. dev_err(&vsi->back->pdev->dev,
  70. "Failed to set LAN Rx queue context for absolute Rx queue %d error: %d\n",
  71. pf_q, err);
  72. return -EIO;
  73. }
  74. /* init queue specific tail register */
  75. ring->tail = hw->hw_addr + QRX_TAIL(pf_q);
  76. writel(0, ring->tail);
  77. ice_alloc_rx_bufs(ring, ICE_DESC_UNUSED(ring));
  78. return 0;
  79. }
  80. /**
  81. * ice_setup_tx_ctx - setup a struct ice_tlan_ctx instance
  82. * @ring: The Tx ring to configure
  83. * @tlan_ctx: Pointer to the Tx LAN queue context structure to be initialized
  84. * @pf_q: queue index in the PF space
  85. *
  86. * Configure the Tx descriptor ring in TLAN context.
  87. */
  88. static void
  89. ice_setup_tx_ctx(struct ice_ring *ring, struct ice_tlan_ctx *tlan_ctx, u16 pf_q)
  90. {
  91. struct ice_vsi *vsi = ring->vsi;
  92. struct ice_hw *hw = &vsi->back->hw;
  93. tlan_ctx->base = ring->dma >> ICE_TLAN_CTX_BASE_S;
  94. tlan_ctx->port_num = vsi->port_info->lport;
  95. /* Transmit Queue Length */
  96. tlan_ctx->qlen = ring->count;
  97. /* PF number */
  98. tlan_ctx->pf_num = hw->pf_id;
  99. /* queue belongs to a specific VSI type
  100. * VF / VM index should be programmed per vmvf_type setting:
  101. * for vmvf_type = VF, it is VF number between 0-256
  102. * for vmvf_type = VM, it is VM number between 0-767
  103. * for PF or EMP this field should be set to zero
  104. */
  105. switch (vsi->type) {
  106. case ICE_VSI_PF:
  107. tlan_ctx->vmvf_type = ICE_TLAN_CTX_VMVF_TYPE_PF;
  108. break;
  109. default:
  110. return;
  111. }
  112. /* make sure the context is associated with the right VSI */
  113. tlan_ctx->src_vsi = ice_get_hw_vsi_num(hw, vsi->idx);
  114. tlan_ctx->tso_ena = ICE_TX_LEGACY;
  115. tlan_ctx->tso_qnum = pf_q;
  116. /* Legacy or Advanced Host Interface:
  117. * 0: Advanced Host Interface
  118. * 1: Legacy Host Interface
  119. */
  120. tlan_ctx->legacy_int = ICE_TX_LEGACY;
  121. }
  122. /**
  123. * ice_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
  124. * @pf: the PF being configured
  125. * @pf_q: the PF queue
  126. * @ena: enable or disable state of the queue
  127. *
  128. * This routine will wait for the given Rx queue of the PF to reach the
  129. * enabled or disabled state.
  130. * Returns -ETIMEDOUT in case of failing to reach the requested state after
  131. * multiple retries; else will return 0 in case of success.
  132. */
  133. static int ice_pf_rxq_wait(struct ice_pf *pf, int pf_q, bool ena)
  134. {
  135. int i;
  136. for (i = 0; i < ICE_Q_WAIT_RETRY_LIMIT; i++) {
  137. u32 rx_reg = rd32(&pf->hw, QRX_CTRL(pf_q));
  138. if (ena == !!(rx_reg & QRX_CTRL_QENA_STAT_M))
  139. break;
  140. usleep_range(10, 20);
  141. }
  142. if (i >= ICE_Q_WAIT_RETRY_LIMIT)
  143. return -ETIMEDOUT;
  144. return 0;
  145. }
  146. /**
  147. * ice_vsi_ctrl_rx_rings - Start or stop a VSI's Rx rings
  148. * @vsi: the VSI being configured
  149. * @ena: start or stop the Rx rings
  150. */
  151. static int ice_vsi_ctrl_rx_rings(struct ice_vsi *vsi, bool ena)
  152. {
  153. struct ice_pf *pf = vsi->back;
  154. struct ice_hw *hw = &pf->hw;
  155. int i, j, ret = 0;
  156. for (i = 0; i < vsi->num_rxq; i++) {
  157. int pf_q = vsi->rxq_map[i];
  158. u32 rx_reg;
  159. for (j = 0; j < ICE_Q_WAIT_MAX_RETRY; j++) {
  160. rx_reg = rd32(hw, QRX_CTRL(pf_q));
  161. if (((rx_reg >> QRX_CTRL_QENA_REQ_S) & 1) ==
  162. ((rx_reg >> QRX_CTRL_QENA_STAT_S) & 1))
  163. break;
  164. usleep_range(1000, 2000);
  165. }
  166. /* Skip if the queue is already in the requested state */
  167. if (ena == !!(rx_reg & QRX_CTRL_QENA_STAT_M))
  168. continue;
  169. /* turn on/off the queue */
  170. if (ena)
  171. rx_reg |= QRX_CTRL_QENA_REQ_M;
  172. else
  173. rx_reg &= ~QRX_CTRL_QENA_REQ_M;
  174. wr32(hw, QRX_CTRL(pf_q), rx_reg);
  175. /* wait for the change to finish */
  176. ret = ice_pf_rxq_wait(pf, pf_q, ena);
  177. if (ret) {
  178. dev_err(&pf->pdev->dev,
  179. "VSI idx %d Rx ring %d %sable timeout\n",
  180. vsi->idx, pf_q, (ena ? "en" : "dis"));
  181. break;
  182. }
  183. }
  184. return ret;
  185. }
  186. /**
  187. * ice_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the VSI
  188. * @vsi: VSI pointer
  189. * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
  190. *
  191. * On error: returns error code (negative)
  192. * On success: returns 0
  193. */
  194. static int ice_vsi_alloc_arrays(struct ice_vsi *vsi, bool alloc_qvectors)
  195. {
  196. struct ice_pf *pf = vsi->back;
  197. /* allocate memory for both Tx and Rx ring pointers */
  198. vsi->tx_rings = devm_kcalloc(&pf->pdev->dev, vsi->alloc_txq,
  199. sizeof(struct ice_ring *), GFP_KERNEL);
  200. if (!vsi->tx_rings)
  201. goto err_txrings;
  202. vsi->rx_rings = devm_kcalloc(&pf->pdev->dev, vsi->alloc_rxq,
  203. sizeof(struct ice_ring *), GFP_KERNEL);
  204. if (!vsi->rx_rings)
  205. goto err_rxrings;
  206. if (alloc_qvectors) {
  207. /* allocate memory for q_vector pointers */
  208. vsi->q_vectors = devm_kcalloc(&pf->pdev->dev,
  209. vsi->num_q_vectors,
  210. sizeof(struct ice_q_vector *),
  211. GFP_KERNEL);
  212. if (!vsi->q_vectors)
  213. goto err_vectors;
  214. }
  215. return 0;
  216. err_vectors:
  217. devm_kfree(&pf->pdev->dev, vsi->rx_rings);
  218. err_rxrings:
  219. devm_kfree(&pf->pdev->dev, vsi->tx_rings);
  220. err_txrings:
  221. return -ENOMEM;
  222. }
  223. /**
  224. * ice_vsi_set_num_qs - Set num queues, descriptors and vectors for a VSI
  225. * @vsi: the VSI being configured
  226. *
  227. * Return 0 on success and a negative value on error
  228. */
  229. static void ice_vsi_set_num_qs(struct ice_vsi *vsi)
  230. {
  231. struct ice_pf *pf = vsi->back;
  232. switch (vsi->type) {
  233. case ICE_VSI_PF:
  234. vsi->alloc_txq = pf->num_lan_tx;
  235. vsi->alloc_rxq = pf->num_lan_rx;
  236. vsi->num_desc = ALIGN(ICE_DFLT_NUM_DESC, ICE_REQ_DESC_MULTIPLE);
  237. vsi->num_q_vectors = max_t(int, pf->num_lan_rx, pf->num_lan_tx);
  238. break;
  239. default:
  240. dev_warn(&vsi->back->pdev->dev, "Unknown VSI type %d\n",
  241. vsi->type);
  242. break;
  243. }
  244. }
  245. /**
  246. * ice_get_free_slot - get the next non-NULL location index in array
  247. * @array: array to search
  248. * @size: size of the array
  249. * @curr: last known occupied index to be used as a search hint
  250. *
  251. * void * is being used to keep the functionality generic. This lets us use this
  252. * function on any array of pointers.
  253. */
  254. static int ice_get_free_slot(void *array, int size, int curr)
  255. {
  256. int **tmp_array = (int **)array;
  257. int next;
  258. if (curr < (size - 1) && !tmp_array[curr + 1]) {
  259. next = curr + 1;
  260. } else {
  261. int i = 0;
  262. while ((i < size) && (tmp_array[i]))
  263. i++;
  264. if (i == size)
  265. next = ICE_NO_VSI;
  266. else
  267. next = i;
  268. }
  269. return next;
  270. }
  271. /**
  272. * ice_vsi_delete - delete a VSI from the switch
  273. * @vsi: pointer to VSI being removed
  274. */
  275. void ice_vsi_delete(struct ice_vsi *vsi)
  276. {
  277. struct ice_pf *pf = vsi->back;
  278. struct ice_vsi_ctx ctxt;
  279. enum ice_status status;
  280. ctxt.vsi_num = vsi->vsi_num;
  281. memcpy(&ctxt.info, &vsi->info, sizeof(struct ice_aqc_vsi_props));
  282. status = ice_free_vsi(&pf->hw, vsi->idx, &ctxt, false, NULL);
  283. if (status)
  284. dev_err(&pf->pdev->dev, "Failed to delete VSI %i in FW\n",
  285. vsi->vsi_num);
  286. }
  287. /**
  288. * ice_vsi_free_arrays - clean up VSI resources
  289. * @vsi: pointer to VSI being cleared
  290. * @free_qvectors: bool to specify if q_vectors should be deallocated
  291. */
  292. static void ice_vsi_free_arrays(struct ice_vsi *vsi, bool free_qvectors)
  293. {
  294. struct ice_pf *pf = vsi->back;
  295. /* free the ring and vector containers */
  296. if (free_qvectors && vsi->q_vectors) {
  297. devm_kfree(&pf->pdev->dev, vsi->q_vectors);
  298. vsi->q_vectors = NULL;
  299. }
  300. if (vsi->tx_rings) {
  301. devm_kfree(&pf->pdev->dev, vsi->tx_rings);
  302. vsi->tx_rings = NULL;
  303. }
  304. if (vsi->rx_rings) {
  305. devm_kfree(&pf->pdev->dev, vsi->rx_rings);
  306. vsi->rx_rings = NULL;
  307. }
  308. }
  309. /**
  310. * ice_vsi_clear - clean up and deallocate the provided VSI
  311. * @vsi: pointer to VSI being cleared
  312. *
  313. * This deallocates the VSI's queue resources, removes it from the PF's
  314. * VSI array if necessary, and deallocates the VSI
  315. *
  316. * Returns 0 on success, negative on failure
  317. */
  318. int ice_vsi_clear(struct ice_vsi *vsi)
  319. {
  320. struct ice_pf *pf = NULL;
  321. if (!vsi)
  322. return 0;
  323. if (!vsi->back)
  324. return -EINVAL;
  325. pf = vsi->back;
  326. if (!pf->vsi[vsi->idx] || pf->vsi[vsi->idx] != vsi) {
  327. dev_dbg(&pf->pdev->dev, "vsi does not exist at pf->vsi[%d]\n",
  328. vsi->idx);
  329. return -EINVAL;
  330. }
  331. mutex_lock(&pf->sw_mutex);
  332. /* updates the PF for this cleared VSI */
  333. pf->vsi[vsi->idx] = NULL;
  334. if (vsi->idx < pf->next_vsi)
  335. pf->next_vsi = vsi->idx;
  336. ice_vsi_free_arrays(vsi, true);
  337. mutex_unlock(&pf->sw_mutex);
  338. devm_kfree(&pf->pdev->dev, vsi);
  339. return 0;
  340. }
  341. /**
  342. * ice_msix_clean_rings - MSIX mode Interrupt Handler
  343. * @irq: interrupt number
  344. * @data: pointer to a q_vector
  345. */
  346. irqreturn_t ice_msix_clean_rings(int __always_unused irq, void *data)
  347. {
  348. struct ice_q_vector *q_vector = (struct ice_q_vector *)data;
  349. if (!q_vector->tx.ring && !q_vector->rx.ring)
  350. return IRQ_HANDLED;
  351. napi_schedule(&q_vector->napi);
  352. return IRQ_HANDLED;
  353. }
  354. /**
  355. * ice_vsi_alloc - Allocates the next available struct VSI in the PF
  356. * @pf: board private structure
  357. * @type: type of VSI
  358. *
  359. * returns a pointer to a VSI on success, NULL on failure.
  360. */
  361. static struct ice_vsi *ice_vsi_alloc(struct ice_pf *pf, enum ice_vsi_type type)
  362. {
  363. struct ice_vsi *vsi = NULL;
  364. /* Need to protect the allocation of the VSIs at the PF level */
  365. mutex_lock(&pf->sw_mutex);
  366. /* If we have already allocated our maximum number of VSIs,
  367. * pf->next_vsi will be ICE_NO_VSI. If not, pf->next_vsi index
  368. * is available to be populated
  369. */
  370. if (pf->next_vsi == ICE_NO_VSI) {
  371. dev_dbg(&pf->pdev->dev, "out of VSI slots!\n");
  372. goto unlock_pf;
  373. }
  374. vsi = devm_kzalloc(&pf->pdev->dev, sizeof(*vsi), GFP_KERNEL);
  375. if (!vsi)
  376. goto unlock_pf;
  377. vsi->type = type;
  378. vsi->back = pf;
  379. set_bit(__ICE_DOWN, vsi->state);
  380. vsi->idx = pf->next_vsi;
  381. vsi->work_lmt = ICE_DFLT_IRQ_WORK;
  382. ice_vsi_set_num_qs(vsi);
  383. switch (vsi->type) {
  384. case ICE_VSI_PF:
  385. if (ice_vsi_alloc_arrays(vsi, true))
  386. goto err_rings;
  387. /* Setup default MSIX irq handler for VSI */
  388. vsi->irq_handler = ice_msix_clean_rings;
  389. break;
  390. default:
  391. dev_warn(&pf->pdev->dev, "Unknown VSI type %d\n", vsi->type);
  392. goto unlock_pf;
  393. }
  394. /* fill VSI slot in the PF struct */
  395. pf->vsi[pf->next_vsi] = vsi;
  396. /* prepare pf->next_vsi for next use */
  397. pf->next_vsi = ice_get_free_slot(pf->vsi, pf->num_alloc_vsi,
  398. pf->next_vsi);
  399. goto unlock_pf;
  400. err_rings:
  401. devm_kfree(&pf->pdev->dev, vsi);
  402. vsi = NULL;
  403. unlock_pf:
  404. mutex_unlock(&pf->sw_mutex);
  405. return vsi;
  406. }
  407. /**
  408. * ice_vsi_get_qs_contig - Assign a contiguous chunk of queues to VSI
  409. * @vsi: the VSI getting queues
  410. *
  411. * Return 0 on success and a negative value on error
  412. */
  413. static int ice_vsi_get_qs_contig(struct ice_vsi *vsi)
  414. {
  415. struct ice_pf *pf = vsi->back;
  416. int offset, ret = 0;
  417. mutex_lock(&pf->avail_q_mutex);
  418. /* look for contiguous block of queues for Tx */
  419. offset = bitmap_find_next_zero_area(pf->avail_txqs, ICE_MAX_TXQS,
  420. 0, vsi->alloc_txq, 0);
  421. if (offset < ICE_MAX_TXQS) {
  422. int i;
  423. bitmap_set(pf->avail_txqs, offset, vsi->alloc_txq);
  424. for (i = 0; i < vsi->alloc_txq; i++)
  425. vsi->txq_map[i] = i + offset;
  426. } else {
  427. ret = -ENOMEM;
  428. vsi->tx_mapping_mode = ICE_VSI_MAP_SCATTER;
  429. }
  430. /* look for contiguous block of queues for Rx */
  431. offset = bitmap_find_next_zero_area(pf->avail_rxqs, ICE_MAX_RXQS,
  432. 0, vsi->alloc_rxq, 0);
  433. if (offset < ICE_MAX_RXQS) {
  434. int i;
  435. bitmap_set(pf->avail_rxqs, offset, vsi->alloc_rxq);
  436. for (i = 0; i < vsi->alloc_rxq; i++)
  437. vsi->rxq_map[i] = i + offset;
  438. } else {
  439. ret = -ENOMEM;
  440. vsi->rx_mapping_mode = ICE_VSI_MAP_SCATTER;
  441. }
  442. mutex_unlock(&pf->avail_q_mutex);
  443. return ret;
  444. }
  445. /**
  446. * ice_vsi_get_qs_scatter - Assign a scattered queues to VSI
  447. * @vsi: the VSI getting queues
  448. *
  449. * Return 0 on success and a negative value on error
  450. */
  451. static int ice_vsi_get_qs_scatter(struct ice_vsi *vsi)
  452. {
  453. struct ice_pf *pf = vsi->back;
  454. int i, index = 0;
  455. mutex_lock(&pf->avail_q_mutex);
  456. if (vsi->tx_mapping_mode == ICE_VSI_MAP_SCATTER) {
  457. for (i = 0; i < vsi->alloc_txq; i++) {
  458. index = find_next_zero_bit(pf->avail_txqs,
  459. ICE_MAX_TXQS, index);
  460. if (index < ICE_MAX_TXQS) {
  461. set_bit(index, pf->avail_txqs);
  462. vsi->txq_map[i] = index;
  463. } else {
  464. goto err_scatter_tx;
  465. }
  466. }
  467. }
  468. if (vsi->rx_mapping_mode == ICE_VSI_MAP_SCATTER) {
  469. for (i = 0; i < vsi->alloc_rxq; i++) {
  470. index = find_next_zero_bit(pf->avail_rxqs,
  471. ICE_MAX_RXQS, index);
  472. if (index < ICE_MAX_RXQS) {
  473. set_bit(index, pf->avail_rxqs);
  474. vsi->rxq_map[i] = index;
  475. } else {
  476. goto err_scatter_rx;
  477. }
  478. }
  479. }
  480. mutex_unlock(&pf->avail_q_mutex);
  481. return 0;
  482. err_scatter_rx:
  483. /* unflag any queues we have grabbed (i is failed position) */
  484. for (index = 0; index < i; index++) {
  485. clear_bit(vsi->rxq_map[index], pf->avail_rxqs);
  486. vsi->rxq_map[index] = 0;
  487. }
  488. i = vsi->alloc_txq;
  489. err_scatter_tx:
  490. /* i is either position of failed attempt or vsi->alloc_txq */
  491. for (index = 0; index < i; index++) {
  492. clear_bit(vsi->txq_map[index], pf->avail_txqs);
  493. vsi->txq_map[index] = 0;
  494. }
  495. mutex_unlock(&pf->avail_q_mutex);
  496. return -ENOMEM;
  497. }
  498. /**
  499. * ice_vsi_get_qs - Assign queues from PF to VSI
  500. * @vsi: the VSI to assign queues to
  501. *
  502. * Returns 0 on success and a negative value on error
  503. */
  504. static int ice_vsi_get_qs(struct ice_vsi *vsi)
  505. {
  506. int ret = 0;
  507. vsi->tx_mapping_mode = ICE_VSI_MAP_CONTIG;
  508. vsi->rx_mapping_mode = ICE_VSI_MAP_CONTIG;
  509. /* NOTE: ice_vsi_get_qs_contig() will set the Rx/Tx mapping
  510. * modes individually to scatter if assigning contiguous queues
  511. * to Rx or Tx fails
  512. */
  513. ret = ice_vsi_get_qs_contig(vsi);
  514. if (ret < 0) {
  515. if (vsi->tx_mapping_mode == ICE_VSI_MAP_SCATTER)
  516. vsi->alloc_txq = max_t(u16, vsi->alloc_txq,
  517. ICE_MAX_SCATTER_TXQS);
  518. if (vsi->rx_mapping_mode == ICE_VSI_MAP_SCATTER)
  519. vsi->alloc_rxq = max_t(u16, vsi->alloc_rxq,
  520. ICE_MAX_SCATTER_RXQS);
  521. ret = ice_vsi_get_qs_scatter(vsi);
  522. }
  523. return ret;
  524. }
  525. /**
  526. * ice_vsi_put_qs - Release queues from VSI to PF
  527. * @vsi: the VSI that is going to release queues
  528. */
  529. void ice_vsi_put_qs(struct ice_vsi *vsi)
  530. {
  531. struct ice_pf *pf = vsi->back;
  532. int i;
  533. mutex_lock(&pf->avail_q_mutex);
  534. for (i = 0; i < vsi->alloc_txq; i++) {
  535. clear_bit(vsi->txq_map[i], pf->avail_txqs);
  536. vsi->txq_map[i] = ICE_INVAL_Q_INDEX;
  537. }
  538. for (i = 0; i < vsi->alloc_rxq; i++) {
  539. clear_bit(vsi->rxq_map[i], pf->avail_rxqs);
  540. vsi->rxq_map[i] = ICE_INVAL_Q_INDEX;
  541. }
  542. mutex_unlock(&pf->avail_q_mutex);
  543. }
  544. /**
  545. * ice_rss_clean - Delete RSS related VSI structures that hold user inputs
  546. * @vsi: the VSI being removed
  547. */
  548. static void ice_rss_clean(struct ice_vsi *vsi)
  549. {
  550. struct ice_pf *pf;
  551. pf = vsi->back;
  552. if (vsi->rss_hkey_user)
  553. devm_kfree(&pf->pdev->dev, vsi->rss_hkey_user);
  554. if (vsi->rss_lut_user)
  555. devm_kfree(&pf->pdev->dev, vsi->rss_lut_user);
  556. }
  557. /**
  558. * ice_vsi_set_rss_params - Setup RSS capabilities per VSI type
  559. * @vsi: the VSI being configured
  560. */
  561. static void ice_vsi_set_rss_params(struct ice_vsi *vsi)
  562. {
  563. struct ice_hw_common_caps *cap;
  564. struct ice_pf *pf = vsi->back;
  565. if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
  566. vsi->rss_size = 1;
  567. return;
  568. }
  569. cap = &pf->hw.func_caps.common_cap;
  570. switch (vsi->type) {
  571. case ICE_VSI_PF:
  572. /* PF VSI will inherit RSS instance of PF */
  573. vsi->rss_table_size = cap->rss_table_size;
  574. vsi->rss_size = min_t(int, num_online_cpus(),
  575. BIT(cap->rss_table_entry_width));
  576. vsi->rss_lut_type = ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_PF;
  577. break;
  578. default:
  579. dev_warn(&pf->pdev->dev, "Unknown VSI type %d\n",
  580. vsi->type);
  581. break;
  582. }
  583. }
  584. /**
  585. * ice_set_dflt_vsi_ctx - Set default VSI context before adding a VSI
  586. * @ctxt: the VSI context being set
  587. *
  588. * This initializes a default VSI context for all sections except the Queues.
  589. */
  590. static void ice_set_dflt_vsi_ctx(struct ice_vsi_ctx *ctxt)
  591. {
  592. u32 table = 0;
  593. memset(&ctxt->info, 0, sizeof(ctxt->info));
  594. /* VSI's should be allocated from shared pool */
  595. ctxt->alloc_from_pool = true;
  596. /* Src pruning enabled by default */
  597. ctxt->info.sw_flags = ICE_AQ_VSI_SW_FLAG_SRC_PRUNE;
  598. /* Traffic from VSI can be sent to LAN */
  599. ctxt->info.sw_flags2 = ICE_AQ_VSI_SW_FLAG_LAN_ENA;
  600. /* By default bits 3 and 4 in vlan_flags are 0's which results in legacy
  601. * behavior (show VLAN, DEI, and UP) in descriptor. Also, allow all
  602. * packets untagged/tagged.
  603. */
  604. ctxt->info.vlan_flags = ((ICE_AQ_VSI_VLAN_MODE_ALL &
  605. ICE_AQ_VSI_VLAN_MODE_M) >>
  606. ICE_AQ_VSI_VLAN_MODE_S);
  607. /* Have 1:1 UP mapping for both ingress/egress tables */
  608. table |= ICE_UP_TABLE_TRANSLATE(0, 0);
  609. table |= ICE_UP_TABLE_TRANSLATE(1, 1);
  610. table |= ICE_UP_TABLE_TRANSLATE(2, 2);
  611. table |= ICE_UP_TABLE_TRANSLATE(3, 3);
  612. table |= ICE_UP_TABLE_TRANSLATE(4, 4);
  613. table |= ICE_UP_TABLE_TRANSLATE(5, 5);
  614. table |= ICE_UP_TABLE_TRANSLATE(6, 6);
  615. table |= ICE_UP_TABLE_TRANSLATE(7, 7);
  616. ctxt->info.ingress_table = cpu_to_le32(table);
  617. ctxt->info.egress_table = cpu_to_le32(table);
  618. /* Have 1:1 UP mapping for outer to inner UP table */
  619. ctxt->info.outer_up_table = cpu_to_le32(table);
  620. /* No Outer tag support outer_tag_flags remains to zero */
  621. }
  622. /**
  623. * ice_vsi_setup_q_map - Setup a VSI queue map
  624. * @vsi: the VSI being configured
  625. * @ctxt: VSI context structure
  626. */
  627. static void ice_vsi_setup_q_map(struct ice_vsi *vsi, struct ice_vsi_ctx *ctxt)
  628. {
  629. u16 offset = 0, qmap = 0, numq_tc;
  630. u16 pow = 0, max_rss = 0, qcount;
  631. u16 qcount_tx = vsi->alloc_txq;
  632. u16 qcount_rx = vsi->alloc_rxq;
  633. bool ena_tc0 = false;
  634. int i;
  635. /* at least TC0 should be enabled by default */
  636. if (vsi->tc_cfg.numtc) {
  637. if (!(vsi->tc_cfg.ena_tc & BIT(0)))
  638. ena_tc0 = true;
  639. } else {
  640. ena_tc0 = true;
  641. }
  642. if (ena_tc0) {
  643. vsi->tc_cfg.numtc++;
  644. vsi->tc_cfg.ena_tc |= 1;
  645. }
  646. numq_tc = qcount_rx / vsi->tc_cfg.numtc;
  647. /* TC mapping is a function of the number of Rx queues assigned to the
  648. * VSI for each traffic class and the offset of these queues.
  649. * The first 10 bits are for queue offset for TC0, next 4 bits for no:of
  650. * queues allocated to TC0. No:of queues is a power-of-2.
  651. *
  652. * If TC is not enabled, the queue offset is set to 0, and allocate one
  653. * queue, this way, traffic for the given TC will be sent to the default
  654. * queue.
  655. *
  656. * Setup number and offset of Rx queues for all TCs for the VSI
  657. */
  658. /* qcount will change if RSS is enabled */
  659. if (test_bit(ICE_FLAG_RSS_ENA, vsi->back->flags)) {
  660. if (vsi->type == ICE_VSI_PF)
  661. max_rss = ICE_MAX_LG_RSS_QS;
  662. else
  663. max_rss = ICE_MAX_SMALL_RSS_QS;
  664. qcount = min_t(int, numq_tc, max_rss);
  665. qcount = min_t(int, qcount, vsi->rss_size);
  666. } else {
  667. qcount = numq_tc;
  668. }
  669. /* find the (rounded up) power-of-2 of qcount */
  670. pow = order_base_2(qcount);
  671. for (i = 0; i < ICE_MAX_TRAFFIC_CLASS; i++) {
  672. if (!(vsi->tc_cfg.ena_tc & BIT(i))) {
  673. /* TC is not enabled */
  674. vsi->tc_cfg.tc_info[i].qoffset = 0;
  675. vsi->tc_cfg.tc_info[i].qcount = 1;
  676. ctxt->info.tc_mapping[i] = 0;
  677. continue;
  678. }
  679. /* TC is enabled */
  680. vsi->tc_cfg.tc_info[i].qoffset = offset;
  681. vsi->tc_cfg.tc_info[i].qcount = qcount;
  682. qmap = ((offset << ICE_AQ_VSI_TC_Q_OFFSET_S) &
  683. ICE_AQ_VSI_TC_Q_OFFSET_M) |
  684. ((pow << ICE_AQ_VSI_TC_Q_NUM_S) &
  685. ICE_AQ_VSI_TC_Q_NUM_M);
  686. offset += qcount;
  687. ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
  688. }
  689. vsi->num_txq = qcount_tx;
  690. vsi->num_rxq = offset;
  691. /* Rx queue mapping */
  692. ctxt->info.mapping_flags |= cpu_to_le16(ICE_AQ_VSI_Q_MAP_CONTIG);
  693. /* q_mapping buffer holds the info for the first queue allocated for
  694. * this VSI in the PF space and also the number of queues associated
  695. * with this VSI.
  696. */
  697. ctxt->info.q_mapping[0] = cpu_to_le16(vsi->rxq_map[0]);
  698. ctxt->info.q_mapping[1] = cpu_to_le16(vsi->num_rxq);
  699. }
  700. /**
  701. * ice_set_rss_vsi_ctx - Set RSS VSI context before adding a VSI
  702. * @ctxt: the VSI context being set
  703. * @vsi: the VSI being configured
  704. */
  705. static void ice_set_rss_vsi_ctx(struct ice_vsi_ctx *ctxt, struct ice_vsi *vsi)
  706. {
  707. u8 lut_type, hash_type;
  708. switch (vsi->type) {
  709. case ICE_VSI_PF:
  710. /* PF VSI will inherit RSS instance of PF */
  711. lut_type = ICE_AQ_VSI_Q_OPT_RSS_LUT_PF;
  712. hash_type = ICE_AQ_VSI_Q_OPT_RSS_TPLZ;
  713. break;
  714. default:
  715. dev_warn(&vsi->back->pdev->dev, "Unknown VSI type %d\n",
  716. vsi->type);
  717. return;
  718. }
  719. ctxt->info.q_opt_rss = ((lut_type << ICE_AQ_VSI_Q_OPT_RSS_LUT_S) &
  720. ICE_AQ_VSI_Q_OPT_RSS_LUT_M) |
  721. ((hash_type << ICE_AQ_VSI_Q_OPT_RSS_HASH_S) &
  722. ICE_AQ_VSI_Q_OPT_RSS_HASH_M);
  723. }
  724. /**
  725. * ice_vsi_init - Create and initialize a VSI
  726. * @vsi: the VSI being configured
  727. *
  728. * This initializes a VSI context depending on the VSI type to be added and
  729. * passes it down to the add_vsi aq command to create a new VSI.
  730. */
  731. static int ice_vsi_init(struct ice_vsi *vsi)
  732. {
  733. struct ice_vsi_ctx ctxt = { 0 };
  734. struct ice_pf *pf = vsi->back;
  735. struct ice_hw *hw = &pf->hw;
  736. int ret = 0;
  737. switch (vsi->type) {
  738. case ICE_VSI_PF:
  739. ctxt.flags = ICE_AQ_VSI_TYPE_PF;
  740. break;
  741. default:
  742. return -ENODEV;
  743. }
  744. ice_set_dflt_vsi_ctx(&ctxt);
  745. /* if the switch is in VEB mode, allow VSI loopback */
  746. if (vsi->vsw->bridge_mode == BRIDGE_MODE_VEB)
  747. ctxt.info.sw_flags |= ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
  748. /* Set LUT type and HASH type if RSS is enabled */
  749. if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
  750. ice_set_rss_vsi_ctx(&ctxt, vsi);
  751. ctxt.info.sw_id = vsi->port_info->sw_id;
  752. ice_vsi_setup_q_map(vsi, &ctxt);
  753. ret = ice_add_vsi(hw, vsi->idx, &ctxt, NULL);
  754. if (ret) {
  755. dev_err(&pf->pdev->dev,
  756. "Add VSI failed, err %d\n", ret);
  757. return -EIO;
  758. }
  759. /* keep context for update VSI operations */
  760. vsi->info = ctxt.info;
  761. /* record VSI number returned */
  762. vsi->vsi_num = ctxt.vsi_num;
  763. return ret;
  764. }
  765. /**
  766. * ice_free_q_vector - Free memory allocated for a specific interrupt vector
  767. * @vsi: VSI having the memory freed
  768. * @v_idx: index of the vector to be freed
  769. */
  770. static void ice_free_q_vector(struct ice_vsi *vsi, int v_idx)
  771. {
  772. struct ice_q_vector *q_vector;
  773. struct ice_ring *ring;
  774. if (!vsi->q_vectors[v_idx]) {
  775. dev_dbg(&vsi->back->pdev->dev, "Queue vector at index %d not found\n",
  776. v_idx);
  777. return;
  778. }
  779. q_vector = vsi->q_vectors[v_idx];
  780. ice_for_each_ring(ring, q_vector->tx)
  781. ring->q_vector = NULL;
  782. ice_for_each_ring(ring, q_vector->rx)
  783. ring->q_vector = NULL;
  784. /* only VSI with an associated netdev is set up with NAPI */
  785. if (vsi->netdev)
  786. netif_napi_del(&q_vector->napi);
  787. devm_kfree(&vsi->back->pdev->dev, q_vector);
  788. vsi->q_vectors[v_idx] = NULL;
  789. }
  790. /**
  791. * ice_vsi_free_q_vectors - Free memory allocated for interrupt vectors
  792. * @vsi: the VSI having memory freed
  793. */
  794. void ice_vsi_free_q_vectors(struct ice_vsi *vsi)
  795. {
  796. int v_idx;
  797. for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
  798. ice_free_q_vector(vsi, v_idx);
  799. }
  800. /**
  801. * ice_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
  802. * @vsi: the VSI being configured
  803. * @v_idx: index of the vector in the VSI struct
  804. *
  805. * We allocate one q_vector. If allocation fails we return -ENOMEM.
  806. */
  807. static int ice_vsi_alloc_q_vector(struct ice_vsi *vsi, int v_idx)
  808. {
  809. struct ice_pf *pf = vsi->back;
  810. struct ice_q_vector *q_vector;
  811. /* allocate q_vector */
  812. q_vector = devm_kzalloc(&pf->pdev->dev, sizeof(*q_vector), GFP_KERNEL);
  813. if (!q_vector)
  814. return -ENOMEM;
  815. q_vector->vsi = vsi;
  816. q_vector->v_idx = v_idx;
  817. /* only set affinity_mask if the CPU is online */
  818. if (cpu_online(v_idx))
  819. cpumask_set_cpu(v_idx, &q_vector->affinity_mask);
  820. /* This will not be called in the driver load path because the netdev
  821. * will not be created yet. All other cases with register the NAPI
  822. * handler here (i.e. resume, reset/rebuild, etc.)
  823. */
  824. if (vsi->netdev)
  825. netif_napi_add(vsi->netdev, &q_vector->napi, ice_napi_poll,
  826. NAPI_POLL_WEIGHT);
  827. /* tie q_vector and VSI together */
  828. vsi->q_vectors[v_idx] = q_vector;
  829. return 0;
  830. }
  831. /**
  832. * ice_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
  833. * @vsi: the VSI being configured
  834. *
  835. * We allocate one q_vector per queue interrupt. If allocation fails we
  836. * return -ENOMEM.
  837. */
  838. static int ice_vsi_alloc_q_vectors(struct ice_vsi *vsi)
  839. {
  840. struct ice_pf *pf = vsi->back;
  841. int v_idx = 0, num_q_vectors;
  842. int err;
  843. if (vsi->q_vectors[0]) {
  844. dev_dbg(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
  845. vsi->vsi_num);
  846. return -EEXIST;
  847. }
  848. if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
  849. num_q_vectors = vsi->num_q_vectors;
  850. } else {
  851. err = -EINVAL;
  852. goto err_out;
  853. }
  854. for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
  855. err = ice_vsi_alloc_q_vector(vsi, v_idx);
  856. if (err)
  857. goto err_out;
  858. }
  859. return 0;
  860. err_out:
  861. while (v_idx--)
  862. ice_free_q_vector(vsi, v_idx);
  863. dev_err(&pf->pdev->dev,
  864. "Failed to allocate %d q_vector for VSI %d, ret=%d\n",
  865. vsi->num_q_vectors, vsi->vsi_num, err);
  866. vsi->num_q_vectors = 0;
  867. return err;
  868. }
  869. /**
  870. * ice_vsi_setup_vector_base - Set up the base vector for the given VSI
  871. * @vsi: ptr to the VSI
  872. *
  873. * This should only be called after ice_vsi_alloc() which allocates the
  874. * corresponding SW VSI structure and initializes num_queue_pairs for the
  875. * newly allocated VSI.
  876. *
  877. * Returns 0 on success or negative on failure
  878. */
  879. static int ice_vsi_setup_vector_base(struct ice_vsi *vsi)
  880. {
  881. struct ice_pf *pf = vsi->back;
  882. int num_q_vectors = 0;
  883. if (vsi->sw_base_vector || vsi->hw_base_vector) {
  884. dev_dbg(&pf->pdev->dev, "VSI %d has non-zero HW base vector %d or SW base vector %d\n",
  885. vsi->vsi_num, vsi->hw_base_vector, vsi->sw_base_vector);
  886. return -EEXIST;
  887. }
  888. if (!test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
  889. return -ENOENT;
  890. switch (vsi->type) {
  891. case ICE_VSI_PF:
  892. num_q_vectors = vsi->num_q_vectors;
  893. /* reserve slots from OS requested IRQs */
  894. vsi->sw_base_vector = ice_get_res(pf, pf->sw_irq_tracker,
  895. num_q_vectors, vsi->idx);
  896. if (vsi->sw_base_vector < 0) {
  897. dev_err(&pf->pdev->dev,
  898. "Failed to get tracking for %d SW vectors for VSI %d, err=%d\n",
  899. num_q_vectors, vsi->vsi_num,
  900. vsi->sw_base_vector);
  901. return -ENOENT;
  902. }
  903. pf->num_avail_sw_msix -= num_q_vectors;
  904. /* reserve slots from HW interrupts */
  905. vsi->hw_base_vector = ice_get_res(pf, pf->hw_irq_tracker,
  906. num_q_vectors, vsi->idx);
  907. break;
  908. default:
  909. dev_warn(&vsi->back->pdev->dev, "Unknown VSI type %d\n",
  910. vsi->type);
  911. break;
  912. }
  913. if (vsi->hw_base_vector < 0) {
  914. dev_err(&pf->pdev->dev,
  915. "Failed to get tracking for %d HW vectors for VSI %d, err=%d\n",
  916. num_q_vectors, vsi->vsi_num, vsi->hw_base_vector);
  917. ice_free_res(vsi->back->sw_irq_tracker, vsi->sw_base_vector,
  918. vsi->idx);
  919. pf->num_avail_sw_msix += num_q_vectors;
  920. return -ENOENT;
  921. }
  922. pf->num_avail_hw_msix -= num_q_vectors;
  923. return 0;
  924. }
  925. /**
  926. * ice_vsi_clear_rings - Deallocates the Tx and Rx rings for VSI
  927. * @vsi: the VSI having rings deallocated
  928. */
  929. static void ice_vsi_clear_rings(struct ice_vsi *vsi)
  930. {
  931. int i;
  932. if (vsi->tx_rings) {
  933. for (i = 0; i < vsi->alloc_txq; i++) {
  934. if (vsi->tx_rings[i]) {
  935. kfree_rcu(vsi->tx_rings[i], rcu);
  936. vsi->tx_rings[i] = NULL;
  937. }
  938. }
  939. }
  940. if (vsi->rx_rings) {
  941. for (i = 0; i < vsi->alloc_rxq; i++) {
  942. if (vsi->rx_rings[i]) {
  943. kfree_rcu(vsi->rx_rings[i], rcu);
  944. vsi->rx_rings[i] = NULL;
  945. }
  946. }
  947. }
  948. }
  949. /**
  950. * ice_vsi_alloc_rings - Allocates Tx and Rx rings for the VSI
  951. * @vsi: VSI which is having rings allocated
  952. */
  953. static int ice_vsi_alloc_rings(struct ice_vsi *vsi)
  954. {
  955. struct ice_pf *pf = vsi->back;
  956. int i;
  957. /* Allocate tx_rings */
  958. for (i = 0; i < vsi->alloc_txq; i++) {
  959. struct ice_ring *ring;
  960. /* allocate with kzalloc(), free with kfree_rcu() */
  961. ring = kzalloc(sizeof(*ring), GFP_KERNEL);
  962. if (!ring)
  963. goto err_out;
  964. ring->q_index = i;
  965. ring->reg_idx = vsi->txq_map[i];
  966. ring->ring_active = false;
  967. ring->vsi = vsi;
  968. ring->dev = &pf->pdev->dev;
  969. ring->count = vsi->num_desc;
  970. vsi->tx_rings[i] = ring;
  971. }
  972. /* Allocate rx_rings */
  973. for (i = 0; i < vsi->alloc_rxq; i++) {
  974. struct ice_ring *ring;
  975. /* allocate with kzalloc(), free with kfree_rcu() */
  976. ring = kzalloc(sizeof(*ring), GFP_KERNEL);
  977. if (!ring)
  978. goto err_out;
  979. ring->q_index = i;
  980. ring->reg_idx = vsi->rxq_map[i];
  981. ring->ring_active = false;
  982. ring->vsi = vsi;
  983. ring->netdev = vsi->netdev;
  984. ring->dev = &pf->pdev->dev;
  985. ring->count = vsi->num_desc;
  986. vsi->rx_rings[i] = ring;
  987. }
  988. return 0;
  989. err_out:
  990. ice_vsi_clear_rings(vsi);
  991. return -ENOMEM;
  992. }
  993. /**
  994. * ice_vsi_map_rings_to_vectors - Map VSI rings to interrupt vectors
  995. * @vsi: the VSI being configured
  996. *
  997. * This function maps descriptor rings to the queue-specific vectors allotted
  998. * through the MSI-X enabling code. On a constrained vector budget, we map Tx
  999. * and Rx rings to the vector as "efficiently" as possible.
  1000. */
  1001. static void ice_vsi_map_rings_to_vectors(struct ice_vsi *vsi)
  1002. {
  1003. int q_vectors = vsi->num_q_vectors;
  1004. int tx_rings_rem, rx_rings_rem;
  1005. int v_id;
  1006. /* initially assigning remaining rings count to VSIs num queue value */
  1007. tx_rings_rem = vsi->num_txq;
  1008. rx_rings_rem = vsi->num_rxq;
  1009. for (v_id = 0; v_id < q_vectors; v_id++) {
  1010. struct ice_q_vector *q_vector = vsi->q_vectors[v_id];
  1011. int tx_rings_per_v, rx_rings_per_v, q_id, q_base;
  1012. /* Tx rings mapping to vector */
  1013. tx_rings_per_v = DIV_ROUND_UP(tx_rings_rem, q_vectors - v_id);
  1014. q_vector->num_ring_tx = tx_rings_per_v;
  1015. q_vector->tx.ring = NULL;
  1016. q_base = vsi->num_txq - tx_rings_rem;
  1017. for (q_id = q_base; q_id < (q_base + tx_rings_per_v); q_id++) {
  1018. struct ice_ring *tx_ring = vsi->tx_rings[q_id];
  1019. tx_ring->q_vector = q_vector;
  1020. tx_ring->next = q_vector->tx.ring;
  1021. q_vector->tx.ring = tx_ring;
  1022. }
  1023. tx_rings_rem -= tx_rings_per_v;
  1024. /* Rx rings mapping to vector */
  1025. rx_rings_per_v = DIV_ROUND_UP(rx_rings_rem, q_vectors - v_id);
  1026. q_vector->num_ring_rx = rx_rings_per_v;
  1027. q_vector->rx.ring = NULL;
  1028. q_base = vsi->num_rxq - rx_rings_rem;
  1029. for (q_id = q_base; q_id < (q_base + rx_rings_per_v); q_id++) {
  1030. struct ice_ring *rx_ring = vsi->rx_rings[q_id];
  1031. rx_ring->q_vector = q_vector;
  1032. rx_ring->next = q_vector->rx.ring;
  1033. q_vector->rx.ring = rx_ring;
  1034. }
  1035. rx_rings_rem -= rx_rings_per_v;
  1036. }
  1037. }
  1038. /**
  1039. * ice_vsi_manage_rss_lut - disable/enable RSS
  1040. * @vsi: the VSI being changed
  1041. * @ena: boolean value indicating if this is an enable or disable request
  1042. *
  1043. * In the event of disable request for RSS, this function will zero out RSS
  1044. * LUT, while in the event of enable request for RSS, it will reconfigure RSS
  1045. * LUT.
  1046. */
  1047. int ice_vsi_manage_rss_lut(struct ice_vsi *vsi, bool ena)
  1048. {
  1049. int err = 0;
  1050. u8 *lut;
  1051. lut = devm_kzalloc(&vsi->back->pdev->dev, vsi->rss_table_size,
  1052. GFP_KERNEL);
  1053. if (!lut)
  1054. return -ENOMEM;
  1055. if (ena) {
  1056. if (vsi->rss_lut_user)
  1057. memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
  1058. else
  1059. ice_fill_rss_lut(lut, vsi->rss_table_size,
  1060. vsi->rss_size);
  1061. }
  1062. err = ice_set_rss(vsi, NULL, lut, vsi->rss_table_size);
  1063. devm_kfree(&vsi->back->pdev->dev, lut);
  1064. return err;
  1065. }
  1066. /**
  1067. * ice_vsi_cfg_rss_lut_key - Configure RSS params for a VSI
  1068. * @vsi: VSI to be configured
  1069. */
  1070. static int ice_vsi_cfg_rss_lut_key(struct ice_vsi *vsi)
  1071. {
  1072. u8 seed[ICE_AQC_GET_SET_RSS_KEY_DATA_RSS_KEY_SIZE];
  1073. struct ice_aqc_get_set_rss_keys *key;
  1074. struct ice_pf *pf = vsi->back;
  1075. enum ice_status status;
  1076. int err = 0;
  1077. u8 *lut;
  1078. vsi->rss_size = min_t(int, vsi->rss_size, vsi->num_rxq);
  1079. lut = devm_kzalloc(&pf->pdev->dev, vsi->rss_table_size, GFP_KERNEL);
  1080. if (!lut)
  1081. return -ENOMEM;
  1082. if (vsi->rss_lut_user)
  1083. memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
  1084. else
  1085. ice_fill_rss_lut(lut, vsi->rss_table_size, vsi->rss_size);
  1086. status = ice_aq_set_rss_lut(&pf->hw, vsi->idx, vsi->rss_lut_type, lut,
  1087. vsi->rss_table_size);
  1088. if (status) {
  1089. dev_err(&vsi->back->pdev->dev,
  1090. "set_rss_lut failed, error %d\n", status);
  1091. err = -EIO;
  1092. goto ice_vsi_cfg_rss_exit;
  1093. }
  1094. key = devm_kzalloc(&vsi->back->pdev->dev, sizeof(*key), GFP_KERNEL);
  1095. if (!key) {
  1096. err = -ENOMEM;
  1097. goto ice_vsi_cfg_rss_exit;
  1098. }
  1099. if (vsi->rss_hkey_user)
  1100. memcpy(seed, vsi->rss_hkey_user,
  1101. ICE_AQC_GET_SET_RSS_KEY_DATA_RSS_KEY_SIZE);
  1102. else
  1103. netdev_rss_key_fill((void *)seed,
  1104. ICE_AQC_GET_SET_RSS_KEY_DATA_RSS_KEY_SIZE);
  1105. memcpy(&key->standard_rss_key, seed,
  1106. ICE_AQC_GET_SET_RSS_KEY_DATA_RSS_KEY_SIZE);
  1107. status = ice_aq_set_rss_key(&pf->hw, vsi->idx, key);
  1108. if (status) {
  1109. dev_err(&vsi->back->pdev->dev, "set_rss_key failed, error %d\n",
  1110. status);
  1111. err = -EIO;
  1112. }
  1113. devm_kfree(&pf->pdev->dev, key);
  1114. ice_vsi_cfg_rss_exit:
  1115. devm_kfree(&pf->pdev->dev, lut);
  1116. return err;
  1117. }
  1118. /**
  1119. * ice_add_mac_to_list - Add a mac address filter entry to the list
  1120. * @vsi: the VSI to be forwarded to
  1121. * @add_list: pointer to the list which contains MAC filter entries
  1122. * @macaddr: the MAC address to be added.
  1123. *
  1124. * Adds mac address filter entry to the temp list
  1125. *
  1126. * Returns 0 on success or ENOMEM on failure.
  1127. */
  1128. int ice_add_mac_to_list(struct ice_vsi *vsi, struct list_head *add_list,
  1129. const u8 *macaddr)
  1130. {
  1131. struct ice_fltr_list_entry *tmp;
  1132. struct ice_pf *pf = vsi->back;
  1133. tmp = devm_kzalloc(&pf->pdev->dev, sizeof(*tmp), GFP_ATOMIC);
  1134. if (!tmp)
  1135. return -ENOMEM;
  1136. tmp->fltr_info.flag = ICE_FLTR_TX;
  1137. tmp->fltr_info.src_id = ICE_SRC_ID_VSI;
  1138. tmp->fltr_info.lkup_type = ICE_SW_LKUP_MAC;
  1139. tmp->fltr_info.fltr_act = ICE_FWD_TO_VSI;
  1140. tmp->fltr_info.vsi_handle = vsi->idx;
  1141. ether_addr_copy(tmp->fltr_info.l_data.mac.mac_addr, macaddr);
  1142. INIT_LIST_HEAD(&tmp->list_entry);
  1143. list_add(&tmp->list_entry, add_list);
  1144. return 0;
  1145. }
  1146. /**
  1147. * ice_update_eth_stats - Update VSI-specific ethernet statistics counters
  1148. * @vsi: the VSI to be updated
  1149. */
  1150. void ice_update_eth_stats(struct ice_vsi *vsi)
  1151. {
  1152. struct ice_eth_stats *prev_es, *cur_es;
  1153. struct ice_hw *hw = &vsi->back->hw;
  1154. u16 vsi_num = vsi->vsi_num; /* HW absolute index of a VSI */
  1155. prev_es = &vsi->eth_stats_prev;
  1156. cur_es = &vsi->eth_stats;
  1157. ice_stat_update40(hw, GLV_GORCH(vsi_num), GLV_GORCL(vsi_num),
  1158. vsi->stat_offsets_loaded, &prev_es->rx_bytes,
  1159. &cur_es->rx_bytes);
  1160. ice_stat_update40(hw, GLV_UPRCH(vsi_num), GLV_UPRCL(vsi_num),
  1161. vsi->stat_offsets_loaded, &prev_es->rx_unicast,
  1162. &cur_es->rx_unicast);
  1163. ice_stat_update40(hw, GLV_MPRCH(vsi_num), GLV_MPRCL(vsi_num),
  1164. vsi->stat_offsets_loaded, &prev_es->rx_multicast,
  1165. &cur_es->rx_multicast);
  1166. ice_stat_update40(hw, GLV_BPRCH(vsi_num), GLV_BPRCL(vsi_num),
  1167. vsi->stat_offsets_loaded, &prev_es->rx_broadcast,
  1168. &cur_es->rx_broadcast);
  1169. ice_stat_update32(hw, GLV_RDPC(vsi_num), vsi->stat_offsets_loaded,
  1170. &prev_es->rx_discards, &cur_es->rx_discards);
  1171. ice_stat_update40(hw, GLV_GOTCH(vsi_num), GLV_GOTCL(vsi_num),
  1172. vsi->stat_offsets_loaded, &prev_es->tx_bytes,
  1173. &cur_es->tx_bytes);
  1174. ice_stat_update40(hw, GLV_UPTCH(vsi_num), GLV_UPTCL(vsi_num),
  1175. vsi->stat_offsets_loaded, &prev_es->tx_unicast,
  1176. &cur_es->tx_unicast);
  1177. ice_stat_update40(hw, GLV_MPTCH(vsi_num), GLV_MPTCL(vsi_num),
  1178. vsi->stat_offsets_loaded, &prev_es->tx_multicast,
  1179. &cur_es->tx_multicast);
  1180. ice_stat_update40(hw, GLV_BPTCH(vsi_num), GLV_BPTCL(vsi_num),
  1181. vsi->stat_offsets_loaded, &prev_es->tx_broadcast,
  1182. &cur_es->tx_broadcast);
  1183. ice_stat_update32(hw, GLV_TEPC(vsi_num), vsi->stat_offsets_loaded,
  1184. &prev_es->tx_errors, &cur_es->tx_errors);
  1185. vsi->stat_offsets_loaded = true;
  1186. }
  1187. /**
  1188. * ice_free_fltr_list - free filter lists helper
  1189. * @dev: pointer to the device struct
  1190. * @h: pointer to the list head to be freed
  1191. *
  1192. * Helper function to free filter lists previously created using
  1193. * ice_add_mac_to_list
  1194. */
  1195. void ice_free_fltr_list(struct device *dev, struct list_head *h)
  1196. {
  1197. struct ice_fltr_list_entry *e, *tmp;
  1198. list_for_each_entry_safe(e, tmp, h, list_entry) {
  1199. list_del(&e->list_entry);
  1200. devm_kfree(dev, e);
  1201. }
  1202. }
  1203. /**
  1204. * ice_vsi_add_vlan - Add VSI membership for given VLAN
  1205. * @vsi: the VSI being configured
  1206. * @vid: VLAN id to be added
  1207. */
  1208. int ice_vsi_add_vlan(struct ice_vsi *vsi, u16 vid)
  1209. {
  1210. struct ice_fltr_list_entry *tmp;
  1211. struct ice_pf *pf = vsi->back;
  1212. LIST_HEAD(tmp_add_list);
  1213. enum ice_status status;
  1214. int err = 0;
  1215. tmp = devm_kzalloc(&pf->pdev->dev, sizeof(*tmp), GFP_KERNEL);
  1216. if (!tmp)
  1217. return -ENOMEM;
  1218. tmp->fltr_info.lkup_type = ICE_SW_LKUP_VLAN;
  1219. tmp->fltr_info.fltr_act = ICE_FWD_TO_VSI;
  1220. tmp->fltr_info.flag = ICE_FLTR_TX;
  1221. tmp->fltr_info.src_id = ICE_SRC_ID_VSI;
  1222. tmp->fltr_info.vsi_handle = vsi->idx;
  1223. tmp->fltr_info.l_data.vlan.vlan_id = vid;
  1224. INIT_LIST_HEAD(&tmp->list_entry);
  1225. list_add(&tmp->list_entry, &tmp_add_list);
  1226. status = ice_add_vlan(&pf->hw, &tmp_add_list);
  1227. if (status) {
  1228. err = -ENODEV;
  1229. dev_err(&pf->pdev->dev, "Failure Adding VLAN %d on VSI %i\n",
  1230. vid, vsi->vsi_num);
  1231. }
  1232. ice_free_fltr_list(&pf->pdev->dev, &tmp_add_list);
  1233. return err;
  1234. }
  1235. /**
  1236. * ice_vsi_kill_vlan - Remove VSI membership for a given VLAN
  1237. * @vsi: the VSI being configured
  1238. * @vid: VLAN id to be removed
  1239. *
  1240. * Returns 0 on success and negative on failure
  1241. */
  1242. int ice_vsi_kill_vlan(struct ice_vsi *vsi, u16 vid)
  1243. {
  1244. struct ice_fltr_list_entry *list;
  1245. struct ice_pf *pf = vsi->back;
  1246. LIST_HEAD(tmp_add_list);
  1247. int status = 0;
  1248. list = devm_kzalloc(&pf->pdev->dev, sizeof(*list), GFP_KERNEL);
  1249. if (!list)
  1250. return -ENOMEM;
  1251. list->fltr_info.lkup_type = ICE_SW_LKUP_VLAN;
  1252. list->fltr_info.vsi_handle = vsi->idx;
  1253. list->fltr_info.fltr_act = ICE_FWD_TO_VSI;
  1254. list->fltr_info.l_data.vlan.vlan_id = vid;
  1255. list->fltr_info.flag = ICE_FLTR_TX;
  1256. list->fltr_info.src_id = ICE_SRC_ID_VSI;
  1257. INIT_LIST_HEAD(&list->list_entry);
  1258. list_add(&list->list_entry, &tmp_add_list);
  1259. if (ice_remove_vlan(&pf->hw, &tmp_add_list)) {
  1260. dev_err(&pf->pdev->dev, "Error removing VLAN %d on vsi %i\n",
  1261. vid, vsi->vsi_num);
  1262. status = -EIO;
  1263. }
  1264. ice_free_fltr_list(&pf->pdev->dev, &tmp_add_list);
  1265. return status;
  1266. }
  1267. /**
  1268. * ice_vsi_cfg_rxqs - Configure the VSI for Rx
  1269. * @vsi: the VSI being configured
  1270. *
  1271. * Return 0 on success and a negative value on error
  1272. * Configure the Rx VSI for operation.
  1273. */
  1274. int ice_vsi_cfg_rxqs(struct ice_vsi *vsi)
  1275. {
  1276. int err = 0;
  1277. u16 i;
  1278. if (vsi->netdev && vsi->netdev->mtu > ETH_DATA_LEN)
  1279. vsi->max_frame = vsi->netdev->mtu +
  1280. ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
  1281. else
  1282. vsi->max_frame = ICE_RXBUF_2048;
  1283. vsi->rx_buf_len = ICE_RXBUF_2048;
  1284. /* set up individual rings */
  1285. for (i = 0; i < vsi->num_rxq && !err; i++)
  1286. err = ice_setup_rx_ctx(vsi->rx_rings[i]);
  1287. if (err) {
  1288. dev_err(&vsi->back->pdev->dev, "ice_setup_rx_ctx failed\n");
  1289. return -EIO;
  1290. }
  1291. return err;
  1292. }
  1293. /**
  1294. * ice_vsi_cfg_txqs - Configure the VSI for Tx
  1295. * @vsi: the VSI being configured
  1296. *
  1297. * Return 0 on success and a negative value on error
  1298. * Configure the Tx VSI for operation.
  1299. */
  1300. int ice_vsi_cfg_txqs(struct ice_vsi *vsi)
  1301. {
  1302. struct ice_aqc_add_tx_qgrp *qg_buf;
  1303. struct ice_aqc_add_txqs_perq *txq;
  1304. struct ice_pf *pf = vsi->back;
  1305. enum ice_status status;
  1306. u16 buf_len, i, pf_q;
  1307. int err = 0, tc = 0;
  1308. u8 num_q_grps;
  1309. buf_len = sizeof(struct ice_aqc_add_tx_qgrp);
  1310. qg_buf = devm_kzalloc(&pf->pdev->dev, buf_len, GFP_KERNEL);
  1311. if (!qg_buf)
  1312. return -ENOMEM;
  1313. if (vsi->num_txq > ICE_MAX_TXQ_PER_TXQG) {
  1314. err = -EINVAL;
  1315. goto err_cfg_txqs;
  1316. }
  1317. qg_buf->num_txqs = 1;
  1318. num_q_grps = 1;
  1319. /* set up and configure the Tx queues */
  1320. ice_for_each_txq(vsi, i) {
  1321. struct ice_tlan_ctx tlan_ctx = { 0 };
  1322. pf_q = vsi->txq_map[i];
  1323. ice_setup_tx_ctx(vsi->tx_rings[i], &tlan_ctx, pf_q);
  1324. /* copy context contents into the qg_buf */
  1325. qg_buf->txqs[0].txq_id = cpu_to_le16(pf_q);
  1326. ice_set_ctx((u8 *)&tlan_ctx, qg_buf->txqs[0].txq_ctx,
  1327. ice_tlan_ctx_info);
  1328. /* init queue specific tail reg. It is referred as transmit
  1329. * comm scheduler queue doorbell.
  1330. */
  1331. vsi->tx_rings[i]->tail = pf->hw.hw_addr + QTX_COMM_DBELL(pf_q);
  1332. status = ice_ena_vsi_txq(vsi->port_info, vsi->idx, tc,
  1333. num_q_grps, qg_buf, buf_len, NULL);
  1334. if (status) {
  1335. dev_err(&vsi->back->pdev->dev,
  1336. "Failed to set LAN Tx queue context, error: %d\n",
  1337. status);
  1338. err = -ENODEV;
  1339. goto err_cfg_txqs;
  1340. }
  1341. /* Add Tx Queue TEID into the VSI Tx ring from the response
  1342. * This will complete configuring and enabling the queue.
  1343. */
  1344. txq = &qg_buf->txqs[0];
  1345. if (pf_q == le16_to_cpu(txq->txq_id))
  1346. vsi->tx_rings[i]->txq_teid =
  1347. le32_to_cpu(txq->q_teid);
  1348. }
  1349. err_cfg_txqs:
  1350. devm_kfree(&pf->pdev->dev, qg_buf);
  1351. return err;
  1352. }
  1353. /**
  1354. * ice_vsi_cfg_msix - MSIX mode Interrupt Config in the HW
  1355. * @vsi: the VSI being configured
  1356. */
  1357. void ice_vsi_cfg_msix(struct ice_vsi *vsi)
  1358. {
  1359. struct ice_pf *pf = vsi->back;
  1360. u16 vector = vsi->hw_base_vector;
  1361. struct ice_hw *hw = &pf->hw;
  1362. u32 txq = 0, rxq = 0;
  1363. int i, q, itr;
  1364. u8 itr_gran;
  1365. for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
  1366. struct ice_q_vector *q_vector = vsi->q_vectors[i];
  1367. itr_gran = hw->itr_gran_200;
  1368. if (q_vector->num_ring_rx) {
  1369. q_vector->rx.itr =
  1370. ITR_TO_REG(vsi->rx_rings[rxq]->rx_itr_setting,
  1371. itr_gran);
  1372. q_vector->rx.latency_range = ICE_LOW_LATENCY;
  1373. }
  1374. if (q_vector->num_ring_tx) {
  1375. q_vector->tx.itr =
  1376. ITR_TO_REG(vsi->tx_rings[txq]->tx_itr_setting,
  1377. itr_gran);
  1378. q_vector->tx.latency_range = ICE_LOW_LATENCY;
  1379. }
  1380. wr32(hw, GLINT_ITR(ICE_RX_ITR, vector), q_vector->rx.itr);
  1381. wr32(hw, GLINT_ITR(ICE_TX_ITR, vector), q_vector->tx.itr);
  1382. /* Both Transmit Queue Interrupt Cause Control register
  1383. * and Receive Queue Interrupt Cause control register
  1384. * expects MSIX_INDX field to be the vector index
  1385. * within the function space and not the absolute
  1386. * vector index across PF or across device.
  1387. * For SR-IOV VF VSIs queue vector index always starts
  1388. * with 1 since first vector index(0) is used for OICR
  1389. * in VF space. Since VMDq and other PF VSIs are within
  1390. * the PF function space, use the vector index that is
  1391. * tracked for this PF.
  1392. */
  1393. for (q = 0; q < q_vector->num_ring_tx; q++) {
  1394. u32 val;
  1395. itr = ICE_ITR_NONE;
  1396. val = QINT_TQCTL_CAUSE_ENA_M |
  1397. (itr << QINT_TQCTL_ITR_INDX_S) |
  1398. (vector << QINT_TQCTL_MSIX_INDX_S);
  1399. wr32(hw, QINT_TQCTL(vsi->txq_map[txq]), val);
  1400. txq++;
  1401. }
  1402. for (q = 0; q < q_vector->num_ring_rx; q++) {
  1403. u32 val;
  1404. itr = ICE_ITR_NONE;
  1405. val = QINT_RQCTL_CAUSE_ENA_M |
  1406. (itr << QINT_RQCTL_ITR_INDX_S) |
  1407. (vector << QINT_RQCTL_MSIX_INDX_S);
  1408. wr32(hw, QINT_RQCTL(vsi->rxq_map[rxq]), val);
  1409. rxq++;
  1410. }
  1411. }
  1412. ice_flush(hw);
  1413. }
  1414. /**
  1415. * ice_vsi_manage_vlan_insertion - Manage VLAN insertion for the VSI for Tx
  1416. * @vsi: the VSI being changed
  1417. */
  1418. int ice_vsi_manage_vlan_insertion(struct ice_vsi *vsi)
  1419. {
  1420. struct device *dev = &vsi->back->pdev->dev;
  1421. struct ice_hw *hw = &vsi->back->hw;
  1422. struct ice_vsi_ctx ctxt = { 0 };
  1423. enum ice_status status;
  1424. /* Here we are configuring the VSI to let the driver add VLAN tags by
  1425. * setting vlan_flags to ICE_AQ_VSI_VLAN_MODE_ALL. The actual VLAN tag
  1426. * insertion happens in the Tx hot path, in ice_tx_map.
  1427. */
  1428. ctxt.info.vlan_flags = ICE_AQ_VSI_VLAN_MODE_ALL;
  1429. ctxt.info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID);
  1430. status = ice_update_vsi(hw, vsi->idx, &ctxt, NULL);
  1431. if (status) {
  1432. dev_err(dev, "update VSI for VLAN insert failed, err %d aq_err %d\n",
  1433. status, hw->adminq.sq_last_status);
  1434. return -EIO;
  1435. }
  1436. vsi->info.vlan_flags = ctxt.info.vlan_flags;
  1437. return 0;
  1438. }
  1439. /**
  1440. * ice_vsi_manage_vlan_stripping - Manage VLAN stripping for the VSI for Rx
  1441. * @vsi: the VSI being changed
  1442. * @ena: boolean value indicating if this is a enable or disable request
  1443. */
  1444. int ice_vsi_manage_vlan_stripping(struct ice_vsi *vsi, bool ena)
  1445. {
  1446. struct device *dev = &vsi->back->pdev->dev;
  1447. struct ice_hw *hw = &vsi->back->hw;
  1448. struct ice_vsi_ctx ctxt = { 0 };
  1449. enum ice_status status;
  1450. /* Here we are configuring what the VSI should do with the VLAN tag in
  1451. * the Rx packet. We can either leave the tag in the packet or put it in
  1452. * the Rx descriptor.
  1453. */
  1454. if (ena) {
  1455. /* Strip VLAN tag from Rx packet and put it in the desc */
  1456. ctxt.info.vlan_flags = ICE_AQ_VSI_VLAN_EMOD_STR_BOTH;
  1457. } else {
  1458. /* Disable stripping. Leave tag in packet */
  1459. ctxt.info.vlan_flags = ICE_AQ_VSI_VLAN_EMOD_NOTHING;
  1460. }
  1461. /* Allow all packets untagged/tagged */
  1462. ctxt.info.vlan_flags |= ICE_AQ_VSI_VLAN_MODE_ALL;
  1463. ctxt.info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID);
  1464. status = ice_update_vsi(hw, vsi->idx, &ctxt, NULL);
  1465. if (status) {
  1466. dev_err(dev, "update VSI for VLAN strip failed, ena = %d err %d aq_err %d\n",
  1467. ena, status, hw->adminq.sq_last_status);
  1468. return -EIO;
  1469. }
  1470. vsi->info.vlan_flags = ctxt.info.vlan_flags;
  1471. return 0;
  1472. }
  1473. /**
  1474. * ice_vsi_start_rx_rings - start VSI's Rx rings
  1475. * @vsi: the VSI whose rings are to be started
  1476. *
  1477. * Returns 0 on success and a negative value on error
  1478. */
  1479. int ice_vsi_start_rx_rings(struct ice_vsi *vsi)
  1480. {
  1481. return ice_vsi_ctrl_rx_rings(vsi, true);
  1482. }
  1483. /**
  1484. * ice_vsi_stop_rx_rings - stop VSI's Rx rings
  1485. * @vsi: the VSI
  1486. *
  1487. * Returns 0 on success and a negative value on error
  1488. */
  1489. int ice_vsi_stop_rx_rings(struct ice_vsi *vsi)
  1490. {
  1491. return ice_vsi_ctrl_rx_rings(vsi, false);
  1492. }
  1493. /**
  1494. * ice_vsi_stop_tx_rings - Disable Tx rings
  1495. * @vsi: the VSI being configured
  1496. */
  1497. int ice_vsi_stop_tx_rings(struct ice_vsi *vsi)
  1498. {
  1499. struct ice_pf *pf = vsi->back;
  1500. struct ice_hw *hw = &pf->hw;
  1501. enum ice_status status;
  1502. u32 *q_teids, val;
  1503. u16 *q_ids, i;
  1504. int err = 0;
  1505. if (vsi->num_txq > ICE_LAN_TXQ_MAX_QDIS)
  1506. return -EINVAL;
  1507. q_teids = devm_kcalloc(&pf->pdev->dev, vsi->num_txq, sizeof(*q_teids),
  1508. GFP_KERNEL);
  1509. if (!q_teids)
  1510. return -ENOMEM;
  1511. q_ids = devm_kcalloc(&pf->pdev->dev, vsi->num_txq, sizeof(*q_ids),
  1512. GFP_KERNEL);
  1513. if (!q_ids) {
  1514. err = -ENOMEM;
  1515. goto err_alloc_q_ids;
  1516. }
  1517. /* set up the Tx queue list to be disabled */
  1518. ice_for_each_txq(vsi, i) {
  1519. u16 v_idx;
  1520. if (!vsi->tx_rings || !vsi->tx_rings[i]) {
  1521. err = -EINVAL;
  1522. goto err_out;
  1523. }
  1524. q_ids[i] = vsi->txq_map[i];
  1525. q_teids[i] = vsi->tx_rings[i]->txq_teid;
  1526. /* clear cause_ena bit for disabled queues */
  1527. val = rd32(hw, QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
  1528. val &= ~QINT_TQCTL_CAUSE_ENA_M;
  1529. wr32(hw, QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
  1530. /* software is expected to wait for 100 ns */
  1531. ndelay(100);
  1532. /* trigger a software interrupt for the vector associated to
  1533. * the queue to schedule NAPI handler
  1534. */
  1535. v_idx = vsi->tx_rings[i]->q_vector->v_idx;
  1536. wr32(hw, GLINT_DYN_CTL(vsi->hw_base_vector + v_idx),
  1537. GLINT_DYN_CTL_SWINT_TRIG_M | GLINT_DYN_CTL_INTENA_MSK_M);
  1538. }
  1539. status = ice_dis_vsi_txq(vsi->port_info, vsi->num_txq, q_ids, q_teids,
  1540. NULL);
  1541. /* if the disable queue command was exercised during an active reset
  1542. * flow, ICE_ERR_RESET_ONGOING is returned. This is not an error as
  1543. * the reset operation disables queues at the hardware level anyway.
  1544. */
  1545. if (status == ICE_ERR_RESET_ONGOING) {
  1546. dev_info(&pf->pdev->dev,
  1547. "Reset in progress. LAN Tx queues already disabled\n");
  1548. } else if (status) {
  1549. dev_err(&pf->pdev->dev,
  1550. "Failed to disable LAN Tx queues, error: %d\n",
  1551. status);
  1552. err = -ENODEV;
  1553. }
  1554. err_out:
  1555. devm_kfree(&pf->pdev->dev, q_ids);
  1556. err_alloc_q_ids:
  1557. devm_kfree(&pf->pdev->dev, q_teids);
  1558. return err;
  1559. }
  1560. /**
  1561. * ice_cfg_vlan_pruning - enable or disable VLAN pruning on the VSI
  1562. * @vsi: VSI to enable or disable VLAN pruning on
  1563. * @ena: set to true to enable VLAN pruning and false to disable it
  1564. *
  1565. * returns 0 if VSI is updated, negative otherwise
  1566. */
  1567. int ice_cfg_vlan_pruning(struct ice_vsi *vsi, bool ena)
  1568. {
  1569. struct ice_vsi_ctx *ctxt;
  1570. struct device *dev;
  1571. int status;
  1572. if (!vsi)
  1573. return -EINVAL;
  1574. dev = &vsi->back->pdev->dev;
  1575. ctxt = devm_kzalloc(dev, sizeof(*ctxt), GFP_KERNEL);
  1576. if (!ctxt)
  1577. return -ENOMEM;
  1578. ctxt->info = vsi->info;
  1579. if (ena) {
  1580. ctxt->info.sec_flags |=
  1581. ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
  1582. ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S;
  1583. ctxt->info.sw_flags2 |= ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
  1584. } else {
  1585. ctxt->info.sec_flags &=
  1586. ~(ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
  1587. ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S);
  1588. ctxt->info.sw_flags2 &= ~ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
  1589. }
  1590. ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID |
  1591. ICE_AQ_VSI_PROP_SW_VALID);
  1592. status = ice_update_vsi(&vsi->back->hw, vsi->idx, ctxt, NULL);
  1593. if (status) {
  1594. netdev_err(vsi->netdev, "%sabling VLAN pruning on VSI handle: %d, VSI HW ID: %d failed, err = %d, aq_err = %d\n",
  1595. ena ? "Ena" : "Dis", vsi->idx, vsi->vsi_num, status,
  1596. vsi->back->hw.adminq.sq_last_status);
  1597. goto err_out;
  1598. }
  1599. vsi->info.sec_flags = ctxt->info.sec_flags;
  1600. vsi->info.sw_flags2 = ctxt->info.sw_flags2;
  1601. devm_kfree(dev, ctxt);
  1602. return 0;
  1603. err_out:
  1604. devm_kfree(dev, ctxt);
  1605. return -EIO;
  1606. }
  1607. /**
  1608. * ice_vsi_setup - Set up a VSI by a given type
  1609. * @pf: board private structure
  1610. * @pi: pointer to the port_info instance
  1611. * @type: VSI type
  1612. * @vf_id: defines VF id to which this VSI connects. This field is meant to be
  1613. * used only for ICE_VSI_VF VSI type. For other VSI types, should
  1614. * fill-in ICE_INVAL_VFID as input.
  1615. *
  1616. * This allocates the sw VSI structure and its queue resources.
  1617. *
  1618. * Returns pointer to the successfully allocated and configured VSI sw struct on
  1619. * success, NULL on failure.
  1620. */
  1621. struct ice_vsi *
  1622. ice_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi,
  1623. enum ice_vsi_type type, u16 __always_unused vf_id)
  1624. {
  1625. u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
  1626. struct device *dev = &pf->pdev->dev;
  1627. struct ice_vsi *vsi;
  1628. int ret, i;
  1629. vsi = ice_vsi_alloc(pf, type);
  1630. if (!vsi) {
  1631. dev_err(dev, "could not allocate VSI\n");
  1632. return NULL;
  1633. }
  1634. vsi->port_info = pi;
  1635. vsi->vsw = pf->first_sw;
  1636. if (ice_vsi_get_qs(vsi)) {
  1637. dev_err(dev, "Failed to allocate queues. vsi->idx = %d\n",
  1638. vsi->idx);
  1639. goto unroll_get_qs;
  1640. }
  1641. /* set RSS capabilities */
  1642. ice_vsi_set_rss_params(vsi);
  1643. /* create the VSI */
  1644. ret = ice_vsi_init(vsi);
  1645. if (ret)
  1646. goto unroll_get_qs;
  1647. switch (vsi->type) {
  1648. case ICE_VSI_PF:
  1649. ret = ice_vsi_alloc_q_vectors(vsi);
  1650. if (ret)
  1651. goto unroll_vsi_init;
  1652. ret = ice_vsi_setup_vector_base(vsi);
  1653. if (ret)
  1654. goto unroll_alloc_q_vector;
  1655. ret = ice_vsi_alloc_rings(vsi);
  1656. if (ret)
  1657. goto unroll_vector_base;
  1658. ice_vsi_map_rings_to_vectors(vsi);
  1659. /* Do not exit if configuring RSS had an issue, at least
  1660. * receive traffic on first queue. Hence no need to capture
  1661. * return value
  1662. */
  1663. if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
  1664. ice_vsi_cfg_rss_lut_key(vsi);
  1665. break;
  1666. default:
  1667. /* if VSI type is not recognized, clean up the resources and
  1668. * exit
  1669. */
  1670. goto unroll_vsi_init;
  1671. }
  1672. ice_vsi_set_tc_cfg(vsi);
  1673. /* configure VSI nodes based on number of queues and TC's */
  1674. for (i = 0; i < vsi->tc_cfg.numtc; i++)
  1675. max_txqs[i] = vsi->num_txq;
  1676. ret = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
  1677. max_txqs);
  1678. if (ret) {
  1679. dev_info(&pf->pdev->dev, "Failed VSI lan queue config\n");
  1680. goto unroll_vector_base;
  1681. }
  1682. return vsi;
  1683. unroll_vector_base:
  1684. /* reclaim SW interrupts back to the common pool */
  1685. ice_free_res(vsi->back->sw_irq_tracker, vsi->sw_base_vector, vsi->idx);
  1686. pf->num_avail_sw_msix += vsi->num_q_vectors;
  1687. /* reclaim HW interrupt back to the common pool */
  1688. ice_free_res(vsi->back->hw_irq_tracker, vsi->hw_base_vector, vsi->idx);
  1689. pf->num_avail_hw_msix += vsi->num_q_vectors;
  1690. unroll_alloc_q_vector:
  1691. ice_vsi_free_q_vectors(vsi);
  1692. unroll_vsi_init:
  1693. ice_vsi_delete(vsi);
  1694. unroll_get_qs:
  1695. ice_vsi_put_qs(vsi);
  1696. pf->q_left_tx += vsi->alloc_txq;
  1697. pf->q_left_rx += vsi->alloc_rxq;
  1698. ice_vsi_clear(vsi);
  1699. return NULL;
  1700. }
  1701. /**
  1702. * ice_vsi_release_msix - Clear the queue to Interrupt mapping in HW
  1703. * @vsi: the VSI being cleaned up
  1704. */
  1705. static void ice_vsi_release_msix(struct ice_vsi *vsi)
  1706. {
  1707. struct ice_pf *pf = vsi->back;
  1708. u16 vector = vsi->hw_base_vector;
  1709. struct ice_hw *hw = &pf->hw;
  1710. u32 txq = 0;
  1711. u32 rxq = 0;
  1712. int i, q;
  1713. for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
  1714. struct ice_q_vector *q_vector = vsi->q_vectors[i];
  1715. wr32(hw, GLINT_ITR(ICE_RX_ITR, vector), 0);
  1716. wr32(hw, GLINT_ITR(ICE_TX_ITR, vector), 0);
  1717. for (q = 0; q < q_vector->num_ring_tx; q++) {
  1718. wr32(hw, QINT_TQCTL(vsi->txq_map[txq]), 0);
  1719. txq++;
  1720. }
  1721. for (q = 0; q < q_vector->num_ring_rx; q++) {
  1722. wr32(hw, QINT_RQCTL(vsi->rxq_map[rxq]), 0);
  1723. rxq++;
  1724. }
  1725. }
  1726. ice_flush(hw);
  1727. }
  1728. /**
  1729. * ice_vsi_free_irq - Free the IRQ association with the OS
  1730. * @vsi: the VSI being configured
  1731. */
  1732. void ice_vsi_free_irq(struct ice_vsi *vsi)
  1733. {
  1734. struct ice_pf *pf = vsi->back;
  1735. int base = vsi->sw_base_vector;
  1736. if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
  1737. int i;
  1738. if (!vsi->q_vectors || !vsi->irqs_ready)
  1739. return;
  1740. ice_vsi_release_msix(vsi);
  1741. vsi->irqs_ready = false;
  1742. for (i = 0; i < vsi->num_q_vectors; i++) {
  1743. u16 vector = i + base;
  1744. int irq_num;
  1745. irq_num = pf->msix_entries[vector].vector;
  1746. /* free only the irqs that were actually requested */
  1747. if (!vsi->q_vectors[i] ||
  1748. !(vsi->q_vectors[i]->num_ring_tx ||
  1749. vsi->q_vectors[i]->num_ring_rx))
  1750. continue;
  1751. /* clear the affinity notifier in the IRQ descriptor */
  1752. irq_set_affinity_notifier(irq_num, NULL);
  1753. /* clear the affinity_mask in the IRQ descriptor */
  1754. irq_set_affinity_hint(irq_num, NULL);
  1755. synchronize_irq(irq_num);
  1756. devm_free_irq(&pf->pdev->dev, irq_num,
  1757. vsi->q_vectors[i]);
  1758. }
  1759. }
  1760. }
  1761. /**
  1762. * ice_vsi_free_tx_rings - Free Tx resources for VSI queues
  1763. * @vsi: the VSI having resources freed
  1764. */
  1765. void ice_vsi_free_tx_rings(struct ice_vsi *vsi)
  1766. {
  1767. int i;
  1768. if (!vsi->tx_rings)
  1769. return;
  1770. ice_for_each_txq(vsi, i)
  1771. if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
  1772. ice_free_tx_ring(vsi->tx_rings[i]);
  1773. }
  1774. /**
  1775. * ice_vsi_free_rx_rings - Free Rx resources for VSI queues
  1776. * @vsi: the VSI having resources freed
  1777. */
  1778. void ice_vsi_free_rx_rings(struct ice_vsi *vsi)
  1779. {
  1780. int i;
  1781. if (!vsi->rx_rings)
  1782. return;
  1783. ice_for_each_rxq(vsi, i)
  1784. if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
  1785. ice_free_rx_ring(vsi->rx_rings[i]);
  1786. }
  1787. /**
  1788. * ice_vsi_close - Shut down a VSI
  1789. * @vsi: the VSI being shut down
  1790. */
  1791. void ice_vsi_close(struct ice_vsi *vsi)
  1792. {
  1793. if (!test_and_set_bit(__ICE_DOWN, vsi->state))
  1794. ice_down(vsi);
  1795. ice_vsi_free_irq(vsi);
  1796. ice_vsi_free_tx_rings(vsi);
  1797. ice_vsi_free_rx_rings(vsi);
  1798. }
  1799. /**
  1800. * ice_free_res - free a block of resources
  1801. * @res: pointer to the resource
  1802. * @index: starting index previously returned by ice_get_res
  1803. * @id: identifier to track owner
  1804. *
  1805. * Returns number of resources freed
  1806. */
  1807. int ice_free_res(struct ice_res_tracker *res, u16 index, u16 id)
  1808. {
  1809. int count = 0;
  1810. int i;
  1811. if (!res || index >= res->num_entries)
  1812. return -EINVAL;
  1813. id |= ICE_RES_VALID_BIT;
  1814. for (i = index; i < res->num_entries && res->list[i] == id; i++) {
  1815. res->list[i] = 0;
  1816. count++;
  1817. }
  1818. return count;
  1819. }
  1820. /**
  1821. * ice_search_res - Search the tracker for a block of resources
  1822. * @res: pointer to the resource
  1823. * @needed: size of the block needed
  1824. * @id: identifier to track owner
  1825. *
  1826. * Returns the base item index of the block, or -ENOMEM for error
  1827. */
  1828. static int ice_search_res(struct ice_res_tracker *res, u16 needed, u16 id)
  1829. {
  1830. int start = res->search_hint;
  1831. int end = start;
  1832. if ((start + needed) > res->num_entries)
  1833. return -ENOMEM;
  1834. id |= ICE_RES_VALID_BIT;
  1835. do {
  1836. /* skip already allocated entries */
  1837. if (res->list[end++] & ICE_RES_VALID_BIT) {
  1838. start = end;
  1839. if ((start + needed) > res->num_entries)
  1840. break;
  1841. }
  1842. if (end == (start + needed)) {
  1843. int i = start;
  1844. /* there was enough, so assign it to the requestor */
  1845. while (i != end)
  1846. res->list[i++] = id;
  1847. if (end == res->num_entries)
  1848. end = 0;
  1849. res->search_hint = end;
  1850. return start;
  1851. }
  1852. } while (1);
  1853. return -ENOMEM;
  1854. }
  1855. /**
  1856. * ice_get_res - get a block of resources
  1857. * @pf: board private structure
  1858. * @res: pointer to the resource
  1859. * @needed: size of the block needed
  1860. * @id: identifier to track owner
  1861. *
  1862. * Returns the base item index of the block, or -ENOMEM for error
  1863. * The search_hint trick and lack of advanced fit-finding only works
  1864. * because we're highly likely to have all the same sized requests.
  1865. * Linear search time and any fragmentation should be minimal.
  1866. */
  1867. int
  1868. ice_get_res(struct ice_pf *pf, struct ice_res_tracker *res, u16 needed, u16 id)
  1869. {
  1870. int ret;
  1871. if (!res || !pf)
  1872. return -EINVAL;
  1873. if (!needed || needed > res->num_entries || id >= ICE_RES_VALID_BIT) {
  1874. dev_err(&pf->pdev->dev,
  1875. "param err: needed=%d, num_entries = %d id=0x%04x\n",
  1876. needed, res->num_entries, id);
  1877. return -EINVAL;
  1878. }
  1879. /* search based on search_hint */
  1880. ret = ice_search_res(res, needed, id);
  1881. if (ret < 0) {
  1882. /* previous search failed. Reset search hint and try again */
  1883. res->search_hint = 0;
  1884. ret = ice_search_res(res, needed, id);
  1885. }
  1886. return ret;
  1887. }
  1888. /**
  1889. * ice_vsi_dis_irq - Mask off queue interrupt generation on the VSI
  1890. * @vsi: the VSI being un-configured
  1891. */
  1892. void ice_vsi_dis_irq(struct ice_vsi *vsi)
  1893. {
  1894. int base = vsi->sw_base_vector;
  1895. struct ice_pf *pf = vsi->back;
  1896. struct ice_hw *hw = &pf->hw;
  1897. u32 val;
  1898. int i;
  1899. /* disable interrupt causation from each queue */
  1900. if (vsi->tx_rings) {
  1901. ice_for_each_txq(vsi, i) {
  1902. if (vsi->tx_rings[i]) {
  1903. u16 reg;
  1904. reg = vsi->tx_rings[i]->reg_idx;
  1905. val = rd32(hw, QINT_TQCTL(reg));
  1906. val &= ~QINT_TQCTL_CAUSE_ENA_M;
  1907. wr32(hw, QINT_TQCTL(reg), val);
  1908. }
  1909. }
  1910. }
  1911. if (vsi->rx_rings) {
  1912. ice_for_each_rxq(vsi, i) {
  1913. if (vsi->rx_rings[i]) {
  1914. u16 reg;
  1915. reg = vsi->rx_rings[i]->reg_idx;
  1916. val = rd32(hw, QINT_RQCTL(reg));
  1917. val &= ~QINT_RQCTL_CAUSE_ENA_M;
  1918. wr32(hw, QINT_RQCTL(reg), val);
  1919. }
  1920. }
  1921. }
  1922. /* disable each interrupt */
  1923. if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
  1924. for (i = vsi->hw_base_vector;
  1925. i < (vsi->num_q_vectors + vsi->hw_base_vector); i++)
  1926. wr32(hw, GLINT_DYN_CTL(i), 0);
  1927. ice_flush(hw);
  1928. for (i = 0; i < vsi->num_q_vectors; i++)
  1929. synchronize_irq(pf->msix_entries[i + base].vector);
  1930. }
  1931. }
  1932. /**
  1933. * ice_vsi_release - Delete a VSI and free its resources
  1934. * @vsi: the VSI being removed
  1935. *
  1936. * Returns 0 on success or < 0 on error
  1937. */
  1938. int ice_vsi_release(struct ice_vsi *vsi)
  1939. {
  1940. struct ice_pf *pf;
  1941. if (!vsi->back)
  1942. return -ENODEV;
  1943. pf = vsi->back;
  1944. /* do not unregister and free netdevs while driver is in the reset
  1945. * recovery pending state. Since reset/rebuild happens through PF
  1946. * service task workqueue, its not a good idea to unregister netdev
  1947. * that is associated to the PF that is running the work queue items
  1948. * currently. This is done to avoid check_flush_dependency() warning
  1949. * on this wq
  1950. */
  1951. if (vsi->netdev && !ice_is_reset_in_progress(pf->state)) {
  1952. unregister_netdev(vsi->netdev);
  1953. free_netdev(vsi->netdev);
  1954. vsi->netdev = NULL;
  1955. }
  1956. if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
  1957. ice_rss_clean(vsi);
  1958. /* Disable VSI and free resources */
  1959. ice_vsi_dis_irq(vsi);
  1960. ice_vsi_close(vsi);
  1961. /* reclaim interrupt vectors back to PF */
  1962. ice_free_res(vsi->back->sw_irq_tracker, vsi->sw_base_vector, vsi->idx);
  1963. pf->num_avail_sw_msix += vsi->num_q_vectors;
  1964. ice_free_res(vsi->back->hw_irq_tracker, vsi->hw_base_vector, vsi->idx);
  1965. pf->num_avail_hw_msix += vsi->num_q_vectors;
  1966. ice_remove_vsi_fltr(&pf->hw, vsi->idx);
  1967. ice_vsi_delete(vsi);
  1968. ice_vsi_free_q_vectors(vsi);
  1969. ice_vsi_clear_rings(vsi);
  1970. ice_vsi_put_qs(vsi);
  1971. pf->q_left_tx += vsi->alloc_txq;
  1972. pf->q_left_rx += vsi->alloc_rxq;
  1973. /* retain SW VSI data structure since it is needed to unregister and
  1974. * free VSI netdev when PF is not in reset recovery pending state,\
  1975. * for ex: during rmmod.
  1976. */
  1977. if (!ice_is_reset_in_progress(pf->state))
  1978. ice_vsi_clear(vsi);
  1979. return 0;
  1980. }
  1981. /**
  1982. * ice_vsi_rebuild - Rebuild VSI after reset
  1983. * @vsi: VSI to be rebuild
  1984. *
  1985. * Returns 0 on success and negative value on failure
  1986. */
  1987. int ice_vsi_rebuild(struct ice_vsi *vsi)
  1988. {
  1989. u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
  1990. int ret, i;
  1991. if (!vsi)
  1992. return -EINVAL;
  1993. ice_vsi_free_q_vectors(vsi);
  1994. ice_free_res(vsi->back->sw_irq_tracker, vsi->sw_base_vector, vsi->idx);
  1995. ice_free_res(vsi->back->hw_irq_tracker, vsi->hw_base_vector, vsi->idx);
  1996. vsi->sw_base_vector = 0;
  1997. vsi->hw_base_vector = 0;
  1998. ice_vsi_clear_rings(vsi);
  1999. ice_vsi_free_arrays(vsi, false);
  2000. ice_vsi_set_num_qs(vsi);
  2001. /* Initialize VSI struct elements and create VSI in FW */
  2002. ret = ice_vsi_init(vsi);
  2003. if (ret < 0)
  2004. goto err_vsi;
  2005. ret = ice_vsi_alloc_arrays(vsi, false);
  2006. if (ret < 0)
  2007. goto err_vsi;
  2008. switch (vsi->type) {
  2009. case ICE_VSI_PF:
  2010. ret = ice_vsi_alloc_q_vectors(vsi);
  2011. if (ret)
  2012. goto err_rings;
  2013. ret = ice_vsi_setup_vector_base(vsi);
  2014. if (ret)
  2015. goto err_vectors;
  2016. ret = ice_vsi_alloc_rings(vsi);
  2017. if (ret)
  2018. goto err_vectors;
  2019. ice_vsi_map_rings_to_vectors(vsi);
  2020. break;
  2021. default:
  2022. break;
  2023. }
  2024. ice_vsi_set_tc_cfg(vsi);
  2025. /* configure VSI nodes based on number of queues and TC's */
  2026. for (i = 0; i < vsi->tc_cfg.numtc; i++)
  2027. max_txqs[i] = vsi->num_txq;
  2028. ret = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
  2029. max_txqs);
  2030. if (ret) {
  2031. dev_info(&vsi->back->pdev->dev,
  2032. "Failed VSI lan queue config\n");
  2033. goto err_vectors;
  2034. }
  2035. return 0;
  2036. err_vectors:
  2037. ice_vsi_free_q_vectors(vsi);
  2038. err_rings:
  2039. if (vsi->netdev) {
  2040. vsi->current_netdev_flags = 0;
  2041. unregister_netdev(vsi->netdev);
  2042. free_netdev(vsi->netdev);
  2043. vsi->netdev = NULL;
  2044. }
  2045. err_vsi:
  2046. ice_vsi_clear(vsi);
  2047. set_bit(__ICE_RESET_FAILED, vsi->back->state);
  2048. return ret;
  2049. }
  2050. /**
  2051. * ice_is_reset_in_progress - check for a reset in progress
  2052. * @state: pf state field
  2053. */
  2054. bool ice_is_reset_in_progress(unsigned long *state)
  2055. {
  2056. return test_bit(__ICE_RESET_OICR_RECV, state) ||
  2057. test_bit(__ICE_PFR_REQ, state) ||
  2058. test_bit(__ICE_CORER_REQ, state) ||
  2059. test_bit(__ICE_GLOBR_REQ, state);
  2060. }