ice_lib.c 60 KB

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