ice_lib.c 67 KB

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