bnx2x_sriov.c 83 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001
  1. /* bnx2x_sriov.c: Broadcom Everest network driver.
  2. *
  3. * Copyright 2009-2013 Broadcom Corporation
  4. *
  5. * Unless you and Broadcom execute a separate written software license
  6. * agreement governing use of this software, this software is licensed to you
  7. * under the terms of the GNU General Public License version 2, available
  8. * at http://www.gnu.org/licenses/old-licenses/gpl-2.0.html (the "GPL").
  9. *
  10. * Notwithstanding the above, under no circumstances may you combine this
  11. * software in any way with any other Broadcom software provided under a
  12. * license other than the GPL, without Broadcom's express prior written
  13. * consent.
  14. *
  15. * Maintained by: Ariel Elior <ariel.elior@qlogic.com>
  16. * Written by: Shmulik Ravid
  17. * Ariel Elior <ariel.elior@qlogic.com>
  18. *
  19. */
  20. #include "bnx2x.h"
  21. #include "bnx2x_init.h"
  22. #include "bnx2x_cmn.h"
  23. #include "bnx2x_sp.h"
  24. #include <linux/crc32.h>
  25. #include <linux/if_vlan.h>
  26. /* General service functions */
  27. static void storm_memset_vf_to_pf(struct bnx2x *bp, u16 abs_fid,
  28. u16 pf_id)
  29. {
  30. REG_WR8(bp, BAR_XSTRORM_INTMEM + XSTORM_VF_TO_PF_OFFSET(abs_fid),
  31. pf_id);
  32. REG_WR8(bp, BAR_CSTRORM_INTMEM + CSTORM_VF_TO_PF_OFFSET(abs_fid),
  33. pf_id);
  34. REG_WR8(bp, BAR_TSTRORM_INTMEM + TSTORM_VF_TO_PF_OFFSET(abs_fid),
  35. pf_id);
  36. REG_WR8(bp, BAR_USTRORM_INTMEM + USTORM_VF_TO_PF_OFFSET(abs_fid),
  37. pf_id);
  38. }
  39. static void storm_memset_func_en(struct bnx2x *bp, u16 abs_fid,
  40. u8 enable)
  41. {
  42. REG_WR8(bp, BAR_XSTRORM_INTMEM + XSTORM_FUNC_EN_OFFSET(abs_fid),
  43. enable);
  44. REG_WR8(bp, BAR_CSTRORM_INTMEM + CSTORM_FUNC_EN_OFFSET(abs_fid),
  45. enable);
  46. REG_WR8(bp, BAR_TSTRORM_INTMEM + TSTORM_FUNC_EN_OFFSET(abs_fid),
  47. enable);
  48. REG_WR8(bp, BAR_USTRORM_INTMEM + USTORM_FUNC_EN_OFFSET(abs_fid),
  49. enable);
  50. }
  51. int bnx2x_vf_idx_by_abs_fid(struct bnx2x *bp, u16 abs_vfid)
  52. {
  53. int idx;
  54. for_each_vf(bp, idx)
  55. if (bnx2x_vf(bp, idx, abs_vfid) == abs_vfid)
  56. break;
  57. return idx;
  58. }
  59. static
  60. struct bnx2x_virtf *bnx2x_vf_by_abs_fid(struct bnx2x *bp, u16 abs_vfid)
  61. {
  62. u16 idx = (u16)bnx2x_vf_idx_by_abs_fid(bp, abs_vfid);
  63. return (idx < BNX2X_NR_VIRTFN(bp)) ? BP_VF(bp, idx) : NULL;
  64. }
  65. static void bnx2x_vf_igu_ack_sb(struct bnx2x *bp, struct bnx2x_virtf *vf,
  66. u8 igu_sb_id, u8 segment, u16 index, u8 op,
  67. u8 update)
  68. {
  69. /* acking a VF sb through the PF - use the GRC */
  70. u32 ctl;
  71. u32 igu_addr_data = IGU_REG_COMMAND_REG_32LSB_DATA;
  72. u32 igu_addr_ctl = IGU_REG_COMMAND_REG_CTRL;
  73. u32 func_encode = vf->abs_vfid;
  74. u32 addr_encode = IGU_CMD_E2_PROD_UPD_BASE + igu_sb_id;
  75. struct igu_regular cmd_data = {0};
  76. cmd_data.sb_id_and_flags =
  77. ((index << IGU_REGULAR_SB_INDEX_SHIFT) |
  78. (segment << IGU_REGULAR_SEGMENT_ACCESS_SHIFT) |
  79. (update << IGU_REGULAR_BUPDATE_SHIFT) |
  80. (op << IGU_REGULAR_ENABLE_INT_SHIFT));
  81. ctl = addr_encode << IGU_CTRL_REG_ADDRESS_SHIFT |
  82. func_encode << IGU_CTRL_REG_FID_SHIFT |
  83. IGU_CTRL_CMD_TYPE_WR << IGU_CTRL_REG_TYPE_SHIFT;
  84. DP(NETIF_MSG_HW, "write 0x%08x to IGU(via GRC) addr 0x%x\n",
  85. cmd_data.sb_id_and_flags, igu_addr_data);
  86. REG_WR(bp, igu_addr_data, cmd_data.sb_id_and_flags);
  87. mmiowb();
  88. barrier();
  89. DP(NETIF_MSG_HW, "write 0x%08x to IGU(via GRC) addr 0x%x\n",
  90. ctl, igu_addr_ctl);
  91. REG_WR(bp, igu_addr_ctl, ctl);
  92. mmiowb();
  93. barrier();
  94. }
  95. static bool bnx2x_validate_vf_sp_objs(struct bnx2x *bp,
  96. struct bnx2x_virtf *vf,
  97. bool print_err)
  98. {
  99. if (!bnx2x_leading_vfq(vf, sp_initialized)) {
  100. if (print_err)
  101. BNX2X_ERR("Slowpath objects not yet initialized!\n");
  102. else
  103. DP(BNX2X_MSG_IOV, "Slowpath objects not yet initialized!\n");
  104. return false;
  105. }
  106. return true;
  107. }
  108. /* VFOP operations states */
  109. void bnx2x_vfop_qctor_dump_tx(struct bnx2x *bp, struct bnx2x_virtf *vf,
  110. struct bnx2x_queue_init_params *init_params,
  111. struct bnx2x_queue_setup_params *setup_params,
  112. u16 q_idx, u16 sb_idx)
  113. {
  114. DP(BNX2X_MSG_IOV,
  115. "VF[%d] Q_SETUP: txq[%d]-- vfsb=%d, sb-index=%d, hc-rate=%d, flags=0x%lx, traffic-type=%d",
  116. vf->abs_vfid,
  117. q_idx,
  118. sb_idx,
  119. init_params->tx.sb_cq_index,
  120. init_params->tx.hc_rate,
  121. setup_params->flags,
  122. setup_params->txq_params.traffic_type);
  123. }
  124. void bnx2x_vfop_qctor_dump_rx(struct bnx2x *bp, struct bnx2x_virtf *vf,
  125. struct bnx2x_queue_init_params *init_params,
  126. struct bnx2x_queue_setup_params *setup_params,
  127. u16 q_idx, u16 sb_idx)
  128. {
  129. struct bnx2x_rxq_setup_params *rxq_params = &setup_params->rxq_params;
  130. DP(BNX2X_MSG_IOV, "VF[%d] Q_SETUP: rxq[%d]-- vfsb=%d, sb-index=%d, hc-rate=%d, mtu=%d, buf-size=%d\n"
  131. "sge-size=%d, max_sge_pkt=%d, tpa-agg-size=%d, flags=0x%lx, drop-flags=0x%x, cache-log=%d\n",
  132. vf->abs_vfid,
  133. q_idx,
  134. sb_idx,
  135. init_params->rx.sb_cq_index,
  136. init_params->rx.hc_rate,
  137. setup_params->gen_params.mtu,
  138. rxq_params->buf_sz,
  139. rxq_params->sge_buf_sz,
  140. rxq_params->max_sges_pkt,
  141. rxq_params->tpa_agg_sz,
  142. setup_params->flags,
  143. rxq_params->drop_flags,
  144. rxq_params->cache_line_log);
  145. }
  146. void bnx2x_vfop_qctor_prep(struct bnx2x *bp,
  147. struct bnx2x_virtf *vf,
  148. struct bnx2x_vf_queue *q,
  149. struct bnx2x_vf_queue_construct_params *p,
  150. unsigned long q_type)
  151. {
  152. struct bnx2x_queue_init_params *init_p = &p->qstate.params.init;
  153. struct bnx2x_queue_setup_params *setup_p = &p->prep_qsetup;
  154. /* INIT */
  155. /* Enable host coalescing in the transition to INIT state */
  156. if (test_bit(BNX2X_Q_FLG_HC, &init_p->rx.flags))
  157. __set_bit(BNX2X_Q_FLG_HC_EN, &init_p->rx.flags);
  158. if (test_bit(BNX2X_Q_FLG_HC, &init_p->tx.flags))
  159. __set_bit(BNX2X_Q_FLG_HC_EN, &init_p->tx.flags);
  160. /* FW SB ID */
  161. init_p->rx.fw_sb_id = vf_igu_sb(vf, q->sb_idx);
  162. init_p->tx.fw_sb_id = vf_igu_sb(vf, q->sb_idx);
  163. /* context */
  164. init_p->cxts[0] = q->cxt;
  165. /* SETUP */
  166. /* Setup-op general parameters */
  167. setup_p->gen_params.spcl_id = vf->sp_cl_id;
  168. setup_p->gen_params.stat_id = vfq_stat_id(vf, q);
  169. /* Setup-op pause params:
  170. * Nothing to do, the pause thresholds are set by default to 0 which
  171. * effectively turns off the feature for this queue. We don't want
  172. * one queue (VF) to interfering with another queue (another VF)
  173. */
  174. if (vf->cfg_flags & VF_CFG_FW_FC)
  175. BNX2X_ERR("No support for pause to VFs (abs_vfid: %d)\n",
  176. vf->abs_vfid);
  177. /* Setup-op flags:
  178. * collect statistics, zero statistics, local-switching, security,
  179. * OV for Flex10, RSS and MCAST for leading
  180. */
  181. if (test_bit(BNX2X_Q_FLG_STATS, &setup_p->flags))
  182. __set_bit(BNX2X_Q_FLG_ZERO_STATS, &setup_p->flags);
  183. /* for VFs, enable tx switching, bd coherency, and mac address
  184. * anti-spoofing
  185. */
  186. __set_bit(BNX2X_Q_FLG_TX_SWITCH, &setup_p->flags);
  187. __set_bit(BNX2X_Q_FLG_TX_SEC, &setup_p->flags);
  188. __set_bit(BNX2X_Q_FLG_ANTI_SPOOF, &setup_p->flags);
  189. /* Setup-op rx parameters */
  190. if (test_bit(BNX2X_Q_TYPE_HAS_RX, &q_type)) {
  191. struct bnx2x_rxq_setup_params *rxq_p = &setup_p->rxq_params;
  192. rxq_p->cl_qzone_id = vfq_qzone_id(vf, q);
  193. rxq_p->fw_sb_id = vf_igu_sb(vf, q->sb_idx);
  194. rxq_p->rss_engine_id = FW_VF_HANDLE(vf->abs_vfid);
  195. if (test_bit(BNX2X_Q_FLG_TPA, &setup_p->flags))
  196. rxq_p->max_tpa_queues = BNX2X_VF_MAX_TPA_AGG_QUEUES;
  197. }
  198. /* Setup-op tx parameters */
  199. if (test_bit(BNX2X_Q_TYPE_HAS_TX, &q_type)) {
  200. setup_p->txq_params.tss_leading_cl_id = vf->leading_rss;
  201. setup_p->txq_params.fw_sb_id = vf_igu_sb(vf, q->sb_idx);
  202. }
  203. }
  204. static int bnx2x_vf_queue_create(struct bnx2x *bp,
  205. struct bnx2x_virtf *vf, int qid,
  206. struct bnx2x_vf_queue_construct_params *qctor)
  207. {
  208. struct bnx2x_queue_state_params *q_params;
  209. int rc = 0;
  210. DP(BNX2X_MSG_IOV, "vf[%d:%d]\n", vf->abs_vfid, qid);
  211. /* Prepare ramrod information */
  212. q_params = &qctor->qstate;
  213. q_params->q_obj = &bnx2x_vfq(vf, qid, sp_obj);
  214. set_bit(RAMROD_COMP_WAIT, &q_params->ramrod_flags);
  215. if (bnx2x_get_q_logical_state(bp, q_params->q_obj) ==
  216. BNX2X_Q_LOGICAL_STATE_ACTIVE) {
  217. DP(BNX2X_MSG_IOV, "queue was already up. Aborting gracefully\n");
  218. goto out;
  219. }
  220. /* Run Queue 'construction' ramrods */
  221. q_params->cmd = BNX2X_Q_CMD_INIT;
  222. rc = bnx2x_queue_state_change(bp, q_params);
  223. if (rc)
  224. goto out;
  225. memcpy(&q_params->params.setup, &qctor->prep_qsetup,
  226. sizeof(struct bnx2x_queue_setup_params));
  227. q_params->cmd = BNX2X_Q_CMD_SETUP;
  228. rc = bnx2x_queue_state_change(bp, q_params);
  229. if (rc)
  230. goto out;
  231. /* enable interrupts */
  232. bnx2x_vf_igu_ack_sb(bp, vf, vf_igu_sb(vf, bnx2x_vfq(vf, qid, sb_idx)),
  233. USTORM_ID, 0, IGU_INT_ENABLE, 0);
  234. out:
  235. return rc;
  236. }
  237. static int bnx2x_vf_queue_destroy(struct bnx2x *bp, struct bnx2x_virtf *vf,
  238. int qid)
  239. {
  240. enum bnx2x_queue_cmd cmds[] = {BNX2X_Q_CMD_HALT,
  241. BNX2X_Q_CMD_TERMINATE,
  242. BNX2X_Q_CMD_CFC_DEL};
  243. struct bnx2x_queue_state_params q_params;
  244. int rc, i;
  245. DP(BNX2X_MSG_IOV, "vf[%d]\n", vf->abs_vfid);
  246. /* Prepare ramrod information */
  247. memset(&q_params, 0, sizeof(struct bnx2x_queue_state_params));
  248. q_params.q_obj = &bnx2x_vfq(vf, qid, sp_obj);
  249. set_bit(RAMROD_COMP_WAIT, &q_params.ramrod_flags);
  250. if (bnx2x_get_q_logical_state(bp, q_params.q_obj) ==
  251. BNX2X_Q_LOGICAL_STATE_STOPPED) {
  252. DP(BNX2X_MSG_IOV, "queue was already stopped. Aborting gracefully\n");
  253. goto out;
  254. }
  255. /* Run Queue 'destruction' ramrods */
  256. for (i = 0; i < ARRAY_SIZE(cmds); i++) {
  257. q_params.cmd = cmds[i];
  258. rc = bnx2x_queue_state_change(bp, &q_params);
  259. if (rc) {
  260. BNX2X_ERR("Failed to run Queue command %d\n", cmds[i]);
  261. return rc;
  262. }
  263. }
  264. out:
  265. /* Clean Context */
  266. if (bnx2x_vfq(vf, qid, cxt)) {
  267. bnx2x_vfq(vf, qid, cxt)->ustorm_ag_context.cdu_usage = 0;
  268. bnx2x_vfq(vf, qid, cxt)->xstorm_ag_context.cdu_reserved = 0;
  269. }
  270. return 0;
  271. }
  272. static void
  273. bnx2x_vf_set_igu_info(struct bnx2x *bp, u8 igu_sb_id, u8 abs_vfid)
  274. {
  275. struct bnx2x_virtf *vf = bnx2x_vf_by_abs_fid(bp, abs_vfid);
  276. if (vf) {
  277. /* the first igu entry belonging to VFs of this PF */
  278. if (!BP_VFDB(bp)->first_vf_igu_entry)
  279. BP_VFDB(bp)->first_vf_igu_entry = igu_sb_id;
  280. /* the first igu entry belonging to this VF */
  281. if (!vf_sb_count(vf))
  282. vf->igu_base_id = igu_sb_id;
  283. ++vf_sb_count(vf);
  284. ++vf->sb_count;
  285. }
  286. BP_VFDB(bp)->vf_sbs_pool++;
  287. }
  288. static inline void bnx2x_vf_vlan_credit(struct bnx2x *bp,
  289. struct bnx2x_vlan_mac_obj *obj,
  290. atomic_t *counter)
  291. {
  292. struct list_head *pos;
  293. int read_lock;
  294. int cnt = 0;
  295. read_lock = bnx2x_vlan_mac_h_read_lock(bp, obj);
  296. if (read_lock)
  297. DP(BNX2X_MSG_SP, "Failed to take vlan mac read head; continuing anyway\n");
  298. list_for_each(pos, &obj->head)
  299. cnt++;
  300. if (!read_lock)
  301. bnx2x_vlan_mac_h_read_unlock(bp, obj);
  302. atomic_set(counter, cnt);
  303. }
  304. static int bnx2x_vf_vlan_mac_clear(struct bnx2x *bp, struct bnx2x_virtf *vf,
  305. int qid, bool drv_only, bool mac)
  306. {
  307. struct bnx2x_vlan_mac_ramrod_params ramrod;
  308. int rc;
  309. DP(BNX2X_MSG_IOV, "vf[%d] - deleting all %s\n", vf->abs_vfid,
  310. mac ? "MACs" : "VLANs");
  311. /* Prepare ramrod params */
  312. memset(&ramrod, 0, sizeof(struct bnx2x_vlan_mac_ramrod_params));
  313. if (mac) {
  314. set_bit(BNX2X_ETH_MAC, &ramrod.user_req.vlan_mac_flags);
  315. ramrod.vlan_mac_obj = &bnx2x_vfq(vf, qid, mac_obj);
  316. } else {
  317. set_bit(BNX2X_DONT_CONSUME_CAM_CREDIT,
  318. &ramrod.user_req.vlan_mac_flags);
  319. ramrod.vlan_mac_obj = &bnx2x_vfq(vf, qid, vlan_obj);
  320. }
  321. ramrod.user_req.cmd = BNX2X_VLAN_MAC_DEL;
  322. set_bit(RAMROD_EXEC, &ramrod.ramrod_flags);
  323. if (drv_only)
  324. set_bit(RAMROD_DRV_CLR_ONLY, &ramrod.ramrod_flags);
  325. else
  326. set_bit(RAMROD_COMP_WAIT, &ramrod.ramrod_flags);
  327. /* Start deleting */
  328. rc = ramrod.vlan_mac_obj->delete_all(bp,
  329. ramrod.vlan_mac_obj,
  330. &ramrod.user_req.vlan_mac_flags,
  331. &ramrod.ramrod_flags);
  332. if (rc) {
  333. BNX2X_ERR("Failed to delete all %s\n",
  334. mac ? "MACs" : "VLANs");
  335. return rc;
  336. }
  337. /* Clear the vlan counters */
  338. if (!mac)
  339. atomic_set(&bnx2x_vfq(vf, qid, vlan_count), 0);
  340. return 0;
  341. }
  342. static int bnx2x_vf_mac_vlan_config(struct bnx2x *bp,
  343. struct bnx2x_virtf *vf, int qid,
  344. struct bnx2x_vf_mac_vlan_filter *filter,
  345. bool drv_only)
  346. {
  347. struct bnx2x_vlan_mac_ramrod_params ramrod;
  348. int rc;
  349. DP(BNX2X_MSG_IOV, "vf[%d] - %s a %s filter\n",
  350. vf->abs_vfid, filter->add ? "Adding" : "Deleting",
  351. filter->type == BNX2X_VF_FILTER_MAC ? "MAC" : "VLAN");
  352. /* Prepare ramrod params */
  353. memset(&ramrod, 0, sizeof(struct bnx2x_vlan_mac_ramrod_params));
  354. if (filter->type == BNX2X_VF_FILTER_VLAN) {
  355. set_bit(BNX2X_DONT_CONSUME_CAM_CREDIT,
  356. &ramrod.user_req.vlan_mac_flags);
  357. ramrod.vlan_mac_obj = &bnx2x_vfq(vf, qid, vlan_obj);
  358. ramrod.user_req.u.vlan.vlan = filter->vid;
  359. } else {
  360. set_bit(BNX2X_ETH_MAC, &ramrod.user_req.vlan_mac_flags);
  361. ramrod.vlan_mac_obj = &bnx2x_vfq(vf, qid, mac_obj);
  362. memcpy(&ramrod.user_req.u.mac.mac, filter->mac, ETH_ALEN);
  363. }
  364. ramrod.user_req.cmd = filter->add ? BNX2X_VLAN_MAC_ADD :
  365. BNX2X_VLAN_MAC_DEL;
  366. /* Verify there are available vlan credits */
  367. if (filter->add && filter->type == BNX2X_VF_FILTER_VLAN &&
  368. (atomic_read(&bnx2x_vfq(vf, qid, vlan_count)) >=
  369. vf_vlan_rules_cnt(vf))) {
  370. BNX2X_ERR("No credits for vlan [%d >= %d]\n",
  371. atomic_read(&bnx2x_vfq(vf, qid, vlan_count)),
  372. vf_vlan_rules_cnt(vf));
  373. return -ENOMEM;
  374. }
  375. set_bit(RAMROD_EXEC, &ramrod.ramrod_flags);
  376. if (drv_only)
  377. set_bit(RAMROD_DRV_CLR_ONLY, &ramrod.ramrod_flags);
  378. else
  379. set_bit(RAMROD_COMP_WAIT, &ramrod.ramrod_flags);
  380. /* Add/Remove the filter */
  381. rc = bnx2x_config_vlan_mac(bp, &ramrod);
  382. if (rc && rc != -EEXIST) {
  383. BNX2X_ERR("Failed to %s %s\n",
  384. filter->add ? "add" : "delete",
  385. filter->type == BNX2X_VF_FILTER_MAC ? "MAC" :
  386. "VLAN");
  387. return rc;
  388. }
  389. /* Update the vlan counters */
  390. if (filter->type == BNX2X_VF_FILTER_VLAN)
  391. bnx2x_vf_vlan_credit(bp, ramrod.vlan_mac_obj,
  392. &bnx2x_vfq(vf, qid, vlan_count));
  393. return 0;
  394. }
  395. int bnx2x_vf_mac_vlan_config_list(struct bnx2x *bp, struct bnx2x_virtf *vf,
  396. struct bnx2x_vf_mac_vlan_filters *filters,
  397. int qid, bool drv_only)
  398. {
  399. int rc = 0, i;
  400. DP(BNX2X_MSG_IOV, "vf[%d]\n", vf->abs_vfid);
  401. if (!bnx2x_validate_vf_sp_objs(bp, vf, true))
  402. return -EINVAL;
  403. /* Prepare ramrod params */
  404. for (i = 0; i < filters->count; i++) {
  405. rc = bnx2x_vf_mac_vlan_config(bp, vf, qid,
  406. &filters->filters[i], drv_only);
  407. if (rc)
  408. break;
  409. }
  410. /* Rollback if needed */
  411. if (i != filters->count) {
  412. BNX2X_ERR("Managed only %d/%d filters - rolling back\n",
  413. i, filters->count + 1);
  414. while (--i >= 0) {
  415. filters->filters[i].add = !filters->filters[i].add;
  416. bnx2x_vf_mac_vlan_config(bp, vf, qid,
  417. &filters->filters[i],
  418. drv_only);
  419. }
  420. }
  421. /* It's our responsibility to free the filters */
  422. kfree(filters);
  423. return rc;
  424. }
  425. int bnx2x_vf_queue_setup(struct bnx2x *bp, struct bnx2x_virtf *vf, int qid,
  426. struct bnx2x_vf_queue_construct_params *qctor)
  427. {
  428. int rc;
  429. DP(BNX2X_MSG_IOV, "vf[%d:%d]\n", vf->abs_vfid, qid);
  430. rc = bnx2x_vf_queue_create(bp, vf, qid, qctor);
  431. if (rc)
  432. goto op_err;
  433. /* Configure vlan0 for leading queue */
  434. if (!qid) {
  435. struct bnx2x_vf_mac_vlan_filter filter;
  436. memset(&filter, 0, sizeof(struct bnx2x_vf_mac_vlan_filter));
  437. filter.type = BNX2X_VF_FILTER_VLAN;
  438. filter.add = true;
  439. filter.vid = 0;
  440. rc = bnx2x_vf_mac_vlan_config(bp, vf, qid, &filter, false);
  441. if (rc)
  442. goto op_err;
  443. }
  444. /* Schedule the configuration of any pending vlan filters */
  445. vf->cfg_flags |= VF_CFG_VLAN;
  446. bnx2x_schedule_sp_rtnl(bp, BNX2X_SP_RTNL_HYPERVISOR_VLAN,
  447. BNX2X_MSG_IOV);
  448. return 0;
  449. op_err:
  450. BNX2X_ERR("QSETUP[%d:%d] error: rc %d\n", vf->abs_vfid, qid, rc);
  451. return rc;
  452. }
  453. static int bnx2x_vf_queue_flr(struct bnx2x *bp, struct bnx2x_virtf *vf,
  454. int qid)
  455. {
  456. int rc;
  457. DP(BNX2X_MSG_IOV, "vf[%d:%d]\n", vf->abs_vfid, qid);
  458. /* If needed, clean the filtering data base */
  459. if ((qid == LEADING_IDX) &&
  460. bnx2x_validate_vf_sp_objs(bp, vf, false)) {
  461. rc = bnx2x_vf_vlan_mac_clear(bp, vf, qid, true, false);
  462. if (rc)
  463. goto op_err;
  464. rc = bnx2x_vf_vlan_mac_clear(bp, vf, qid, true, true);
  465. if (rc)
  466. goto op_err;
  467. }
  468. /* Terminate queue */
  469. if (bnx2x_vfq(vf, qid, sp_obj).state != BNX2X_Q_STATE_RESET) {
  470. struct bnx2x_queue_state_params qstate;
  471. memset(&qstate, 0, sizeof(struct bnx2x_queue_state_params));
  472. qstate.q_obj = &bnx2x_vfq(vf, qid, sp_obj);
  473. qstate.q_obj->state = BNX2X_Q_STATE_STOPPED;
  474. qstate.cmd = BNX2X_Q_CMD_TERMINATE;
  475. set_bit(RAMROD_COMP_WAIT, &qstate.ramrod_flags);
  476. rc = bnx2x_queue_state_change(bp, &qstate);
  477. if (rc)
  478. goto op_err;
  479. }
  480. return 0;
  481. op_err:
  482. BNX2X_ERR("vf[%d:%d] error: rc %d\n", vf->abs_vfid, qid, rc);
  483. return rc;
  484. }
  485. int bnx2x_vf_mcast(struct bnx2x *bp, struct bnx2x_virtf *vf,
  486. bnx2x_mac_addr_t *mcasts, int mc_num, bool drv_only)
  487. {
  488. struct bnx2x_mcast_list_elem *mc = NULL;
  489. struct bnx2x_mcast_ramrod_params mcast;
  490. int rc, i;
  491. DP(BNX2X_MSG_IOV, "vf[%d]\n", vf->abs_vfid);
  492. /* Prepare Multicast command */
  493. memset(&mcast, 0, sizeof(struct bnx2x_mcast_ramrod_params));
  494. mcast.mcast_obj = &vf->mcast_obj;
  495. if (drv_only)
  496. set_bit(RAMROD_DRV_CLR_ONLY, &mcast.ramrod_flags);
  497. else
  498. set_bit(RAMROD_COMP_WAIT, &mcast.ramrod_flags);
  499. if (mc_num) {
  500. mc = kzalloc(mc_num * sizeof(struct bnx2x_mcast_list_elem),
  501. GFP_KERNEL);
  502. if (!mc) {
  503. BNX2X_ERR("Cannot Configure mulicasts due to lack of memory\n");
  504. return -ENOMEM;
  505. }
  506. }
  507. /* clear existing mcasts */
  508. mcast.mcast_list_len = vf->mcast_list_len;
  509. vf->mcast_list_len = mc_num;
  510. rc = bnx2x_config_mcast(bp, &mcast, BNX2X_MCAST_CMD_DEL);
  511. if (rc) {
  512. BNX2X_ERR("Failed to remove multicasts\n");
  513. if (mc)
  514. kfree(mc);
  515. return rc;
  516. }
  517. /* update mcast list on the ramrod params */
  518. if (mc_num) {
  519. INIT_LIST_HEAD(&mcast.mcast_list);
  520. for (i = 0; i < mc_num; i++) {
  521. mc[i].mac = mcasts[i];
  522. list_add_tail(&mc[i].link,
  523. &mcast.mcast_list);
  524. }
  525. /* add new mcasts */
  526. mcast.mcast_list_len = mc_num;
  527. rc = bnx2x_config_mcast(bp, &mcast, BNX2X_MCAST_CMD_ADD);
  528. if (rc)
  529. BNX2X_ERR("Faled to add multicasts\n");
  530. kfree(mc);
  531. }
  532. return rc;
  533. }
  534. static void bnx2x_vf_prep_rx_mode(struct bnx2x *bp, u8 qid,
  535. struct bnx2x_rx_mode_ramrod_params *ramrod,
  536. struct bnx2x_virtf *vf,
  537. unsigned long accept_flags)
  538. {
  539. struct bnx2x_vf_queue *vfq = vfq_get(vf, qid);
  540. memset(ramrod, 0, sizeof(*ramrod));
  541. ramrod->cid = vfq->cid;
  542. ramrod->cl_id = vfq_cl_id(vf, vfq);
  543. ramrod->rx_mode_obj = &bp->rx_mode_obj;
  544. ramrod->func_id = FW_VF_HANDLE(vf->abs_vfid);
  545. ramrod->rx_accept_flags = accept_flags;
  546. ramrod->tx_accept_flags = accept_flags;
  547. ramrod->pstate = &vf->filter_state;
  548. ramrod->state = BNX2X_FILTER_RX_MODE_PENDING;
  549. set_bit(BNX2X_FILTER_RX_MODE_PENDING, &vf->filter_state);
  550. set_bit(RAMROD_RX, &ramrod->ramrod_flags);
  551. set_bit(RAMROD_TX, &ramrod->ramrod_flags);
  552. ramrod->rdata = bnx2x_vf_sp(bp, vf, rx_mode_rdata.e2);
  553. ramrod->rdata_mapping = bnx2x_vf_sp_map(bp, vf, rx_mode_rdata.e2);
  554. }
  555. int bnx2x_vf_rxmode(struct bnx2x *bp, struct bnx2x_virtf *vf,
  556. int qid, unsigned long accept_flags)
  557. {
  558. struct bnx2x_rx_mode_ramrod_params ramrod;
  559. DP(BNX2X_MSG_IOV, "vf[%d]\n", vf->abs_vfid);
  560. bnx2x_vf_prep_rx_mode(bp, qid, &ramrod, vf, accept_flags);
  561. set_bit(RAMROD_COMP_WAIT, &ramrod.ramrod_flags);
  562. vfq_get(vf, qid)->accept_flags = ramrod.rx_accept_flags;
  563. return bnx2x_config_rx_mode(bp, &ramrod);
  564. }
  565. int bnx2x_vf_queue_teardown(struct bnx2x *bp, struct bnx2x_virtf *vf, int qid)
  566. {
  567. int rc;
  568. DP(BNX2X_MSG_IOV, "vf[%d:%d]\n", vf->abs_vfid, qid);
  569. /* Remove all classification configuration for leading queue */
  570. if (qid == LEADING_IDX) {
  571. rc = bnx2x_vf_rxmode(bp, vf, qid, 0);
  572. if (rc)
  573. goto op_err;
  574. /* Remove filtering if feasible */
  575. if (bnx2x_validate_vf_sp_objs(bp, vf, true)) {
  576. rc = bnx2x_vf_vlan_mac_clear(bp, vf, qid,
  577. false, false);
  578. if (rc)
  579. goto op_err;
  580. rc = bnx2x_vf_vlan_mac_clear(bp, vf, qid,
  581. false, true);
  582. if (rc)
  583. goto op_err;
  584. rc = bnx2x_vf_mcast(bp, vf, NULL, 0, false);
  585. if (rc)
  586. goto op_err;
  587. }
  588. }
  589. /* Destroy queue */
  590. rc = bnx2x_vf_queue_destroy(bp, vf, qid);
  591. if (rc)
  592. goto op_err;
  593. return rc;
  594. op_err:
  595. BNX2X_ERR("vf[%d:%d] error: rc %d\n",
  596. vf->abs_vfid, qid, rc);
  597. return rc;
  598. }
  599. /* VF enable primitives
  600. * when pretend is required the caller is responsible
  601. * for calling pretend prior to calling these routines
  602. */
  603. /* internal vf enable - until vf is enabled internally all transactions
  604. * are blocked. This routine should always be called last with pretend.
  605. */
  606. static void bnx2x_vf_enable_internal(struct bnx2x *bp, u8 enable)
  607. {
  608. REG_WR(bp, PGLUE_B_REG_INTERNAL_VFID_ENABLE, enable ? 1 : 0);
  609. }
  610. /* clears vf error in all semi blocks */
  611. static void bnx2x_vf_semi_clear_err(struct bnx2x *bp, u8 abs_vfid)
  612. {
  613. REG_WR(bp, TSEM_REG_VFPF_ERR_NUM, abs_vfid);
  614. REG_WR(bp, USEM_REG_VFPF_ERR_NUM, abs_vfid);
  615. REG_WR(bp, CSEM_REG_VFPF_ERR_NUM, abs_vfid);
  616. REG_WR(bp, XSEM_REG_VFPF_ERR_NUM, abs_vfid);
  617. }
  618. static void bnx2x_vf_pglue_clear_err(struct bnx2x *bp, u8 abs_vfid)
  619. {
  620. u32 was_err_group = (2 * BP_PATH(bp) + abs_vfid) >> 5;
  621. u32 was_err_reg = 0;
  622. switch (was_err_group) {
  623. case 0:
  624. was_err_reg = PGLUE_B_REG_WAS_ERROR_VF_31_0_CLR;
  625. break;
  626. case 1:
  627. was_err_reg = PGLUE_B_REG_WAS_ERROR_VF_63_32_CLR;
  628. break;
  629. case 2:
  630. was_err_reg = PGLUE_B_REG_WAS_ERROR_VF_95_64_CLR;
  631. break;
  632. case 3:
  633. was_err_reg = PGLUE_B_REG_WAS_ERROR_VF_127_96_CLR;
  634. break;
  635. }
  636. REG_WR(bp, was_err_reg, 1 << (abs_vfid & 0x1f));
  637. }
  638. static void bnx2x_vf_igu_reset(struct bnx2x *bp, struct bnx2x_virtf *vf)
  639. {
  640. int i;
  641. u32 val;
  642. /* Set VF masks and configuration - pretend */
  643. bnx2x_pretend_func(bp, HW_VF_HANDLE(bp, vf->abs_vfid));
  644. REG_WR(bp, IGU_REG_SB_INT_BEFORE_MASK_LSB, 0);
  645. REG_WR(bp, IGU_REG_SB_INT_BEFORE_MASK_MSB, 0);
  646. REG_WR(bp, IGU_REG_SB_MASK_LSB, 0);
  647. REG_WR(bp, IGU_REG_SB_MASK_MSB, 0);
  648. REG_WR(bp, IGU_REG_PBA_STATUS_LSB, 0);
  649. REG_WR(bp, IGU_REG_PBA_STATUS_MSB, 0);
  650. val = REG_RD(bp, IGU_REG_VF_CONFIGURATION);
  651. val |= (IGU_VF_CONF_FUNC_EN | IGU_VF_CONF_MSI_MSIX_EN);
  652. if (vf->cfg_flags & VF_CFG_INT_SIMD)
  653. val |= IGU_VF_CONF_SINGLE_ISR_EN;
  654. val &= ~IGU_VF_CONF_PARENT_MASK;
  655. val |= (BP_ABS_FUNC(bp) >> 1) << IGU_VF_CONF_PARENT_SHIFT;
  656. REG_WR(bp, IGU_REG_VF_CONFIGURATION, val);
  657. DP(BNX2X_MSG_IOV,
  658. "value in IGU_REG_VF_CONFIGURATION of vf %d after write is 0x%08x\n",
  659. vf->abs_vfid, val);
  660. bnx2x_pretend_func(bp, BP_ABS_FUNC(bp));
  661. /* iterate over all queues, clear sb consumer */
  662. for (i = 0; i < vf_sb_count(vf); i++) {
  663. u8 igu_sb_id = vf_igu_sb(vf, i);
  664. /* zero prod memory */
  665. REG_WR(bp, IGU_REG_PROD_CONS_MEMORY + igu_sb_id * 4, 0);
  666. /* clear sb state machine */
  667. bnx2x_igu_clear_sb_gen(bp, vf->abs_vfid, igu_sb_id,
  668. false /* VF */);
  669. /* disable + update */
  670. bnx2x_vf_igu_ack_sb(bp, vf, igu_sb_id, USTORM_ID, 0,
  671. IGU_INT_DISABLE, 1);
  672. }
  673. }
  674. void bnx2x_vf_enable_access(struct bnx2x *bp, u8 abs_vfid)
  675. {
  676. /* set the VF-PF association in the FW */
  677. storm_memset_vf_to_pf(bp, FW_VF_HANDLE(abs_vfid), BP_FUNC(bp));
  678. storm_memset_func_en(bp, FW_VF_HANDLE(abs_vfid), 1);
  679. /* clear vf errors*/
  680. bnx2x_vf_semi_clear_err(bp, abs_vfid);
  681. bnx2x_vf_pglue_clear_err(bp, abs_vfid);
  682. /* internal vf-enable - pretend */
  683. bnx2x_pretend_func(bp, HW_VF_HANDLE(bp, abs_vfid));
  684. DP(BNX2X_MSG_IOV, "enabling internal access for vf %x\n", abs_vfid);
  685. bnx2x_vf_enable_internal(bp, true);
  686. bnx2x_pretend_func(bp, BP_ABS_FUNC(bp));
  687. }
  688. static void bnx2x_vf_enable_traffic(struct bnx2x *bp, struct bnx2x_virtf *vf)
  689. {
  690. /* Reset vf in IGU interrupts are still disabled */
  691. bnx2x_vf_igu_reset(bp, vf);
  692. /* pretend to enable the vf with the PBF */
  693. bnx2x_pretend_func(bp, HW_VF_HANDLE(bp, vf->abs_vfid));
  694. REG_WR(bp, PBF_REG_DISABLE_VF, 0);
  695. bnx2x_pretend_func(bp, BP_ABS_FUNC(bp));
  696. }
  697. static u8 bnx2x_vf_is_pcie_pending(struct bnx2x *bp, u8 abs_vfid)
  698. {
  699. struct pci_dev *dev;
  700. struct bnx2x_virtf *vf = bnx2x_vf_by_abs_fid(bp, abs_vfid);
  701. if (!vf)
  702. return false;
  703. dev = pci_get_bus_and_slot(vf->bus, vf->devfn);
  704. if (dev)
  705. return bnx2x_is_pcie_pending(dev);
  706. return false;
  707. }
  708. int bnx2x_vf_flr_clnup_epilog(struct bnx2x *bp, u8 abs_vfid)
  709. {
  710. /* Verify no pending pci transactions */
  711. if (bnx2x_vf_is_pcie_pending(bp, abs_vfid))
  712. BNX2X_ERR("PCIE Transactions still pending\n");
  713. return 0;
  714. }
  715. static void bnx2x_iov_re_set_vlan_filters(struct bnx2x *bp,
  716. struct bnx2x_virtf *vf,
  717. int new)
  718. {
  719. int num = vf_vlan_rules_cnt(vf);
  720. int diff = new - num;
  721. bool rc = true;
  722. DP(BNX2X_MSG_IOV, "vf[%d] - %d vlan filter credits [previously %d]\n",
  723. vf->abs_vfid, new, num);
  724. if (diff > 0)
  725. rc = bp->vlans_pool.get(&bp->vlans_pool, diff);
  726. else if (diff < 0)
  727. rc = bp->vlans_pool.put(&bp->vlans_pool, -diff);
  728. if (rc)
  729. vf_vlan_rules_cnt(vf) = new;
  730. else
  731. DP(BNX2X_MSG_IOV, "vf[%d] - Failed to configure vlan filter credits change\n",
  732. vf->abs_vfid);
  733. }
  734. /* must be called after the number of PF queues and the number of VFs are
  735. * both known
  736. */
  737. static void
  738. bnx2x_iov_static_resc(struct bnx2x *bp, struct bnx2x_virtf *vf)
  739. {
  740. struct vf_pf_resc_request *resc = &vf->alloc_resc;
  741. u16 vlan_count = 0;
  742. /* will be set only during VF-ACQUIRE */
  743. resc->num_rxqs = 0;
  744. resc->num_txqs = 0;
  745. /* no credit calculations for macs (just yet) */
  746. resc->num_mac_filters = 1;
  747. /* divvy up vlan rules */
  748. bnx2x_iov_re_set_vlan_filters(bp, vf, 0);
  749. vlan_count = bp->vlans_pool.check(&bp->vlans_pool);
  750. vlan_count = 1 << ilog2(vlan_count);
  751. bnx2x_iov_re_set_vlan_filters(bp, vf,
  752. vlan_count / BNX2X_NR_VIRTFN(bp));
  753. /* no real limitation */
  754. resc->num_mc_filters = 0;
  755. /* num_sbs already set */
  756. resc->num_sbs = vf->sb_count;
  757. }
  758. /* FLR routines: */
  759. static void bnx2x_vf_free_resc(struct bnx2x *bp, struct bnx2x_virtf *vf)
  760. {
  761. /* reset the state variables */
  762. bnx2x_iov_static_resc(bp, vf);
  763. vf->state = VF_FREE;
  764. }
  765. static void bnx2x_vf_flr_clnup_hw(struct bnx2x *bp, struct bnx2x_virtf *vf)
  766. {
  767. u32 poll_cnt = bnx2x_flr_clnup_poll_count(bp);
  768. /* DQ usage counter */
  769. bnx2x_pretend_func(bp, HW_VF_HANDLE(bp, vf->abs_vfid));
  770. bnx2x_flr_clnup_poll_hw_counter(bp, DORQ_REG_VF_USAGE_CNT,
  771. "DQ VF usage counter timed out",
  772. poll_cnt);
  773. bnx2x_pretend_func(bp, BP_ABS_FUNC(bp));
  774. /* FW cleanup command - poll for the results */
  775. if (bnx2x_send_final_clnup(bp, (u8)FW_VF_HANDLE(vf->abs_vfid),
  776. poll_cnt))
  777. BNX2X_ERR("VF[%d] Final cleanup timed-out\n", vf->abs_vfid);
  778. /* verify TX hw is flushed */
  779. bnx2x_tx_hw_flushed(bp, poll_cnt);
  780. }
  781. static void bnx2x_vf_flr(struct bnx2x *bp, struct bnx2x_virtf *vf)
  782. {
  783. int rc, i;
  784. DP(BNX2X_MSG_IOV, "vf[%d]\n", vf->abs_vfid);
  785. /* the cleanup operations are valid if and only if the VF
  786. * was first acquired.
  787. */
  788. for (i = 0; i < vf_rxq_count(vf); i++) {
  789. rc = bnx2x_vf_queue_flr(bp, vf, i);
  790. if (rc)
  791. goto out;
  792. }
  793. /* remove multicasts */
  794. bnx2x_vf_mcast(bp, vf, NULL, 0, true);
  795. /* dispatch final cleanup and wait for HW queues to flush */
  796. bnx2x_vf_flr_clnup_hw(bp, vf);
  797. /* release VF resources */
  798. bnx2x_vf_free_resc(bp, vf);
  799. /* re-open the mailbox */
  800. bnx2x_vf_enable_mbx(bp, vf->abs_vfid);
  801. return;
  802. out:
  803. BNX2X_ERR("vf[%d:%d] failed flr: rc %d\n",
  804. vf->abs_vfid, i, rc);
  805. }
  806. static void bnx2x_vf_flr_clnup(struct bnx2x *bp)
  807. {
  808. struct bnx2x_virtf *vf;
  809. int i;
  810. for (i = 0; i < BNX2X_NR_VIRTFN(bp); i++) {
  811. /* VF should be RESET & in FLR cleanup states */
  812. if (bnx2x_vf(bp, i, state) != VF_RESET ||
  813. !bnx2x_vf(bp, i, flr_clnup_stage))
  814. continue;
  815. DP(BNX2X_MSG_IOV, "next vf to cleanup: %d. Num of vfs: %d\n",
  816. i, BNX2X_NR_VIRTFN(bp));
  817. vf = BP_VF(bp, i);
  818. /* lock the vf pf channel */
  819. bnx2x_lock_vf_pf_channel(bp, vf, CHANNEL_TLV_FLR);
  820. /* invoke the VF FLR SM */
  821. bnx2x_vf_flr(bp, vf);
  822. /* mark the VF to be ACKED and continue */
  823. vf->flr_clnup_stage = false;
  824. bnx2x_unlock_vf_pf_channel(bp, vf, CHANNEL_TLV_FLR);
  825. }
  826. /* Acknowledge the handled VFs.
  827. * we are acknowledge all the vfs which an flr was requested for, even
  828. * if amongst them there are such that we never opened, since the mcp
  829. * will interrupt us immediately again if we only ack some of the bits,
  830. * resulting in an endless loop. This can happen for example in KVM
  831. * where an 'all ones' flr request is sometimes given by hyper visor
  832. */
  833. DP(BNX2X_MSG_MCP, "DRV_STATUS_VF_DISABLED ACK for vfs 0x%x 0x%x\n",
  834. bp->vfdb->flrd_vfs[0], bp->vfdb->flrd_vfs[1]);
  835. for (i = 0; i < FLRD_VFS_DWORDS; i++)
  836. SHMEM2_WR(bp, drv_ack_vf_disabled[BP_FW_MB_IDX(bp)][i],
  837. bp->vfdb->flrd_vfs[i]);
  838. bnx2x_fw_command(bp, DRV_MSG_CODE_VF_DISABLED_DONE, 0);
  839. /* clear the acked bits - better yet if the MCP implemented
  840. * write to clear semantics
  841. */
  842. for (i = 0; i < FLRD_VFS_DWORDS; i++)
  843. SHMEM2_WR(bp, drv_ack_vf_disabled[BP_FW_MB_IDX(bp)][i], 0);
  844. }
  845. void bnx2x_vf_handle_flr_event(struct bnx2x *bp)
  846. {
  847. int i;
  848. /* Read FLR'd VFs */
  849. for (i = 0; i < FLRD_VFS_DWORDS; i++)
  850. bp->vfdb->flrd_vfs[i] = SHMEM2_RD(bp, mcp_vf_disabled[i]);
  851. DP(BNX2X_MSG_MCP,
  852. "DRV_STATUS_VF_DISABLED received for vfs 0x%x 0x%x\n",
  853. bp->vfdb->flrd_vfs[0], bp->vfdb->flrd_vfs[1]);
  854. for_each_vf(bp, i) {
  855. struct bnx2x_virtf *vf = BP_VF(bp, i);
  856. u32 reset = 0;
  857. if (vf->abs_vfid < 32)
  858. reset = bp->vfdb->flrd_vfs[0] & (1 << vf->abs_vfid);
  859. else
  860. reset = bp->vfdb->flrd_vfs[1] &
  861. (1 << (vf->abs_vfid - 32));
  862. if (reset) {
  863. /* set as reset and ready for cleanup */
  864. vf->state = VF_RESET;
  865. vf->flr_clnup_stage = true;
  866. DP(BNX2X_MSG_IOV,
  867. "Initiating Final cleanup for VF %d\n",
  868. vf->abs_vfid);
  869. }
  870. }
  871. /* do the FLR cleanup for all marked VFs*/
  872. bnx2x_vf_flr_clnup(bp);
  873. }
  874. /* IOV global initialization routines */
  875. void bnx2x_iov_init_dq(struct bnx2x *bp)
  876. {
  877. if (!IS_SRIOV(bp))
  878. return;
  879. /* Set the DQ such that the CID reflect the abs_vfid */
  880. REG_WR(bp, DORQ_REG_VF_NORM_VF_BASE, 0);
  881. REG_WR(bp, DORQ_REG_MAX_RVFID_SIZE, ilog2(BNX2X_MAX_NUM_OF_VFS));
  882. /* Set VFs starting CID. If its > 0 the preceding CIDs are belong to
  883. * the PF L2 queues
  884. */
  885. REG_WR(bp, DORQ_REG_VF_NORM_CID_BASE, BNX2X_FIRST_VF_CID);
  886. /* The VF window size is the log2 of the max number of CIDs per VF */
  887. REG_WR(bp, DORQ_REG_VF_NORM_CID_WND_SIZE, BNX2X_VF_CID_WND);
  888. /* The VF doorbell size 0 - *B, 4 - 128B. We set it here to match
  889. * the Pf doorbell size although the 2 are independent.
  890. */
  891. REG_WR(bp, DORQ_REG_VF_NORM_CID_OFST, 3);
  892. /* No security checks for now -
  893. * configure single rule (out of 16) mask = 0x1, value = 0x0,
  894. * CID range 0 - 0x1ffff
  895. */
  896. REG_WR(bp, DORQ_REG_VF_TYPE_MASK_0, 1);
  897. REG_WR(bp, DORQ_REG_VF_TYPE_VALUE_0, 0);
  898. REG_WR(bp, DORQ_REG_VF_TYPE_MIN_MCID_0, 0);
  899. REG_WR(bp, DORQ_REG_VF_TYPE_MAX_MCID_0, 0x1ffff);
  900. /* set the VF doorbell threshold. This threshold represents the amount
  901. * of doorbells allowed in the main DORQ fifo for a specific VF.
  902. */
  903. REG_WR(bp, DORQ_REG_VF_USAGE_CT_LIMIT, 64);
  904. }
  905. void bnx2x_iov_init_dmae(struct bnx2x *bp)
  906. {
  907. if (pci_find_ext_capability(bp->pdev, PCI_EXT_CAP_ID_SRIOV))
  908. REG_WR(bp, DMAE_REG_BACKWARD_COMP_EN, 0);
  909. }
  910. static int bnx2x_vf_bus(struct bnx2x *bp, int vfid)
  911. {
  912. struct pci_dev *dev = bp->pdev;
  913. struct bnx2x_sriov *iov = &bp->vfdb->sriov;
  914. return dev->bus->number + ((dev->devfn + iov->offset +
  915. iov->stride * vfid) >> 8);
  916. }
  917. static int bnx2x_vf_devfn(struct bnx2x *bp, int vfid)
  918. {
  919. struct pci_dev *dev = bp->pdev;
  920. struct bnx2x_sriov *iov = &bp->vfdb->sriov;
  921. return (dev->devfn + iov->offset + iov->stride * vfid) & 0xff;
  922. }
  923. static void bnx2x_vf_set_bars(struct bnx2x *bp, struct bnx2x_virtf *vf)
  924. {
  925. int i, n;
  926. struct pci_dev *dev = bp->pdev;
  927. struct bnx2x_sriov *iov = &bp->vfdb->sriov;
  928. for (i = 0, n = 0; i < PCI_SRIOV_NUM_BARS; i += 2, n++) {
  929. u64 start = pci_resource_start(dev, PCI_IOV_RESOURCES + i);
  930. u32 size = pci_resource_len(dev, PCI_IOV_RESOURCES + i);
  931. size /= iov->total;
  932. vf->bars[n].bar = start + size * vf->abs_vfid;
  933. vf->bars[n].size = size;
  934. }
  935. }
  936. static int bnx2x_ari_enabled(struct pci_dev *dev)
  937. {
  938. return dev->bus->self && dev->bus->self->ari_enabled;
  939. }
  940. static void
  941. bnx2x_get_vf_igu_cam_info(struct bnx2x *bp)
  942. {
  943. int sb_id;
  944. u32 val;
  945. u8 fid, current_pf = 0;
  946. /* IGU in normal mode - read CAM */
  947. for (sb_id = 0; sb_id < IGU_REG_MAPPING_MEMORY_SIZE; sb_id++) {
  948. val = REG_RD(bp, IGU_REG_MAPPING_MEMORY + sb_id * 4);
  949. if (!(val & IGU_REG_MAPPING_MEMORY_VALID))
  950. continue;
  951. fid = GET_FIELD((val), IGU_REG_MAPPING_MEMORY_FID);
  952. if (fid & IGU_FID_ENCODE_IS_PF)
  953. current_pf = fid & IGU_FID_PF_NUM_MASK;
  954. else if (current_pf == BP_FUNC(bp))
  955. bnx2x_vf_set_igu_info(bp, sb_id,
  956. (fid & IGU_FID_VF_NUM_MASK));
  957. DP(BNX2X_MSG_IOV, "%s[%d], igu_sb_id=%d, msix=%d\n",
  958. ((fid & IGU_FID_ENCODE_IS_PF) ? "PF" : "VF"),
  959. ((fid & IGU_FID_ENCODE_IS_PF) ? (fid & IGU_FID_PF_NUM_MASK) :
  960. (fid & IGU_FID_VF_NUM_MASK)), sb_id,
  961. GET_FIELD((val), IGU_REG_MAPPING_MEMORY_VECTOR));
  962. }
  963. DP(BNX2X_MSG_IOV, "vf_sbs_pool is %d\n", BP_VFDB(bp)->vf_sbs_pool);
  964. }
  965. static void __bnx2x_iov_free_vfdb(struct bnx2x *bp)
  966. {
  967. if (bp->vfdb) {
  968. kfree(bp->vfdb->vfqs);
  969. kfree(bp->vfdb->vfs);
  970. kfree(bp->vfdb);
  971. }
  972. bp->vfdb = NULL;
  973. }
  974. static int bnx2x_sriov_pci_cfg_info(struct bnx2x *bp, struct bnx2x_sriov *iov)
  975. {
  976. int pos;
  977. struct pci_dev *dev = bp->pdev;
  978. pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_SRIOV);
  979. if (!pos) {
  980. BNX2X_ERR("failed to find SRIOV capability in device\n");
  981. return -ENODEV;
  982. }
  983. iov->pos = pos;
  984. DP(BNX2X_MSG_IOV, "sriov ext pos %d\n", pos);
  985. pci_read_config_word(dev, pos + PCI_SRIOV_CTRL, &iov->ctrl);
  986. pci_read_config_word(dev, pos + PCI_SRIOV_TOTAL_VF, &iov->total);
  987. pci_read_config_word(dev, pos + PCI_SRIOV_INITIAL_VF, &iov->initial);
  988. pci_read_config_word(dev, pos + PCI_SRIOV_VF_OFFSET, &iov->offset);
  989. pci_read_config_word(dev, pos + PCI_SRIOV_VF_STRIDE, &iov->stride);
  990. pci_read_config_dword(dev, pos + PCI_SRIOV_SUP_PGSIZE, &iov->pgsz);
  991. pci_read_config_dword(dev, pos + PCI_SRIOV_CAP, &iov->cap);
  992. pci_read_config_byte(dev, pos + PCI_SRIOV_FUNC_LINK, &iov->link);
  993. return 0;
  994. }
  995. static int bnx2x_sriov_info(struct bnx2x *bp, struct bnx2x_sriov *iov)
  996. {
  997. u32 val;
  998. /* read the SRIOV capability structure
  999. * The fields can be read via configuration read or
  1000. * directly from the device (starting at offset PCICFG_OFFSET)
  1001. */
  1002. if (bnx2x_sriov_pci_cfg_info(bp, iov))
  1003. return -ENODEV;
  1004. /* get the number of SRIOV bars */
  1005. iov->nres = 0;
  1006. /* read the first_vfid */
  1007. val = REG_RD(bp, PCICFG_OFFSET + GRC_CONFIG_REG_PF_INIT_VF);
  1008. iov->first_vf_in_pf = ((val & GRC_CR_PF_INIT_VF_PF_FIRST_VF_NUM_MASK)
  1009. * 8) - (BNX2X_MAX_NUM_OF_VFS * BP_PATH(bp));
  1010. DP(BNX2X_MSG_IOV,
  1011. "IOV info[%d]: first vf %d, nres %d, cap 0x%x, ctrl 0x%x, total %d, initial %d, num vfs %d, offset %d, stride %d, page size 0x%x\n",
  1012. BP_FUNC(bp),
  1013. iov->first_vf_in_pf, iov->nres, iov->cap, iov->ctrl, iov->total,
  1014. iov->initial, iov->nr_virtfn, iov->offset, iov->stride, iov->pgsz);
  1015. return 0;
  1016. }
  1017. /* must be called after PF bars are mapped */
  1018. int bnx2x_iov_init_one(struct bnx2x *bp, int int_mode_param,
  1019. int num_vfs_param)
  1020. {
  1021. int err, i;
  1022. struct bnx2x_sriov *iov;
  1023. struct pci_dev *dev = bp->pdev;
  1024. bp->vfdb = NULL;
  1025. /* verify is pf */
  1026. if (IS_VF(bp))
  1027. return 0;
  1028. /* verify sriov capability is present in configuration space */
  1029. if (!pci_find_ext_capability(dev, PCI_EXT_CAP_ID_SRIOV))
  1030. return 0;
  1031. /* verify chip revision */
  1032. if (CHIP_IS_E1x(bp))
  1033. return 0;
  1034. /* check if SRIOV support is turned off */
  1035. if (!num_vfs_param)
  1036. return 0;
  1037. /* SRIOV assumes that num of PF CIDs < BNX2X_FIRST_VF_CID */
  1038. if (BNX2X_L2_MAX_CID(bp) >= BNX2X_FIRST_VF_CID) {
  1039. BNX2X_ERR("PF cids %d are overspilling into vf space (starts at %d). Abort SRIOV\n",
  1040. BNX2X_L2_MAX_CID(bp), BNX2X_FIRST_VF_CID);
  1041. return 0;
  1042. }
  1043. /* SRIOV can be enabled only with MSIX */
  1044. if (int_mode_param == BNX2X_INT_MODE_MSI ||
  1045. int_mode_param == BNX2X_INT_MODE_INTX) {
  1046. BNX2X_ERR("Forced MSI/INTx mode is incompatible with SRIOV\n");
  1047. return 0;
  1048. }
  1049. err = -EIO;
  1050. /* verify ari is enabled */
  1051. if (!bnx2x_ari_enabled(bp->pdev)) {
  1052. BNX2X_ERR("ARI not supported (check pci bridge ARI forwarding), SRIOV can not be enabled\n");
  1053. return 0;
  1054. }
  1055. /* verify igu is in normal mode */
  1056. if (CHIP_INT_MODE_IS_BC(bp)) {
  1057. BNX2X_ERR("IGU not normal mode, SRIOV can not be enabled\n");
  1058. return 0;
  1059. }
  1060. /* allocate the vfs database */
  1061. bp->vfdb = kzalloc(sizeof(*(bp->vfdb)), GFP_KERNEL);
  1062. if (!bp->vfdb) {
  1063. BNX2X_ERR("failed to allocate vf database\n");
  1064. err = -ENOMEM;
  1065. goto failed;
  1066. }
  1067. /* get the sriov info - Linux already collected all the pertinent
  1068. * information, however the sriov structure is for the private use
  1069. * of the pci module. Also we want this information regardless
  1070. * of the hyper-visor.
  1071. */
  1072. iov = &(bp->vfdb->sriov);
  1073. err = bnx2x_sriov_info(bp, iov);
  1074. if (err)
  1075. goto failed;
  1076. /* SR-IOV capability was enabled but there are no VFs*/
  1077. if (iov->total == 0)
  1078. goto failed;
  1079. iov->nr_virtfn = min_t(u16, iov->total, num_vfs_param);
  1080. DP(BNX2X_MSG_IOV, "num_vfs_param was %d, nr_virtfn was %d\n",
  1081. num_vfs_param, iov->nr_virtfn);
  1082. /* allocate the vf array */
  1083. bp->vfdb->vfs = kzalloc(sizeof(struct bnx2x_virtf) *
  1084. BNX2X_NR_VIRTFN(bp), GFP_KERNEL);
  1085. if (!bp->vfdb->vfs) {
  1086. BNX2X_ERR("failed to allocate vf array\n");
  1087. err = -ENOMEM;
  1088. goto failed;
  1089. }
  1090. /* Initial VF init - index and abs_vfid - nr_virtfn must be set */
  1091. for_each_vf(bp, i) {
  1092. bnx2x_vf(bp, i, index) = i;
  1093. bnx2x_vf(bp, i, abs_vfid) = iov->first_vf_in_pf + i;
  1094. bnx2x_vf(bp, i, state) = VF_FREE;
  1095. mutex_init(&bnx2x_vf(bp, i, op_mutex));
  1096. bnx2x_vf(bp, i, op_current) = CHANNEL_TLV_NONE;
  1097. }
  1098. /* re-read the IGU CAM for VFs - index and abs_vfid must be set */
  1099. bnx2x_get_vf_igu_cam_info(bp);
  1100. /* allocate the queue arrays for all VFs */
  1101. bp->vfdb->vfqs = kzalloc(
  1102. BNX2X_MAX_NUM_VF_QUEUES * sizeof(struct bnx2x_vf_queue),
  1103. GFP_KERNEL);
  1104. DP(BNX2X_MSG_IOV, "bp->vfdb->vfqs was %p\n", bp->vfdb->vfqs);
  1105. if (!bp->vfdb->vfqs) {
  1106. BNX2X_ERR("failed to allocate vf queue array\n");
  1107. err = -ENOMEM;
  1108. goto failed;
  1109. }
  1110. /* Prepare the VFs event synchronization mechanism */
  1111. mutex_init(&bp->vfdb->event_mutex);
  1112. return 0;
  1113. failed:
  1114. DP(BNX2X_MSG_IOV, "Failed err=%d\n", err);
  1115. __bnx2x_iov_free_vfdb(bp);
  1116. return err;
  1117. }
  1118. void bnx2x_iov_remove_one(struct bnx2x *bp)
  1119. {
  1120. int vf_idx;
  1121. /* if SRIOV is not enabled there's nothing to do */
  1122. if (!IS_SRIOV(bp))
  1123. return;
  1124. DP(BNX2X_MSG_IOV, "about to call disable sriov\n");
  1125. pci_disable_sriov(bp->pdev);
  1126. DP(BNX2X_MSG_IOV, "sriov disabled\n");
  1127. /* disable access to all VFs */
  1128. for (vf_idx = 0; vf_idx < bp->vfdb->sriov.total; vf_idx++) {
  1129. bnx2x_pretend_func(bp,
  1130. HW_VF_HANDLE(bp,
  1131. bp->vfdb->sriov.first_vf_in_pf +
  1132. vf_idx));
  1133. DP(BNX2X_MSG_IOV, "disabling internal access for vf %d\n",
  1134. bp->vfdb->sriov.first_vf_in_pf + vf_idx);
  1135. bnx2x_vf_enable_internal(bp, 0);
  1136. bnx2x_pretend_func(bp, BP_ABS_FUNC(bp));
  1137. }
  1138. /* free vf database */
  1139. __bnx2x_iov_free_vfdb(bp);
  1140. }
  1141. void bnx2x_iov_free_mem(struct bnx2x *bp)
  1142. {
  1143. int i;
  1144. if (!IS_SRIOV(bp))
  1145. return;
  1146. /* free vfs hw contexts */
  1147. for (i = 0; i < BNX2X_VF_CIDS/ILT_PAGE_CIDS; i++) {
  1148. struct hw_dma *cxt = &bp->vfdb->context[i];
  1149. BNX2X_PCI_FREE(cxt->addr, cxt->mapping, cxt->size);
  1150. }
  1151. BNX2X_PCI_FREE(BP_VFDB(bp)->sp_dma.addr,
  1152. BP_VFDB(bp)->sp_dma.mapping,
  1153. BP_VFDB(bp)->sp_dma.size);
  1154. BNX2X_PCI_FREE(BP_VF_MBX_DMA(bp)->addr,
  1155. BP_VF_MBX_DMA(bp)->mapping,
  1156. BP_VF_MBX_DMA(bp)->size);
  1157. BNX2X_PCI_FREE(BP_VF_BULLETIN_DMA(bp)->addr,
  1158. BP_VF_BULLETIN_DMA(bp)->mapping,
  1159. BP_VF_BULLETIN_DMA(bp)->size);
  1160. }
  1161. int bnx2x_iov_alloc_mem(struct bnx2x *bp)
  1162. {
  1163. size_t tot_size;
  1164. int i, rc = 0;
  1165. if (!IS_SRIOV(bp))
  1166. return rc;
  1167. /* allocate vfs hw contexts */
  1168. tot_size = (BP_VFDB(bp)->sriov.first_vf_in_pf + BNX2X_NR_VIRTFN(bp)) *
  1169. BNX2X_CIDS_PER_VF * sizeof(union cdu_context);
  1170. for (i = 0; i < BNX2X_VF_CIDS/ILT_PAGE_CIDS; i++) {
  1171. struct hw_dma *cxt = BP_VF_CXT_PAGE(bp, i);
  1172. cxt->size = min_t(size_t, tot_size, CDU_ILT_PAGE_SZ);
  1173. if (cxt->size) {
  1174. cxt->addr = BNX2X_PCI_ALLOC(&cxt->mapping, cxt->size);
  1175. if (!cxt->addr)
  1176. goto alloc_mem_err;
  1177. } else {
  1178. cxt->addr = NULL;
  1179. cxt->mapping = 0;
  1180. }
  1181. tot_size -= cxt->size;
  1182. }
  1183. /* allocate vfs ramrods dma memory - client_init and set_mac */
  1184. tot_size = BNX2X_NR_VIRTFN(bp) * sizeof(struct bnx2x_vf_sp);
  1185. BP_VFDB(bp)->sp_dma.addr = BNX2X_PCI_ALLOC(&BP_VFDB(bp)->sp_dma.mapping,
  1186. tot_size);
  1187. if (!BP_VFDB(bp)->sp_dma.addr)
  1188. goto alloc_mem_err;
  1189. BP_VFDB(bp)->sp_dma.size = tot_size;
  1190. /* allocate mailboxes */
  1191. tot_size = BNX2X_NR_VIRTFN(bp) * MBX_MSG_ALIGNED_SIZE;
  1192. BP_VF_MBX_DMA(bp)->addr = BNX2X_PCI_ALLOC(&BP_VF_MBX_DMA(bp)->mapping,
  1193. tot_size);
  1194. if (!BP_VF_MBX_DMA(bp)->addr)
  1195. goto alloc_mem_err;
  1196. BP_VF_MBX_DMA(bp)->size = tot_size;
  1197. /* allocate local bulletin boards */
  1198. tot_size = BNX2X_NR_VIRTFN(bp) * BULLETIN_CONTENT_SIZE;
  1199. BP_VF_BULLETIN_DMA(bp)->addr = BNX2X_PCI_ALLOC(&BP_VF_BULLETIN_DMA(bp)->mapping,
  1200. tot_size);
  1201. if (!BP_VF_BULLETIN_DMA(bp)->addr)
  1202. goto alloc_mem_err;
  1203. BP_VF_BULLETIN_DMA(bp)->size = tot_size;
  1204. return 0;
  1205. alloc_mem_err:
  1206. return -ENOMEM;
  1207. }
  1208. static void bnx2x_vfq_init(struct bnx2x *bp, struct bnx2x_virtf *vf,
  1209. struct bnx2x_vf_queue *q)
  1210. {
  1211. u8 cl_id = vfq_cl_id(vf, q);
  1212. u8 func_id = FW_VF_HANDLE(vf->abs_vfid);
  1213. unsigned long q_type = 0;
  1214. set_bit(BNX2X_Q_TYPE_HAS_TX, &q_type);
  1215. set_bit(BNX2X_Q_TYPE_HAS_RX, &q_type);
  1216. /* Queue State object */
  1217. bnx2x_init_queue_obj(bp, &q->sp_obj,
  1218. cl_id, &q->cid, 1, func_id,
  1219. bnx2x_vf_sp(bp, vf, q_data),
  1220. bnx2x_vf_sp_map(bp, vf, q_data),
  1221. q_type);
  1222. /* sp indication is set only when vlan/mac/etc. are initialized */
  1223. q->sp_initialized = false;
  1224. DP(BNX2X_MSG_IOV,
  1225. "initialized vf %d's queue object. func id set to %d. cid set to 0x%x\n",
  1226. vf->abs_vfid, q->sp_obj.func_id, q->cid);
  1227. }
  1228. /* called by bnx2x_nic_load */
  1229. int bnx2x_iov_nic_init(struct bnx2x *bp)
  1230. {
  1231. int vfid;
  1232. if (!IS_SRIOV(bp)) {
  1233. DP(BNX2X_MSG_IOV, "vfdb was not allocated\n");
  1234. return 0;
  1235. }
  1236. DP(BNX2X_MSG_IOV, "num of vfs: %d\n", (bp)->vfdb->sriov.nr_virtfn);
  1237. /* let FLR complete ... */
  1238. msleep(100);
  1239. /* initialize vf database */
  1240. for_each_vf(bp, vfid) {
  1241. struct bnx2x_virtf *vf = BP_VF(bp, vfid);
  1242. int base_vf_cid = (BP_VFDB(bp)->sriov.first_vf_in_pf + vfid) *
  1243. BNX2X_CIDS_PER_VF;
  1244. union cdu_context *base_cxt = (union cdu_context *)
  1245. BP_VF_CXT_PAGE(bp, base_vf_cid/ILT_PAGE_CIDS)->addr +
  1246. (base_vf_cid & (ILT_PAGE_CIDS-1));
  1247. DP(BNX2X_MSG_IOV,
  1248. "VF[%d] Max IGU SBs: %d, base vf cid 0x%x, base cid 0x%x, base cxt %p\n",
  1249. vf->abs_vfid, vf_sb_count(vf), base_vf_cid,
  1250. BNX2X_FIRST_VF_CID + base_vf_cid, base_cxt);
  1251. /* init statically provisioned resources */
  1252. bnx2x_iov_static_resc(bp, vf);
  1253. /* queues are initialized during VF-ACQUIRE */
  1254. vf->filter_state = 0;
  1255. vf->sp_cl_id = bnx2x_fp(bp, 0, cl_id);
  1256. /* init mcast object - This object will be re-initialized
  1257. * during VF-ACQUIRE with the proper cl_id and cid.
  1258. * It needs to be initialized here so that it can be safely
  1259. * handled by a subsequent FLR flow.
  1260. */
  1261. vf->mcast_list_len = 0;
  1262. bnx2x_init_mcast_obj(bp, &vf->mcast_obj, 0xFF,
  1263. 0xFF, 0xFF, 0xFF,
  1264. bnx2x_vf_sp(bp, vf, mcast_rdata),
  1265. bnx2x_vf_sp_map(bp, vf, mcast_rdata),
  1266. BNX2X_FILTER_MCAST_PENDING,
  1267. &vf->filter_state,
  1268. BNX2X_OBJ_TYPE_RX_TX);
  1269. /* set the mailbox message addresses */
  1270. BP_VF_MBX(bp, vfid)->msg = (struct bnx2x_vf_mbx_msg *)
  1271. (((u8 *)BP_VF_MBX_DMA(bp)->addr) + vfid *
  1272. MBX_MSG_ALIGNED_SIZE);
  1273. BP_VF_MBX(bp, vfid)->msg_mapping = BP_VF_MBX_DMA(bp)->mapping +
  1274. vfid * MBX_MSG_ALIGNED_SIZE;
  1275. /* Enable vf mailbox */
  1276. bnx2x_vf_enable_mbx(bp, vf->abs_vfid);
  1277. }
  1278. /* Final VF init */
  1279. for_each_vf(bp, vfid) {
  1280. struct bnx2x_virtf *vf = BP_VF(bp, vfid);
  1281. /* fill in the BDF and bars */
  1282. vf->bus = bnx2x_vf_bus(bp, vfid);
  1283. vf->devfn = bnx2x_vf_devfn(bp, vfid);
  1284. bnx2x_vf_set_bars(bp, vf);
  1285. DP(BNX2X_MSG_IOV,
  1286. "VF info[%d]: bus 0x%x, devfn 0x%x, bar0 [0x%x, %d], bar1 [0x%x, %d], bar2 [0x%x, %d]\n",
  1287. vf->abs_vfid, vf->bus, vf->devfn,
  1288. (unsigned)vf->bars[0].bar, vf->bars[0].size,
  1289. (unsigned)vf->bars[1].bar, vf->bars[1].size,
  1290. (unsigned)vf->bars[2].bar, vf->bars[2].size);
  1291. }
  1292. return 0;
  1293. }
  1294. /* called by bnx2x_chip_cleanup */
  1295. int bnx2x_iov_chip_cleanup(struct bnx2x *bp)
  1296. {
  1297. int i;
  1298. if (!IS_SRIOV(bp))
  1299. return 0;
  1300. /* release all the VFs */
  1301. for_each_vf(bp, i)
  1302. bnx2x_vf_release(bp, BP_VF(bp, i));
  1303. return 0;
  1304. }
  1305. /* called by bnx2x_init_hw_func, returns the next ilt line */
  1306. int bnx2x_iov_init_ilt(struct bnx2x *bp, u16 line)
  1307. {
  1308. int i;
  1309. struct bnx2x_ilt *ilt = BP_ILT(bp);
  1310. if (!IS_SRIOV(bp))
  1311. return line;
  1312. /* set vfs ilt lines */
  1313. for (i = 0; i < BNX2X_VF_CIDS/ILT_PAGE_CIDS; i++) {
  1314. struct hw_dma *hw_cxt = BP_VF_CXT_PAGE(bp, i);
  1315. ilt->lines[line+i].page = hw_cxt->addr;
  1316. ilt->lines[line+i].page_mapping = hw_cxt->mapping;
  1317. ilt->lines[line+i].size = hw_cxt->size; /* doesn't matter */
  1318. }
  1319. return line + i;
  1320. }
  1321. static u8 bnx2x_iov_is_vf_cid(struct bnx2x *bp, u16 cid)
  1322. {
  1323. return ((cid >= BNX2X_FIRST_VF_CID) &&
  1324. ((cid - BNX2X_FIRST_VF_CID) < BNX2X_VF_CIDS));
  1325. }
  1326. static
  1327. void bnx2x_vf_handle_classification_eqe(struct bnx2x *bp,
  1328. struct bnx2x_vf_queue *vfq,
  1329. union event_ring_elem *elem)
  1330. {
  1331. unsigned long ramrod_flags = 0;
  1332. int rc = 0;
  1333. /* Always push next commands out, don't wait here */
  1334. set_bit(RAMROD_CONT, &ramrod_flags);
  1335. switch (elem->message.data.eth_event.echo >> BNX2X_SWCID_SHIFT) {
  1336. case BNX2X_FILTER_MAC_PENDING:
  1337. rc = vfq->mac_obj.complete(bp, &vfq->mac_obj, elem,
  1338. &ramrod_flags);
  1339. break;
  1340. case BNX2X_FILTER_VLAN_PENDING:
  1341. rc = vfq->vlan_obj.complete(bp, &vfq->vlan_obj, elem,
  1342. &ramrod_flags);
  1343. break;
  1344. default:
  1345. BNX2X_ERR("Unsupported classification command: %d\n",
  1346. elem->message.data.eth_event.echo);
  1347. return;
  1348. }
  1349. if (rc < 0)
  1350. BNX2X_ERR("Failed to schedule new commands: %d\n", rc);
  1351. else if (rc > 0)
  1352. DP(BNX2X_MSG_IOV, "Scheduled next pending commands...\n");
  1353. }
  1354. static
  1355. void bnx2x_vf_handle_mcast_eqe(struct bnx2x *bp,
  1356. struct bnx2x_virtf *vf)
  1357. {
  1358. struct bnx2x_mcast_ramrod_params rparam = {NULL};
  1359. int rc;
  1360. rparam.mcast_obj = &vf->mcast_obj;
  1361. vf->mcast_obj.raw.clear_pending(&vf->mcast_obj.raw);
  1362. /* If there are pending mcast commands - send them */
  1363. if (vf->mcast_obj.check_pending(&vf->mcast_obj)) {
  1364. rc = bnx2x_config_mcast(bp, &rparam, BNX2X_MCAST_CMD_CONT);
  1365. if (rc < 0)
  1366. BNX2X_ERR("Failed to send pending mcast commands: %d\n",
  1367. rc);
  1368. }
  1369. }
  1370. static
  1371. void bnx2x_vf_handle_filters_eqe(struct bnx2x *bp,
  1372. struct bnx2x_virtf *vf)
  1373. {
  1374. smp_mb__before_atomic();
  1375. clear_bit(BNX2X_FILTER_RX_MODE_PENDING, &vf->filter_state);
  1376. smp_mb__after_atomic();
  1377. }
  1378. static void bnx2x_vf_handle_rss_update_eqe(struct bnx2x *bp,
  1379. struct bnx2x_virtf *vf)
  1380. {
  1381. vf->rss_conf_obj.raw.clear_pending(&vf->rss_conf_obj.raw);
  1382. }
  1383. int bnx2x_iov_eq_sp_event(struct bnx2x *bp, union event_ring_elem *elem)
  1384. {
  1385. struct bnx2x_virtf *vf;
  1386. int qidx = 0, abs_vfid;
  1387. u8 opcode;
  1388. u16 cid = 0xffff;
  1389. if (!IS_SRIOV(bp))
  1390. return 1;
  1391. /* first get the cid - the only events we handle here are cfc-delete
  1392. * and set-mac completion
  1393. */
  1394. opcode = elem->message.opcode;
  1395. switch (opcode) {
  1396. case EVENT_RING_OPCODE_CFC_DEL:
  1397. cid = SW_CID((__force __le32)
  1398. elem->message.data.cfc_del_event.cid);
  1399. DP(BNX2X_MSG_IOV, "checking cfc-del comp cid=%d\n", cid);
  1400. break;
  1401. case EVENT_RING_OPCODE_CLASSIFICATION_RULES:
  1402. case EVENT_RING_OPCODE_MULTICAST_RULES:
  1403. case EVENT_RING_OPCODE_FILTERS_RULES:
  1404. case EVENT_RING_OPCODE_RSS_UPDATE_RULES:
  1405. cid = (elem->message.data.eth_event.echo &
  1406. BNX2X_SWCID_MASK);
  1407. DP(BNX2X_MSG_IOV, "checking filtering comp cid=%d\n", cid);
  1408. break;
  1409. case EVENT_RING_OPCODE_VF_FLR:
  1410. abs_vfid = elem->message.data.vf_flr_event.vf_id;
  1411. DP(BNX2X_MSG_IOV, "Got VF FLR notification abs_vfid=%d\n",
  1412. abs_vfid);
  1413. goto get_vf;
  1414. case EVENT_RING_OPCODE_MALICIOUS_VF:
  1415. abs_vfid = elem->message.data.malicious_vf_event.vf_id;
  1416. BNX2X_ERR("Got VF MALICIOUS notification abs_vfid=%d err_id=0x%x\n",
  1417. abs_vfid,
  1418. elem->message.data.malicious_vf_event.err_id);
  1419. goto get_vf;
  1420. default:
  1421. return 1;
  1422. }
  1423. /* check if the cid is the VF range */
  1424. if (!bnx2x_iov_is_vf_cid(bp, cid)) {
  1425. DP(BNX2X_MSG_IOV, "cid is outside vf range: %d\n", cid);
  1426. return 1;
  1427. }
  1428. /* extract vf and rxq index from vf_cid - relies on the following:
  1429. * 1. vfid on cid reflects the true abs_vfid
  1430. * 2. The max number of VFs (per path) is 64
  1431. */
  1432. qidx = cid & ((1 << BNX2X_VF_CID_WND)-1);
  1433. abs_vfid = (cid >> BNX2X_VF_CID_WND) & (BNX2X_MAX_NUM_OF_VFS-1);
  1434. get_vf:
  1435. vf = bnx2x_vf_by_abs_fid(bp, abs_vfid);
  1436. if (!vf) {
  1437. BNX2X_ERR("EQ completion for unknown VF, cid %d, abs_vfid %d\n",
  1438. cid, abs_vfid);
  1439. return 0;
  1440. }
  1441. switch (opcode) {
  1442. case EVENT_RING_OPCODE_CFC_DEL:
  1443. DP(BNX2X_MSG_IOV, "got VF [%d:%d] cfc delete ramrod\n",
  1444. vf->abs_vfid, qidx);
  1445. vfq_get(vf, qidx)->sp_obj.complete_cmd(bp,
  1446. &vfq_get(vf,
  1447. qidx)->sp_obj,
  1448. BNX2X_Q_CMD_CFC_DEL);
  1449. break;
  1450. case EVENT_RING_OPCODE_CLASSIFICATION_RULES:
  1451. DP(BNX2X_MSG_IOV, "got VF [%d:%d] set mac/vlan ramrod\n",
  1452. vf->abs_vfid, qidx);
  1453. bnx2x_vf_handle_classification_eqe(bp, vfq_get(vf, qidx), elem);
  1454. break;
  1455. case EVENT_RING_OPCODE_MULTICAST_RULES:
  1456. DP(BNX2X_MSG_IOV, "got VF [%d:%d] set mcast ramrod\n",
  1457. vf->abs_vfid, qidx);
  1458. bnx2x_vf_handle_mcast_eqe(bp, vf);
  1459. break;
  1460. case EVENT_RING_OPCODE_FILTERS_RULES:
  1461. DP(BNX2X_MSG_IOV, "got VF [%d:%d] set rx-mode ramrod\n",
  1462. vf->abs_vfid, qidx);
  1463. bnx2x_vf_handle_filters_eqe(bp, vf);
  1464. break;
  1465. case EVENT_RING_OPCODE_RSS_UPDATE_RULES:
  1466. DP(BNX2X_MSG_IOV, "got VF [%d:%d] RSS update ramrod\n",
  1467. vf->abs_vfid, qidx);
  1468. bnx2x_vf_handle_rss_update_eqe(bp, vf);
  1469. case EVENT_RING_OPCODE_VF_FLR:
  1470. case EVENT_RING_OPCODE_MALICIOUS_VF:
  1471. /* Do nothing for now */
  1472. return 0;
  1473. }
  1474. return 0;
  1475. }
  1476. static struct bnx2x_virtf *bnx2x_vf_by_cid(struct bnx2x *bp, int vf_cid)
  1477. {
  1478. /* extract the vf from vf_cid - relies on the following:
  1479. * 1. vfid on cid reflects the true abs_vfid
  1480. * 2. The max number of VFs (per path) is 64
  1481. */
  1482. int abs_vfid = (vf_cid >> BNX2X_VF_CID_WND) & (BNX2X_MAX_NUM_OF_VFS-1);
  1483. return bnx2x_vf_by_abs_fid(bp, abs_vfid);
  1484. }
  1485. void bnx2x_iov_set_queue_sp_obj(struct bnx2x *bp, int vf_cid,
  1486. struct bnx2x_queue_sp_obj **q_obj)
  1487. {
  1488. struct bnx2x_virtf *vf;
  1489. if (!IS_SRIOV(bp))
  1490. return;
  1491. vf = bnx2x_vf_by_cid(bp, vf_cid);
  1492. if (vf) {
  1493. /* extract queue index from vf_cid - relies on the following:
  1494. * 1. vfid on cid reflects the true abs_vfid
  1495. * 2. The max number of VFs (per path) is 64
  1496. */
  1497. int q_index = vf_cid & ((1 << BNX2X_VF_CID_WND)-1);
  1498. *q_obj = &bnx2x_vfq(vf, q_index, sp_obj);
  1499. } else {
  1500. BNX2X_ERR("No vf matching cid %d\n", vf_cid);
  1501. }
  1502. }
  1503. void bnx2x_iov_adjust_stats_req(struct bnx2x *bp)
  1504. {
  1505. int i;
  1506. int first_queue_query_index, num_queues_req;
  1507. dma_addr_t cur_data_offset;
  1508. struct stats_query_entry *cur_query_entry;
  1509. u8 stats_count = 0;
  1510. bool is_fcoe = false;
  1511. if (!IS_SRIOV(bp))
  1512. return;
  1513. if (!NO_FCOE(bp))
  1514. is_fcoe = true;
  1515. /* fcoe adds one global request and one queue request */
  1516. num_queues_req = BNX2X_NUM_ETH_QUEUES(bp) + is_fcoe;
  1517. first_queue_query_index = BNX2X_FIRST_QUEUE_QUERY_IDX -
  1518. (is_fcoe ? 0 : 1);
  1519. DP_AND((BNX2X_MSG_IOV | BNX2X_MSG_STATS),
  1520. "BNX2X_NUM_ETH_QUEUES %d, is_fcoe %d, first_queue_query_index %d => determined the last non virtual statistics query index is %d. Will add queries on top of that\n",
  1521. BNX2X_NUM_ETH_QUEUES(bp), is_fcoe, first_queue_query_index,
  1522. first_queue_query_index + num_queues_req);
  1523. cur_data_offset = bp->fw_stats_data_mapping +
  1524. offsetof(struct bnx2x_fw_stats_data, queue_stats) +
  1525. num_queues_req * sizeof(struct per_queue_stats);
  1526. cur_query_entry = &bp->fw_stats_req->
  1527. query[first_queue_query_index + num_queues_req];
  1528. for_each_vf(bp, i) {
  1529. int j;
  1530. struct bnx2x_virtf *vf = BP_VF(bp, i);
  1531. if (vf->state != VF_ENABLED) {
  1532. DP_AND((BNX2X_MSG_IOV | BNX2X_MSG_STATS),
  1533. "vf %d not enabled so no stats for it\n",
  1534. vf->abs_vfid);
  1535. continue;
  1536. }
  1537. DP(BNX2X_MSG_IOV, "add addresses for vf %d\n", vf->abs_vfid);
  1538. for_each_vfq(vf, j) {
  1539. struct bnx2x_vf_queue *rxq = vfq_get(vf, j);
  1540. dma_addr_t q_stats_addr =
  1541. vf->fw_stat_map + j * vf->stats_stride;
  1542. /* collect stats fro active queues only */
  1543. if (bnx2x_get_q_logical_state(bp, &rxq->sp_obj) ==
  1544. BNX2X_Q_LOGICAL_STATE_STOPPED)
  1545. continue;
  1546. /* create stats query entry for this queue */
  1547. cur_query_entry->kind = STATS_TYPE_QUEUE;
  1548. cur_query_entry->index = vfq_stat_id(vf, rxq);
  1549. cur_query_entry->funcID =
  1550. cpu_to_le16(FW_VF_HANDLE(vf->abs_vfid));
  1551. cur_query_entry->address.hi =
  1552. cpu_to_le32(U64_HI(q_stats_addr));
  1553. cur_query_entry->address.lo =
  1554. cpu_to_le32(U64_LO(q_stats_addr));
  1555. DP(BNX2X_MSG_IOV,
  1556. "added address %x %x for vf %d queue %d client %d\n",
  1557. cur_query_entry->address.hi,
  1558. cur_query_entry->address.lo, cur_query_entry->funcID,
  1559. j, cur_query_entry->index);
  1560. cur_query_entry++;
  1561. cur_data_offset += sizeof(struct per_queue_stats);
  1562. stats_count++;
  1563. /* all stats are coalesced to the leading queue */
  1564. if (vf->cfg_flags & VF_CFG_STATS_COALESCE)
  1565. break;
  1566. }
  1567. }
  1568. bp->fw_stats_req->hdr.cmd_num = bp->fw_stats_num + stats_count;
  1569. }
  1570. static inline
  1571. struct bnx2x_virtf *__vf_from_stat_id(struct bnx2x *bp, u8 stat_id)
  1572. {
  1573. int i;
  1574. struct bnx2x_virtf *vf = NULL;
  1575. for_each_vf(bp, i) {
  1576. vf = BP_VF(bp, i);
  1577. if (stat_id >= vf->igu_base_id &&
  1578. stat_id < vf->igu_base_id + vf_sb_count(vf))
  1579. break;
  1580. }
  1581. return vf;
  1582. }
  1583. /* VF API helpers */
  1584. static void bnx2x_vf_qtbl_set_q(struct bnx2x *bp, u8 abs_vfid, u8 qid,
  1585. u8 enable)
  1586. {
  1587. u32 reg = PXP_REG_HST_ZONE_PERMISSION_TABLE + qid * 4;
  1588. u32 val = enable ? (abs_vfid | (1 << 6)) : 0;
  1589. REG_WR(bp, reg, val);
  1590. }
  1591. static void bnx2x_vf_clr_qtbl(struct bnx2x *bp, struct bnx2x_virtf *vf)
  1592. {
  1593. int i;
  1594. for_each_vfq(vf, i)
  1595. bnx2x_vf_qtbl_set_q(bp, vf->abs_vfid,
  1596. vfq_qzone_id(vf, vfq_get(vf, i)), false);
  1597. }
  1598. static void bnx2x_vf_igu_disable(struct bnx2x *bp, struct bnx2x_virtf *vf)
  1599. {
  1600. u32 val;
  1601. /* clear the VF configuration - pretend */
  1602. bnx2x_pretend_func(bp, HW_VF_HANDLE(bp, vf->abs_vfid));
  1603. val = REG_RD(bp, IGU_REG_VF_CONFIGURATION);
  1604. val &= ~(IGU_VF_CONF_MSI_MSIX_EN | IGU_VF_CONF_SINGLE_ISR_EN |
  1605. IGU_VF_CONF_FUNC_EN | IGU_VF_CONF_PARENT_MASK);
  1606. REG_WR(bp, IGU_REG_VF_CONFIGURATION, val);
  1607. bnx2x_pretend_func(bp, BP_ABS_FUNC(bp));
  1608. }
  1609. u8 bnx2x_vf_max_queue_cnt(struct bnx2x *bp, struct bnx2x_virtf *vf)
  1610. {
  1611. return min_t(u8, min_t(u8, vf_sb_count(vf), BNX2X_CIDS_PER_VF),
  1612. BNX2X_VF_MAX_QUEUES);
  1613. }
  1614. static
  1615. int bnx2x_vf_chk_avail_resc(struct bnx2x *bp, struct bnx2x_virtf *vf,
  1616. struct vf_pf_resc_request *req_resc)
  1617. {
  1618. u8 rxq_cnt = vf_rxq_count(vf) ? : bnx2x_vf_max_queue_cnt(bp, vf);
  1619. u8 txq_cnt = vf_txq_count(vf) ? : bnx2x_vf_max_queue_cnt(bp, vf);
  1620. /* Save a vlan filter for the Hypervisor */
  1621. return ((req_resc->num_rxqs <= rxq_cnt) &&
  1622. (req_resc->num_txqs <= txq_cnt) &&
  1623. (req_resc->num_sbs <= vf_sb_count(vf)) &&
  1624. (req_resc->num_mac_filters <= vf_mac_rules_cnt(vf)) &&
  1625. (req_resc->num_vlan_filters <= vf_vlan_rules_visible_cnt(vf)));
  1626. }
  1627. /* CORE VF API */
  1628. int bnx2x_vf_acquire(struct bnx2x *bp, struct bnx2x_virtf *vf,
  1629. struct vf_pf_resc_request *resc)
  1630. {
  1631. int base_vf_cid = (BP_VFDB(bp)->sriov.first_vf_in_pf + vf->index) *
  1632. BNX2X_CIDS_PER_VF;
  1633. union cdu_context *base_cxt = (union cdu_context *)
  1634. BP_VF_CXT_PAGE(bp, base_vf_cid/ILT_PAGE_CIDS)->addr +
  1635. (base_vf_cid & (ILT_PAGE_CIDS-1));
  1636. int i;
  1637. /* if state is 'acquired' the VF was not released or FLR'd, in
  1638. * this case the returned resources match the acquired already
  1639. * acquired resources. Verify that the requested numbers do
  1640. * not exceed the already acquired numbers.
  1641. */
  1642. if (vf->state == VF_ACQUIRED) {
  1643. DP(BNX2X_MSG_IOV, "VF[%d] Trying to re-acquire resources (VF was not released or FLR'd)\n",
  1644. vf->abs_vfid);
  1645. if (!bnx2x_vf_chk_avail_resc(bp, vf, resc)) {
  1646. BNX2X_ERR("VF[%d] When re-acquiring resources, requested numbers must be <= then previously acquired numbers\n",
  1647. vf->abs_vfid);
  1648. return -EINVAL;
  1649. }
  1650. return 0;
  1651. }
  1652. /* Otherwise vf state must be 'free' or 'reset' */
  1653. if (vf->state != VF_FREE && vf->state != VF_RESET) {
  1654. BNX2X_ERR("VF[%d] Can not acquire a VF with state %d\n",
  1655. vf->abs_vfid, vf->state);
  1656. return -EINVAL;
  1657. }
  1658. /* static allocation:
  1659. * the global maximum number are fixed per VF. Fail the request if
  1660. * requested number exceed these globals
  1661. */
  1662. if (!bnx2x_vf_chk_avail_resc(bp, vf, resc)) {
  1663. DP(BNX2X_MSG_IOV,
  1664. "cannot fulfill vf resource request. Placing maximal available values in response\n");
  1665. /* set the max resource in the vf */
  1666. return -ENOMEM;
  1667. }
  1668. /* Set resources counters - 0 request means max available */
  1669. vf_sb_count(vf) = resc->num_sbs;
  1670. vf_rxq_count(vf) = resc->num_rxqs ? : bnx2x_vf_max_queue_cnt(bp, vf);
  1671. vf_txq_count(vf) = resc->num_txqs ? : bnx2x_vf_max_queue_cnt(bp, vf);
  1672. if (resc->num_mac_filters)
  1673. vf_mac_rules_cnt(vf) = resc->num_mac_filters;
  1674. /* Add an additional vlan filter credit for the hypervisor */
  1675. bnx2x_iov_re_set_vlan_filters(bp, vf, resc->num_vlan_filters + 1);
  1676. DP(BNX2X_MSG_IOV,
  1677. "Fulfilling vf request: sb count %d, tx_count %d, rx_count %d, mac_rules_count %d, vlan_rules_count %d\n",
  1678. vf_sb_count(vf), vf_rxq_count(vf),
  1679. vf_txq_count(vf), vf_mac_rules_cnt(vf),
  1680. vf_vlan_rules_visible_cnt(vf));
  1681. /* Initialize the queues */
  1682. if (!vf->vfqs) {
  1683. DP(BNX2X_MSG_IOV, "vf->vfqs was not allocated\n");
  1684. return -EINVAL;
  1685. }
  1686. for_each_vfq(vf, i) {
  1687. struct bnx2x_vf_queue *q = vfq_get(vf, i);
  1688. if (!q) {
  1689. BNX2X_ERR("q number %d was not allocated\n", i);
  1690. return -EINVAL;
  1691. }
  1692. q->index = i;
  1693. q->cxt = &((base_cxt + i)->eth);
  1694. q->cid = BNX2X_FIRST_VF_CID + base_vf_cid + i;
  1695. DP(BNX2X_MSG_IOV, "VFQ[%d:%d]: index %d, cid 0x%x, cxt %p\n",
  1696. vf->abs_vfid, i, q->index, q->cid, q->cxt);
  1697. /* init SP objects */
  1698. bnx2x_vfq_init(bp, vf, q);
  1699. }
  1700. vf->state = VF_ACQUIRED;
  1701. return 0;
  1702. }
  1703. int bnx2x_vf_init(struct bnx2x *bp, struct bnx2x_virtf *vf, dma_addr_t *sb_map)
  1704. {
  1705. struct bnx2x_func_init_params func_init = {0};
  1706. u16 flags = 0;
  1707. int i;
  1708. /* the sb resources are initialized at this point, do the
  1709. * FW/HW initializations
  1710. */
  1711. for_each_vf_sb(vf, i)
  1712. bnx2x_init_sb(bp, (dma_addr_t)sb_map[i], vf->abs_vfid, true,
  1713. vf_igu_sb(vf, i), vf_igu_sb(vf, i));
  1714. /* Sanity checks */
  1715. if (vf->state != VF_ACQUIRED) {
  1716. DP(BNX2X_MSG_IOV, "VF[%d] is not in VF_ACQUIRED, but %d\n",
  1717. vf->abs_vfid, vf->state);
  1718. return -EINVAL;
  1719. }
  1720. /* let FLR complete ... */
  1721. msleep(100);
  1722. /* FLR cleanup epilogue */
  1723. if (bnx2x_vf_flr_clnup_epilog(bp, vf->abs_vfid))
  1724. return -EBUSY;
  1725. /* reset IGU VF statistics: MSIX */
  1726. REG_WR(bp, IGU_REG_STATISTIC_NUM_MESSAGE_SENT + vf->abs_vfid * 4 , 0);
  1727. /* vf init */
  1728. if (vf->cfg_flags & VF_CFG_STATS)
  1729. flags |= (FUNC_FLG_STATS | FUNC_FLG_SPQ);
  1730. if (vf->cfg_flags & VF_CFG_TPA)
  1731. flags |= FUNC_FLG_TPA;
  1732. if (is_vf_multi(vf))
  1733. flags |= FUNC_FLG_RSS;
  1734. /* function setup */
  1735. func_init.func_flgs = flags;
  1736. func_init.pf_id = BP_FUNC(bp);
  1737. func_init.func_id = FW_VF_HANDLE(vf->abs_vfid);
  1738. func_init.fw_stat_map = vf->fw_stat_map;
  1739. func_init.spq_map = vf->spq_map;
  1740. func_init.spq_prod = 0;
  1741. bnx2x_func_init(bp, &func_init);
  1742. /* Enable the vf */
  1743. bnx2x_vf_enable_access(bp, vf->abs_vfid);
  1744. bnx2x_vf_enable_traffic(bp, vf);
  1745. /* queue protection table */
  1746. for_each_vfq(vf, i)
  1747. bnx2x_vf_qtbl_set_q(bp, vf->abs_vfid,
  1748. vfq_qzone_id(vf, vfq_get(vf, i)), true);
  1749. vf->state = VF_ENABLED;
  1750. /* update vf bulletin board */
  1751. bnx2x_post_vf_bulletin(bp, vf->index);
  1752. return 0;
  1753. }
  1754. struct set_vf_state_cookie {
  1755. struct bnx2x_virtf *vf;
  1756. u8 state;
  1757. };
  1758. static void bnx2x_set_vf_state(void *cookie)
  1759. {
  1760. struct set_vf_state_cookie *p = (struct set_vf_state_cookie *)cookie;
  1761. p->vf->state = p->state;
  1762. }
  1763. int bnx2x_vf_close(struct bnx2x *bp, struct bnx2x_virtf *vf)
  1764. {
  1765. int rc = 0, i;
  1766. DP(BNX2X_MSG_IOV, "vf[%d]\n", vf->abs_vfid);
  1767. /* Close all queues */
  1768. for (i = 0; i < vf_rxq_count(vf); i++) {
  1769. rc = bnx2x_vf_queue_teardown(bp, vf, i);
  1770. if (rc)
  1771. goto op_err;
  1772. }
  1773. /* disable the interrupts */
  1774. DP(BNX2X_MSG_IOV, "disabling igu\n");
  1775. bnx2x_vf_igu_disable(bp, vf);
  1776. /* disable the VF */
  1777. DP(BNX2X_MSG_IOV, "clearing qtbl\n");
  1778. bnx2x_vf_clr_qtbl(bp, vf);
  1779. /* need to make sure there are no outstanding stats ramrods which may
  1780. * cause the device to access the VF's stats buffer which it will free
  1781. * as soon as we return from the close flow.
  1782. */
  1783. {
  1784. struct set_vf_state_cookie cookie;
  1785. cookie.vf = vf;
  1786. cookie.state = VF_ACQUIRED;
  1787. bnx2x_stats_safe_exec(bp, bnx2x_set_vf_state, &cookie);
  1788. }
  1789. DP(BNX2X_MSG_IOV, "set state to acquired\n");
  1790. return 0;
  1791. op_err:
  1792. BNX2X_ERR("vf[%d] CLOSE error: rc %d\n", vf->abs_vfid, rc);
  1793. return rc;
  1794. }
  1795. /* VF release can be called either: 1. The VF was acquired but
  1796. * not enabled 2. the vf was enabled or in the process of being
  1797. * enabled
  1798. */
  1799. int bnx2x_vf_free(struct bnx2x *bp, struct bnx2x_virtf *vf)
  1800. {
  1801. int rc;
  1802. DP(BNX2X_MSG_IOV, "VF[%d] STATE: %s\n", vf->abs_vfid,
  1803. vf->state == VF_FREE ? "Free" :
  1804. vf->state == VF_ACQUIRED ? "Acquired" :
  1805. vf->state == VF_ENABLED ? "Enabled" :
  1806. vf->state == VF_RESET ? "Reset" :
  1807. "Unknown");
  1808. switch (vf->state) {
  1809. case VF_ENABLED:
  1810. rc = bnx2x_vf_close(bp, vf);
  1811. if (rc)
  1812. goto op_err;
  1813. /* Fallthrough to release resources */
  1814. case VF_ACQUIRED:
  1815. DP(BNX2X_MSG_IOV, "about to free resources\n");
  1816. bnx2x_vf_free_resc(bp, vf);
  1817. break;
  1818. case VF_FREE:
  1819. case VF_RESET:
  1820. default:
  1821. break;
  1822. }
  1823. return 0;
  1824. op_err:
  1825. BNX2X_ERR("VF[%d] RELEASE error: rc %d\n", vf->abs_vfid, rc);
  1826. return rc;
  1827. }
  1828. int bnx2x_vf_rss_update(struct bnx2x *bp, struct bnx2x_virtf *vf,
  1829. struct bnx2x_config_rss_params *rss)
  1830. {
  1831. DP(BNX2X_MSG_IOV, "vf[%d]\n", vf->abs_vfid);
  1832. set_bit(RAMROD_COMP_WAIT, &rss->ramrod_flags);
  1833. return bnx2x_config_rss(bp, rss);
  1834. }
  1835. int bnx2x_vf_tpa_update(struct bnx2x *bp, struct bnx2x_virtf *vf,
  1836. struct vfpf_tpa_tlv *tlv,
  1837. struct bnx2x_queue_update_tpa_params *params)
  1838. {
  1839. aligned_u64 *sge_addr = tlv->tpa_client_info.sge_addr;
  1840. struct bnx2x_queue_state_params qstate;
  1841. int qid, rc = 0;
  1842. DP(BNX2X_MSG_IOV, "vf[%d]\n", vf->abs_vfid);
  1843. /* Set ramrod params */
  1844. memset(&qstate, 0, sizeof(struct bnx2x_queue_state_params));
  1845. memcpy(&qstate.params.update_tpa, params,
  1846. sizeof(struct bnx2x_queue_update_tpa_params));
  1847. qstate.cmd = BNX2X_Q_CMD_UPDATE_TPA;
  1848. set_bit(RAMROD_COMP_WAIT, &qstate.ramrod_flags);
  1849. for (qid = 0; qid < vf_rxq_count(vf); qid++) {
  1850. qstate.q_obj = &bnx2x_vfq(vf, qid, sp_obj);
  1851. qstate.params.update_tpa.sge_map = sge_addr[qid];
  1852. DP(BNX2X_MSG_IOV, "sge_addr[%d:%d] %08x:%08x\n",
  1853. vf->abs_vfid, qid, U64_HI(sge_addr[qid]),
  1854. U64_LO(sge_addr[qid]));
  1855. rc = bnx2x_queue_state_change(bp, &qstate);
  1856. if (rc) {
  1857. BNX2X_ERR("Failed to configure sge_addr %08x:%08x for [%d:%d]\n",
  1858. U64_HI(sge_addr[qid]), U64_LO(sge_addr[qid]),
  1859. vf->abs_vfid, qid);
  1860. return rc;
  1861. }
  1862. }
  1863. return rc;
  1864. }
  1865. /* VF release ~ VF close + VF release-resources
  1866. * Release is the ultimate SW shutdown and is called whenever an
  1867. * irrecoverable error is encountered.
  1868. */
  1869. int bnx2x_vf_release(struct bnx2x *bp, struct bnx2x_virtf *vf)
  1870. {
  1871. int rc;
  1872. DP(BNX2X_MSG_IOV, "PF releasing vf %d\n", vf->abs_vfid);
  1873. bnx2x_lock_vf_pf_channel(bp, vf, CHANNEL_TLV_PF_RELEASE_VF);
  1874. rc = bnx2x_vf_free(bp, vf);
  1875. if (rc)
  1876. WARN(rc,
  1877. "VF[%d] Failed to allocate resources for release op- rc=%d\n",
  1878. vf->abs_vfid, rc);
  1879. bnx2x_unlock_vf_pf_channel(bp, vf, CHANNEL_TLV_PF_RELEASE_VF);
  1880. return rc;
  1881. }
  1882. static inline void bnx2x_vf_get_sbdf(struct bnx2x *bp,
  1883. struct bnx2x_virtf *vf, u32 *sbdf)
  1884. {
  1885. *sbdf = vf->devfn | (vf->bus << 8);
  1886. }
  1887. void bnx2x_lock_vf_pf_channel(struct bnx2x *bp, struct bnx2x_virtf *vf,
  1888. enum channel_tlvs tlv)
  1889. {
  1890. /* we don't lock the channel for unsupported tlvs */
  1891. if (!bnx2x_tlv_supported(tlv)) {
  1892. BNX2X_ERR("attempting to lock with unsupported tlv. Aborting\n");
  1893. return;
  1894. }
  1895. /* lock the channel */
  1896. mutex_lock(&vf->op_mutex);
  1897. /* record the locking op */
  1898. vf->op_current = tlv;
  1899. /* log the lock */
  1900. DP(BNX2X_MSG_IOV, "VF[%d]: vf pf channel locked by %d\n",
  1901. vf->abs_vfid, tlv);
  1902. }
  1903. void bnx2x_unlock_vf_pf_channel(struct bnx2x *bp, struct bnx2x_virtf *vf,
  1904. enum channel_tlvs expected_tlv)
  1905. {
  1906. enum channel_tlvs current_tlv;
  1907. if (!vf) {
  1908. BNX2X_ERR("VF was %p\n", vf);
  1909. return;
  1910. }
  1911. current_tlv = vf->op_current;
  1912. /* we don't unlock the channel for unsupported tlvs */
  1913. if (!bnx2x_tlv_supported(expected_tlv))
  1914. return;
  1915. WARN(expected_tlv != vf->op_current,
  1916. "lock mismatch: expected %d found %d", expected_tlv,
  1917. vf->op_current);
  1918. /* record the locking op */
  1919. vf->op_current = CHANNEL_TLV_NONE;
  1920. /* lock the channel */
  1921. mutex_unlock(&vf->op_mutex);
  1922. /* log the unlock */
  1923. DP(BNX2X_MSG_IOV, "VF[%d]: vf pf channel unlocked by %d\n",
  1924. vf->abs_vfid, vf->op_current);
  1925. }
  1926. static int bnx2x_set_pf_tx_switching(struct bnx2x *bp, bool enable)
  1927. {
  1928. struct bnx2x_queue_state_params q_params;
  1929. u32 prev_flags;
  1930. int i, rc;
  1931. /* Verify changes are needed and record current Tx switching state */
  1932. prev_flags = bp->flags;
  1933. if (enable)
  1934. bp->flags |= TX_SWITCHING;
  1935. else
  1936. bp->flags &= ~TX_SWITCHING;
  1937. if (prev_flags == bp->flags)
  1938. return 0;
  1939. /* Verify state enables the sending of queue ramrods */
  1940. if ((bp->state != BNX2X_STATE_OPEN) ||
  1941. (bnx2x_get_q_logical_state(bp,
  1942. &bnx2x_sp_obj(bp, &bp->fp[0]).q_obj) !=
  1943. BNX2X_Q_LOGICAL_STATE_ACTIVE))
  1944. return 0;
  1945. /* send q. update ramrod to configure Tx switching */
  1946. memset(&q_params, 0, sizeof(q_params));
  1947. __set_bit(RAMROD_COMP_WAIT, &q_params.ramrod_flags);
  1948. q_params.cmd = BNX2X_Q_CMD_UPDATE;
  1949. __set_bit(BNX2X_Q_UPDATE_TX_SWITCHING_CHNG,
  1950. &q_params.params.update.update_flags);
  1951. if (enable)
  1952. __set_bit(BNX2X_Q_UPDATE_TX_SWITCHING,
  1953. &q_params.params.update.update_flags);
  1954. else
  1955. __clear_bit(BNX2X_Q_UPDATE_TX_SWITCHING,
  1956. &q_params.params.update.update_flags);
  1957. /* send the ramrod on all the queues of the PF */
  1958. for_each_eth_queue(bp, i) {
  1959. struct bnx2x_fastpath *fp = &bp->fp[i];
  1960. /* Set the appropriate Queue object */
  1961. q_params.q_obj = &bnx2x_sp_obj(bp, fp).q_obj;
  1962. /* Update the Queue state */
  1963. rc = bnx2x_queue_state_change(bp, &q_params);
  1964. if (rc) {
  1965. BNX2X_ERR("Failed to configure Tx switching\n");
  1966. return rc;
  1967. }
  1968. }
  1969. DP(BNX2X_MSG_IOV, "%s Tx Switching\n", enable ? "Enabled" : "Disabled");
  1970. return 0;
  1971. }
  1972. int bnx2x_sriov_configure(struct pci_dev *dev, int num_vfs_param)
  1973. {
  1974. struct bnx2x *bp = netdev_priv(pci_get_drvdata(dev));
  1975. if (!IS_SRIOV(bp)) {
  1976. BNX2X_ERR("failed to configure SR-IOV since vfdb was not allocated. Check dmesg for errors in probe stage\n");
  1977. return -EINVAL;
  1978. }
  1979. DP(BNX2X_MSG_IOV, "bnx2x_sriov_configure called with %d, BNX2X_NR_VIRTFN(bp) was %d\n",
  1980. num_vfs_param, BNX2X_NR_VIRTFN(bp));
  1981. /* HW channel is only operational when PF is up */
  1982. if (bp->state != BNX2X_STATE_OPEN) {
  1983. BNX2X_ERR("VF num configuration via sysfs not supported while PF is down\n");
  1984. return -EINVAL;
  1985. }
  1986. /* we are always bound by the total_vfs in the configuration space */
  1987. if (num_vfs_param > BNX2X_NR_VIRTFN(bp)) {
  1988. BNX2X_ERR("truncating requested number of VFs (%d) down to maximum allowed (%d)\n",
  1989. num_vfs_param, BNX2X_NR_VIRTFN(bp));
  1990. num_vfs_param = BNX2X_NR_VIRTFN(bp);
  1991. }
  1992. bp->requested_nr_virtfn = num_vfs_param;
  1993. if (num_vfs_param == 0) {
  1994. bnx2x_set_pf_tx_switching(bp, false);
  1995. pci_disable_sriov(dev);
  1996. return 0;
  1997. } else {
  1998. return bnx2x_enable_sriov(bp);
  1999. }
  2000. }
  2001. #define IGU_ENTRY_SIZE 4
  2002. int bnx2x_enable_sriov(struct bnx2x *bp)
  2003. {
  2004. int rc = 0, req_vfs = bp->requested_nr_virtfn;
  2005. int vf_idx, sb_idx, vfq_idx, qcount, first_vf;
  2006. u32 igu_entry, address;
  2007. u16 num_vf_queues;
  2008. if (req_vfs == 0)
  2009. return 0;
  2010. first_vf = bp->vfdb->sriov.first_vf_in_pf;
  2011. /* statically distribute vf sb pool between VFs */
  2012. num_vf_queues = min_t(u16, BNX2X_VF_MAX_QUEUES,
  2013. BP_VFDB(bp)->vf_sbs_pool / req_vfs);
  2014. /* zero previous values learned from igu cam */
  2015. for (vf_idx = 0; vf_idx < req_vfs; vf_idx++) {
  2016. struct bnx2x_virtf *vf = BP_VF(bp, vf_idx);
  2017. vf->sb_count = 0;
  2018. vf_sb_count(BP_VF(bp, vf_idx)) = 0;
  2019. }
  2020. bp->vfdb->vf_sbs_pool = 0;
  2021. /* prepare IGU cam */
  2022. sb_idx = BP_VFDB(bp)->first_vf_igu_entry;
  2023. address = IGU_REG_MAPPING_MEMORY + sb_idx * IGU_ENTRY_SIZE;
  2024. for (vf_idx = first_vf; vf_idx < first_vf + req_vfs; vf_idx++) {
  2025. for (vfq_idx = 0; vfq_idx < num_vf_queues; vfq_idx++) {
  2026. igu_entry = vf_idx << IGU_REG_MAPPING_MEMORY_FID_SHIFT |
  2027. vfq_idx << IGU_REG_MAPPING_MEMORY_VECTOR_SHIFT |
  2028. IGU_REG_MAPPING_MEMORY_VALID;
  2029. DP(BNX2X_MSG_IOV, "assigning sb %d to vf %d\n",
  2030. sb_idx, vf_idx);
  2031. REG_WR(bp, address, igu_entry);
  2032. sb_idx++;
  2033. address += IGU_ENTRY_SIZE;
  2034. }
  2035. }
  2036. /* Reinitialize vf database according to igu cam */
  2037. bnx2x_get_vf_igu_cam_info(bp);
  2038. DP(BNX2X_MSG_IOV, "vf_sbs_pool %d, num_vf_queues %d\n",
  2039. BP_VFDB(bp)->vf_sbs_pool, num_vf_queues);
  2040. qcount = 0;
  2041. for_each_vf(bp, vf_idx) {
  2042. struct bnx2x_virtf *vf = BP_VF(bp, vf_idx);
  2043. /* set local queue arrays */
  2044. vf->vfqs = &bp->vfdb->vfqs[qcount];
  2045. qcount += vf_sb_count(vf);
  2046. bnx2x_iov_static_resc(bp, vf);
  2047. }
  2048. /* prepare msix vectors in VF configuration space - the value in the
  2049. * PCI configuration space should be the index of the last entry,
  2050. * namely one less than the actual size of the table
  2051. */
  2052. for (vf_idx = first_vf; vf_idx < first_vf + req_vfs; vf_idx++) {
  2053. bnx2x_pretend_func(bp, HW_VF_HANDLE(bp, vf_idx));
  2054. REG_WR(bp, PCICFG_OFFSET + GRC_CONFIG_REG_VF_MSIX_CONTROL,
  2055. num_vf_queues - 1);
  2056. DP(BNX2X_MSG_IOV, "set msix vec num in VF %d cfg space to %d\n",
  2057. vf_idx, num_vf_queues - 1);
  2058. }
  2059. bnx2x_pretend_func(bp, BP_ABS_FUNC(bp));
  2060. /* enable sriov. This will probe all the VFs, and consequentially cause
  2061. * the "acquire" messages to appear on the VF PF channel.
  2062. */
  2063. DP(BNX2X_MSG_IOV, "about to call enable sriov\n");
  2064. bnx2x_disable_sriov(bp);
  2065. rc = bnx2x_set_pf_tx_switching(bp, true);
  2066. if (rc)
  2067. return rc;
  2068. rc = pci_enable_sriov(bp->pdev, req_vfs);
  2069. if (rc) {
  2070. BNX2X_ERR("pci_enable_sriov failed with %d\n", rc);
  2071. return rc;
  2072. }
  2073. DP(BNX2X_MSG_IOV, "sriov enabled (%d vfs)\n", req_vfs);
  2074. return req_vfs;
  2075. }
  2076. void bnx2x_pf_set_vfs_vlan(struct bnx2x *bp)
  2077. {
  2078. int vfidx;
  2079. struct pf_vf_bulletin_content *bulletin;
  2080. DP(BNX2X_MSG_IOV, "configuring vlan for VFs from sp-task\n");
  2081. for_each_vf(bp, vfidx) {
  2082. bulletin = BP_VF_BULLETIN(bp, vfidx);
  2083. if (BP_VF(bp, vfidx)->cfg_flags & VF_CFG_VLAN)
  2084. bnx2x_set_vf_vlan(bp->dev, vfidx, bulletin->vlan, 0);
  2085. }
  2086. }
  2087. void bnx2x_disable_sriov(struct bnx2x *bp)
  2088. {
  2089. pci_disable_sriov(bp->pdev);
  2090. }
  2091. static int bnx2x_vf_ndo_prep(struct bnx2x *bp, int vfidx,
  2092. struct bnx2x_virtf **vf,
  2093. struct pf_vf_bulletin_content **bulletin)
  2094. {
  2095. if (bp->state != BNX2X_STATE_OPEN) {
  2096. BNX2X_ERR("vf ndo called though PF is down\n");
  2097. return -EINVAL;
  2098. }
  2099. if (!IS_SRIOV(bp)) {
  2100. BNX2X_ERR("vf ndo called though sriov is disabled\n");
  2101. return -EINVAL;
  2102. }
  2103. if (vfidx >= BNX2X_NR_VIRTFN(bp)) {
  2104. BNX2X_ERR("vf ndo called for uninitialized VF. vfidx was %d BNX2X_NR_VIRTFN was %d\n",
  2105. vfidx, BNX2X_NR_VIRTFN(bp));
  2106. return -EINVAL;
  2107. }
  2108. /* init members */
  2109. *vf = BP_VF(bp, vfidx);
  2110. *bulletin = BP_VF_BULLETIN(bp, vfidx);
  2111. if (!*vf) {
  2112. BNX2X_ERR("vf ndo called but vf struct is null. vfidx was %d\n",
  2113. vfidx);
  2114. return -EINVAL;
  2115. }
  2116. if (!(*vf)->vfqs) {
  2117. BNX2X_ERR("vf ndo called but vfqs struct is null. Was ndo invoked before dynamically enabling SR-IOV? vfidx was %d\n",
  2118. vfidx);
  2119. return -EINVAL;
  2120. }
  2121. if (!*bulletin) {
  2122. BNX2X_ERR("vf ndo called but Bulletin Board struct is null. vfidx was %d\n",
  2123. vfidx);
  2124. return -EINVAL;
  2125. }
  2126. return 0;
  2127. }
  2128. int bnx2x_get_vf_config(struct net_device *dev, int vfidx,
  2129. struct ifla_vf_info *ivi)
  2130. {
  2131. struct bnx2x *bp = netdev_priv(dev);
  2132. struct bnx2x_virtf *vf = NULL;
  2133. struct pf_vf_bulletin_content *bulletin = NULL;
  2134. struct bnx2x_vlan_mac_obj *mac_obj;
  2135. struct bnx2x_vlan_mac_obj *vlan_obj;
  2136. int rc;
  2137. /* sanity and init */
  2138. rc = bnx2x_vf_ndo_prep(bp, vfidx, &vf, &bulletin);
  2139. if (rc)
  2140. return rc;
  2141. mac_obj = &bnx2x_leading_vfq(vf, mac_obj);
  2142. vlan_obj = &bnx2x_leading_vfq(vf, vlan_obj);
  2143. if (!mac_obj || !vlan_obj) {
  2144. BNX2X_ERR("VF partially initialized\n");
  2145. return -EINVAL;
  2146. }
  2147. ivi->vf = vfidx;
  2148. ivi->qos = 0;
  2149. ivi->max_tx_rate = 10000; /* always 10G. TBA take from link struct */
  2150. ivi->min_tx_rate = 0;
  2151. ivi->spoofchk = 1; /*always enabled */
  2152. if (vf->state == VF_ENABLED) {
  2153. /* mac and vlan are in vlan_mac objects */
  2154. if (bnx2x_validate_vf_sp_objs(bp, vf, false)) {
  2155. mac_obj->get_n_elements(bp, mac_obj, 1, (u8 *)&ivi->mac,
  2156. 0, ETH_ALEN);
  2157. vlan_obj->get_n_elements(bp, vlan_obj, 1,
  2158. (u8 *)&ivi->vlan, 0,
  2159. VLAN_HLEN);
  2160. }
  2161. } else {
  2162. /* mac */
  2163. if (bulletin->valid_bitmap & (1 << MAC_ADDR_VALID))
  2164. /* mac configured by ndo so its in bulletin board */
  2165. memcpy(&ivi->mac, bulletin->mac, ETH_ALEN);
  2166. else
  2167. /* function has not been loaded yet. Show mac as 0s */
  2168. memset(&ivi->mac, 0, ETH_ALEN);
  2169. /* vlan */
  2170. if (bulletin->valid_bitmap & (1 << VLAN_VALID))
  2171. /* vlan configured by ndo so its in bulletin board */
  2172. memcpy(&ivi->vlan, &bulletin->vlan, VLAN_HLEN);
  2173. else
  2174. /* function has not been loaded yet. Show vlans as 0s */
  2175. memset(&ivi->vlan, 0, VLAN_HLEN);
  2176. }
  2177. return 0;
  2178. }
  2179. /* New mac for VF. Consider these cases:
  2180. * 1. VF hasn't been acquired yet - save the mac in local bulletin board and
  2181. * supply at acquire.
  2182. * 2. VF has already been acquired but has not yet initialized - store in local
  2183. * bulletin board. mac will be posted on VF bulletin board after VF init. VF
  2184. * will configure this mac when it is ready.
  2185. * 3. VF has already initialized but has not yet setup a queue - post the new
  2186. * mac on VF's bulletin board right now. VF will configure this mac when it
  2187. * is ready.
  2188. * 4. VF has already set a queue - delete any macs already configured for this
  2189. * queue and manually config the new mac.
  2190. * In any event, once this function has been called refuse any attempts by the
  2191. * VF to configure any mac for itself except for this mac. In case of a race
  2192. * where the VF fails to see the new post on its bulletin board before sending a
  2193. * mac configuration request, the PF will simply fail the request and VF can try
  2194. * again after consulting its bulletin board.
  2195. */
  2196. int bnx2x_set_vf_mac(struct net_device *dev, int vfidx, u8 *mac)
  2197. {
  2198. struct bnx2x *bp = netdev_priv(dev);
  2199. int rc, q_logical_state;
  2200. struct bnx2x_virtf *vf = NULL;
  2201. struct pf_vf_bulletin_content *bulletin = NULL;
  2202. /* sanity and init */
  2203. rc = bnx2x_vf_ndo_prep(bp, vfidx, &vf, &bulletin);
  2204. if (rc)
  2205. return rc;
  2206. if (!is_valid_ether_addr(mac)) {
  2207. BNX2X_ERR("mac address invalid\n");
  2208. return -EINVAL;
  2209. }
  2210. /* update PF's copy of the VF's bulletin. Will no longer accept mac
  2211. * configuration requests from vf unless match this mac
  2212. */
  2213. bulletin->valid_bitmap |= 1 << MAC_ADDR_VALID;
  2214. memcpy(bulletin->mac, mac, ETH_ALEN);
  2215. /* Post update on VF's bulletin board */
  2216. rc = bnx2x_post_vf_bulletin(bp, vfidx);
  2217. if (rc) {
  2218. BNX2X_ERR("failed to update VF[%d] bulletin\n", vfidx);
  2219. return rc;
  2220. }
  2221. q_logical_state =
  2222. bnx2x_get_q_logical_state(bp, &bnx2x_leading_vfq(vf, sp_obj));
  2223. if (vf->state == VF_ENABLED &&
  2224. q_logical_state == BNX2X_Q_LOGICAL_STATE_ACTIVE) {
  2225. /* configure the mac in device on this vf's queue */
  2226. unsigned long ramrod_flags = 0;
  2227. struct bnx2x_vlan_mac_obj *mac_obj;
  2228. /* User should be able to see failure reason in system logs */
  2229. if (!bnx2x_validate_vf_sp_objs(bp, vf, true))
  2230. return -EINVAL;
  2231. /* must lock vfpf channel to protect against vf flows */
  2232. bnx2x_lock_vf_pf_channel(bp, vf, CHANNEL_TLV_PF_SET_MAC);
  2233. /* remove existing eth macs */
  2234. mac_obj = &bnx2x_leading_vfq(vf, mac_obj);
  2235. rc = bnx2x_del_all_macs(bp, mac_obj, BNX2X_ETH_MAC, true);
  2236. if (rc) {
  2237. BNX2X_ERR("failed to delete eth macs\n");
  2238. rc = -EINVAL;
  2239. goto out;
  2240. }
  2241. /* remove existing uc list macs */
  2242. rc = bnx2x_del_all_macs(bp, mac_obj, BNX2X_UC_LIST_MAC, true);
  2243. if (rc) {
  2244. BNX2X_ERR("failed to delete uc_list macs\n");
  2245. rc = -EINVAL;
  2246. goto out;
  2247. }
  2248. /* configure the new mac to device */
  2249. __set_bit(RAMROD_COMP_WAIT, &ramrod_flags);
  2250. bnx2x_set_mac_one(bp, (u8 *)&bulletin->mac, mac_obj, true,
  2251. BNX2X_ETH_MAC, &ramrod_flags);
  2252. out:
  2253. bnx2x_unlock_vf_pf_channel(bp, vf, CHANNEL_TLV_PF_SET_MAC);
  2254. }
  2255. return rc;
  2256. }
  2257. int bnx2x_set_vf_vlan(struct net_device *dev, int vfidx, u16 vlan, u8 qos)
  2258. {
  2259. struct bnx2x_queue_state_params q_params = {NULL};
  2260. struct bnx2x_vlan_mac_ramrod_params ramrod_param;
  2261. struct bnx2x_queue_update_params *update_params;
  2262. struct pf_vf_bulletin_content *bulletin = NULL;
  2263. struct bnx2x_rx_mode_ramrod_params rx_ramrod;
  2264. struct bnx2x *bp = netdev_priv(dev);
  2265. struct bnx2x_vlan_mac_obj *vlan_obj;
  2266. unsigned long vlan_mac_flags = 0;
  2267. unsigned long ramrod_flags = 0;
  2268. struct bnx2x_virtf *vf = NULL;
  2269. unsigned long accept_flags;
  2270. int rc;
  2271. /* sanity and init */
  2272. rc = bnx2x_vf_ndo_prep(bp, vfidx, &vf, &bulletin);
  2273. if (rc)
  2274. return rc;
  2275. if (vlan > 4095) {
  2276. BNX2X_ERR("illegal vlan value %d\n", vlan);
  2277. return -EINVAL;
  2278. }
  2279. DP(BNX2X_MSG_IOV, "configuring VF %d with VLAN %d qos %d\n",
  2280. vfidx, vlan, 0);
  2281. /* update PF's copy of the VF's bulletin. No point in posting the vlan
  2282. * to the VF since it doesn't have anything to do with it. But it useful
  2283. * to store it here in case the VF is not up yet and we can only
  2284. * configure the vlan later when it does. Treat vlan id 0 as remove the
  2285. * Host tag.
  2286. */
  2287. if (vlan > 0)
  2288. bulletin->valid_bitmap |= 1 << VLAN_VALID;
  2289. else
  2290. bulletin->valid_bitmap &= ~(1 << VLAN_VALID);
  2291. bulletin->vlan = vlan;
  2292. /* is vf initialized and queue set up? */
  2293. if (vf->state != VF_ENABLED ||
  2294. bnx2x_get_q_logical_state(bp, &bnx2x_leading_vfq(vf, sp_obj)) !=
  2295. BNX2X_Q_LOGICAL_STATE_ACTIVE)
  2296. return rc;
  2297. /* User should be able to see error in system logs */
  2298. if (!bnx2x_validate_vf_sp_objs(bp, vf, true))
  2299. return -EINVAL;
  2300. /* must lock vfpf channel to protect against vf flows */
  2301. bnx2x_lock_vf_pf_channel(bp, vf, CHANNEL_TLV_PF_SET_VLAN);
  2302. /* remove existing vlans */
  2303. __set_bit(RAMROD_COMP_WAIT, &ramrod_flags);
  2304. vlan_obj = &bnx2x_leading_vfq(vf, vlan_obj);
  2305. rc = vlan_obj->delete_all(bp, vlan_obj, &vlan_mac_flags,
  2306. &ramrod_flags);
  2307. if (rc) {
  2308. BNX2X_ERR("failed to delete vlans\n");
  2309. rc = -EINVAL;
  2310. goto out;
  2311. }
  2312. /* need to remove/add the VF's accept_any_vlan bit */
  2313. accept_flags = bnx2x_leading_vfq(vf, accept_flags);
  2314. if (vlan)
  2315. clear_bit(BNX2X_ACCEPT_ANY_VLAN, &accept_flags);
  2316. else
  2317. set_bit(BNX2X_ACCEPT_ANY_VLAN, &accept_flags);
  2318. bnx2x_vf_prep_rx_mode(bp, LEADING_IDX, &rx_ramrod, vf,
  2319. accept_flags);
  2320. bnx2x_leading_vfq(vf, accept_flags) = accept_flags;
  2321. bnx2x_config_rx_mode(bp, &rx_ramrod);
  2322. /* configure the new vlan to device */
  2323. memset(&ramrod_param, 0, sizeof(ramrod_param));
  2324. __set_bit(RAMROD_COMP_WAIT, &ramrod_flags);
  2325. ramrod_param.vlan_mac_obj = vlan_obj;
  2326. ramrod_param.ramrod_flags = ramrod_flags;
  2327. set_bit(BNX2X_DONT_CONSUME_CAM_CREDIT,
  2328. &ramrod_param.user_req.vlan_mac_flags);
  2329. ramrod_param.user_req.u.vlan.vlan = vlan;
  2330. ramrod_param.user_req.cmd = BNX2X_VLAN_MAC_ADD;
  2331. rc = bnx2x_config_vlan_mac(bp, &ramrod_param);
  2332. if (rc) {
  2333. BNX2X_ERR("failed to configure vlan\n");
  2334. rc = -EINVAL;
  2335. goto out;
  2336. }
  2337. /* send queue update ramrod to configure default vlan and silent
  2338. * vlan removal
  2339. */
  2340. __set_bit(RAMROD_COMP_WAIT, &q_params.ramrod_flags);
  2341. q_params.cmd = BNX2X_Q_CMD_UPDATE;
  2342. q_params.q_obj = &bnx2x_leading_vfq(vf, sp_obj);
  2343. update_params = &q_params.params.update;
  2344. __set_bit(BNX2X_Q_UPDATE_DEF_VLAN_EN_CHNG,
  2345. &update_params->update_flags);
  2346. __set_bit(BNX2X_Q_UPDATE_SILENT_VLAN_REM_CHNG,
  2347. &update_params->update_flags);
  2348. if (vlan == 0) {
  2349. /* if vlan is 0 then we want to leave the VF traffic
  2350. * untagged, and leave the incoming traffic untouched
  2351. * (i.e. do not remove any vlan tags).
  2352. */
  2353. __clear_bit(BNX2X_Q_UPDATE_DEF_VLAN_EN,
  2354. &update_params->update_flags);
  2355. __clear_bit(BNX2X_Q_UPDATE_SILENT_VLAN_REM,
  2356. &update_params->update_flags);
  2357. } else {
  2358. /* configure default vlan to vf queue and set silent
  2359. * vlan removal (the vf remains unaware of this vlan).
  2360. */
  2361. __set_bit(BNX2X_Q_UPDATE_DEF_VLAN_EN,
  2362. &update_params->update_flags);
  2363. __set_bit(BNX2X_Q_UPDATE_SILENT_VLAN_REM,
  2364. &update_params->update_flags);
  2365. update_params->def_vlan = vlan;
  2366. update_params->silent_removal_value =
  2367. vlan & VLAN_VID_MASK;
  2368. update_params->silent_removal_mask = VLAN_VID_MASK;
  2369. }
  2370. /* Update the Queue state */
  2371. rc = bnx2x_queue_state_change(bp, &q_params);
  2372. if (rc) {
  2373. BNX2X_ERR("Failed to configure default VLAN\n");
  2374. goto out;
  2375. }
  2376. /* clear the flag indicating that this VF needs its vlan
  2377. * (will only be set if the HV configured the Vlan before vf was
  2378. * up and we were called because the VF came up later
  2379. */
  2380. out:
  2381. vf->cfg_flags &= ~VF_CFG_VLAN;
  2382. bnx2x_unlock_vf_pf_channel(bp, vf, CHANNEL_TLV_PF_SET_VLAN);
  2383. return rc;
  2384. }
  2385. /* crc is the first field in the bulletin board. Compute the crc over the
  2386. * entire bulletin board excluding the crc field itself. Use the length field
  2387. * as the Bulletin Board was posted by a PF with possibly a different version
  2388. * from the vf which will sample it. Therefore, the length is computed by the
  2389. * PF and the used blindly by the VF.
  2390. */
  2391. u32 bnx2x_crc_vf_bulletin(struct bnx2x *bp,
  2392. struct pf_vf_bulletin_content *bulletin)
  2393. {
  2394. return crc32(BULLETIN_CRC_SEED,
  2395. ((u8 *)bulletin) + sizeof(bulletin->crc),
  2396. bulletin->length - sizeof(bulletin->crc));
  2397. }
  2398. /* Check for new posts on the bulletin board */
  2399. enum sample_bulletin_result bnx2x_sample_bulletin(struct bnx2x *bp)
  2400. {
  2401. struct pf_vf_bulletin_content bulletin = bp->pf2vf_bulletin->content;
  2402. int attempts;
  2403. /* bulletin board hasn't changed since last sample */
  2404. if (bp->old_bulletin.version == bulletin.version)
  2405. return PFVF_BULLETIN_UNCHANGED;
  2406. /* validate crc of new bulletin board */
  2407. if (bp->old_bulletin.version != bp->pf2vf_bulletin->content.version) {
  2408. /* sampling structure in mid post may result with corrupted data
  2409. * validate crc to ensure coherency.
  2410. */
  2411. for (attempts = 0; attempts < BULLETIN_ATTEMPTS; attempts++) {
  2412. bulletin = bp->pf2vf_bulletin->content;
  2413. if (bulletin.crc == bnx2x_crc_vf_bulletin(bp,
  2414. &bulletin))
  2415. break;
  2416. BNX2X_ERR("bad crc on bulletin board. Contained %x computed %x\n",
  2417. bulletin.crc,
  2418. bnx2x_crc_vf_bulletin(bp, &bulletin));
  2419. }
  2420. if (attempts >= BULLETIN_ATTEMPTS) {
  2421. BNX2X_ERR("pf to vf bulletin board crc was wrong %d consecutive times. Aborting\n",
  2422. attempts);
  2423. return PFVF_BULLETIN_CRC_ERR;
  2424. }
  2425. }
  2426. /* the mac address in bulletin board is valid and is new */
  2427. if (bulletin.valid_bitmap & 1 << MAC_ADDR_VALID &&
  2428. !ether_addr_equal(bulletin.mac, bp->old_bulletin.mac)) {
  2429. /* update new mac to net device */
  2430. memcpy(bp->dev->dev_addr, bulletin.mac, ETH_ALEN);
  2431. }
  2432. /* the vlan in bulletin board is valid and is new */
  2433. if (bulletin.valid_bitmap & 1 << VLAN_VALID)
  2434. memcpy(&bulletin.vlan, &bp->old_bulletin.vlan, VLAN_HLEN);
  2435. /* copy new bulletin board to bp */
  2436. bp->old_bulletin = bulletin;
  2437. return PFVF_BULLETIN_UPDATED;
  2438. }
  2439. void bnx2x_timer_sriov(struct bnx2x *bp)
  2440. {
  2441. bnx2x_sample_bulletin(bp);
  2442. /* if channel is down we need to self destruct */
  2443. if (bp->old_bulletin.valid_bitmap & 1 << CHANNEL_DOWN)
  2444. bnx2x_schedule_sp_rtnl(bp, BNX2X_SP_RTNL_VFPF_CHANNEL_DOWN,
  2445. BNX2X_MSG_IOV);
  2446. }
  2447. void __iomem *bnx2x_vf_doorbells(struct bnx2x *bp)
  2448. {
  2449. /* vf doorbells are embedded within the regview */
  2450. return bp->regview + PXP_VF_ADDR_DB_START;
  2451. }
  2452. void bnx2x_vf_pci_dealloc(struct bnx2x *bp)
  2453. {
  2454. BNX2X_PCI_FREE(bp->vf2pf_mbox, bp->vf2pf_mbox_mapping,
  2455. sizeof(struct bnx2x_vf_mbx_msg));
  2456. BNX2X_PCI_FREE(bp->vf2pf_mbox, bp->pf2vf_bulletin_mapping,
  2457. sizeof(union pf_vf_bulletin));
  2458. }
  2459. int bnx2x_vf_pci_alloc(struct bnx2x *bp)
  2460. {
  2461. mutex_init(&bp->vf2pf_mutex);
  2462. /* allocate vf2pf mailbox for vf to pf channel */
  2463. bp->vf2pf_mbox = BNX2X_PCI_ALLOC(&bp->vf2pf_mbox_mapping,
  2464. sizeof(struct bnx2x_vf_mbx_msg));
  2465. if (!bp->vf2pf_mbox)
  2466. goto alloc_mem_err;
  2467. /* allocate pf 2 vf bulletin board */
  2468. bp->pf2vf_bulletin = BNX2X_PCI_ALLOC(&bp->pf2vf_bulletin_mapping,
  2469. sizeof(union pf_vf_bulletin));
  2470. if (!bp->pf2vf_bulletin)
  2471. goto alloc_mem_err;
  2472. return 0;
  2473. alloc_mem_err:
  2474. bnx2x_vf_pci_dealloc(bp);
  2475. return -ENOMEM;
  2476. }
  2477. void bnx2x_iov_channel_down(struct bnx2x *bp)
  2478. {
  2479. int vf_idx;
  2480. struct pf_vf_bulletin_content *bulletin;
  2481. if (!IS_SRIOV(bp))
  2482. return;
  2483. for_each_vf(bp, vf_idx) {
  2484. /* locate this VFs bulletin board and update the channel down
  2485. * bit
  2486. */
  2487. bulletin = BP_VF_BULLETIN(bp, vf_idx);
  2488. bulletin->valid_bitmap |= 1 << CHANNEL_DOWN;
  2489. /* update vf bulletin board */
  2490. bnx2x_post_vf_bulletin(bp, vf_idx);
  2491. }
  2492. }
  2493. void bnx2x_iov_task(struct work_struct *work)
  2494. {
  2495. struct bnx2x *bp = container_of(work, struct bnx2x, iov_task.work);
  2496. if (!netif_running(bp->dev))
  2497. return;
  2498. if (test_and_clear_bit(BNX2X_IOV_HANDLE_FLR,
  2499. &bp->iov_task_state))
  2500. bnx2x_vf_handle_flr_event(bp);
  2501. if (test_and_clear_bit(BNX2X_IOV_HANDLE_VF_MSG,
  2502. &bp->iov_task_state))
  2503. bnx2x_vf_mbx(bp);
  2504. }
  2505. void bnx2x_schedule_iov_task(struct bnx2x *bp, enum bnx2x_iov_flag flag)
  2506. {
  2507. smp_mb__before_atomic();
  2508. set_bit(flag, &bp->iov_task_state);
  2509. smp_mb__after_atomic();
  2510. DP(BNX2X_MSG_IOV, "Scheduling iov task [Flag: %d]\n", flag);
  2511. queue_delayed_work(bnx2x_iov_wq, &bp->iov_task, 0);
  2512. }