ice_lib.c 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461
  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. ring->itr_setting = ICE_DFLT_TX_ITR;
  971. vsi->tx_rings[i] = ring;
  972. }
  973. /* Allocate rx_rings */
  974. for (i = 0; i < vsi->alloc_rxq; i++) {
  975. struct ice_ring *ring;
  976. /* allocate with kzalloc(), free with kfree_rcu() */
  977. ring = kzalloc(sizeof(*ring), GFP_KERNEL);
  978. if (!ring)
  979. goto err_out;
  980. ring->q_index = i;
  981. ring->reg_idx = vsi->rxq_map[i];
  982. ring->ring_active = false;
  983. ring->vsi = vsi;
  984. ring->netdev = vsi->netdev;
  985. ring->dev = &pf->pdev->dev;
  986. ring->count = vsi->num_desc;
  987. ring->itr_setting = ICE_DFLT_RX_ITR;
  988. vsi->rx_rings[i] = ring;
  989. }
  990. return 0;
  991. err_out:
  992. ice_vsi_clear_rings(vsi);
  993. return -ENOMEM;
  994. }
  995. /**
  996. * ice_vsi_map_rings_to_vectors - Map VSI rings to interrupt vectors
  997. * @vsi: the VSI being configured
  998. *
  999. * This function maps descriptor rings to the queue-specific vectors allotted
  1000. * through the MSI-X enabling code. On a constrained vector budget, we map Tx
  1001. * and Rx rings to the vector as "efficiently" as possible.
  1002. */
  1003. static void ice_vsi_map_rings_to_vectors(struct ice_vsi *vsi)
  1004. {
  1005. int q_vectors = vsi->num_q_vectors;
  1006. int tx_rings_rem, rx_rings_rem;
  1007. int v_id;
  1008. /* initially assigning remaining rings count to VSIs num queue value */
  1009. tx_rings_rem = vsi->num_txq;
  1010. rx_rings_rem = vsi->num_rxq;
  1011. for (v_id = 0; v_id < q_vectors; v_id++) {
  1012. struct ice_q_vector *q_vector = vsi->q_vectors[v_id];
  1013. int tx_rings_per_v, rx_rings_per_v, q_id, q_base;
  1014. /* Tx rings mapping to vector */
  1015. tx_rings_per_v = DIV_ROUND_UP(tx_rings_rem, q_vectors - v_id);
  1016. q_vector->num_ring_tx = tx_rings_per_v;
  1017. q_vector->tx.ring = NULL;
  1018. q_base = vsi->num_txq - tx_rings_rem;
  1019. for (q_id = q_base; q_id < (q_base + tx_rings_per_v); q_id++) {
  1020. struct ice_ring *tx_ring = vsi->tx_rings[q_id];
  1021. tx_ring->q_vector = q_vector;
  1022. tx_ring->next = q_vector->tx.ring;
  1023. q_vector->tx.ring = tx_ring;
  1024. }
  1025. tx_rings_rem -= tx_rings_per_v;
  1026. /* Rx rings mapping to vector */
  1027. rx_rings_per_v = DIV_ROUND_UP(rx_rings_rem, q_vectors - v_id);
  1028. q_vector->num_ring_rx = rx_rings_per_v;
  1029. q_vector->rx.ring = NULL;
  1030. q_base = vsi->num_rxq - rx_rings_rem;
  1031. for (q_id = q_base; q_id < (q_base + rx_rings_per_v); q_id++) {
  1032. struct ice_ring *rx_ring = vsi->rx_rings[q_id];
  1033. rx_ring->q_vector = q_vector;
  1034. rx_ring->next = q_vector->rx.ring;
  1035. q_vector->rx.ring = rx_ring;
  1036. }
  1037. rx_rings_rem -= rx_rings_per_v;
  1038. }
  1039. }
  1040. /**
  1041. * ice_vsi_manage_rss_lut - disable/enable RSS
  1042. * @vsi: the VSI being changed
  1043. * @ena: boolean value indicating if this is an enable or disable request
  1044. *
  1045. * In the event of disable request for RSS, this function will zero out RSS
  1046. * LUT, while in the event of enable request for RSS, it will reconfigure RSS
  1047. * LUT.
  1048. */
  1049. int ice_vsi_manage_rss_lut(struct ice_vsi *vsi, bool ena)
  1050. {
  1051. int err = 0;
  1052. u8 *lut;
  1053. lut = devm_kzalloc(&vsi->back->pdev->dev, vsi->rss_table_size,
  1054. GFP_KERNEL);
  1055. if (!lut)
  1056. return -ENOMEM;
  1057. if (ena) {
  1058. if (vsi->rss_lut_user)
  1059. memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
  1060. else
  1061. ice_fill_rss_lut(lut, vsi->rss_table_size,
  1062. vsi->rss_size);
  1063. }
  1064. err = ice_set_rss(vsi, NULL, lut, vsi->rss_table_size);
  1065. devm_kfree(&vsi->back->pdev->dev, lut);
  1066. return err;
  1067. }
  1068. /**
  1069. * ice_vsi_cfg_rss_lut_key - Configure RSS params for a VSI
  1070. * @vsi: VSI to be configured
  1071. */
  1072. static int ice_vsi_cfg_rss_lut_key(struct ice_vsi *vsi)
  1073. {
  1074. u8 seed[ICE_AQC_GET_SET_RSS_KEY_DATA_RSS_KEY_SIZE];
  1075. struct ice_aqc_get_set_rss_keys *key;
  1076. struct ice_pf *pf = vsi->back;
  1077. enum ice_status status;
  1078. int err = 0;
  1079. u8 *lut;
  1080. vsi->rss_size = min_t(int, vsi->rss_size, vsi->num_rxq);
  1081. lut = devm_kzalloc(&pf->pdev->dev, vsi->rss_table_size, GFP_KERNEL);
  1082. if (!lut)
  1083. return -ENOMEM;
  1084. if (vsi->rss_lut_user)
  1085. memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
  1086. else
  1087. ice_fill_rss_lut(lut, vsi->rss_table_size, vsi->rss_size);
  1088. status = ice_aq_set_rss_lut(&pf->hw, vsi->idx, vsi->rss_lut_type, lut,
  1089. vsi->rss_table_size);
  1090. if (status) {
  1091. dev_err(&vsi->back->pdev->dev,
  1092. "set_rss_lut failed, error %d\n", status);
  1093. err = -EIO;
  1094. goto ice_vsi_cfg_rss_exit;
  1095. }
  1096. key = devm_kzalloc(&vsi->back->pdev->dev, sizeof(*key), GFP_KERNEL);
  1097. if (!key) {
  1098. err = -ENOMEM;
  1099. goto ice_vsi_cfg_rss_exit;
  1100. }
  1101. if (vsi->rss_hkey_user)
  1102. memcpy(seed, vsi->rss_hkey_user,
  1103. ICE_AQC_GET_SET_RSS_KEY_DATA_RSS_KEY_SIZE);
  1104. else
  1105. netdev_rss_key_fill((void *)seed,
  1106. ICE_AQC_GET_SET_RSS_KEY_DATA_RSS_KEY_SIZE);
  1107. memcpy(&key->standard_rss_key, seed,
  1108. ICE_AQC_GET_SET_RSS_KEY_DATA_RSS_KEY_SIZE);
  1109. status = ice_aq_set_rss_key(&pf->hw, vsi->idx, key);
  1110. if (status) {
  1111. dev_err(&vsi->back->pdev->dev, "set_rss_key failed, error %d\n",
  1112. status);
  1113. err = -EIO;
  1114. }
  1115. devm_kfree(&pf->pdev->dev, key);
  1116. ice_vsi_cfg_rss_exit:
  1117. devm_kfree(&pf->pdev->dev, lut);
  1118. return err;
  1119. }
  1120. /**
  1121. * ice_add_mac_to_list - Add a mac address filter entry to the list
  1122. * @vsi: the VSI to be forwarded to
  1123. * @add_list: pointer to the list which contains MAC filter entries
  1124. * @macaddr: the MAC address to be added.
  1125. *
  1126. * Adds mac address filter entry to the temp list
  1127. *
  1128. * Returns 0 on success or ENOMEM on failure.
  1129. */
  1130. int ice_add_mac_to_list(struct ice_vsi *vsi, struct list_head *add_list,
  1131. const u8 *macaddr)
  1132. {
  1133. struct ice_fltr_list_entry *tmp;
  1134. struct ice_pf *pf = vsi->back;
  1135. tmp = devm_kzalloc(&pf->pdev->dev, sizeof(*tmp), GFP_ATOMIC);
  1136. if (!tmp)
  1137. return -ENOMEM;
  1138. tmp->fltr_info.flag = ICE_FLTR_TX;
  1139. tmp->fltr_info.src_id = ICE_SRC_ID_VSI;
  1140. tmp->fltr_info.lkup_type = ICE_SW_LKUP_MAC;
  1141. tmp->fltr_info.fltr_act = ICE_FWD_TO_VSI;
  1142. tmp->fltr_info.vsi_handle = vsi->idx;
  1143. ether_addr_copy(tmp->fltr_info.l_data.mac.mac_addr, macaddr);
  1144. INIT_LIST_HEAD(&tmp->list_entry);
  1145. list_add(&tmp->list_entry, add_list);
  1146. return 0;
  1147. }
  1148. /**
  1149. * ice_update_eth_stats - Update VSI-specific ethernet statistics counters
  1150. * @vsi: the VSI to be updated
  1151. */
  1152. void ice_update_eth_stats(struct ice_vsi *vsi)
  1153. {
  1154. struct ice_eth_stats *prev_es, *cur_es;
  1155. struct ice_hw *hw = &vsi->back->hw;
  1156. u16 vsi_num = vsi->vsi_num; /* HW absolute index of a VSI */
  1157. prev_es = &vsi->eth_stats_prev;
  1158. cur_es = &vsi->eth_stats;
  1159. ice_stat_update40(hw, GLV_GORCH(vsi_num), GLV_GORCL(vsi_num),
  1160. vsi->stat_offsets_loaded, &prev_es->rx_bytes,
  1161. &cur_es->rx_bytes);
  1162. ice_stat_update40(hw, GLV_UPRCH(vsi_num), GLV_UPRCL(vsi_num),
  1163. vsi->stat_offsets_loaded, &prev_es->rx_unicast,
  1164. &cur_es->rx_unicast);
  1165. ice_stat_update40(hw, GLV_MPRCH(vsi_num), GLV_MPRCL(vsi_num),
  1166. vsi->stat_offsets_loaded, &prev_es->rx_multicast,
  1167. &cur_es->rx_multicast);
  1168. ice_stat_update40(hw, GLV_BPRCH(vsi_num), GLV_BPRCL(vsi_num),
  1169. vsi->stat_offsets_loaded, &prev_es->rx_broadcast,
  1170. &cur_es->rx_broadcast);
  1171. ice_stat_update32(hw, GLV_RDPC(vsi_num), vsi->stat_offsets_loaded,
  1172. &prev_es->rx_discards, &cur_es->rx_discards);
  1173. ice_stat_update40(hw, GLV_GOTCH(vsi_num), GLV_GOTCL(vsi_num),
  1174. vsi->stat_offsets_loaded, &prev_es->tx_bytes,
  1175. &cur_es->tx_bytes);
  1176. ice_stat_update40(hw, GLV_UPTCH(vsi_num), GLV_UPTCL(vsi_num),
  1177. vsi->stat_offsets_loaded, &prev_es->tx_unicast,
  1178. &cur_es->tx_unicast);
  1179. ice_stat_update40(hw, GLV_MPTCH(vsi_num), GLV_MPTCL(vsi_num),
  1180. vsi->stat_offsets_loaded, &prev_es->tx_multicast,
  1181. &cur_es->tx_multicast);
  1182. ice_stat_update40(hw, GLV_BPTCH(vsi_num), GLV_BPTCL(vsi_num),
  1183. vsi->stat_offsets_loaded, &prev_es->tx_broadcast,
  1184. &cur_es->tx_broadcast);
  1185. ice_stat_update32(hw, GLV_TEPC(vsi_num), vsi->stat_offsets_loaded,
  1186. &prev_es->tx_errors, &cur_es->tx_errors);
  1187. vsi->stat_offsets_loaded = true;
  1188. }
  1189. /**
  1190. * ice_free_fltr_list - free filter lists helper
  1191. * @dev: pointer to the device struct
  1192. * @h: pointer to the list head to be freed
  1193. *
  1194. * Helper function to free filter lists previously created using
  1195. * ice_add_mac_to_list
  1196. */
  1197. void ice_free_fltr_list(struct device *dev, struct list_head *h)
  1198. {
  1199. struct ice_fltr_list_entry *e, *tmp;
  1200. list_for_each_entry_safe(e, tmp, h, list_entry) {
  1201. list_del(&e->list_entry);
  1202. devm_kfree(dev, e);
  1203. }
  1204. }
  1205. /**
  1206. * ice_vsi_add_vlan - Add VSI membership for given VLAN
  1207. * @vsi: the VSI being configured
  1208. * @vid: VLAN id to be added
  1209. */
  1210. int ice_vsi_add_vlan(struct ice_vsi *vsi, u16 vid)
  1211. {
  1212. struct ice_fltr_list_entry *tmp;
  1213. struct ice_pf *pf = vsi->back;
  1214. LIST_HEAD(tmp_add_list);
  1215. enum ice_status status;
  1216. int err = 0;
  1217. tmp = devm_kzalloc(&pf->pdev->dev, sizeof(*tmp), GFP_KERNEL);
  1218. if (!tmp)
  1219. return -ENOMEM;
  1220. tmp->fltr_info.lkup_type = ICE_SW_LKUP_VLAN;
  1221. tmp->fltr_info.fltr_act = ICE_FWD_TO_VSI;
  1222. tmp->fltr_info.flag = ICE_FLTR_TX;
  1223. tmp->fltr_info.src_id = ICE_SRC_ID_VSI;
  1224. tmp->fltr_info.vsi_handle = vsi->idx;
  1225. tmp->fltr_info.l_data.vlan.vlan_id = vid;
  1226. INIT_LIST_HEAD(&tmp->list_entry);
  1227. list_add(&tmp->list_entry, &tmp_add_list);
  1228. status = ice_add_vlan(&pf->hw, &tmp_add_list);
  1229. if (status) {
  1230. err = -ENODEV;
  1231. dev_err(&pf->pdev->dev, "Failure Adding VLAN %d on VSI %i\n",
  1232. vid, vsi->vsi_num);
  1233. }
  1234. ice_free_fltr_list(&pf->pdev->dev, &tmp_add_list);
  1235. return err;
  1236. }
  1237. /**
  1238. * ice_vsi_kill_vlan - Remove VSI membership for a given VLAN
  1239. * @vsi: the VSI being configured
  1240. * @vid: VLAN id to be removed
  1241. *
  1242. * Returns 0 on success and negative on failure
  1243. */
  1244. int ice_vsi_kill_vlan(struct ice_vsi *vsi, u16 vid)
  1245. {
  1246. struct ice_fltr_list_entry *list;
  1247. struct ice_pf *pf = vsi->back;
  1248. LIST_HEAD(tmp_add_list);
  1249. int status = 0;
  1250. list = devm_kzalloc(&pf->pdev->dev, sizeof(*list), GFP_KERNEL);
  1251. if (!list)
  1252. return -ENOMEM;
  1253. list->fltr_info.lkup_type = ICE_SW_LKUP_VLAN;
  1254. list->fltr_info.vsi_handle = vsi->idx;
  1255. list->fltr_info.fltr_act = ICE_FWD_TO_VSI;
  1256. list->fltr_info.l_data.vlan.vlan_id = vid;
  1257. list->fltr_info.flag = ICE_FLTR_TX;
  1258. list->fltr_info.src_id = ICE_SRC_ID_VSI;
  1259. INIT_LIST_HEAD(&list->list_entry);
  1260. list_add(&list->list_entry, &tmp_add_list);
  1261. if (ice_remove_vlan(&pf->hw, &tmp_add_list)) {
  1262. dev_err(&pf->pdev->dev, "Error removing VLAN %d on vsi %i\n",
  1263. vid, vsi->vsi_num);
  1264. status = -EIO;
  1265. }
  1266. ice_free_fltr_list(&pf->pdev->dev, &tmp_add_list);
  1267. return status;
  1268. }
  1269. /**
  1270. * ice_vsi_cfg_rxqs - Configure the VSI for Rx
  1271. * @vsi: the VSI being configured
  1272. *
  1273. * Return 0 on success and a negative value on error
  1274. * Configure the Rx VSI for operation.
  1275. */
  1276. int ice_vsi_cfg_rxqs(struct ice_vsi *vsi)
  1277. {
  1278. int err = 0;
  1279. u16 i;
  1280. if (vsi->netdev && vsi->netdev->mtu > ETH_DATA_LEN)
  1281. vsi->max_frame = vsi->netdev->mtu +
  1282. ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
  1283. else
  1284. vsi->max_frame = ICE_RXBUF_2048;
  1285. vsi->rx_buf_len = ICE_RXBUF_2048;
  1286. /* set up individual rings */
  1287. for (i = 0; i < vsi->num_rxq && !err; i++)
  1288. err = ice_setup_rx_ctx(vsi->rx_rings[i]);
  1289. if (err) {
  1290. dev_err(&vsi->back->pdev->dev, "ice_setup_rx_ctx failed\n");
  1291. return -EIO;
  1292. }
  1293. return err;
  1294. }
  1295. /**
  1296. * ice_vsi_cfg_txqs - Configure the VSI for Tx
  1297. * @vsi: the VSI being configured
  1298. *
  1299. * Return 0 on success and a negative value on error
  1300. * Configure the Tx VSI for operation.
  1301. */
  1302. int ice_vsi_cfg_txqs(struct ice_vsi *vsi)
  1303. {
  1304. struct ice_aqc_add_tx_qgrp *qg_buf;
  1305. struct ice_aqc_add_txqs_perq *txq;
  1306. struct ice_pf *pf = vsi->back;
  1307. enum ice_status status;
  1308. u16 buf_len, i, pf_q;
  1309. int err = 0, tc = 0;
  1310. u8 num_q_grps;
  1311. buf_len = sizeof(struct ice_aqc_add_tx_qgrp);
  1312. qg_buf = devm_kzalloc(&pf->pdev->dev, buf_len, GFP_KERNEL);
  1313. if (!qg_buf)
  1314. return -ENOMEM;
  1315. if (vsi->num_txq > ICE_MAX_TXQ_PER_TXQG) {
  1316. err = -EINVAL;
  1317. goto err_cfg_txqs;
  1318. }
  1319. qg_buf->num_txqs = 1;
  1320. num_q_grps = 1;
  1321. /* set up and configure the Tx queues */
  1322. ice_for_each_txq(vsi, i) {
  1323. struct ice_tlan_ctx tlan_ctx = { 0 };
  1324. pf_q = vsi->txq_map[i];
  1325. ice_setup_tx_ctx(vsi->tx_rings[i], &tlan_ctx, pf_q);
  1326. /* copy context contents into the qg_buf */
  1327. qg_buf->txqs[0].txq_id = cpu_to_le16(pf_q);
  1328. ice_set_ctx((u8 *)&tlan_ctx, qg_buf->txqs[0].txq_ctx,
  1329. ice_tlan_ctx_info);
  1330. /* init queue specific tail reg. It is referred as transmit
  1331. * comm scheduler queue doorbell.
  1332. */
  1333. vsi->tx_rings[i]->tail = pf->hw.hw_addr + QTX_COMM_DBELL(pf_q);
  1334. status = ice_ena_vsi_txq(vsi->port_info, vsi->idx, tc,
  1335. num_q_grps, qg_buf, buf_len, NULL);
  1336. if (status) {
  1337. dev_err(&vsi->back->pdev->dev,
  1338. "Failed to set LAN Tx queue context, error: %d\n",
  1339. status);
  1340. err = -ENODEV;
  1341. goto err_cfg_txqs;
  1342. }
  1343. /* Add Tx Queue TEID into the VSI Tx ring from the response
  1344. * This will complete configuring and enabling the queue.
  1345. */
  1346. txq = &qg_buf->txqs[0];
  1347. if (pf_q == le16_to_cpu(txq->txq_id))
  1348. vsi->tx_rings[i]->txq_teid =
  1349. le32_to_cpu(txq->q_teid);
  1350. }
  1351. err_cfg_txqs:
  1352. devm_kfree(&pf->pdev->dev, qg_buf);
  1353. return err;
  1354. }
  1355. /**
  1356. * ice_intrl_usec_to_reg - convert interrupt rate limit to register value
  1357. * @intrl: interrupt rate limit in usecs
  1358. * @gran: interrupt rate limit granularity in usecs
  1359. *
  1360. * This function converts a decimal interrupt rate limit in usecs to the format
  1361. * expected by firmware.
  1362. */
  1363. static u32 ice_intrl_usec_to_reg(u8 intrl, u8 gran)
  1364. {
  1365. u32 val = intrl / gran;
  1366. if (val)
  1367. return val | GLINT_RATE_INTRL_ENA_M;
  1368. return 0;
  1369. }
  1370. /**
  1371. * ice_vsi_cfg_msix - MSIX mode Interrupt Config in the HW
  1372. * @vsi: the VSI being configured
  1373. */
  1374. void ice_vsi_cfg_msix(struct ice_vsi *vsi)
  1375. {
  1376. struct ice_pf *pf = vsi->back;
  1377. u16 vector = vsi->hw_base_vector;
  1378. struct ice_hw *hw = &pf->hw;
  1379. u32 txq = 0, rxq = 0;
  1380. int i, q, itr;
  1381. u8 itr_gran;
  1382. for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
  1383. struct ice_q_vector *q_vector = vsi->q_vectors[i];
  1384. itr_gran = hw->itr_gran;
  1385. q_vector->intrl = ICE_DFLT_INTRL;
  1386. if (q_vector->num_ring_rx) {
  1387. q_vector->rx.itr =
  1388. ITR_TO_REG(vsi->rx_rings[rxq]->itr_setting,
  1389. itr_gran);
  1390. q_vector->rx.latency_range = ICE_LOW_LATENCY;
  1391. }
  1392. if (q_vector->num_ring_tx) {
  1393. q_vector->tx.itr =
  1394. ITR_TO_REG(vsi->tx_rings[txq]->itr_setting,
  1395. itr_gran);
  1396. q_vector->tx.latency_range = ICE_LOW_LATENCY;
  1397. }
  1398. wr32(hw, GLINT_ITR(ICE_RX_ITR, vector), q_vector->rx.itr);
  1399. wr32(hw, GLINT_ITR(ICE_TX_ITR, vector), q_vector->tx.itr);
  1400. wr32(hw, GLINT_RATE(vector),
  1401. ice_intrl_usec_to_reg(q_vector->intrl, hw->intrl_gran));
  1402. /* Both Transmit Queue Interrupt Cause Control register
  1403. * and Receive Queue Interrupt Cause control register
  1404. * expects MSIX_INDX field to be the vector index
  1405. * within the function space and not the absolute
  1406. * vector index across PF or across device.
  1407. * For SR-IOV VF VSIs queue vector index always starts
  1408. * with 1 since first vector index(0) is used for OICR
  1409. * in VF space. Since VMDq and other PF VSIs are within
  1410. * the PF function space, use the vector index that is
  1411. * tracked for this PF.
  1412. */
  1413. for (q = 0; q < q_vector->num_ring_tx; q++) {
  1414. u32 val;
  1415. itr = ICE_ITR_NONE;
  1416. val = QINT_TQCTL_CAUSE_ENA_M |
  1417. (itr << QINT_TQCTL_ITR_INDX_S) |
  1418. (vector << QINT_TQCTL_MSIX_INDX_S);
  1419. wr32(hw, QINT_TQCTL(vsi->txq_map[txq]), val);
  1420. txq++;
  1421. }
  1422. for (q = 0; q < q_vector->num_ring_rx; q++) {
  1423. u32 val;
  1424. itr = ICE_ITR_NONE;
  1425. val = QINT_RQCTL_CAUSE_ENA_M |
  1426. (itr << QINT_RQCTL_ITR_INDX_S) |
  1427. (vector << QINT_RQCTL_MSIX_INDX_S);
  1428. wr32(hw, QINT_RQCTL(vsi->rxq_map[rxq]), val);
  1429. rxq++;
  1430. }
  1431. }
  1432. ice_flush(hw);
  1433. }
  1434. /**
  1435. * ice_vsi_manage_vlan_insertion - Manage VLAN insertion for the VSI for Tx
  1436. * @vsi: the VSI being changed
  1437. */
  1438. int ice_vsi_manage_vlan_insertion(struct ice_vsi *vsi)
  1439. {
  1440. struct device *dev = &vsi->back->pdev->dev;
  1441. struct ice_hw *hw = &vsi->back->hw;
  1442. struct ice_vsi_ctx ctxt = { 0 };
  1443. enum ice_status status;
  1444. /* Here we are configuring the VSI to let the driver add VLAN tags by
  1445. * setting vlan_flags to ICE_AQ_VSI_VLAN_MODE_ALL. The actual VLAN tag
  1446. * insertion happens in the Tx hot path, in ice_tx_map.
  1447. */
  1448. ctxt.info.vlan_flags = ICE_AQ_VSI_VLAN_MODE_ALL;
  1449. ctxt.info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID);
  1450. status = ice_update_vsi(hw, vsi->idx, &ctxt, NULL);
  1451. if (status) {
  1452. dev_err(dev, "update VSI for VLAN insert failed, err %d aq_err %d\n",
  1453. status, hw->adminq.sq_last_status);
  1454. return -EIO;
  1455. }
  1456. vsi->info.vlan_flags = ctxt.info.vlan_flags;
  1457. return 0;
  1458. }
  1459. /**
  1460. * ice_vsi_manage_vlan_stripping - Manage VLAN stripping for the VSI for Rx
  1461. * @vsi: the VSI being changed
  1462. * @ena: boolean value indicating if this is a enable or disable request
  1463. */
  1464. int ice_vsi_manage_vlan_stripping(struct ice_vsi *vsi, bool ena)
  1465. {
  1466. struct device *dev = &vsi->back->pdev->dev;
  1467. struct ice_hw *hw = &vsi->back->hw;
  1468. struct ice_vsi_ctx ctxt = { 0 };
  1469. enum ice_status status;
  1470. /* Here we are configuring what the VSI should do with the VLAN tag in
  1471. * the Rx packet. We can either leave the tag in the packet or put it in
  1472. * the Rx descriptor.
  1473. */
  1474. if (ena) {
  1475. /* Strip VLAN tag from Rx packet and put it in the desc */
  1476. ctxt.info.vlan_flags = ICE_AQ_VSI_VLAN_EMOD_STR_BOTH;
  1477. } else {
  1478. /* Disable stripping. Leave tag in packet */
  1479. ctxt.info.vlan_flags = ICE_AQ_VSI_VLAN_EMOD_NOTHING;
  1480. }
  1481. /* Allow all packets untagged/tagged */
  1482. ctxt.info.vlan_flags |= ICE_AQ_VSI_VLAN_MODE_ALL;
  1483. ctxt.info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID);
  1484. status = ice_update_vsi(hw, vsi->idx, &ctxt, NULL);
  1485. if (status) {
  1486. dev_err(dev, "update VSI for VLAN strip failed, ena = %d err %d aq_err %d\n",
  1487. ena, status, hw->adminq.sq_last_status);
  1488. return -EIO;
  1489. }
  1490. vsi->info.vlan_flags = ctxt.info.vlan_flags;
  1491. return 0;
  1492. }
  1493. /**
  1494. * ice_vsi_start_rx_rings - start VSI's Rx rings
  1495. * @vsi: the VSI whose rings are to be started
  1496. *
  1497. * Returns 0 on success and a negative value on error
  1498. */
  1499. int ice_vsi_start_rx_rings(struct ice_vsi *vsi)
  1500. {
  1501. return ice_vsi_ctrl_rx_rings(vsi, true);
  1502. }
  1503. /**
  1504. * ice_vsi_stop_rx_rings - stop VSI's Rx rings
  1505. * @vsi: the VSI
  1506. *
  1507. * Returns 0 on success and a negative value on error
  1508. */
  1509. int ice_vsi_stop_rx_rings(struct ice_vsi *vsi)
  1510. {
  1511. return ice_vsi_ctrl_rx_rings(vsi, false);
  1512. }
  1513. /**
  1514. * ice_vsi_stop_tx_rings - Disable Tx rings
  1515. * @vsi: the VSI being configured
  1516. */
  1517. int ice_vsi_stop_tx_rings(struct ice_vsi *vsi)
  1518. {
  1519. struct ice_pf *pf = vsi->back;
  1520. struct ice_hw *hw = &pf->hw;
  1521. enum ice_status status;
  1522. u32 *q_teids, val;
  1523. u16 *q_ids, i;
  1524. int err = 0;
  1525. if (vsi->num_txq > ICE_LAN_TXQ_MAX_QDIS)
  1526. return -EINVAL;
  1527. q_teids = devm_kcalloc(&pf->pdev->dev, vsi->num_txq, sizeof(*q_teids),
  1528. GFP_KERNEL);
  1529. if (!q_teids)
  1530. return -ENOMEM;
  1531. q_ids = devm_kcalloc(&pf->pdev->dev, vsi->num_txq, sizeof(*q_ids),
  1532. GFP_KERNEL);
  1533. if (!q_ids) {
  1534. err = -ENOMEM;
  1535. goto err_alloc_q_ids;
  1536. }
  1537. /* set up the Tx queue list to be disabled */
  1538. ice_for_each_txq(vsi, i) {
  1539. u16 v_idx;
  1540. if (!vsi->tx_rings || !vsi->tx_rings[i]) {
  1541. err = -EINVAL;
  1542. goto err_out;
  1543. }
  1544. q_ids[i] = vsi->txq_map[i];
  1545. q_teids[i] = vsi->tx_rings[i]->txq_teid;
  1546. /* clear cause_ena bit for disabled queues */
  1547. val = rd32(hw, QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
  1548. val &= ~QINT_TQCTL_CAUSE_ENA_M;
  1549. wr32(hw, QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
  1550. /* software is expected to wait for 100 ns */
  1551. ndelay(100);
  1552. /* trigger a software interrupt for the vector associated to
  1553. * the queue to schedule NAPI handler
  1554. */
  1555. v_idx = vsi->tx_rings[i]->q_vector->v_idx;
  1556. wr32(hw, GLINT_DYN_CTL(vsi->hw_base_vector + v_idx),
  1557. GLINT_DYN_CTL_SWINT_TRIG_M | GLINT_DYN_CTL_INTENA_MSK_M);
  1558. }
  1559. status = ice_dis_vsi_txq(vsi->port_info, vsi->num_txq, q_ids, q_teids,
  1560. NULL);
  1561. /* if the disable queue command was exercised during an active reset
  1562. * flow, ICE_ERR_RESET_ONGOING is returned. This is not an error as
  1563. * the reset operation disables queues at the hardware level anyway.
  1564. */
  1565. if (status == ICE_ERR_RESET_ONGOING) {
  1566. dev_info(&pf->pdev->dev,
  1567. "Reset in progress. LAN Tx queues already disabled\n");
  1568. } else if (status) {
  1569. dev_err(&pf->pdev->dev,
  1570. "Failed to disable LAN Tx queues, error: %d\n",
  1571. status);
  1572. err = -ENODEV;
  1573. }
  1574. err_out:
  1575. devm_kfree(&pf->pdev->dev, q_ids);
  1576. err_alloc_q_ids:
  1577. devm_kfree(&pf->pdev->dev, q_teids);
  1578. return err;
  1579. }
  1580. /**
  1581. * ice_cfg_vlan_pruning - enable or disable VLAN pruning on the VSI
  1582. * @vsi: VSI to enable or disable VLAN pruning on
  1583. * @ena: set to true to enable VLAN pruning and false to disable it
  1584. *
  1585. * returns 0 if VSI is updated, negative otherwise
  1586. */
  1587. int ice_cfg_vlan_pruning(struct ice_vsi *vsi, bool ena)
  1588. {
  1589. struct ice_vsi_ctx *ctxt;
  1590. struct device *dev;
  1591. int status;
  1592. if (!vsi)
  1593. return -EINVAL;
  1594. dev = &vsi->back->pdev->dev;
  1595. ctxt = devm_kzalloc(dev, sizeof(*ctxt), GFP_KERNEL);
  1596. if (!ctxt)
  1597. return -ENOMEM;
  1598. ctxt->info = vsi->info;
  1599. if (ena) {
  1600. ctxt->info.sec_flags |=
  1601. ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
  1602. ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S;
  1603. ctxt->info.sw_flags2 |= ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
  1604. } else {
  1605. ctxt->info.sec_flags &=
  1606. ~(ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
  1607. ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S);
  1608. ctxt->info.sw_flags2 &= ~ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
  1609. }
  1610. ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID |
  1611. ICE_AQ_VSI_PROP_SW_VALID);
  1612. status = ice_update_vsi(&vsi->back->hw, vsi->idx, ctxt, NULL);
  1613. if (status) {
  1614. netdev_err(vsi->netdev, "%sabling VLAN pruning on VSI handle: %d, VSI HW ID: %d failed, err = %d, aq_err = %d\n",
  1615. ena ? "Ena" : "Dis", vsi->idx, vsi->vsi_num, status,
  1616. vsi->back->hw.adminq.sq_last_status);
  1617. goto err_out;
  1618. }
  1619. vsi->info.sec_flags = ctxt->info.sec_flags;
  1620. vsi->info.sw_flags2 = ctxt->info.sw_flags2;
  1621. devm_kfree(dev, ctxt);
  1622. return 0;
  1623. err_out:
  1624. devm_kfree(dev, ctxt);
  1625. return -EIO;
  1626. }
  1627. /**
  1628. * ice_vsi_setup - Set up a VSI by a given type
  1629. * @pf: board private structure
  1630. * @pi: pointer to the port_info instance
  1631. * @type: VSI type
  1632. * @vf_id: defines VF id to which this VSI connects. This field is meant to be
  1633. * used only for ICE_VSI_VF VSI type. For other VSI types, should
  1634. * fill-in ICE_INVAL_VFID as input.
  1635. *
  1636. * This allocates the sw VSI structure and its queue resources.
  1637. *
  1638. * Returns pointer to the successfully allocated and configured VSI sw struct on
  1639. * success, NULL on failure.
  1640. */
  1641. struct ice_vsi *
  1642. ice_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi,
  1643. enum ice_vsi_type type, u16 __always_unused vf_id)
  1644. {
  1645. u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
  1646. struct device *dev = &pf->pdev->dev;
  1647. struct ice_vsi *vsi;
  1648. int ret, i;
  1649. vsi = ice_vsi_alloc(pf, type);
  1650. if (!vsi) {
  1651. dev_err(dev, "could not allocate VSI\n");
  1652. return NULL;
  1653. }
  1654. vsi->port_info = pi;
  1655. vsi->vsw = pf->first_sw;
  1656. if (ice_vsi_get_qs(vsi)) {
  1657. dev_err(dev, "Failed to allocate queues. vsi->idx = %d\n",
  1658. vsi->idx);
  1659. goto unroll_get_qs;
  1660. }
  1661. /* set RSS capabilities */
  1662. ice_vsi_set_rss_params(vsi);
  1663. /* create the VSI */
  1664. ret = ice_vsi_init(vsi);
  1665. if (ret)
  1666. goto unroll_get_qs;
  1667. switch (vsi->type) {
  1668. case ICE_VSI_PF:
  1669. ret = ice_vsi_alloc_q_vectors(vsi);
  1670. if (ret)
  1671. goto unroll_vsi_init;
  1672. ret = ice_vsi_setup_vector_base(vsi);
  1673. if (ret)
  1674. goto unroll_alloc_q_vector;
  1675. ret = ice_vsi_alloc_rings(vsi);
  1676. if (ret)
  1677. goto unroll_vector_base;
  1678. ice_vsi_map_rings_to_vectors(vsi);
  1679. /* Do not exit if configuring RSS had an issue, at least
  1680. * receive traffic on first queue. Hence no need to capture
  1681. * return value
  1682. */
  1683. if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
  1684. ice_vsi_cfg_rss_lut_key(vsi);
  1685. break;
  1686. default:
  1687. /* if VSI type is not recognized, clean up the resources and
  1688. * exit
  1689. */
  1690. goto unroll_vsi_init;
  1691. }
  1692. ice_vsi_set_tc_cfg(vsi);
  1693. /* configure VSI nodes based on number of queues and TC's */
  1694. for (i = 0; i < vsi->tc_cfg.numtc; i++)
  1695. max_txqs[i] = vsi->num_txq;
  1696. ret = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
  1697. max_txqs);
  1698. if (ret) {
  1699. dev_info(&pf->pdev->dev, "Failed VSI lan queue config\n");
  1700. goto unroll_vector_base;
  1701. }
  1702. return vsi;
  1703. unroll_vector_base:
  1704. /* reclaim SW interrupts back to the common pool */
  1705. ice_free_res(vsi->back->sw_irq_tracker, vsi->sw_base_vector, vsi->idx);
  1706. pf->num_avail_sw_msix += vsi->num_q_vectors;
  1707. /* reclaim HW interrupt back to the common pool */
  1708. ice_free_res(vsi->back->hw_irq_tracker, vsi->hw_base_vector, vsi->idx);
  1709. pf->num_avail_hw_msix += vsi->num_q_vectors;
  1710. unroll_alloc_q_vector:
  1711. ice_vsi_free_q_vectors(vsi);
  1712. unroll_vsi_init:
  1713. ice_vsi_delete(vsi);
  1714. unroll_get_qs:
  1715. ice_vsi_put_qs(vsi);
  1716. pf->q_left_tx += vsi->alloc_txq;
  1717. pf->q_left_rx += vsi->alloc_rxq;
  1718. ice_vsi_clear(vsi);
  1719. return NULL;
  1720. }
  1721. /**
  1722. * ice_vsi_release_msix - Clear the queue to Interrupt mapping in HW
  1723. * @vsi: the VSI being cleaned up
  1724. */
  1725. static void ice_vsi_release_msix(struct ice_vsi *vsi)
  1726. {
  1727. struct ice_pf *pf = vsi->back;
  1728. u16 vector = vsi->hw_base_vector;
  1729. struct ice_hw *hw = &pf->hw;
  1730. u32 txq = 0;
  1731. u32 rxq = 0;
  1732. int i, q;
  1733. for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
  1734. struct ice_q_vector *q_vector = vsi->q_vectors[i];
  1735. wr32(hw, GLINT_ITR(ICE_RX_ITR, vector), 0);
  1736. wr32(hw, GLINT_ITR(ICE_TX_ITR, vector), 0);
  1737. for (q = 0; q < q_vector->num_ring_tx; q++) {
  1738. wr32(hw, QINT_TQCTL(vsi->txq_map[txq]), 0);
  1739. txq++;
  1740. }
  1741. for (q = 0; q < q_vector->num_ring_rx; q++) {
  1742. wr32(hw, QINT_RQCTL(vsi->rxq_map[rxq]), 0);
  1743. rxq++;
  1744. }
  1745. }
  1746. ice_flush(hw);
  1747. }
  1748. /**
  1749. * ice_vsi_free_irq - Free the IRQ association with the OS
  1750. * @vsi: the VSI being configured
  1751. */
  1752. void ice_vsi_free_irq(struct ice_vsi *vsi)
  1753. {
  1754. struct ice_pf *pf = vsi->back;
  1755. int base = vsi->sw_base_vector;
  1756. if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
  1757. int i;
  1758. if (!vsi->q_vectors || !vsi->irqs_ready)
  1759. return;
  1760. ice_vsi_release_msix(vsi);
  1761. vsi->irqs_ready = false;
  1762. for (i = 0; i < vsi->num_q_vectors; i++) {
  1763. u16 vector = i + base;
  1764. int irq_num;
  1765. irq_num = pf->msix_entries[vector].vector;
  1766. /* free only the irqs that were actually requested */
  1767. if (!vsi->q_vectors[i] ||
  1768. !(vsi->q_vectors[i]->num_ring_tx ||
  1769. vsi->q_vectors[i]->num_ring_rx))
  1770. continue;
  1771. /* clear the affinity notifier in the IRQ descriptor */
  1772. irq_set_affinity_notifier(irq_num, NULL);
  1773. /* clear the affinity_mask in the IRQ descriptor */
  1774. irq_set_affinity_hint(irq_num, NULL);
  1775. synchronize_irq(irq_num);
  1776. devm_free_irq(&pf->pdev->dev, irq_num,
  1777. vsi->q_vectors[i]);
  1778. }
  1779. }
  1780. }
  1781. /**
  1782. * ice_vsi_free_tx_rings - Free Tx resources for VSI queues
  1783. * @vsi: the VSI having resources freed
  1784. */
  1785. void ice_vsi_free_tx_rings(struct ice_vsi *vsi)
  1786. {
  1787. int i;
  1788. if (!vsi->tx_rings)
  1789. return;
  1790. ice_for_each_txq(vsi, i)
  1791. if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
  1792. ice_free_tx_ring(vsi->tx_rings[i]);
  1793. }
  1794. /**
  1795. * ice_vsi_free_rx_rings - Free Rx resources for VSI queues
  1796. * @vsi: the VSI having resources freed
  1797. */
  1798. void ice_vsi_free_rx_rings(struct ice_vsi *vsi)
  1799. {
  1800. int i;
  1801. if (!vsi->rx_rings)
  1802. return;
  1803. ice_for_each_rxq(vsi, i)
  1804. if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
  1805. ice_free_rx_ring(vsi->rx_rings[i]);
  1806. }
  1807. /**
  1808. * ice_vsi_close - Shut down a VSI
  1809. * @vsi: the VSI being shut down
  1810. */
  1811. void ice_vsi_close(struct ice_vsi *vsi)
  1812. {
  1813. if (!test_and_set_bit(__ICE_DOWN, vsi->state))
  1814. ice_down(vsi);
  1815. ice_vsi_free_irq(vsi);
  1816. ice_vsi_free_tx_rings(vsi);
  1817. ice_vsi_free_rx_rings(vsi);
  1818. }
  1819. /**
  1820. * ice_free_res - free a block of resources
  1821. * @res: pointer to the resource
  1822. * @index: starting index previously returned by ice_get_res
  1823. * @id: identifier to track owner
  1824. *
  1825. * Returns number of resources freed
  1826. */
  1827. int ice_free_res(struct ice_res_tracker *res, u16 index, u16 id)
  1828. {
  1829. int count = 0;
  1830. int i;
  1831. if (!res || index >= res->num_entries)
  1832. return -EINVAL;
  1833. id |= ICE_RES_VALID_BIT;
  1834. for (i = index; i < res->num_entries && res->list[i] == id; i++) {
  1835. res->list[i] = 0;
  1836. count++;
  1837. }
  1838. return count;
  1839. }
  1840. /**
  1841. * ice_search_res - Search the tracker for a block of resources
  1842. * @res: pointer to the resource
  1843. * @needed: size of the block needed
  1844. * @id: identifier to track owner
  1845. *
  1846. * Returns the base item index of the block, or -ENOMEM for error
  1847. */
  1848. static int ice_search_res(struct ice_res_tracker *res, u16 needed, u16 id)
  1849. {
  1850. int start = res->search_hint;
  1851. int end = start;
  1852. if ((start + needed) > res->num_entries)
  1853. return -ENOMEM;
  1854. id |= ICE_RES_VALID_BIT;
  1855. do {
  1856. /* skip already allocated entries */
  1857. if (res->list[end++] & ICE_RES_VALID_BIT) {
  1858. start = end;
  1859. if ((start + needed) > res->num_entries)
  1860. break;
  1861. }
  1862. if (end == (start + needed)) {
  1863. int i = start;
  1864. /* there was enough, so assign it to the requestor */
  1865. while (i != end)
  1866. res->list[i++] = id;
  1867. if (end == res->num_entries)
  1868. end = 0;
  1869. res->search_hint = end;
  1870. return start;
  1871. }
  1872. } while (1);
  1873. return -ENOMEM;
  1874. }
  1875. /**
  1876. * ice_get_res - get a block of resources
  1877. * @pf: board private structure
  1878. * @res: pointer to the resource
  1879. * @needed: size of the block needed
  1880. * @id: identifier to track owner
  1881. *
  1882. * Returns the base item index of the block, or -ENOMEM for error
  1883. * The search_hint trick and lack of advanced fit-finding only works
  1884. * because we're highly likely to have all the same sized requests.
  1885. * Linear search time and any fragmentation should be minimal.
  1886. */
  1887. int
  1888. ice_get_res(struct ice_pf *pf, struct ice_res_tracker *res, u16 needed, u16 id)
  1889. {
  1890. int ret;
  1891. if (!res || !pf)
  1892. return -EINVAL;
  1893. if (!needed || needed > res->num_entries || id >= ICE_RES_VALID_BIT) {
  1894. dev_err(&pf->pdev->dev,
  1895. "param err: needed=%d, num_entries = %d id=0x%04x\n",
  1896. needed, res->num_entries, id);
  1897. return -EINVAL;
  1898. }
  1899. /* search based on search_hint */
  1900. ret = ice_search_res(res, needed, id);
  1901. if (ret < 0) {
  1902. /* previous search failed. Reset search hint and try again */
  1903. res->search_hint = 0;
  1904. ret = ice_search_res(res, needed, id);
  1905. }
  1906. return ret;
  1907. }
  1908. /**
  1909. * ice_vsi_dis_irq - Mask off queue interrupt generation on the VSI
  1910. * @vsi: the VSI being un-configured
  1911. */
  1912. void ice_vsi_dis_irq(struct ice_vsi *vsi)
  1913. {
  1914. int base = vsi->sw_base_vector;
  1915. struct ice_pf *pf = vsi->back;
  1916. struct ice_hw *hw = &pf->hw;
  1917. u32 val;
  1918. int i;
  1919. /* disable interrupt causation from each queue */
  1920. if (vsi->tx_rings) {
  1921. ice_for_each_txq(vsi, i) {
  1922. if (vsi->tx_rings[i]) {
  1923. u16 reg;
  1924. reg = vsi->tx_rings[i]->reg_idx;
  1925. val = rd32(hw, QINT_TQCTL(reg));
  1926. val &= ~QINT_TQCTL_CAUSE_ENA_M;
  1927. wr32(hw, QINT_TQCTL(reg), val);
  1928. }
  1929. }
  1930. }
  1931. if (vsi->rx_rings) {
  1932. ice_for_each_rxq(vsi, i) {
  1933. if (vsi->rx_rings[i]) {
  1934. u16 reg;
  1935. reg = vsi->rx_rings[i]->reg_idx;
  1936. val = rd32(hw, QINT_RQCTL(reg));
  1937. val &= ~QINT_RQCTL_CAUSE_ENA_M;
  1938. wr32(hw, QINT_RQCTL(reg), val);
  1939. }
  1940. }
  1941. }
  1942. /* disable each interrupt */
  1943. if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
  1944. for (i = vsi->hw_base_vector;
  1945. i < (vsi->num_q_vectors + vsi->hw_base_vector); i++)
  1946. wr32(hw, GLINT_DYN_CTL(i), 0);
  1947. ice_flush(hw);
  1948. for (i = 0; i < vsi->num_q_vectors; i++)
  1949. synchronize_irq(pf->msix_entries[i + base].vector);
  1950. }
  1951. }
  1952. /**
  1953. * ice_vsi_release - Delete a VSI and free its resources
  1954. * @vsi: the VSI being removed
  1955. *
  1956. * Returns 0 on success or < 0 on error
  1957. */
  1958. int ice_vsi_release(struct ice_vsi *vsi)
  1959. {
  1960. struct ice_pf *pf;
  1961. if (!vsi->back)
  1962. return -ENODEV;
  1963. pf = vsi->back;
  1964. /* do not unregister and free netdevs while driver is in the reset
  1965. * recovery pending state. Since reset/rebuild happens through PF
  1966. * service task workqueue, its not a good idea to unregister netdev
  1967. * that is associated to the PF that is running the work queue items
  1968. * currently. This is done to avoid check_flush_dependency() warning
  1969. * on this wq
  1970. */
  1971. if (vsi->netdev && !ice_is_reset_in_progress(pf->state)) {
  1972. unregister_netdev(vsi->netdev);
  1973. free_netdev(vsi->netdev);
  1974. vsi->netdev = NULL;
  1975. }
  1976. if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
  1977. ice_rss_clean(vsi);
  1978. /* Disable VSI and free resources */
  1979. ice_vsi_dis_irq(vsi);
  1980. ice_vsi_close(vsi);
  1981. /* reclaim interrupt vectors back to PF */
  1982. ice_free_res(vsi->back->sw_irq_tracker, vsi->sw_base_vector, vsi->idx);
  1983. pf->num_avail_sw_msix += vsi->num_q_vectors;
  1984. ice_free_res(vsi->back->hw_irq_tracker, vsi->hw_base_vector, vsi->idx);
  1985. pf->num_avail_hw_msix += vsi->num_q_vectors;
  1986. ice_remove_vsi_fltr(&pf->hw, vsi->idx);
  1987. ice_vsi_delete(vsi);
  1988. ice_vsi_free_q_vectors(vsi);
  1989. ice_vsi_clear_rings(vsi);
  1990. ice_vsi_put_qs(vsi);
  1991. pf->q_left_tx += vsi->alloc_txq;
  1992. pf->q_left_rx += vsi->alloc_rxq;
  1993. /* retain SW VSI data structure since it is needed to unregister and
  1994. * free VSI netdev when PF is not in reset recovery pending state,\
  1995. * for ex: during rmmod.
  1996. */
  1997. if (!ice_is_reset_in_progress(pf->state))
  1998. ice_vsi_clear(vsi);
  1999. return 0;
  2000. }
  2001. /**
  2002. * ice_vsi_rebuild - Rebuild VSI after reset
  2003. * @vsi: VSI to be rebuild
  2004. *
  2005. * Returns 0 on success and negative value on failure
  2006. */
  2007. int ice_vsi_rebuild(struct ice_vsi *vsi)
  2008. {
  2009. u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
  2010. int ret, i;
  2011. if (!vsi)
  2012. return -EINVAL;
  2013. ice_vsi_free_q_vectors(vsi);
  2014. ice_free_res(vsi->back->sw_irq_tracker, vsi->sw_base_vector, vsi->idx);
  2015. ice_free_res(vsi->back->hw_irq_tracker, vsi->hw_base_vector, vsi->idx);
  2016. vsi->sw_base_vector = 0;
  2017. vsi->hw_base_vector = 0;
  2018. ice_vsi_clear_rings(vsi);
  2019. ice_vsi_free_arrays(vsi, false);
  2020. ice_vsi_set_num_qs(vsi);
  2021. /* Initialize VSI struct elements and create VSI in FW */
  2022. ret = ice_vsi_init(vsi);
  2023. if (ret < 0)
  2024. goto err_vsi;
  2025. ret = ice_vsi_alloc_arrays(vsi, false);
  2026. if (ret < 0)
  2027. goto err_vsi;
  2028. switch (vsi->type) {
  2029. case ICE_VSI_PF:
  2030. ret = ice_vsi_alloc_q_vectors(vsi);
  2031. if (ret)
  2032. goto err_rings;
  2033. ret = ice_vsi_setup_vector_base(vsi);
  2034. if (ret)
  2035. goto err_vectors;
  2036. ret = ice_vsi_alloc_rings(vsi);
  2037. if (ret)
  2038. goto err_vectors;
  2039. ice_vsi_map_rings_to_vectors(vsi);
  2040. break;
  2041. default:
  2042. break;
  2043. }
  2044. ice_vsi_set_tc_cfg(vsi);
  2045. /* configure VSI nodes based on number of queues and TC's */
  2046. for (i = 0; i < vsi->tc_cfg.numtc; i++)
  2047. max_txqs[i] = vsi->num_txq;
  2048. ret = ice_cfg_vsi_lan(vsi->port_info, vsi->idx, vsi->tc_cfg.ena_tc,
  2049. max_txqs);
  2050. if (ret) {
  2051. dev_info(&vsi->back->pdev->dev,
  2052. "Failed VSI lan queue config\n");
  2053. goto err_vectors;
  2054. }
  2055. return 0;
  2056. err_vectors:
  2057. ice_vsi_free_q_vectors(vsi);
  2058. err_rings:
  2059. if (vsi->netdev) {
  2060. vsi->current_netdev_flags = 0;
  2061. unregister_netdev(vsi->netdev);
  2062. free_netdev(vsi->netdev);
  2063. vsi->netdev = NULL;
  2064. }
  2065. err_vsi:
  2066. ice_vsi_clear(vsi);
  2067. set_bit(__ICE_RESET_FAILED, vsi->back->state);
  2068. return ret;
  2069. }
  2070. /**
  2071. * ice_is_reset_in_progress - check for a reset in progress
  2072. * @state: pf state field
  2073. */
  2074. bool ice_is_reset_in_progress(unsigned long *state)
  2075. {
  2076. return test_bit(__ICE_RESET_OICR_RECV, state) ||
  2077. test_bit(__ICE_PFR_REQ, state) ||
  2078. test_bit(__ICE_CORER_REQ, state) ||
  2079. test_bit(__ICE_GLOBR_REQ, state);
  2080. }