ice_lib.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379
  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 = vsi->vsi_num;
  1095. tmp->fltr_info.lkup_type = ICE_SW_LKUP_MAC;
  1096. tmp->fltr_info.fltr_act = ICE_FWD_TO_VSI;
  1097. tmp->fltr_info.fwd_id.vsi_id = vsi->vsi_num;
  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 = vsi->vsi_num;
  1179. tmp->fltr_info.fwd_id.vsi_id = vsi->vsi_num;
  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.fwd_id.vsi_id = vsi->vsi_num;
  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 = vsi->vsi_num;
  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. ctxt.vsi_num = vsi->vsi_num;
  1388. status = ice_aq_update_vsi(hw, &ctxt, NULL);
  1389. if (status) {
  1390. dev_err(dev, "update VSI for VLAN insert failed, err %d aq_err %d\n",
  1391. status, hw->adminq.sq_last_status);
  1392. return -EIO;
  1393. }
  1394. vsi->info.vlan_flags = ctxt.info.vlan_flags;
  1395. return 0;
  1396. }
  1397. /**
  1398. * ice_vsi_manage_vlan_stripping - Manage VLAN stripping for the VSI for Rx
  1399. * @vsi: the VSI being changed
  1400. * @ena: boolean value indicating if this is a enable or disable request
  1401. */
  1402. int ice_vsi_manage_vlan_stripping(struct ice_vsi *vsi, bool ena)
  1403. {
  1404. struct device *dev = &vsi->back->pdev->dev;
  1405. struct ice_hw *hw = &vsi->back->hw;
  1406. struct ice_vsi_ctx ctxt = { 0 };
  1407. enum ice_status status;
  1408. /* Here we are configuring what the VSI should do with the VLAN tag in
  1409. * the Rx packet. We can either leave the tag in the packet or put it in
  1410. * the Rx descriptor.
  1411. */
  1412. if (ena) {
  1413. /* Strip VLAN tag from Rx packet and put it in the desc */
  1414. ctxt.info.vlan_flags = ICE_AQ_VSI_VLAN_EMOD_STR_BOTH;
  1415. } else {
  1416. /* Disable stripping. Leave tag in packet */
  1417. ctxt.info.vlan_flags = ICE_AQ_VSI_VLAN_EMOD_NOTHING;
  1418. }
  1419. /* Allow all packets untagged/tagged */
  1420. ctxt.info.vlan_flags |= ICE_AQ_VSI_VLAN_MODE_ALL;
  1421. ctxt.info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID);
  1422. ctxt.vsi_num = vsi->vsi_num;
  1423. status = ice_aq_update_vsi(hw, &ctxt, NULL);
  1424. if (status) {
  1425. dev_err(dev, "update VSI for VLAN strip failed, ena = %d err %d aq_err %d\n",
  1426. ena, status, hw->adminq.sq_last_status);
  1427. return -EIO;
  1428. }
  1429. vsi->info.vlan_flags = ctxt.info.vlan_flags;
  1430. return 0;
  1431. }
  1432. /**
  1433. * ice_vsi_start_rx_rings - start VSI's Rx rings
  1434. * @vsi: the VSI whose rings are to be started
  1435. *
  1436. * Returns 0 on success and a negative value on error
  1437. */
  1438. int ice_vsi_start_rx_rings(struct ice_vsi *vsi)
  1439. {
  1440. return ice_vsi_ctrl_rx_rings(vsi, true);
  1441. }
  1442. /**
  1443. * ice_vsi_stop_rx_rings - stop VSI's Rx rings
  1444. * @vsi: the VSI
  1445. *
  1446. * Returns 0 on success and a negative value on error
  1447. */
  1448. int ice_vsi_stop_rx_rings(struct ice_vsi *vsi)
  1449. {
  1450. return ice_vsi_ctrl_rx_rings(vsi, false);
  1451. }
  1452. /**
  1453. * ice_vsi_stop_tx_rings - Disable Tx rings
  1454. * @vsi: the VSI being configured
  1455. */
  1456. int ice_vsi_stop_tx_rings(struct ice_vsi *vsi)
  1457. {
  1458. struct ice_pf *pf = vsi->back;
  1459. struct ice_hw *hw = &pf->hw;
  1460. enum ice_status status;
  1461. u32 *q_teids, val;
  1462. u16 *q_ids, i;
  1463. int err = 0;
  1464. if (vsi->num_txq > ICE_LAN_TXQ_MAX_QDIS)
  1465. return -EINVAL;
  1466. q_teids = devm_kcalloc(&pf->pdev->dev, vsi->num_txq, sizeof(*q_teids),
  1467. GFP_KERNEL);
  1468. if (!q_teids)
  1469. return -ENOMEM;
  1470. q_ids = devm_kcalloc(&pf->pdev->dev, vsi->num_txq, sizeof(*q_ids),
  1471. GFP_KERNEL);
  1472. if (!q_ids) {
  1473. err = -ENOMEM;
  1474. goto err_alloc_q_ids;
  1475. }
  1476. /* set up the Tx queue list to be disabled */
  1477. ice_for_each_txq(vsi, i) {
  1478. u16 v_idx;
  1479. if (!vsi->tx_rings || !vsi->tx_rings[i]) {
  1480. err = -EINVAL;
  1481. goto err_out;
  1482. }
  1483. q_ids[i] = vsi->txq_map[i];
  1484. q_teids[i] = vsi->tx_rings[i]->txq_teid;
  1485. /* clear cause_ena bit for disabled queues */
  1486. val = rd32(hw, QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
  1487. val &= ~QINT_TQCTL_CAUSE_ENA_M;
  1488. wr32(hw, QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
  1489. /* software is expected to wait for 100 ns */
  1490. ndelay(100);
  1491. /* trigger a software interrupt for the vector associated to
  1492. * the queue to schedule NAPI handler
  1493. */
  1494. v_idx = vsi->tx_rings[i]->q_vector->v_idx;
  1495. wr32(hw, GLINT_DYN_CTL(vsi->base_vector + v_idx),
  1496. GLINT_DYN_CTL_SWINT_TRIG_M | GLINT_DYN_CTL_INTENA_MSK_M);
  1497. }
  1498. status = ice_dis_vsi_txq(vsi->port_info, vsi->num_txq, q_ids, q_teids,
  1499. NULL);
  1500. /* if the disable queue command was exercised during an active reset
  1501. * flow, ICE_ERR_RESET_ONGOING is returned. This is not an error as
  1502. * the reset operation disables queues at the hardware level anyway.
  1503. */
  1504. if (status == ICE_ERR_RESET_ONGOING) {
  1505. dev_info(&pf->pdev->dev,
  1506. "Reset in progress. LAN Tx queues already disabled\n");
  1507. } else if (status) {
  1508. dev_err(&pf->pdev->dev,
  1509. "Failed to disable LAN Tx queues, error: %d\n",
  1510. status);
  1511. err = -ENODEV;
  1512. }
  1513. err_out:
  1514. devm_kfree(&pf->pdev->dev, q_ids);
  1515. err_alloc_q_ids:
  1516. devm_kfree(&pf->pdev->dev, q_teids);
  1517. return err;
  1518. }
  1519. /**
  1520. * ice_cfg_vlan_pruning - enable or disable VLAN pruning on the VSI
  1521. * @vsi: VSI to enable or disable VLAN pruning on
  1522. * @ena: set to true to enable VLAN pruning and false to disable it
  1523. *
  1524. * returns 0 if VSI is updated, negative otherwise
  1525. */
  1526. int ice_cfg_vlan_pruning(struct ice_vsi *vsi, bool ena)
  1527. {
  1528. struct ice_vsi_ctx *ctxt;
  1529. struct device *dev;
  1530. int status;
  1531. if (!vsi)
  1532. return -EINVAL;
  1533. dev = &vsi->back->pdev->dev;
  1534. ctxt = devm_kzalloc(dev, sizeof(*ctxt), GFP_KERNEL);
  1535. if (!ctxt)
  1536. return -ENOMEM;
  1537. ctxt->info = vsi->info;
  1538. if (ena) {
  1539. ctxt->info.sec_flags |=
  1540. ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
  1541. ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S;
  1542. ctxt->info.sw_flags2 |= ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
  1543. } else {
  1544. ctxt->info.sec_flags &=
  1545. ~(ICE_AQ_VSI_SEC_TX_VLAN_PRUNE_ENA <<
  1546. ICE_AQ_VSI_SEC_TX_PRUNE_ENA_S);
  1547. ctxt->info.sw_flags2 &= ~ICE_AQ_VSI_SW_FLAG_RX_VLAN_PRUNE_ENA;
  1548. }
  1549. ctxt->info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID |
  1550. ICE_AQ_VSI_PROP_SW_VALID);
  1551. ctxt->vsi_num = vsi->vsi_num;
  1552. status = ice_aq_update_vsi(&vsi->back->hw, ctxt, NULL);
  1553. if (status) {
  1554. netdev_err(vsi->netdev, "%sabling VLAN pruning on VSI %d failed, err = %d, aq_err = %d\n",
  1555. ena ? "Ena" : "Dis", vsi->vsi_num, status,
  1556. vsi->back->hw.adminq.sq_last_status);
  1557. goto err_out;
  1558. }
  1559. vsi->info.sec_flags = ctxt->info.sec_flags;
  1560. vsi->info.sw_flags2 = ctxt->info.sw_flags2;
  1561. devm_kfree(dev, ctxt);
  1562. return 0;
  1563. err_out:
  1564. devm_kfree(dev, ctxt);
  1565. return -EIO;
  1566. }
  1567. /**
  1568. * ice_vsi_setup - Set up a VSI by a given type
  1569. * @pf: board private structure
  1570. * @pi: pointer to the port_info instance
  1571. * @type: VSI type
  1572. * @vf_id: defines VF id to which this VSI connects. This field is meant to be
  1573. * used only for ICE_VSI_VF VSI type. For other VSI types, should
  1574. * fill-in ICE_INVAL_VFID as input.
  1575. *
  1576. * This allocates the sw VSI structure and its queue resources.
  1577. *
  1578. * Returns pointer to the successfully allocated and configured VSI sw struct on
  1579. * success, NULL on failure.
  1580. */
  1581. struct ice_vsi *
  1582. ice_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi,
  1583. enum ice_vsi_type type, u16 __always_unused vf_id)
  1584. {
  1585. u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
  1586. struct device *dev = &pf->pdev->dev;
  1587. struct ice_vsi *vsi;
  1588. int ret, i;
  1589. vsi = ice_vsi_alloc(pf, type);
  1590. if (!vsi) {
  1591. dev_err(dev, "could not allocate VSI\n");
  1592. return NULL;
  1593. }
  1594. vsi->port_info = pi;
  1595. vsi->vsw = pf->first_sw;
  1596. if (ice_vsi_get_qs(vsi)) {
  1597. dev_err(dev, "Failed to allocate queues. vsi->idx = %d\n",
  1598. vsi->idx);
  1599. goto unroll_get_qs;
  1600. }
  1601. /* set RSS capabilities */
  1602. ice_vsi_set_rss_params(vsi);
  1603. /* create the VSI */
  1604. ret = ice_vsi_init(vsi);
  1605. if (ret)
  1606. goto unroll_get_qs;
  1607. switch (vsi->type) {
  1608. case ICE_VSI_PF:
  1609. ret = ice_vsi_alloc_q_vectors(vsi);
  1610. if (ret)
  1611. goto unroll_vsi_init;
  1612. ret = ice_vsi_setup_vector_base(vsi);
  1613. if (ret)
  1614. goto unroll_alloc_q_vector;
  1615. ret = ice_vsi_alloc_rings(vsi);
  1616. if (ret)
  1617. goto unroll_vector_base;
  1618. ice_vsi_map_rings_to_vectors(vsi);
  1619. /* Do not exit if configuring RSS had an issue, at least
  1620. * receive traffic on first queue. Hence no need to capture
  1621. * return value
  1622. */
  1623. if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
  1624. ice_vsi_cfg_rss_lut_key(vsi);
  1625. break;
  1626. default:
  1627. /* if VSI type is not recognized, clean up the resources and
  1628. * exit
  1629. */
  1630. goto unroll_vsi_init;
  1631. }
  1632. ice_vsi_set_tc_cfg(vsi);
  1633. /* configure VSI nodes based on number of queues and TC's */
  1634. for (i = 0; i < vsi->tc_cfg.numtc; i++)
  1635. max_txqs[i] = vsi->num_txq;
  1636. ret = ice_cfg_vsi_lan(vsi->port_info, vsi->vsi_num,
  1637. vsi->tc_cfg.ena_tc, max_txqs);
  1638. if (ret) {
  1639. dev_info(&pf->pdev->dev, "Failed VSI lan queue config\n");
  1640. goto unroll_vector_base;
  1641. }
  1642. return vsi;
  1643. unroll_vector_base:
  1644. ice_free_res(vsi->back->irq_tracker, vsi->base_vector, vsi->idx);
  1645. unroll_alloc_q_vector:
  1646. ice_vsi_free_q_vectors(vsi);
  1647. unroll_vsi_init:
  1648. ice_vsi_delete(vsi);
  1649. unroll_get_qs:
  1650. ice_vsi_put_qs(vsi);
  1651. pf->q_left_tx += vsi->alloc_txq;
  1652. pf->q_left_rx += vsi->alloc_rxq;
  1653. ice_vsi_clear(vsi);
  1654. return NULL;
  1655. }
  1656. /**
  1657. * ice_vsi_release_msix - Clear the queue to Interrupt mapping in HW
  1658. * @vsi: the VSI being cleaned up
  1659. */
  1660. static void ice_vsi_release_msix(struct ice_vsi *vsi)
  1661. {
  1662. struct ice_pf *pf = vsi->back;
  1663. u16 vector = vsi->base_vector;
  1664. struct ice_hw *hw = &pf->hw;
  1665. u32 txq = 0;
  1666. u32 rxq = 0;
  1667. int i, q;
  1668. for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
  1669. struct ice_q_vector *q_vector = vsi->q_vectors[i];
  1670. wr32(hw, GLINT_ITR(ICE_RX_ITR, vector), 0);
  1671. wr32(hw, GLINT_ITR(ICE_TX_ITR, vector), 0);
  1672. for (q = 0; q < q_vector->num_ring_tx; q++) {
  1673. wr32(hw, QINT_TQCTL(vsi->txq_map[txq]), 0);
  1674. txq++;
  1675. }
  1676. for (q = 0; q < q_vector->num_ring_rx; q++) {
  1677. wr32(hw, QINT_RQCTL(vsi->rxq_map[rxq]), 0);
  1678. rxq++;
  1679. }
  1680. }
  1681. ice_flush(hw);
  1682. }
  1683. /**
  1684. * ice_vsi_free_irq - Free the IRQ association with the OS
  1685. * @vsi: the VSI being configured
  1686. */
  1687. void ice_vsi_free_irq(struct ice_vsi *vsi)
  1688. {
  1689. struct ice_pf *pf = vsi->back;
  1690. int base = vsi->base_vector;
  1691. if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
  1692. int i;
  1693. if (!vsi->q_vectors || !vsi->irqs_ready)
  1694. return;
  1695. vsi->irqs_ready = false;
  1696. for (i = 0; i < vsi->num_q_vectors; i++) {
  1697. u16 vector = i + base;
  1698. int irq_num;
  1699. irq_num = pf->msix_entries[vector].vector;
  1700. /* free only the irqs that were actually requested */
  1701. if (!vsi->q_vectors[i] ||
  1702. !(vsi->q_vectors[i]->num_ring_tx ||
  1703. vsi->q_vectors[i]->num_ring_rx))
  1704. continue;
  1705. /* clear the affinity notifier in the IRQ descriptor */
  1706. irq_set_affinity_notifier(irq_num, NULL);
  1707. /* clear the affinity_mask in the IRQ descriptor */
  1708. irq_set_affinity_hint(irq_num, NULL);
  1709. synchronize_irq(irq_num);
  1710. devm_free_irq(&pf->pdev->dev, irq_num,
  1711. vsi->q_vectors[i]);
  1712. }
  1713. ice_vsi_release_msix(vsi);
  1714. }
  1715. }
  1716. /**
  1717. * ice_vsi_free_tx_rings - Free Tx resources for VSI queues
  1718. * @vsi: the VSI having resources freed
  1719. */
  1720. void ice_vsi_free_tx_rings(struct ice_vsi *vsi)
  1721. {
  1722. int i;
  1723. if (!vsi->tx_rings)
  1724. return;
  1725. ice_for_each_txq(vsi, i)
  1726. if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
  1727. ice_free_tx_ring(vsi->tx_rings[i]);
  1728. }
  1729. /**
  1730. * ice_vsi_free_rx_rings - Free Rx resources for VSI queues
  1731. * @vsi: the VSI having resources freed
  1732. */
  1733. void ice_vsi_free_rx_rings(struct ice_vsi *vsi)
  1734. {
  1735. int i;
  1736. if (!vsi->rx_rings)
  1737. return;
  1738. ice_for_each_rxq(vsi, i)
  1739. if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
  1740. ice_free_rx_ring(vsi->rx_rings[i]);
  1741. }
  1742. /**
  1743. * ice_vsi_close - Shut down a VSI
  1744. * @vsi: the VSI being shut down
  1745. */
  1746. void ice_vsi_close(struct ice_vsi *vsi)
  1747. {
  1748. if (!test_and_set_bit(__ICE_DOWN, vsi->state))
  1749. ice_down(vsi);
  1750. ice_vsi_free_irq(vsi);
  1751. ice_vsi_free_tx_rings(vsi);
  1752. ice_vsi_free_rx_rings(vsi);
  1753. }
  1754. /**
  1755. * ice_free_res - free a block of resources
  1756. * @res: pointer to the resource
  1757. * @index: starting index previously returned by ice_get_res
  1758. * @id: identifier to track owner
  1759. *
  1760. * Returns number of resources freed
  1761. */
  1762. int ice_free_res(struct ice_res_tracker *res, u16 index, u16 id)
  1763. {
  1764. int count = 0;
  1765. int i;
  1766. if (!res || index >= res->num_entries)
  1767. return -EINVAL;
  1768. id |= ICE_RES_VALID_BIT;
  1769. for (i = index; i < res->num_entries && res->list[i] == id; i++) {
  1770. res->list[i] = 0;
  1771. count++;
  1772. }
  1773. return count;
  1774. }
  1775. /**
  1776. * ice_search_res - Search the tracker for a block of resources
  1777. * @res: pointer to the resource
  1778. * @needed: size of the block needed
  1779. * @id: identifier to track owner
  1780. *
  1781. * Returns the base item index of the block, or -ENOMEM for error
  1782. */
  1783. static int ice_search_res(struct ice_res_tracker *res, u16 needed, u16 id)
  1784. {
  1785. int start = res->search_hint;
  1786. int end = start;
  1787. id |= ICE_RES_VALID_BIT;
  1788. do {
  1789. /* skip already allocated entries */
  1790. if (res->list[end++] & ICE_RES_VALID_BIT) {
  1791. start = end;
  1792. if ((start + needed) > res->num_entries)
  1793. break;
  1794. }
  1795. if (end == (start + needed)) {
  1796. int i = start;
  1797. /* there was enough, so assign it to the requestor */
  1798. while (i != end)
  1799. res->list[i++] = id;
  1800. if (end == res->num_entries)
  1801. end = 0;
  1802. res->search_hint = end;
  1803. return start;
  1804. }
  1805. } while (1);
  1806. return -ENOMEM;
  1807. }
  1808. /**
  1809. * ice_get_res - get a block of resources
  1810. * @pf: board private structure
  1811. * @res: pointer to the resource
  1812. * @needed: size of the block needed
  1813. * @id: identifier to track owner
  1814. *
  1815. * Returns the base item index of the block, or -ENOMEM for error
  1816. * The search_hint trick and lack of advanced fit-finding only works
  1817. * because we're highly likely to have all the same sized requests.
  1818. * Linear search time and any fragmentation should be minimal.
  1819. */
  1820. int
  1821. ice_get_res(struct ice_pf *pf, struct ice_res_tracker *res, u16 needed, u16 id)
  1822. {
  1823. int ret;
  1824. if (!res || !pf)
  1825. return -EINVAL;
  1826. if (!needed || needed > res->num_entries || id >= ICE_RES_VALID_BIT) {
  1827. dev_err(&pf->pdev->dev,
  1828. "param err: needed=%d, num_entries = %d id=0x%04x\n",
  1829. needed, res->num_entries, id);
  1830. return -EINVAL;
  1831. }
  1832. /* search based on search_hint */
  1833. ret = ice_search_res(res, needed, id);
  1834. if (ret < 0) {
  1835. /* previous search failed. Reset search hint and try again */
  1836. res->search_hint = 0;
  1837. ret = ice_search_res(res, needed, id);
  1838. }
  1839. return ret;
  1840. }
  1841. /**
  1842. * ice_vsi_dis_irq - Mask off queue interrupt generation on the VSI
  1843. * @vsi: the VSI being un-configured
  1844. */
  1845. void ice_vsi_dis_irq(struct ice_vsi *vsi)
  1846. {
  1847. struct ice_pf *pf = vsi->back;
  1848. struct ice_hw *hw = &pf->hw;
  1849. int base = vsi->base_vector;
  1850. u32 val;
  1851. int i;
  1852. /* disable interrupt causation from each queue */
  1853. if (vsi->tx_rings) {
  1854. ice_for_each_txq(vsi, i) {
  1855. if (vsi->tx_rings[i]) {
  1856. u16 reg;
  1857. reg = vsi->tx_rings[i]->reg_idx;
  1858. val = rd32(hw, QINT_TQCTL(reg));
  1859. val &= ~QINT_TQCTL_CAUSE_ENA_M;
  1860. wr32(hw, QINT_TQCTL(reg), val);
  1861. }
  1862. }
  1863. }
  1864. if (vsi->rx_rings) {
  1865. ice_for_each_rxq(vsi, i) {
  1866. if (vsi->rx_rings[i]) {
  1867. u16 reg;
  1868. reg = vsi->rx_rings[i]->reg_idx;
  1869. val = rd32(hw, QINT_RQCTL(reg));
  1870. val &= ~QINT_RQCTL_CAUSE_ENA_M;
  1871. wr32(hw, QINT_RQCTL(reg), val);
  1872. }
  1873. }
  1874. }
  1875. /* disable each interrupt */
  1876. if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
  1877. for (i = vsi->base_vector;
  1878. i < (vsi->num_q_vectors + vsi->base_vector); i++)
  1879. wr32(hw, GLINT_DYN_CTL(i), 0);
  1880. ice_flush(hw);
  1881. for (i = 0; i < vsi->num_q_vectors; i++)
  1882. synchronize_irq(pf->msix_entries[i + base].vector);
  1883. }
  1884. }
  1885. /**
  1886. * ice_vsi_release - Delete a VSI and free its resources
  1887. * @vsi: the VSI being removed
  1888. *
  1889. * Returns 0 on success or < 0 on error
  1890. */
  1891. int ice_vsi_release(struct ice_vsi *vsi)
  1892. {
  1893. struct ice_pf *pf;
  1894. if (!vsi->back)
  1895. return -ENODEV;
  1896. pf = vsi->back;
  1897. /* do not unregister and free netdevs while driver is in the reset
  1898. * recovery pending state. Since reset/rebuild happens through PF
  1899. * service task workqueue, its not a good idea to unregister netdev
  1900. * that is associated to the PF that is running the work queue items
  1901. * currently. This is done to avoid check_flush_dependency() warning
  1902. * on this wq
  1903. */
  1904. if (vsi->netdev && !ice_is_reset_in_progress(pf->state)) {
  1905. unregister_netdev(vsi->netdev);
  1906. free_netdev(vsi->netdev);
  1907. vsi->netdev = NULL;
  1908. }
  1909. if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
  1910. ice_rss_clean(vsi);
  1911. /* Disable VSI and free resources */
  1912. ice_vsi_dis_irq(vsi);
  1913. ice_vsi_close(vsi);
  1914. /* reclaim interrupt vectors back to PF */
  1915. ice_free_res(vsi->back->irq_tracker, vsi->base_vector, vsi->idx);
  1916. pf->num_avail_msix += vsi->num_q_vectors;
  1917. ice_remove_vsi_fltr(&pf->hw, vsi->vsi_num);
  1918. ice_vsi_delete(vsi);
  1919. ice_vsi_free_q_vectors(vsi);
  1920. ice_vsi_clear_rings(vsi);
  1921. ice_vsi_put_qs(vsi);
  1922. pf->q_left_tx += vsi->alloc_txq;
  1923. pf->q_left_rx += vsi->alloc_rxq;
  1924. /* retain SW VSI data structure since it is needed to unregister and
  1925. * free VSI netdev when PF is not in reset recovery pending state,\
  1926. * for ex: during rmmod.
  1927. */
  1928. if (!ice_is_reset_in_progress(pf->state))
  1929. ice_vsi_clear(vsi);
  1930. return 0;
  1931. }
  1932. /**
  1933. * ice_vsi_rebuild - Rebuild VSI after reset
  1934. * @vsi: VSI to be rebuild
  1935. *
  1936. * Returns 0 on success and negative value on failure
  1937. */
  1938. int ice_vsi_rebuild(struct ice_vsi *vsi)
  1939. {
  1940. u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
  1941. int ret, i;
  1942. if (!vsi)
  1943. return -EINVAL;
  1944. ice_vsi_free_q_vectors(vsi);
  1945. ice_free_res(vsi->back->irq_tracker, vsi->base_vector, vsi->idx);
  1946. vsi->base_vector = 0;
  1947. ice_vsi_clear_rings(vsi);
  1948. ice_vsi_free_arrays(vsi, false);
  1949. ice_vsi_set_num_qs(vsi);
  1950. /* Initialize VSI struct elements and create VSI in FW */
  1951. ret = ice_vsi_init(vsi);
  1952. if (ret < 0)
  1953. goto err_vsi;
  1954. ret = ice_vsi_alloc_arrays(vsi, false);
  1955. if (ret < 0)
  1956. goto err_vsi;
  1957. switch (vsi->type) {
  1958. case ICE_VSI_PF:
  1959. ret = ice_vsi_alloc_q_vectors(vsi);
  1960. if (ret)
  1961. goto err_rings;
  1962. ret = ice_vsi_setup_vector_base(vsi);
  1963. if (ret)
  1964. goto err_vectors;
  1965. ret = ice_vsi_alloc_rings(vsi);
  1966. if (ret)
  1967. goto err_vectors;
  1968. ice_vsi_map_rings_to_vectors(vsi);
  1969. break;
  1970. default:
  1971. break;
  1972. }
  1973. ice_vsi_set_tc_cfg(vsi);
  1974. /* configure VSI nodes based on number of queues and TC's */
  1975. for (i = 0; i < vsi->tc_cfg.numtc; i++)
  1976. max_txqs[i] = vsi->num_txq;
  1977. ret = ice_cfg_vsi_lan(vsi->port_info, vsi->vsi_num,
  1978. vsi->tc_cfg.ena_tc, max_txqs);
  1979. if (ret) {
  1980. dev_info(&vsi->back->pdev->dev,
  1981. "Failed VSI lan queue config\n");
  1982. goto err_vectors;
  1983. }
  1984. return 0;
  1985. err_vectors:
  1986. ice_vsi_free_q_vectors(vsi);
  1987. err_rings:
  1988. if (vsi->netdev) {
  1989. vsi->current_netdev_flags = 0;
  1990. unregister_netdev(vsi->netdev);
  1991. free_netdev(vsi->netdev);
  1992. vsi->netdev = NULL;
  1993. }
  1994. err_vsi:
  1995. ice_vsi_clear(vsi);
  1996. set_bit(__ICE_RESET_FAILED, vsi->back->state);
  1997. return ret;
  1998. }
  1999. /**
  2000. * ice_is_reset_in_progress - check for a reset in progress
  2001. * @state: pf state field
  2002. */
  2003. bool ice_is_reset_in_progress(unsigned long *state)
  2004. {
  2005. return test_bit(__ICE_RESET_OICR_RECV, state) ||
  2006. test_bit(__ICE_PFR_REQ, state) ||
  2007. test_bit(__ICE_CORER_REQ, state) ||
  2008. test_bit(__ICE_GLOBR_REQ, state);
  2009. }