ice_lib.c 59 KB

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