ice_switch.c 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2018, Intel Corporation. */
  3. #include "ice_switch.h"
  4. #define ICE_ETH_DA_OFFSET 0
  5. #define ICE_ETH_ETHTYPE_OFFSET 12
  6. #define ICE_ETH_VLAN_TCI_OFFSET 14
  7. #define ICE_MAX_VLAN_ID 0xFFF
  8. /* Dummy ethernet header needed in the ice_aqc_sw_rules_elem
  9. * struct to configure any switch filter rules.
  10. * {DA (6 bytes), SA(6 bytes),
  11. * Ether type (2 bytes for header without VLAN tag) OR
  12. * VLAN tag (4 bytes for header with VLAN tag) }
  13. *
  14. * Word on Hardcoded values
  15. * byte 0 = 0x2: to identify it as locally administered DA MAC
  16. * byte 6 = 0x2: to identify it as locally administered SA MAC
  17. * byte 12 = 0x81 & byte 13 = 0x00:
  18. * In case of VLAN filter first two bytes defines ether type (0x8100)
  19. * and remaining two bytes are placeholder for programming a given VLAN id
  20. * In case of Ether type filter it is treated as header without VLAN tag
  21. * and byte 12 and 13 is used to program a given Ether type instead
  22. */
  23. #define DUMMY_ETH_HDR_LEN 16
  24. static const u8 dummy_eth_header[DUMMY_ETH_HDR_LEN] = { 0x2, 0, 0, 0, 0, 0,
  25. 0x2, 0, 0, 0, 0, 0,
  26. 0x81, 0, 0, 0};
  27. #define ICE_SW_RULE_RX_TX_ETH_HDR_SIZE \
  28. (sizeof(struct ice_aqc_sw_rules_elem) - \
  29. sizeof(((struct ice_aqc_sw_rules_elem *)0)->pdata) + \
  30. sizeof(struct ice_sw_rule_lkup_rx_tx) + DUMMY_ETH_HDR_LEN - 1)
  31. #define ICE_SW_RULE_RX_TX_NO_HDR_SIZE \
  32. (sizeof(struct ice_aqc_sw_rules_elem) - \
  33. sizeof(((struct ice_aqc_sw_rules_elem *)0)->pdata) + \
  34. sizeof(struct ice_sw_rule_lkup_rx_tx) - 1)
  35. #define ICE_SW_RULE_LG_ACT_SIZE(n) \
  36. (sizeof(struct ice_aqc_sw_rules_elem) - \
  37. sizeof(((struct ice_aqc_sw_rules_elem *)0)->pdata) + \
  38. sizeof(struct ice_sw_rule_lg_act) - \
  39. sizeof(((struct ice_sw_rule_lg_act *)0)->act) + \
  40. ((n) * sizeof(((struct ice_sw_rule_lg_act *)0)->act)))
  41. #define ICE_SW_RULE_VSI_LIST_SIZE(n) \
  42. (sizeof(struct ice_aqc_sw_rules_elem) - \
  43. sizeof(((struct ice_aqc_sw_rules_elem *)0)->pdata) + \
  44. sizeof(struct ice_sw_rule_vsi_list) - \
  45. sizeof(((struct ice_sw_rule_vsi_list *)0)->vsi) + \
  46. ((n) * sizeof(((struct ice_sw_rule_vsi_list *)0)->vsi)))
  47. /**
  48. * ice_aq_alloc_free_res - command to allocate/free resources
  49. * @hw: pointer to the hw struct
  50. * @num_entries: number of resource entries in buffer
  51. * @buf: Indirect buffer to hold data parameters and response
  52. * @buf_size: size of buffer for indirect commands
  53. * @opc: pass in the command opcode
  54. * @cd: pointer to command details structure or NULL
  55. *
  56. * Helper function to allocate/free resources using the admin queue commands
  57. */
  58. static enum ice_status
  59. ice_aq_alloc_free_res(struct ice_hw *hw, u16 num_entries,
  60. struct ice_aqc_alloc_free_res_elem *buf, u16 buf_size,
  61. enum ice_adminq_opc opc, struct ice_sq_cd *cd)
  62. {
  63. struct ice_aqc_alloc_free_res_cmd *cmd;
  64. struct ice_aq_desc desc;
  65. cmd = &desc.params.sw_res_ctrl;
  66. if (!buf)
  67. return ICE_ERR_PARAM;
  68. if (buf_size < (num_entries * sizeof(buf->elem[0])))
  69. return ICE_ERR_PARAM;
  70. ice_fill_dflt_direct_cmd_desc(&desc, opc);
  71. desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
  72. cmd->num_entries = cpu_to_le16(num_entries);
  73. return ice_aq_send_cmd(hw, &desc, buf, buf_size, cd);
  74. }
  75. /**
  76. * ice_init_def_sw_recp - initialize the recipe book keeping tables
  77. * @hw: pointer to the hw struct
  78. *
  79. * Allocate memory for the entire recipe table and initialize the structures/
  80. * entries corresponding to basic recipes.
  81. */
  82. enum ice_status
  83. ice_init_def_sw_recp(struct ice_hw *hw)
  84. {
  85. struct ice_sw_recipe *recps;
  86. u8 i;
  87. recps = devm_kcalloc(ice_hw_to_dev(hw), ICE_MAX_NUM_RECIPES,
  88. sizeof(struct ice_sw_recipe), GFP_KERNEL);
  89. if (!recps)
  90. return ICE_ERR_NO_MEMORY;
  91. for (i = 0; i < ICE_SW_LKUP_LAST; i++) {
  92. recps[i].root_rid = i;
  93. INIT_LIST_HEAD(&recps[i].filt_rules);
  94. INIT_LIST_HEAD(&recps[i].filt_replay_rules);
  95. mutex_init(&recps[i].filt_rule_lock);
  96. }
  97. hw->switch_info->recp_list = recps;
  98. return 0;
  99. }
  100. /**
  101. * ice_aq_get_sw_cfg - get switch configuration
  102. * @hw: pointer to the hardware structure
  103. * @buf: pointer to the result buffer
  104. * @buf_size: length of the buffer available for response
  105. * @req_desc: pointer to requested descriptor
  106. * @num_elems: pointer to number of elements
  107. * @cd: pointer to command details structure or NULL
  108. *
  109. * Get switch configuration (0x0200) to be placed in 'buff'.
  110. * This admin command returns information such as initial VSI/port number
  111. * and switch ID it belongs to.
  112. *
  113. * NOTE: *req_desc is both an input/output parameter.
  114. * The caller of this function first calls this function with *request_desc set
  115. * to 0. If the response from f/w has *req_desc set to 0, all the switch
  116. * configuration information has been returned; if non-zero (meaning not all
  117. * the information was returned), the caller should call this function again
  118. * with *req_desc set to the previous value returned by f/w to get the
  119. * next block of switch configuration information.
  120. *
  121. * *num_elems is output only parameter. This reflects the number of elements
  122. * in response buffer. The caller of this function to use *num_elems while
  123. * parsing the response buffer.
  124. */
  125. static enum ice_status
  126. ice_aq_get_sw_cfg(struct ice_hw *hw, struct ice_aqc_get_sw_cfg_resp *buf,
  127. u16 buf_size, u16 *req_desc, u16 *num_elems,
  128. struct ice_sq_cd *cd)
  129. {
  130. struct ice_aqc_get_sw_cfg *cmd;
  131. enum ice_status status;
  132. struct ice_aq_desc desc;
  133. ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_sw_cfg);
  134. cmd = &desc.params.get_sw_conf;
  135. cmd->element = cpu_to_le16(*req_desc);
  136. status = ice_aq_send_cmd(hw, &desc, buf, buf_size, cd);
  137. if (!status) {
  138. *req_desc = le16_to_cpu(cmd->element);
  139. *num_elems = le16_to_cpu(cmd->num_elems);
  140. }
  141. return status;
  142. }
  143. /**
  144. * ice_aq_add_vsi
  145. * @hw: pointer to the hw struct
  146. * @vsi_ctx: pointer to a VSI context struct
  147. * @cd: pointer to command details structure or NULL
  148. *
  149. * Add a VSI context to the hardware (0x0210)
  150. */
  151. static enum ice_status
  152. ice_aq_add_vsi(struct ice_hw *hw, struct ice_vsi_ctx *vsi_ctx,
  153. struct ice_sq_cd *cd)
  154. {
  155. struct ice_aqc_add_update_free_vsi_resp *res;
  156. struct ice_aqc_add_get_update_free_vsi *cmd;
  157. struct ice_aq_desc desc;
  158. enum ice_status status;
  159. cmd = &desc.params.vsi_cmd;
  160. res = &desc.params.add_update_free_vsi_res;
  161. ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_add_vsi);
  162. if (!vsi_ctx->alloc_from_pool)
  163. cmd->vsi_num = cpu_to_le16(vsi_ctx->vsi_num |
  164. ICE_AQ_VSI_IS_VALID);
  165. cmd->vf_id = vsi_ctx->vf_num;
  166. cmd->vsi_flags = cpu_to_le16(vsi_ctx->flags);
  167. desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
  168. status = ice_aq_send_cmd(hw, &desc, &vsi_ctx->info,
  169. sizeof(vsi_ctx->info), cd);
  170. if (!status) {
  171. vsi_ctx->vsi_num = le16_to_cpu(res->vsi_num) & ICE_AQ_VSI_NUM_M;
  172. vsi_ctx->vsis_allocd = le16_to_cpu(res->vsi_used);
  173. vsi_ctx->vsis_unallocated = le16_to_cpu(res->vsi_free);
  174. }
  175. return status;
  176. }
  177. /**
  178. * ice_aq_free_vsi
  179. * @hw: pointer to the hw struct
  180. * @vsi_ctx: pointer to a VSI context struct
  181. * @keep_vsi_alloc: keep VSI allocation as part of this PF's resources
  182. * @cd: pointer to command details structure or NULL
  183. *
  184. * Free VSI context info from hardware (0x0213)
  185. */
  186. static enum ice_status
  187. ice_aq_free_vsi(struct ice_hw *hw, struct ice_vsi_ctx *vsi_ctx,
  188. bool keep_vsi_alloc, struct ice_sq_cd *cd)
  189. {
  190. struct ice_aqc_add_update_free_vsi_resp *resp;
  191. struct ice_aqc_add_get_update_free_vsi *cmd;
  192. struct ice_aq_desc desc;
  193. enum ice_status status;
  194. cmd = &desc.params.vsi_cmd;
  195. resp = &desc.params.add_update_free_vsi_res;
  196. ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_free_vsi);
  197. cmd->vsi_num = cpu_to_le16(vsi_ctx->vsi_num | ICE_AQ_VSI_IS_VALID);
  198. if (keep_vsi_alloc)
  199. cmd->cmd_flags = cpu_to_le16(ICE_AQ_VSI_KEEP_ALLOC);
  200. status = ice_aq_send_cmd(hw, &desc, NULL, 0, cd);
  201. if (!status) {
  202. vsi_ctx->vsis_allocd = le16_to_cpu(resp->vsi_used);
  203. vsi_ctx->vsis_unallocated = le16_to_cpu(resp->vsi_free);
  204. }
  205. return status;
  206. }
  207. /**
  208. * ice_aq_update_vsi
  209. * @hw: pointer to the hw struct
  210. * @vsi_ctx: pointer to a VSI context struct
  211. * @cd: pointer to command details structure or NULL
  212. *
  213. * Update VSI context in the hardware (0x0211)
  214. */
  215. static enum ice_status
  216. ice_aq_update_vsi(struct ice_hw *hw, struct ice_vsi_ctx *vsi_ctx,
  217. struct ice_sq_cd *cd)
  218. {
  219. struct ice_aqc_add_update_free_vsi_resp *resp;
  220. struct ice_aqc_add_get_update_free_vsi *cmd;
  221. struct ice_aq_desc desc;
  222. enum ice_status status;
  223. cmd = &desc.params.vsi_cmd;
  224. resp = &desc.params.add_update_free_vsi_res;
  225. ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_update_vsi);
  226. cmd->vsi_num = cpu_to_le16(vsi_ctx->vsi_num | ICE_AQ_VSI_IS_VALID);
  227. desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
  228. status = ice_aq_send_cmd(hw, &desc, &vsi_ctx->info,
  229. sizeof(vsi_ctx->info), cd);
  230. if (!status) {
  231. vsi_ctx->vsis_allocd = le16_to_cpu(resp->vsi_used);
  232. vsi_ctx->vsis_unallocated = le16_to_cpu(resp->vsi_free);
  233. }
  234. return status;
  235. }
  236. /**
  237. * ice_is_vsi_valid - check whether the VSI is valid or not
  238. * @hw: pointer to the hw struct
  239. * @vsi_handle: VSI handle
  240. *
  241. * check whether the VSI is valid or not
  242. */
  243. bool ice_is_vsi_valid(struct ice_hw *hw, u16 vsi_handle)
  244. {
  245. return vsi_handle < ICE_MAX_VSI && hw->vsi_ctx[vsi_handle];
  246. }
  247. /**
  248. * ice_get_hw_vsi_num - return the hw VSI number
  249. * @hw: pointer to the hw struct
  250. * @vsi_handle: VSI handle
  251. *
  252. * return the hw VSI number
  253. * Caution: call this function only if VSI is valid (ice_is_vsi_valid)
  254. */
  255. u16 ice_get_hw_vsi_num(struct ice_hw *hw, u16 vsi_handle)
  256. {
  257. return hw->vsi_ctx[vsi_handle]->vsi_num;
  258. }
  259. /**
  260. * ice_get_vsi_ctx - return the VSI context entry for a given VSI handle
  261. * @hw: pointer to the hw struct
  262. * @vsi_handle: VSI handle
  263. *
  264. * return the VSI context entry for a given VSI handle
  265. */
  266. struct ice_vsi_ctx *ice_get_vsi_ctx(struct ice_hw *hw, u16 vsi_handle)
  267. {
  268. return (vsi_handle >= ICE_MAX_VSI) ? NULL : hw->vsi_ctx[vsi_handle];
  269. }
  270. /**
  271. * ice_save_vsi_ctx - save the VSI context for a given VSI handle
  272. * @hw: pointer to the hw struct
  273. * @vsi_handle: VSI handle
  274. * @vsi: VSI context pointer
  275. *
  276. * save the VSI context entry for a given VSI handle
  277. */
  278. static void ice_save_vsi_ctx(struct ice_hw *hw, u16 vsi_handle,
  279. struct ice_vsi_ctx *vsi)
  280. {
  281. hw->vsi_ctx[vsi_handle] = vsi;
  282. }
  283. /**
  284. * ice_clear_vsi_ctx - clear the VSI context entry
  285. * @hw: pointer to the hw struct
  286. * @vsi_handle: VSI handle
  287. *
  288. * clear the VSI context entry
  289. */
  290. static void ice_clear_vsi_ctx(struct ice_hw *hw, u16 vsi_handle)
  291. {
  292. struct ice_vsi_ctx *vsi;
  293. vsi = ice_get_vsi_ctx(hw, vsi_handle);
  294. if (vsi) {
  295. devm_kfree(ice_hw_to_dev(hw), vsi);
  296. hw->vsi_ctx[vsi_handle] = NULL;
  297. }
  298. }
  299. /**
  300. * ice_add_vsi - add VSI context to the hardware and VSI handle list
  301. * @hw: pointer to the hw struct
  302. * @vsi_handle: unique VSI handle provided by drivers
  303. * @vsi_ctx: pointer to a VSI context struct
  304. * @cd: pointer to command details structure or NULL
  305. *
  306. * Add a VSI context to the hardware also add it into the VSI handle list.
  307. * If this function gets called after reset for existing VSIs then update
  308. * with the new HW VSI number in the corresponding VSI handle list entry.
  309. */
  310. enum ice_status
  311. ice_add_vsi(struct ice_hw *hw, u16 vsi_handle, struct ice_vsi_ctx *vsi_ctx,
  312. struct ice_sq_cd *cd)
  313. {
  314. struct ice_vsi_ctx *tmp_vsi_ctx;
  315. enum ice_status status;
  316. if (vsi_handle >= ICE_MAX_VSI)
  317. return ICE_ERR_PARAM;
  318. status = ice_aq_add_vsi(hw, vsi_ctx, cd);
  319. if (status)
  320. return status;
  321. tmp_vsi_ctx = ice_get_vsi_ctx(hw, vsi_handle);
  322. if (!tmp_vsi_ctx) {
  323. /* Create a new vsi context */
  324. tmp_vsi_ctx = devm_kzalloc(ice_hw_to_dev(hw),
  325. sizeof(*tmp_vsi_ctx), GFP_KERNEL);
  326. if (!tmp_vsi_ctx) {
  327. ice_aq_free_vsi(hw, vsi_ctx, false, cd);
  328. return ICE_ERR_NO_MEMORY;
  329. }
  330. *tmp_vsi_ctx = *vsi_ctx;
  331. ice_save_vsi_ctx(hw, vsi_handle, tmp_vsi_ctx);
  332. } else {
  333. /* update with new HW VSI num */
  334. if (tmp_vsi_ctx->vsi_num != vsi_ctx->vsi_num)
  335. tmp_vsi_ctx->vsi_num = vsi_ctx->vsi_num;
  336. }
  337. return status;
  338. }
  339. /**
  340. * ice_free_vsi- free VSI context from hardware and VSI handle list
  341. * @hw: pointer to the hw struct
  342. * @vsi_handle: unique VSI handle
  343. * @vsi_ctx: pointer to a VSI context struct
  344. * @keep_vsi_alloc: keep VSI allocation as part of this PF's resources
  345. * @cd: pointer to command details structure or NULL
  346. *
  347. * Free VSI context info from hardware as well as from VSI handle list
  348. */
  349. enum ice_status
  350. ice_free_vsi(struct ice_hw *hw, u16 vsi_handle, struct ice_vsi_ctx *vsi_ctx,
  351. bool keep_vsi_alloc, struct ice_sq_cd *cd)
  352. {
  353. enum ice_status status;
  354. if (!ice_is_vsi_valid(hw, vsi_handle))
  355. return ICE_ERR_PARAM;
  356. vsi_ctx->vsi_num = ice_get_hw_vsi_num(hw, vsi_handle);
  357. status = ice_aq_free_vsi(hw, vsi_ctx, keep_vsi_alloc, cd);
  358. if (!status)
  359. ice_clear_vsi_ctx(hw, vsi_handle);
  360. return status;
  361. }
  362. /**
  363. * ice_update_vsi
  364. * @hw: pointer to the hw struct
  365. * @vsi_handle: unique VSI handle
  366. * @vsi_ctx: pointer to a VSI context struct
  367. * @cd: pointer to command details structure or NULL
  368. *
  369. * Update VSI context in the hardware
  370. */
  371. enum ice_status
  372. ice_update_vsi(struct ice_hw *hw, u16 vsi_handle, struct ice_vsi_ctx *vsi_ctx,
  373. struct ice_sq_cd *cd)
  374. {
  375. if (!ice_is_vsi_valid(hw, vsi_handle))
  376. return ICE_ERR_PARAM;
  377. vsi_ctx->vsi_num = ice_get_hw_vsi_num(hw, vsi_handle);
  378. return ice_aq_update_vsi(hw, vsi_ctx, cd);
  379. }
  380. /**
  381. * ice_aq_alloc_free_vsi_list
  382. * @hw: pointer to the hw struct
  383. * @vsi_list_id: VSI list id returned or used for lookup
  384. * @lkup_type: switch rule filter lookup type
  385. * @opc: switch rules population command type - pass in the command opcode
  386. *
  387. * allocates or free a VSI list resource
  388. */
  389. static enum ice_status
  390. ice_aq_alloc_free_vsi_list(struct ice_hw *hw, u16 *vsi_list_id,
  391. enum ice_sw_lkup_type lkup_type,
  392. enum ice_adminq_opc opc)
  393. {
  394. struct ice_aqc_alloc_free_res_elem *sw_buf;
  395. struct ice_aqc_res_elem *vsi_ele;
  396. enum ice_status status;
  397. u16 buf_len;
  398. buf_len = sizeof(*sw_buf);
  399. sw_buf = devm_kzalloc(ice_hw_to_dev(hw), buf_len, GFP_KERNEL);
  400. if (!sw_buf)
  401. return ICE_ERR_NO_MEMORY;
  402. sw_buf->num_elems = cpu_to_le16(1);
  403. if (lkup_type == ICE_SW_LKUP_MAC ||
  404. lkup_type == ICE_SW_LKUP_MAC_VLAN ||
  405. lkup_type == ICE_SW_LKUP_ETHERTYPE ||
  406. lkup_type == ICE_SW_LKUP_ETHERTYPE_MAC ||
  407. lkup_type == ICE_SW_LKUP_PROMISC ||
  408. lkup_type == ICE_SW_LKUP_PROMISC_VLAN) {
  409. sw_buf->res_type = cpu_to_le16(ICE_AQC_RES_TYPE_VSI_LIST_REP);
  410. } else if (lkup_type == ICE_SW_LKUP_VLAN) {
  411. sw_buf->res_type =
  412. cpu_to_le16(ICE_AQC_RES_TYPE_VSI_LIST_PRUNE);
  413. } else {
  414. status = ICE_ERR_PARAM;
  415. goto ice_aq_alloc_free_vsi_list_exit;
  416. }
  417. if (opc == ice_aqc_opc_free_res)
  418. sw_buf->elem[0].e.sw_resp = cpu_to_le16(*vsi_list_id);
  419. status = ice_aq_alloc_free_res(hw, 1, sw_buf, buf_len, opc, NULL);
  420. if (status)
  421. goto ice_aq_alloc_free_vsi_list_exit;
  422. if (opc == ice_aqc_opc_alloc_res) {
  423. vsi_ele = &sw_buf->elem[0];
  424. *vsi_list_id = le16_to_cpu(vsi_ele->e.sw_resp);
  425. }
  426. ice_aq_alloc_free_vsi_list_exit:
  427. devm_kfree(ice_hw_to_dev(hw), sw_buf);
  428. return status;
  429. }
  430. /**
  431. * ice_aq_sw_rules - add/update/remove switch rules
  432. * @hw: pointer to the hw struct
  433. * @rule_list: pointer to switch rule population list
  434. * @rule_list_sz: total size of the rule list in bytes
  435. * @num_rules: number of switch rules in the rule_list
  436. * @opc: switch rules population command type - pass in the command opcode
  437. * @cd: pointer to command details structure or NULL
  438. *
  439. * Add(0x02a0)/Update(0x02a1)/Remove(0x02a2) switch rules commands to firmware
  440. */
  441. static enum ice_status
  442. ice_aq_sw_rules(struct ice_hw *hw, void *rule_list, u16 rule_list_sz,
  443. u8 num_rules, enum ice_adminq_opc opc, struct ice_sq_cd *cd)
  444. {
  445. struct ice_aq_desc desc;
  446. if (opc != ice_aqc_opc_add_sw_rules &&
  447. opc != ice_aqc_opc_update_sw_rules &&
  448. opc != ice_aqc_opc_remove_sw_rules)
  449. return ICE_ERR_PARAM;
  450. ice_fill_dflt_direct_cmd_desc(&desc, opc);
  451. desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
  452. desc.params.sw_rules.num_rules_fltr_entry_index =
  453. cpu_to_le16(num_rules);
  454. return ice_aq_send_cmd(hw, &desc, rule_list, rule_list_sz, cd);
  455. }
  456. /* ice_init_port_info - Initialize port_info with switch configuration data
  457. * @pi: pointer to port_info
  458. * @vsi_port_num: VSI number or port number
  459. * @type: Type of switch element (port or VSI)
  460. * @swid: switch ID of the switch the element is attached to
  461. * @pf_vf_num: PF or VF number
  462. * @is_vf: true if the element is a VF, false otherwise
  463. */
  464. static void
  465. ice_init_port_info(struct ice_port_info *pi, u16 vsi_port_num, u8 type,
  466. u16 swid, u16 pf_vf_num, bool is_vf)
  467. {
  468. switch (type) {
  469. case ICE_AQC_GET_SW_CONF_RESP_PHYS_PORT:
  470. pi->lport = (u8)(vsi_port_num & ICE_LPORT_MASK);
  471. pi->sw_id = swid;
  472. pi->pf_vf_num = pf_vf_num;
  473. pi->is_vf = is_vf;
  474. pi->dflt_tx_vsi_num = ICE_DFLT_VSI_INVAL;
  475. pi->dflt_rx_vsi_num = ICE_DFLT_VSI_INVAL;
  476. break;
  477. default:
  478. ice_debug(pi->hw, ICE_DBG_SW,
  479. "incorrect VSI/port type received\n");
  480. break;
  481. }
  482. }
  483. /* ice_get_initial_sw_cfg - Get initial port and default VSI data
  484. * @hw: pointer to the hardware structure
  485. */
  486. enum ice_status ice_get_initial_sw_cfg(struct ice_hw *hw)
  487. {
  488. struct ice_aqc_get_sw_cfg_resp *rbuf;
  489. enum ice_status status;
  490. u16 req_desc = 0;
  491. u16 num_elems;
  492. u16 i;
  493. rbuf = devm_kzalloc(ice_hw_to_dev(hw), ICE_SW_CFG_MAX_BUF_LEN,
  494. GFP_KERNEL);
  495. if (!rbuf)
  496. return ICE_ERR_NO_MEMORY;
  497. /* Multiple calls to ice_aq_get_sw_cfg may be required
  498. * to get all the switch configuration information. The need
  499. * for additional calls is indicated by ice_aq_get_sw_cfg
  500. * writing a non-zero value in req_desc
  501. */
  502. do {
  503. status = ice_aq_get_sw_cfg(hw, rbuf, ICE_SW_CFG_MAX_BUF_LEN,
  504. &req_desc, &num_elems, NULL);
  505. if (status)
  506. break;
  507. for (i = 0; i < num_elems; i++) {
  508. struct ice_aqc_get_sw_cfg_resp_elem *ele;
  509. u16 pf_vf_num, swid, vsi_port_num;
  510. bool is_vf = false;
  511. u8 type;
  512. ele = rbuf[i].elements;
  513. vsi_port_num = le16_to_cpu(ele->vsi_port_num) &
  514. ICE_AQC_GET_SW_CONF_RESP_VSI_PORT_NUM_M;
  515. pf_vf_num = le16_to_cpu(ele->pf_vf_num) &
  516. ICE_AQC_GET_SW_CONF_RESP_FUNC_NUM_M;
  517. swid = le16_to_cpu(ele->swid);
  518. if (le16_to_cpu(ele->pf_vf_num) &
  519. ICE_AQC_GET_SW_CONF_RESP_IS_VF)
  520. is_vf = true;
  521. type = le16_to_cpu(ele->vsi_port_num) >>
  522. ICE_AQC_GET_SW_CONF_RESP_TYPE_S;
  523. if (type == ICE_AQC_GET_SW_CONF_RESP_VSI) {
  524. /* FW VSI is not needed. Just continue. */
  525. continue;
  526. }
  527. ice_init_port_info(hw->port_info, vsi_port_num,
  528. type, swid, pf_vf_num, is_vf);
  529. }
  530. } while (req_desc && !status);
  531. devm_kfree(ice_hw_to_dev(hw), (void *)rbuf);
  532. return status;
  533. }
  534. /**
  535. * ice_fill_sw_info - Helper function to populate lb_en and lan_en
  536. * @hw: pointer to the hardware structure
  537. * @f_info: filter info structure to fill/update
  538. *
  539. * This helper function populates the lb_en and lan_en elements of the provided
  540. * ice_fltr_info struct using the switch's type and characteristics of the
  541. * switch rule being configured.
  542. */
  543. static void ice_fill_sw_info(struct ice_hw *hw, struct ice_fltr_info *f_info)
  544. {
  545. f_info->lb_en = false;
  546. f_info->lan_en = false;
  547. if ((f_info->flag & ICE_FLTR_TX) &&
  548. (f_info->fltr_act == ICE_FWD_TO_VSI ||
  549. f_info->fltr_act == ICE_FWD_TO_VSI_LIST ||
  550. f_info->fltr_act == ICE_FWD_TO_Q ||
  551. f_info->fltr_act == ICE_FWD_TO_QGRP)) {
  552. f_info->lb_en = true;
  553. if (!(hw->evb_veb && f_info->lkup_type == ICE_SW_LKUP_MAC &&
  554. is_unicast_ether_addr(f_info->l_data.mac.mac_addr)))
  555. f_info->lan_en = true;
  556. }
  557. }
  558. /**
  559. * ice_fill_sw_rule - Helper function to fill switch rule structure
  560. * @hw: pointer to the hardware structure
  561. * @f_info: entry containing packet forwarding information
  562. * @s_rule: switch rule structure to be filled in based on mac_entry
  563. * @opc: switch rules population command type - pass in the command opcode
  564. */
  565. static void
  566. ice_fill_sw_rule(struct ice_hw *hw, struct ice_fltr_info *f_info,
  567. struct ice_aqc_sw_rules_elem *s_rule, enum ice_adminq_opc opc)
  568. {
  569. u16 vlan_id = ICE_MAX_VLAN_ID + 1;
  570. void *daddr = NULL;
  571. u16 eth_hdr_sz;
  572. u8 *eth_hdr;
  573. u32 act = 0;
  574. __be16 *off;
  575. u8 q_rgn;
  576. if (opc == ice_aqc_opc_remove_sw_rules) {
  577. s_rule->pdata.lkup_tx_rx.act = 0;
  578. s_rule->pdata.lkup_tx_rx.index =
  579. cpu_to_le16(f_info->fltr_rule_id);
  580. s_rule->pdata.lkup_tx_rx.hdr_len = 0;
  581. return;
  582. }
  583. eth_hdr_sz = sizeof(dummy_eth_header);
  584. eth_hdr = s_rule->pdata.lkup_tx_rx.hdr;
  585. /* initialize the ether header with a dummy header */
  586. memcpy(eth_hdr, dummy_eth_header, eth_hdr_sz);
  587. ice_fill_sw_info(hw, f_info);
  588. switch (f_info->fltr_act) {
  589. case ICE_FWD_TO_VSI:
  590. act |= (f_info->fwd_id.hw_vsi_id << ICE_SINGLE_ACT_VSI_ID_S) &
  591. ICE_SINGLE_ACT_VSI_ID_M;
  592. if (f_info->lkup_type != ICE_SW_LKUP_VLAN)
  593. act |= ICE_SINGLE_ACT_VSI_FORWARDING |
  594. ICE_SINGLE_ACT_VALID_BIT;
  595. break;
  596. case ICE_FWD_TO_VSI_LIST:
  597. act |= ICE_SINGLE_ACT_VSI_LIST;
  598. act |= (f_info->fwd_id.vsi_list_id <<
  599. ICE_SINGLE_ACT_VSI_LIST_ID_S) &
  600. ICE_SINGLE_ACT_VSI_LIST_ID_M;
  601. if (f_info->lkup_type != ICE_SW_LKUP_VLAN)
  602. act |= ICE_SINGLE_ACT_VSI_FORWARDING |
  603. ICE_SINGLE_ACT_VALID_BIT;
  604. break;
  605. case ICE_FWD_TO_Q:
  606. act |= ICE_SINGLE_ACT_TO_Q;
  607. act |= (f_info->fwd_id.q_id << ICE_SINGLE_ACT_Q_INDEX_S) &
  608. ICE_SINGLE_ACT_Q_INDEX_M;
  609. break;
  610. case ICE_DROP_PACKET:
  611. act |= ICE_SINGLE_ACT_VSI_FORWARDING | ICE_SINGLE_ACT_DROP |
  612. ICE_SINGLE_ACT_VALID_BIT;
  613. break;
  614. case ICE_FWD_TO_QGRP:
  615. q_rgn = f_info->qgrp_size > 0 ?
  616. (u8)ilog2(f_info->qgrp_size) : 0;
  617. act |= ICE_SINGLE_ACT_TO_Q;
  618. act |= (f_info->fwd_id.q_id << ICE_SINGLE_ACT_Q_INDEX_S) &
  619. ICE_SINGLE_ACT_Q_INDEX_M;
  620. act |= (q_rgn << ICE_SINGLE_ACT_Q_REGION_S) &
  621. ICE_SINGLE_ACT_Q_REGION_M;
  622. break;
  623. default:
  624. return;
  625. }
  626. if (f_info->lb_en)
  627. act |= ICE_SINGLE_ACT_LB_ENABLE;
  628. if (f_info->lan_en)
  629. act |= ICE_SINGLE_ACT_LAN_ENABLE;
  630. switch (f_info->lkup_type) {
  631. case ICE_SW_LKUP_MAC:
  632. daddr = f_info->l_data.mac.mac_addr;
  633. break;
  634. case ICE_SW_LKUP_VLAN:
  635. vlan_id = f_info->l_data.vlan.vlan_id;
  636. if (f_info->fltr_act == ICE_FWD_TO_VSI ||
  637. f_info->fltr_act == ICE_FWD_TO_VSI_LIST) {
  638. act |= ICE_SINGLE_ACT_PRUNE;
  639. act |= ICE_SINGLE_ACT_EGRESS | ICE_SINGLE_ACT_INGRESS;
  640. }
  641. break;
  642. case ICE_SW_LKUP_ETHERTYPE_MAC:
  643. daddr = f_info->l_data.ethertype_mac.mac_addr;
  644. /* fall-through */
  645. case ICE_SW_LKUP_ETHERTYPE:
  646. off = (__be16 *)(eth_hdr + ICE_ETH_ETHTYPE_OFFSET);
  647. *off = cpu_to_be16(f_info->l_data.ethertype_mac.ethertype);
  648. break;
  649. case ICE_SW_LKUP_MAC_VLAN:
  650. daddr = f_info->l_data.mac_vlan.mac_addr;
  651. vlan_id = f_info->l_data.mac_vlan.vlan_id;
  652. break;
  653. case ICE_SW_LKUP_PROMISC_VLAN:
  654. vlan_id = f_info->l_data.mac_vlan.vlan_id;
  655. /* fall-through */
  656. case ICE_SW_LKUP_PROMISC:
  657. daddr = f_info->l_data.mac_vlan.mac_addr;
  658. break;
  659. default:
  660. break;
  661. }
  662. s_rule->type = (f_info->flag & ICE_FLTR_RX) ?
  663. cpu_to_le16(ICE_AQC_SW_RULES_T_LKUP_RX) :
  664. cpu_to_le16(ICE_AQC_SW_RULES_T_LKUP_TX);
  665. /* Recipe set depending on lookup type */
  666. s_rule->pdata.lkup_tx_rx.recipe_id = cpu_to_le16(f_info->lkup_type);
  667. s_rule->pdata.lkup_tx_rx.src = cpu_to_le16(f_info->src);
  668. s_rule->pdata.lkup_tx_rx.act = cpu_to_le32(act);
  669. if (daddr)
  670. ether_addr_copy(eth_hdr + ICE_ETH_DA_OFFSET, daddr);
  671. if (!(vlan_id > ICE_MAX_VLAN_ID)) {
  672. off = (__be16 *)(eth_hdr + ICE_ETH_VLAN_TCI_OFFSET);
  673. *off = cpu_to_be16(vlan_id);
  674. }
  675. /* Create the switch rule with the final dummy Ethernet header */
  676. if (opc != ice_aqc_opc_update_sw_rules)
  677. s_rule->pdata.lkup_tx_rx.hdr_len = cpu_to_le16(eth_hdr_sz);
  678. }
  679. /**
  680. * ice_add_marker_act
  681. * @hw: pointer to the hardware structure
  682. * @m_ent: the management entry for which sw marker needs to be added
  683. * @sw_marker: sw marker to tag the Rx descriptor with
  684. * @l_id: large action resource id
  685. *
  686. * Create a large action to hold software marker and update the switch rule
  687. * entry pointed by m_ent with newly created large action
  688. */
  689. static enum ice_status
  690. ice_add_marker_act(struct ice_hw *hw, struct ice_fltr_mgmt_list_entry *m_ent,
  691. u16 sw_marker, u16 l_id)
  692. {
  693. struct ice_aqc_sw_rules_elem *lg_act, *rx_tx;
  694. /* For software marker we need 3 large actions
  695. * 1. FWD action: FWD TO VSI or VSI LIST
  696. * 2. GENERIC VALUE action to hold the profile id
  697. * 3. GENERIC VALUE action to hold the software marker id
  698. */
  699. const u16 num_lg_acts = 3;
  700. enum ice_status status;
  701. u16 lg_act_size;
  702. u16 rules_size;
  703. u32 act;
  704. u16 id;
  705. if (m_ent->fltr_info.lkup_type != ICE_SW_LKUP_MAC)
  706. return ICE_ERR_PARAM;
  707. /* Create two back-to-back switch rules and submit them to the HW using
  708. * one memory buffer:
  709. * 1. Large Action
  710. * 2. Look up tx rx
  711. */
  712. lg_act_size = (u16)ICE_SW_RULE_LG_ACT_SIZE(num_lg_acts);
  713. rules_size = lg_act_size + ICE_SW_RULE_RX_TX_ETH_HDR_SIZE;
  714. lg_act = devm_kzalloc(ice_hw_to_dev(hw), rules_size, GFP_KERNEL);
  715. if (!lg_act)
  716. return ICE_ERR_NO_MEMORY;
  717. rx_tx = (struct ice_aqc_sw_rules_elem *)((u8 *)lg_act + lg_act_size);
  718. /* Fill in the first switch rule i.e. large action */
  719. lg_act->type = cpu_to_le16(ICE_AQC_SW_RULES_T_LG_ACT);
  720. lg_act->pdata.lg_act.index = cpu_to_le16(l_id);
  721. lg_act->pdata.lg_act.size = cpu_to_le16(num_lg_acts);
  722. /* First action VSI forwarding or VSI list forwarding depending on how
  723. * many VSIs
  724. */
  725. id = (m_ent->vsi_count > 1) ? m_ent->fltr_info.fwd_id.vsi_list_id :
  726. m_ent->fltr_info.fwd_id.hw_vsi_id;
  727. act = ICE_LG_ACT_VSI_FORWARDING | ICE_LG_ACT_VALID_BIT;
  728. act |= (id << ICE_LG_ACT_VSI_LIST_ID_S) &
  729. ICE_LG_ACT_VSI_LIST_ID_M;
  730. if (m_ent->vsi_count > 1)
  731. act |= ICE_LG_ACT_VSI_LIST;
  732. lg_act->pdata.lg_act.act[0] = cpu_to_le32(act);
  733. /* Second action descriptor type */
  734. act = ICE_LG_ACT_GENERIC;
  735. act |= (1 << ICE_LG_ACT_GENERIC_VALUE_S) & ICE_LG_ACT_GENERIC_VALUE_M;
  736. lg_act->pdata.lg_act.act[1] = cpu_to_le32(act);
  737. act = (ICE_LG_ACT_GENERIC_OFF_RX_DESC_PROF_IDX <<
  738. ICE_LG_ACT_GENERIC_OFFSET_S) & ICE_LG_ACT_GENERIC_OFFSET_M;
  739. /* Third action Marker value */
  740. act |= ICE_LG_ACT_GENERIC;
  741. act |= (sw_marker << ICE_LG_ACT_GENERIC_VALUE_S) &
  742. ICE_LG_ACT_GENERIC_VALUE_M;
  743. lg_act->pdata.lg_act.act[2] = cpu_to_le32(act);
  744. /* call the fill switch rule to fill the lookup tx rx structure */
  745. ice_fill_sw_rule(hw, &m_ent->fltr_info, rx_tx,
  746. ice_aqc_opc_update_sw_rules);
  747. /* Update the action to point to the large action id */
  748. rx_tx->pdata.lkup_tx_rx.act =
  749. cpu_to_le32(ICE_SINGLE_ACT_PTR |
  750. ((l_id << ICE_SINGLE_ACT_PTR_VAL_S) &
  751. ICE_SINGLE_ACT_PTR_VAL_M));
  752. /* Use the filter rule id of the previously created rule with single
  753. * act. Once the update happens, hardware will treat this as large
  754. * action
  755. */
  756. rx_tx->pdata.lkup_tx_rx.index =
  757. cpu_to_le16(m_ent->fltr_info.fltr_rule_id);
  758. status = ice_aq_sw_rules(hw, lg_act, rules_size, 2,
  759. ice_aqc_opc_update_sw_rules, NULL);
  760. if (!status) {
  761. m_ent->lg_act_idx = l_id;
  762. m_ent->sw_marker_id = sw_marker;
  763. }
  764. devm_kfree(ice_hw_to_dev(hw), lg_act);
  765. return status;
  766. }
  767. /**
  768. * ice_create_vsi_list_map
  769. * @hw: pointer to the hardware structure
  770. * @vsi_handle_arr: array of VSI handles to set in the VSI mapping
  771. * @num_vsi: number of VSI handles in the array
  772. * @vsi_list_id: VSI list id generated as part of allocate resource
  773. *
  774. * Helper function to create a new entry of VSI list id to VSI mapping
  775. * using the given VSI list id
  776. */
  777. static struct ice_vsi_list_map_info *
  778. ice_create_vsi_list_map(struct ice_hw *hw, u16 *vsi_handle_arr, u16 num_vsi,
  779. u16 vsi_list_id)
  780. {
  781. struct ice_switch_info *sw = hw->switch_info;
  782. struct ice_vsi_list_map_info *v_map;
  783. int i;
  784. v_map = devm_kcalloc(ice_hw_to_dev(hw), 1, sizeof(*v_map), GFP_KERNEL);
  785. if (!v_map)
  786. return NULL;
  787. v_map->vsi_list_id = vsi_list_id;
  788. v_map->ref_cnt = 1;
  789. for (i = 0; i < num_vsi; i++)
  790. set_bit(vsi_handle_arr[i], v_map->vsi_map);
  791. list_add(&v_map->list_entry, &sw->vsi_list_map_head);
  792. return v_map;
  793. }
  794. /**
  795. * ice_update_vsi_list_rule
  796. * @hw: pointer to the hardware structure
  797. * @vsi_handle_arr: array of VSI handles to form a VSI list
  798. * @num_vsi: number of VSI handles in the array
  799. * @vsi_list_id: VSI list id generated as part of allocate resource
  800. * @remove: Boolean value to indicate if this is a remove action
  801. * @opc: switch rules population command type - pass in the command opcode
  802. * @lkup_type: lookup type of the filter
  803. *
  804. * Call AQ command to add a new switch rule or update existing switch rule
  805. * using the given VSI list id
  806. */
  807. static enum ice_status
  808. ice_update_vsi_list_rule(struct ice_hw *hw, u16 *vsi_handle_arr, u16 num_vsi,
  809. u16 vsi_list_id, bool remove, enum ice_adminq_opc opc,
  810. enum ice_sw_lkup_type lkup_type)
  811. {
  812. struct ice_aqc_sw_rules_elem *s_rule;
  813. enum ice_status status;
  814. u16 s_rule_size;
  815. u16 type;
  816. int i;
  817. if (!num_vsi)
  818. return ICE_ERR_PARAM;
  819. if (lkup_type == ICE_SW_LKUP_MAC ||
  820. lkup_type == ICE_SW_LKUP_MAC_VLAN ||
  821. lkup_type == ICE_SW_LKUP_ETHERTYPE ||
  822. lkup_type == ICE_SW_LKUP_ETHERTYPE_MAC ||
  823. lkup_type == ICE_SW_LKUP_PROMISC ||
  824. lkup_type == ICE_SW_LKUP_PROMISC_VLAN)
  825. type = remove ? ICE_AQC_SW_RULES_T_VSI_LIST_CLEAR :
  826. ICE_AQC_SW_RULES_T_VSI_LIST_SET;
  827. else if (lkup_type == ICE_SW_LKUP_VLAN)
  828. type = remove ? ICE_AQC_SW_RULES_T_PRUNE_LIST_CLEAR :
  829. ICE_AQC_SW_RULES_T_PRUNE_LIST_SET;
  830. else
  831. return ICE_ERR_PARAM;
  832. s_rule_size = (u16)ICE_SW_RULE_VSI_LIST_SIZE(num_vsi);
  833. s_rule = devm_kzalloc(ice_hw_to_dev(hw), s_rule_size, GFP_KERNEL);
  834. if (!s_rule)
  835. return ICE_ERR_NO_MEMORY;
  836. for (i = 0; i < num_vsi; i++) {
  837. if (!ice_is_vsi_valid(hw, vsi_handle_arr[i])) {
  838. status = ICE_ERR_PARAM;
  839. goto exit;
  840. }
  841. /* AQ call requires hw_vsi_id(s) */
  842. s_rule->pdata.vsi_list.vsi[i] =
  843. cpu_to_le16(ice_get_hw_vsi_num(hw, vsi_handle_arr[i]));
  844. }
  845. s_rule->type = cpu_to_le16(type);
  846. s_rule->pdata.vsi_list.number_vsi = cpu_to_le16(num_vsi);
  847. s_rule->pdata.vsi_list.index = cpu_to_le16(vsi_list_id);
  848. status = ice_aq_sw_rules(hw, s_rule, s_rule_size, 1, opc, NULL);
  849. exit:
  850. devm_kfree(ice_hw_to_dev(hw), s_rule);
  851. return status;
  852. }
  853. /**
  854. * ice_create_vsi_list_rule - Creates and populates a VSI list rule
  855. * @hw: pointer to the hw struct
  856. * @vsi_handle_arr: array of VSI handles to form a VSI list
  857. * @num_vsi: number of VSI handles in the array
  858. * @vsi_list_id: stores the ID of the VSI list to be created
  859. * @lkup_type: switch rule filter's lookup type
  860. */
  861. static enum ice_status
  862. ice_create_vsi_list_rule(struct ice_hw *hw, u16 *vsi_handle_arr, u16 num_vsi,
  863. u16 *vsi_list_id, enum ice_sw_lkup_type lkup_type)
  864. {
  865. enum ice_status status;
  866. status = ice_aq_alloc_free_vsi_list(hw, vsi_list_id, lkup_type,
  867. ice_aqc_opc_alloc_res);
  868. if (status)
  869. return status;
  870. /* Update the newly created VSI list to include the specified VSIs */
  871. return ice_update_vsi_list_rule(hw, vsi_handle_arr, num_vsi,
  872. *vsi_list_id, false,
  873. ice_aqc_opc_add_sw_rules, lkup_type);
  874. }
  875. /**
  876. * ice_create_pkt_fwd_rule
  877. * @hw: pointer to the hardware structure
  878. * @f_entry: entry containing packet forwarding information
  879. *
  880. * Create switch rule with given filter information and add an entry
  881. * to the corresponding filter management list to track this switch rule
  882. * and VSI mapping
  883. */
  884. static enum ice_status
  885. ice_create_pkt_fwd_rule(struct ice_hw *hw,
  886. struct ice_fltr_list_entry *f_entry)
  887. {
  888. struct ice_fltr_mgmt_list_entry *fm_entry;
  889. struct ice_aqc_sw_rules_elem *s_rule;
  890. enum ice_sw_lkup_type l_type;
  891. struct ice_sw_recipe *recp;
  892. enum ice_status status;
  893. s_rule = devm_kzalloc(ice_hw_to_dev(hw),
  894. ICE_SW_RULE_RX_TX_ETH_HDR_SIZE, GFP_KERNEL);
  895. if (!s_rule)
  896. return ICE_ERR_NO_MEMORY;
  897. fm_entry = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*fm_entry),
  898. GFP_KERNEL);
  899. if (!fm_entry) {
  900. status = ICE_ERR_NO_MEMORY;
  901. goto ice_create_pkt_fwd_rule_exit;
  902. }
  903. fm_entry->fltr_info = f_entry->fltr_info;
  904. /* Initialize all the fields for the management entry */
  905. fm_entry->vsi_count = 1;
  906. fm_entry->lg_act_idx = ICE_INVAL_LG_ACT_INDEX;
  907. fm_entry->sw_marker_id = ICE_INVAL_SW_MARKER_ID;
  908. fm_entry->counter_index = ICE_INVAL_COUNTER_ID;
  909. ice_fill_sw_rule(hw, &fm_entry->fltr_info, s_rule,
  910. ice_aqc_opc_add_sw_rules);
  911. status = ice_aq_sw_rules(hw, s_rule, ICE_SW_RULE_RX_TX_ETH_HDR_SIZE, 1,
  912. ice_aqc_opc_add_sw_rules, NULL);
  913. if (status) {
  914. devm_kfree(ice_hw_to_dev(hw), fm_entry);
  915. goto ice_create_pkt_fwd_rule_exit;
  916. }
  917. f_entry->fltr_info.fltr_rule_id =
  918. le16_to_cpu(s_rule->pdata.lkup_tx_rx.index);
  919. fm_entry->fltr_info.fltr_rule_id =
  920. le16_to_cpu(s_rule->pdata.lkup_tx_rx.index);
  921. /* The book keeping entries will get removed when base driver
  922. * calls remove filter AQ command
  923. */
  924. l_type = fm_entry->fltr_info.lkup_type;
  925. recp = &hw->switch_info->recp_list[l_type];
  926. list_add(&fm_entry->list_entry, &recp->filt_rules);
  927. ice_create_pkt_fwd_rule_exit:
  928. devm_kfree(ice_hw_to_dev(hw), s_rule);
  929. return status;
  930. }
  931. /**
  932. * ice_update_pkt_fwd_rule
  933. * @hw: pointer to the hardware structure
  934. * @f_info: filter information for switch rule
  935. *
  936. * Call AQ command to update a previously created switch rule with a
  937. * VSI list id
  938. */
  939. static enum ice_status
  940. ice_update_pkt_fwd_rule(struct ice_hw *hw, struct ice_fltr_info *f_info)
  941. {
  942. struct ice_aqc_sw_rules_elem *s_rule;
  943. enum ice_status status;
  944. s_rule = devm_kzalloc(ice_hw_to_dev(hw),
  945. ICE_SW_RULE_RX_TX_ETH_HDR_SIZE, GFP_KERNEL);
  946. if (!s_rule)
  947. return ICE_ERR_NO_MEMORY;
  948. ice_fill_sw_rule(hw, f_info, s_rule, ice_aqc_opc_update_sw_rules);
  949. s_rule->pdata.lkup_tx_rx.index = cpu_to_le16(f_info->fltr_rule_id);
  950. /* Update switch rule with new rule set to forward VSI list */
  951. status = ice_aq_sw_rules(hw, s_rule, ICE_SW_RULE_RX_TX_ETH_HDR_SIZE, 1,
  952. ice_aqc_opc_update_sw_rules, NULL);
  953. devm_kfree(ice_hw_to_dev(hw), s_rule);
  954. return status;
  955. }
  956. /**
  957. * ice_update_sw_rule_bridge_mode
  958. * @hw: pointer to the hw struct
  959. *
  960. * Updates unicast switch filter rules based on VEB/VEPA mode
  961. */
  962. enum ice_status ice_update_sw_rule_bridge_mode(struct ice_hw *hw)
  963. {
  964. struct ice_switch_info *sw = hw->switch_info;
  965. struct ice_fltr_mgmt_list_entry *fm_entry;
  966. enum ice_status status = 0;
  967. struct list_head *rule_head;
  968. struct mutex *rule_lock; /* Lock to protect filter rule list */
  969. rule_lock = &sw->recp_list[ICE_SW_LKUP_MAC].filt_rule_lock;
  970. rule_head = &sw->recp_list[ICE_SW_LKUP_MAC].filt_rules;
  971. mutex_lock(rule_lock);
  972. list_for_each_entry(fm_entry, rule_head, list_entry) {
  973. struct ice_fltr_info *fi = &fm_entry->fltr_info;
  974. u8 *addr = fi->l_data.mac.mac_addr;
  975. /* Update unicast Tx rules to reflect the selected
  976. * VEB/VEPA mode
  977. */
  978. if ((fi->flag & ICE_FLTR_TX) && is_unicast_ether_addr(addr) &&
  979. (fi->fltr_act == ICE_FWD_TO_VSI ||
  980. fi->fltr_act == ICE_FWD_TO_VSI_LIST ||
  981. fi->fltr_act == ICE_FWD_TO_Q ||
  982. fi->fltr_act == ICE_FWD_TO_QGRP)) {
  983. status = ice_update_pkt_fwd_rule(hw, fi);
  984. if (status)
  985. break;
  986. }
  987. }
  988. mutex_unlock(rule_lock);
  989. return status;
  990. }
  991. /**
  992. * ice_add_update_vsi_list
  993. * @hw: pointer to the hardware structure
  994. * @m_entry: pointer to current filter management list entry
  995. * @cur_fltr: filter information from the book keeping entry
  996. * @new_fltr: filter information with the new VSI to be added
  997. *
  998. * Call AQ command to add or update previously created VSI list with new VSI.
  999. *
  1000. * Helper function to do book keeping associated with adding filter information
  1001. * The algorithm to do the booking keeping is described below :
  1002. * When a VSI needs to subscribe to a given filter( MAC/VLAN/Ethtype etc.)
  1003. * if only one VSI has been added till now
  1004. * Allocate a new VSI list and add two VSIs
  1005. * to this list using switch rule command
  1006. * Update the previously created switch rule with the
  1007. * newly created VSI list id
  1008. * if a VSI list was previously created
  1009. * Add the new VSI to the previously created VSI list set
  1010. * using the update switch rule command
  1011. */
  1012. static enum ice_status
  1013. ice_add_update_vsi_list(struct ice_hw *hw,
  1014. struct ice_fltr_mgmt_list_entry *m_entry,
  1015. struct ice_fltr_info *cur_fltr,
  1016. struct ice_fltr_info *new_fltr)
  1017. {
  1018. enum ice_status status = 0;
  1019. u16 vsi_list_id = 0;
  1020. if ((cur_fltr->fltr_act == ICE_FWD_TO_Q ||
  1021. cur_fltr->fltr_act == ICE_FWD_TO_QGRP))
  1022. return ICE_ERR_NOT_IMPL;
  1023. if ((new_fltr->fltr_act == ICE_FWD_TO_Q ||
  1024. new_fltr->fltr_act == ICE_FWD_TO_QGRP) &&
  1025. (cur_fltr->fltr_act == ICE_FWD_TO_VSI ||
  1026. cur_fltr->fltr_act == ICE_FWD_TO_VSI_LIST))
  1027. return ICE_ERR_NOT_IMPL;
  1028. if (m_entry->vsi_count < 2 && !m_entry->vsi_list_info) {
  1029. /* Only one entry existed in the mapping and it was not already
  1030. * a part of a VSI list. So, create a VSI list with the old and
  1031. * new VSIs.
  1032. */
  1033. struct ice_fltr_info tmp_fltr;
  1034. u16 vsi_handle_arr[2];
  1035. /* A rule already exists with the new VSI being added */
  1036. if (cur_fltr->fwd_id.hw_vsi_id == new_fltr->fwd_id.hw_vsi_id)
  1037. return ICE_ERR_ALREADY_EXISTS;
  1038. vsi_handle_arr[0] = cur_fltr->vsi_handle;
  1039. vsi_handle_arr[1] = new_fltr->vsi_handle;
  1040. status = ice_create_vsi_list_rule(hw, &vsi_handle_arr[0], 2,
  1041. &vsi_list_id,
  1042. new_fltr->lkup_type);
  1043. if (status)
  1044. return status;
  1045. tmp_fltr = *new_fltr;
  1046. tmp_fltr.fltr_rule_id = cur_fltr->fltr_rule_id;
  1047. tmp_fltr.fltr_act = ICE_FWD_TO_VSI_LIST;
  1048. tmp_fltr.fwd_id.vsi_list_id = vsi_list_id;
  1049. /* Update the previous switch rule of "MAC forward to VSI" to
  1050. * "MAC fwd to VSI list"
  1051. */
  1052. status = ice_update_pkt_fwd_rule(hw, &tmp_fltr);
  1053. if (status)
  1054. return status;
  1055. cur_fltr->fwd_id.vsi_list_id = vsi_list_id;
  1056. cur_fltr->fltr_act = ICE_FWD_TO_VSI_LIST;
  1057. m_entry->vsi_list_info =
  1058. ice_create_vsi_list_map(hw, &vsi_handle_arr[0], 2,
  1059. vsi_list_id);
  1060. /* If this entry was large action then the large action needs
  1061. * to be updated to point to FWD to VSI list
  1062. */
  1063. if (m_entry->sw_marker_id != ICE_INVAL_SW_MARKER_ID)
  1064. status =
  1065. ice_add_marker_act(hw, m_entry,
  1066. m_entry->sw_marker_id,
  1067. m_entry->lg_act_idx);
  1068. } else {
  1069. u16 vsi_handle = new_fltr->vsi_handle;
  1070. enum ice_adminq_opc opcode;
  1071. /* A rule already exists with the new VSI being added */
  1072. if (test_bit(vsi_handle, m_entry->vsi_list_info->vsi_map))
  1073. return 0;
  1074. /* Update the previously created VSI list set with
  1075. * the new VSI id passed in
  1076. */
  1077. vsi_list_id = cur_fltr->fwd_id.vsi_list_id;
  1078. opcode = ice_aqc_opc_update_sw_rules;
  1079. status = ice_update_vsi_list_rule(hw, &vsi_handle, 1,
  1080. vsi_list_id, false, opcode,
  1081. new_fltr->lkup_type);
  1082. /* update VSI list mapping info with new VSI id */
  1083. if (!status)
  1084. set_bit(vsi_handle, m_entry->vsi_list_info->vsi_map);
  1085. }
  1086. if (!status)
  1087. m_entry->vsi_count++;
  1088. return status;
  1089. }
  1090. /**
  1091. * ice_find_rule_entry - Search a rule entry
  1092. * @hw: pointer to the hardware structure
  1093. * @recp_id: lookup type for which the specified rule needs to be searched
  1094. * @f_info: rule information
  1095. *
  1096. * Helper function to search for a given rule entry
  1097. * Returns pointer to entry storing the rule if found
  1098. */
  1099. static struct ice_fltr_mgmt_list_entry *
  1100. ice_find_rule_entry(struct ice_hw *hw, u8 recp_id, struct ice_fltr_info *f_info)
  1101. {
  1102. struct ice_fltr_mgmt_list_entry *list_itr, *ret = NULL;
  1103. struct ice_switch_info *sw = hw->switch_info;
  1104. struct list_head *list_head;
  1105. list_head = &sw->recp_list[recp_id].filt_rules;
  1106. list_for_each_entry(list_itr, list_head, list_entry) {
  1107. if (!memcmp(&f_info->l_data, &list_itr->fltr_info.l_data,
  1108. sizeof(f_info->l_data)) &&
  1109. f_info->flag == list_itr->fltr_info.flag) {
  1110. ret = list_itr;
  1111. break;
  1112. }
  1113. }
  1114. return ret;
  1115. }
  1116. /**
  1117. * ice_find_vsi_list_entry - Search VSI list map with VSI count 1
  1118. * @hw: pointer to the hardware structure
  1119. * @recp_id: lookup type for which VSI lists needs to be searched
  1120. * @vsi_handle: VSI handle to be found in VSI list
  1121. * @vsi_list_id: VSI list id found containing vsi_handle
  1122. *
  1123. * Helper function to search a VSI list with single entry containing given VSI
  1124. * handle element. This can be extended further to search VSI list with more
  1125. * than 1 vsi_count. Returns pointer to VSI list entry if found.
  1126. */
  1127. static struct ice_vsi_list_map_info *
  1128. ice_find_vsi_list_entry(struct ice_hw *hw, u8 recp_id, u16 vsi_handle,
  1129. u16 *vsi_list_id)
  1130. {
  1131. struct ice_vsi_list_map_info *map_info = NULL;
  1132. struct ice_switch_info *sw = hw->switch_info;
  1133. struct ice_fltr_mgmt_list_entry *list_itr;
  1134. struct list_head *list_head;
  1135. list_head = &sw->recp_list[recp_id].filt_rules;
  1136. list_for_each_entry(list_itr, list_head, list_entry) {
  1137. if (list_itr->vsi_count == 1 && list_itr->vsi_list_info) {
  1138. map_info = list_itr->vsi_list_info;
  1139. if (test_bit(vsi_handle, map_info->vsi_map)) {
  1140. *vsi_list_id = map_info->vsi_list_id;
  1141. return map_info;
  1142. }
  1143. }
  1144. }
  1145. return NULL;
  1146. }
  1147. /**
  1148. * ice_add_rule_internal - add rule for a given lookup type
  1149. * @hw: pointer to the hardware structure
  1150. * @recp_id: lookup type (recipe id) for which rule has to be added
  1151. * @f_entry: structure containing MAC forwarding information
  1152. *
  1153. * Adds or updates the rule lists for a given recipe
  1154. */
  1155. static enum ice_status
  1156. ice_add_rule_internal(struct ice_hw *hw, u8 recp_id,
  1157. struct ice_fltr_list_entry *f_entry)
  1158. {
  1159. struct ice_switch_info *sw = hw->switch_info;
  1160. struct ice_fltr_info *new_fltr, *cur_fltr;
  1161. struct ice_fltr_mgmt_list_entry *m_entry;
  1162. struct mutex *rule_lock; /* Lock to protect filter rule list */
  1163. enum ice_status status = 0;
  1164. if (!ice_is_vsi_valid(hw, f_entry->fltr_info.vsi_handle))
  1165. return ICE_ERR_PARAM;
  1166. f_entry->fltr_info.fwd_id.hw_vsi_id =
  1167. ice_get_hw_vsi_num(hw, f_entry->fltr_info.vsi_handle);
  1168. rule_lock = &sw->recp_list[recp_id].filt_rule_lock;
  1169. mutex_lock(rule_lock);
  1170. new_fltr = &f_entry->fltr_info;
  1171. if (new_fltr->flag & ICE_FLTR_RX)
  1172. new_fltr->src = hw->port_info->lport;
  1173. else if (new_fltr->flag & ICE_FLTR_TX)
  1174. new_fltr->src = f_entry->fltr_info.fwd_id.hw_vsi_id;
  1175. m_entry = ice_find_rule_entry(hw, recp_id, new_fltr);
  1176. if (!m_entry) {
  1177. mutex_unlock(rule_lock);
  1178. return ice_create_pkt_fwd_rule(hw, f_entry);
  1179. }
  1180. cur_fltr = &m_entry->fltr_info;
  1181. status = ice_add_update_vsi_list(hw, m_entry, cur_fltr, new_fltr);
  1182. mutex_unlock(rule_lock);
  1183. return status;
  1184. }
  1185. /**
  1186. * ice_remove_vsi_list_rule
  1187. * @hw: pointer to the hardware structure
  1188. * @vsi_list_id: VSI list id generated as part of allocate resource
  1189. * @lkup_type: switch rule filter lookup type
  1190. *
  1191. * The VSI list should be emptied before this function is called to remove the
  1192. * VSI list.
  1193. */
  1194. static enum ice_status
  1195. ice_remove_vsi_list_rule(struct ice_hw *hw, u16 vsi_list_id,
  1196. enum ice_sw_lkup_type lkup_type)
  1197. {
  1198. struct ice_aqc_sw_rules_elem *s_rule;
  1199. enum ice_status status;
  1200. u16 s_rule_size;
  1201. s_rule_size = (u16)ICE_SW_RULE_VSI_LIST_SIZE(0);
  1202. s_rule = devm_kzalloc(ice_hw_to_dev(hw), s_rule_size, GFP_KERNEL);
  1203. if (!s_rule)
  1204. return ICE_ERR_NO_MEMORY;
  1205. s_rule->type = cpu_to_le16(ICE_AQC_SW_RULES_T_VSI_LIST_CLEAR);
  1206. s_rule->pdata.vsi_list.index = cpu_to_le16(vsi_list_id);
  1207. /* Free the vsi_list resource that we allocated. It is assumed that the
  1208. * list is empty at this point.
  1209. */
  1210. status = ice_aq_alloc_free_vsi_list(hw, &vsi_list_id, lkup_type,
  1211. ice_aqc_opc_free_res);
  1212. devm_kfree(ice_hw_to_dev(hw), s_rule);
  1213. return status;
  1214. }
  1215. /**
  1216. * ice_rem_update_vsi_list
  1217. * @hw: pointer to the hardware structure
  1218. * @vsi_handle: VSI handle of the VSI to remove
  1219. * @fm_list: filter management entry for which the VSI list management needs to
  1220. * be done
  1221. */
  1222. static enum ice_status
  1223. ice_rem_update_vsi_list(struct ice_hw *hw, u16 vsi_handle,
  1224. struct ice_fltr_mgmt_list_entry *fm_list)
  1225. {
  1226. enum ice_sw_lkup_type lkup_type;
  1227. enum ice_status status = 0;
  1228. u16 vsi_list_id;
  1229. if (fm_list->fltr_info.fltr_act != ICE_FWD_TO_VSI_LIST ||
  1230. fm_list->vsi_count == 0)
  1231. return ICE_ERR_PARAM;
  1232. /* A rule with the VSI being removed does not exist */
  1233. if (!test_bit(vsi_handle, fm_list->vsi_list_info->vsi_map))
  1234. return ICE_ERR_DOES_NOT_EXIST;
  1235. lkup_type = fm_list->fltr_info.lkup_type;
  1236. vsi_list_id = fm_list->fltr_info.fwd_id.vsi_list_id;
  1237. status = ice_update_vsi_list_rule(hw, &vsi_handle, 1, vsi_list_id, true,
  1238. ice_aqc_opc_update_sw_rules,
  1239. lkup_type);
  1240. if (status)
  1241. return status;
  1242. fm_list->vsi_count--;
  1243. clear_bit(vsi_handle, fm_list->vsi_list_info->vsi_map);
  1244. if (fm_list->vsi_count == 1 && lkup_type != ICE_SW_LKUP_VLAN) {
  1245. struct ice_fltr_info tmp_fltr_info = fm_list->fltr_info;
  1246. struct ice_vsi_list_map_info *vsi_list_info =
  1247. fm_list->vsi_list_info;
  1248. u16 rem_vsi_handle;
  1249. rem_vsi_handle = find_first_bit(vsi_list_info->vsi_map,
  1250. ICE_MAX_VSI);
  1251. if (!ice_is_vsi_valid(hw, rem_vsi_handle))
  1252. return ICE_ERR_OUT_OF_RANGE;
  1253. /* Make sure VSI list is empty before removing it below */
  1254. status = ice_update_vsi_list_rule(hw, &rem_vsi_handle, 1,
  1255. vsi_list_id, true,
  1256. ice_aqc_opc_update_sw_rules,
  1257. lkup_type);
  1258. if (status)
  1259. return status;
  1260. tmp_fltr_info.fltr_act = ICE_FWD_TO_VSI;
  1261. tmp_fltr_info.fwd_id.hw_vsi_id =
  1262. ice_get_hw_vsi_num(hw, rem_vsi_handle);
  1263. tmp_fltr_info.vsi_handle = rem_vsi_handle;
  1264. status = ice_update_pkt_fwd_rule(hw, &tmp_fltr_info);
  1265. if (status) {
  1266. ice_debug(hw, ICE_DBG_SW,
  1267. "Failed to update pkt fwd rule to FWD_TO_VSI on HW VSI %d, error %d\n",
  1268. tmp_fltr_info.fwd_id.hw_vsi_id, status);
  1269. return status;
  1270. }
  1271. fm_list->fltr_info = tmp_fltr_info;
  1272. }
  1273. if ((fm_list->vsi_count == 1 && lkup_type != ICE_SW_LKUP_VLAN) ||
  1274. (fm_list->vsi_count == 0 && lkup_type == ICE_SW_LKUP_VLAN)) {
  1275. struct ice_vsi_list_map_info *vsi_list_info =
  1276. fm_list->vsi_list_info;
  1277. /* Remove the VSI list since it is no longer used */
  1278. status = ice_remove_vsi_list_rule(hw, vsi_list_id, lkup_type);
  1279. if (status) {
  1280. ice_debug(hw, ICE_DBG_SW,
  1281. "Failed to remove VSI list %d, error %d\n",
  1282. vsi_list_id, status);
  1283. return status;
  1284. }
  1285. list_del(&vsi_list_info->list_entry);
  1286. devm_kfree(ice_hw_to_dev(hw), vsi_list_info);
  1287. fm_list->vsi_list_info = NULL;
  1288. }
  1289. return status;
  1290. }
  1291. /**
  1292. * ice_remove_rule_internal - Remove a filter rule of a given type
  1293. * @hw: pointer to the hardware structure
  1294. * @recp_id: recipe id for which the rule needs to removed
  1295. * @f_entry: rule entry containing filter information
  1296. */
  1297. static enum ice_status
  1298. ice_remove_rule_internal(struct ice_hw *hw, u8 recp_id,
  1299. struct ice_fltr_list_entry *f_entry)
  1300. {
  1301. struct ice_switch_info *sw = hw->switch_info;
  1302. struct ice_fltr_mgmt_list_entry *list_elem;
  1303. struct mutex *rule_lock; /* Lock to protect filter rule list */
  1304. enum ice_status status = 0;
  1305. bool remove_rule = false;
  1306. u16 vsi_handle;
  1307. if (!ice_is_vsi_valid(hw, f_entry->fltr_info.vsi_handle))
  1308. return ICE_ERR_PARAM;
  1309. f_entry->fltr_info.fwd_id.hw_vsi_id =
  1310. ice_get_hw_vsi_num(hw, f_entry->fltr_info.vsi_handle);
  1311. rule_lock = &sw->recp_list[recp_id].filt_rule_lock;
  1312. mutex_lock(rule_lock);
  1313. list_elem = ice_find_rule_entry(hw, recp_id, &f_entry->fltr_info);
  1314. if (!list_elem) {
  1315. status = ICE_ERR_DOES_NOT_EXIST;
  1316. goto exit;
  1317. }
  1318. if (list_elem->fltr_info.fltr_act != ICE_FWD_TO_VSI_LIST) {
  1319. remove_rule = true;
  1320. } else if (!list_elem->vsi_list_info) {
  1321. status = ICE_ERR_DOES_NOT_EXIST;
  1322. goto exit;
  1323. } else {
  1324. if (list_elem->vsi_list_info->ref_cnt > 1)
  1325. list_elem->vsi_list_info->ref_cnt--;
  1326. vsi_handle = f_entry->fltr_info.vsi_handle;
  1327. status = ice_rem_update_vsi_list(hw, vsi_handle, list_elem);
  1328. if (status)
  1329. goto exit;
  1330. /* if vsi count goes to zero after updating the vsi list */
  1331. if (list_elem->vsi_count == 0)
  1332. remove_rule = true;
  1333. }
  1334. if (remove_rule) {
  1335. /* Remove the lookup rule */
  1336. struct ice_aqc_sw_rules_elem *s_rule;
  1337. s_rule = devm_kzalloc(ice_hw_to_dev(hw),
  1338. ICE_SW_RULE_RX_TX_NO_HDR_SIZE,
  1339. GFP_KERNEL);
  1340. if (!s_rule) {
  1341. status = ICE_ERR_NO_MEMORY;
  1342. goto exit;
  1343. }
  1344. ice_fill_sw_rule(hw, &list_elem->fltr_info, s_rule,
  1345. ice_aqc_opc_remove_sw_rules);
  1346. status = ice_aq_sw_rules(hw, s_rule,
  1347. ICE_SW_RULE_RX_TX_NO_HDR_SIZE, 1,
  1348. ice_aqc_opc_remove_sw_rules, NULL);
  1349. if (status)
  1350. goto exit;
  1351. /* Remove a book keeping from the list */
  1352. devm_kfree(ice_hw_to_dev(hw), s_rule);
  1353. list_del(&list_elem->list_entry);
  1354. devm_kfree(ice_hw_to_dev(hw), list_elem);
  1355. }
  1356. exit:
  1357. mutex_unlock(rule_lock);
  1358. return status;
  1359. }
  1360. /**
  1361. * ice_add_mac - Add a MAC address based filter rule
  1362. * @hw: pointer to the hardware structure
  1363. * @m_list: list of MAC addresses and forwarding information
  1364. *
  1365. * IMPORTANT: When the ucast_shared flag is set to false and m_list has
  1366. * multiple unicast addresses, the function assumes that all the
  1367. * addresses are unique in a given add_mac call. It doesn't
  1368. * check for duplicates in this case, removing duplicates from a given
  1369. * list should be taken care of in the caller of this function.
  1370. */
  1371. enum ice_status
  1372. ice_add_mac(struct ice_hw *hw, struct list_head *m_list)
  1373. {
  1374. struct ice_aqc_sw_rules_elem *s_rule, *r_iter;
  1375. struct ice_fltr_list_entry *m_list_itr;
  1376. struct list_head *rule_head;
  1377. u16 elem_sent, total_elem_left;
  1378. struct ice_switch_info *sw;
  1379. struct mutex *rule_lock; /* Lock to protect filter rule list */
  1380. enum ice_status status = 0;
  1381. u16 num_unicast = 0;
  1382. u16 s_rule_size;
  1383. if (!m_list || !hw)
  1384. return ICE_ERR_PARAM;
  1385. s_rule = NULL;
  1386. sw = hw->switch_info;
  1387. rule_lock = &sw->recp_list[ICE_SW_LKUP_MAC].filt_rule_lock;
  1388. list_for_each_entry(m_list_itr, m_list, list_entry) {
  1389. u8 *add = &m_list_itr->fltr_info.l_data.mac.mac_addr[0];
  1390. u16 vsi_handle;
  1391. u16 hw_vsi_id;
  1392. m_list_itr->fltr_info.flag = ICE_FLTR_TX;
  1393. vsi_handle = m_list_itr->fltr_info.vsi_handle;
  1394. if (!ice_is_vsi_valid(hw, vsi_handle))
  1395. return ICE_ERR_PARAM;
  1396. hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
  1397. m_list_itr->fltr_info.fwd_id.hw_vsi_id = hw_vsi_id;
  1398. /* update the src in case it is vsi num */
  1399. if (m_list_itr->fltr_info.src_id != ICE_SRC_ID_VSI)
  1400. return ICE_ERR_PARAM;
  1401. m_list_itr->fltr_info.src = hw_vsi_id;
  1402. if (m_list_itr->fltr_info.lkup_type != ICE_SW_LKUP_MAC ||
  1403. is_zero_ether_addr(add))
  1404. return ICE_ERR_PARAM;
  1405. if (is_unicast_ether_addr(add) && !hw->ucast_shared) {
  1406. /* Don't overwrite the unicast address */
  1407. mutex_lock(rule_lock);
  1408. if (ice_find_rule_entry(hw, ICE_SW_LKUP_MAC,
  1409. &m_list_itr->fltr_info)) {
  1410. mutex_unlock(rule_lock);
  1411. return ICE_ERR_ALREADY_EXISTS;
  1412. }
  1413. mutex_unlock(rule_lock);
  1414. num_unicast++;
  1415. } else if (is_multicast_ether_addr(add) ||
  1416. (is_unicast_ether_addr(add) && hw->ucast_shared)) {
  1417. m_list_itr->status =
  1418. ice_add_rule_internal(hw, ICE_SW_LKUP_MAC,
  1419. m_list_itr);
  1420. if (m_list_itr->status)
  1421. return m_list_itr->status;
  1422. }
  1423. }
  1424. mutex_lock(rule_lock);
  1425. /* Exit if no suitable entries were found for adding bulk switch rule */
  1426. if (!num_unicast) {
  1427. status = 0;
  1428. goto ice_add_mac_exit;
  1429. }
  1430. rule_head = &sw->recp_list[ICE_SW_LKUP_MAC].filt_rules;
  1431. /* Allocate switch rule buffer for the bulk update for unicast */
  1432. s_rule_size = ICE_SW_RULE_RX_TX_ETH_HDR_SIZE;
  1433. s_rule = devm_kcalloc(ice_hw_to_dev(hw), num_unicast, s_rule_size,
  1434. GFP_KERNEL);
  1435. if (!s_rule) {
  1436. status = ICE_ERR_NO_MEMORY;
  1437. goto ice_add_mac_exit;
  1438. }
  1439. r_iter = s_rule;
  1440. list_for_each_entry(m_list_itr, m_list, list_entry) {
  1441. struct ice_fltr_info *f_info = &m_list_itr->fltr_info;
  1442. u8 *mac_addr = &f_info->l_data.mac.mac_addr[0];
  1443. if (is_unicast_ether_addr(mac_addr)) {
  1444. ice_fill_sw_rule(hw, &m_list_itr->fltr_info, r_iter,
  1445. ice_aqc_opc_add_sw_rules);
  1446. r_iter = (struct ice_aqc_sw_rules_elem *)
  1447. ((u8 *)r_iter + s_rule_size);
  1448. }
  1449. }
  1450. /* Call AQ bulk switch rule update for all unicast addresses */
  1451. r_iter = s_rule;
  1452. /* Call AQ switch rule in AQ_MAX chunk */
  1453. for (total_elem_left = num_unicast; total_elem_left > 0;
  1454. total_elem_left -= elem_sent) {
  1455. struct ice_aqc_sw_rules_elem *entry = r_iter;
  1456. elem_sent = min(total_elem_left,
  1457. (u16)(ICE_AQ_MAX_BUF_LEN / s_rule_size));
  1458. status = ice_aq_sw_rules(hw, entry, elem_sent * s_rule_size,
  1459. elem_sent, ice_aqc_opc_add_sw_rules,
  1460. NULL);
  1461. if (status)
  1462. goto ice_add_mac_exit;
  1463. r_iter = (struct ice_aqc_sw_rules_elem *)
  1464. ((u8 *)r_iter + (elem_sent * s_rule_size));
  1465. }
  1466. /* Fill up rule id based on the value returned from FW */
  1467. r_iter = s_rule;
  1468. list_for_each_entry(m_list_itr, m_list, list_entry) {
  1469. struct ice_fltr_info *f_info = &m_list_itr->fltr_info;
  1470. u8 *mac_addr = &f_info->l_data.mac.mac_addr[0];
  1471. struct ice_fltr_mgmt_list_entry *fm_entry;
  1472. if (is_unicast_ether_addr(mac_addr)) {
  1473. f_info->fltr_rule_id =
  1474. le16_to_cpu(r_iter->pdata.lkup_tx_rx.index);
  1475. f_info->fltr_act = ICE_FWD_TO_VSI;
  1476. /* Create an entry to track this MAC address */
  1477. fm_entry = devm_kzalloc(ice_hw_to_dev(hw),
  1478. sizeof(*fm_entry), GFP_KERNEL);
  1479. if (!fm_entry) {
  1480. status = ICE_ERR_NO_MEMORY;
  1481. goto ice_add_mac_exit;
  1482. }
  1483. fm_entry->fltr_info = *f_info;
  1484. fm_entry->vsi_count = 1;
  1485. /* The book keeping entries will get removed when
  1486. * base driver calls remove filter AQ command
  1487. */
  1488. list_add(&fm_entry->list_entry, rule_head);
  1489. r_iter = (struct ice_aqc_sw_rules_elem *)
  1490. ((u8 *)r_iter + s_rule_size);
  1491. }
  1492. }
  1493. ice_add_mac_exit:
  1494. mutex_unlock(rule_lock);
  1495. if (s_rule)
  1496. devm_kfree(ice_hw_to_dev(hw), s_rule);
  1497. return status;
  1498. }
  1499. /**
  1500. * ice_add_vlan_internal - Add one VLAN based filter rule
  1501. * @hw: pointer to the hardware structure
  1502. * @f_entry: filter entry containing one VLAN information
  1503. */
  1504. static enum ice_status
  1505. ice_add_vlan_internal(struct ice_hw *hw, struct ice_fltr_list_entry *f_entry)
  1506. {
  1507. struct ice_switch_info *sw = hw->switch_info;
  1508. struct ice_fltr_mgmt_list_entry *v_list_itr;
  1509. struct ice_fltr_info *new_fltr, *cur_fltr;
  1510. enum ice_sw_lkup_type lkup_type;
  1511. u16 vsi_list_id = 0, vsi_handle;
  1512. struct mutex *rule_lock; /* Lock to protect filter rule list */
  1513. enum ice_status status = 0;
  1514. if (!ice_is_vsi_valid(hw, f_entry->fltr_info.vsi_handle))
  1515. return ICE_ERR_PARAM;
  1516. f_entry->fltr_info.fwd_id.hw_vsi_id =
  1517. ice_get_hw_vsi_num(hw, f_entry->fltr_info.vsi_handle);
  1518. new_fltr = &f_entry->fltr_info;
  1519. /* VLAN id should only be 12 bits */
  1520. if (new_fltr->l_data.vlan.vlan_id > ICE_MAX_VLAN_ID)
  1521. return ICE_ERR_PARAM;
  1522. if (new_fltr->src_id != ICE_SRC_ID_VSI)
  1523. return ICE_ERR_PARAM;
  1524. new_fltr->src = new_fltr->fwd_id.hw_vsi_id;
  1525. lkup_type = new_fltr->lkup_type;
  1526. vsi_handle = new_fltr->vsi_handle;
  1527. rule_lock = &sw->recp_list[ICE_SW_LKUP_VLAN].filt_rule_lock;
  1528. mutex_lock(rule_lock);
  1529. v_list_itr = ice_find_rule_entry(hw, ICE_SW_LKUP_VLAN, new_fltr);
  1530. if (!v_list_itr) {
  1531. struct ice_vsi_list_map_info *map_info = NULL;
  1532. if (new_fltr->fltr_act == ICE_FWD_TO_VSI) {
  1533. /* All VLAN pruning rules use a VSI list. Check if
  1534. * there is already a VSI list containing VSI that we
  1535. * want to add. If found, use the same vsi_list_id for
  1536. * this new VLAN rule or else create a new list.
  1537. */
  1538. map_info = ice_find_vsi_list_entry(hw, ICE_SW_LKUP_VLAN,
  1539. vsi_handle,
  1540. &vsi_list_id);
  1541. if (!map_info) {
  1542. status = ice_create_vsi_list_rule(hw,
  1543. &vsi_handle,
  1544. 1,
  1545. &vsi_list_id,
  1546. lkup_type);
  1547. if (status)
  1548. goto exit;
  1549. }
  1550. /* Convert the action to forwarding to a VSI list. */
  1551. new_fltr->fltr_act = ICE_FWD_TO_VSI_LIST;
  1552. new_fltr->fwd_id.vsi_list_id = vsi_list_id;
  1553. }
  1554. status = ice_create_pkt_fwd_rule(hw, f_entry);
  1555. if (!status) {
  1556. v_list_itr = ice_find_rule_entry(hw, ICE_SW_LKUP_VLAN,
  1557. new_fltr);
  1558. if (!v_list_itr) {
  1559. status = ICE_ERR_DOES_NOT_EXIST;
  1560. goto exit;
  1561. }
  1562. /* reuse VSI list for new rule and increment ref_cnt */
  1563. if (map_info) {
  1564. v_list_itr->vsi_list_info = map_info;
  1565. map_info->ref_cnt++;
  1566. } else {
  1567. v_list_itr->vsi_list_info =
  1568. ice_create_vsi_list_map(hw, &vsi_handle,
  1569. 1, vsi_list_id);
  1570. }
  1571. }
  1572. } else if (v_list_itr->vsi_list_info->ref_cnt == 1) {
  1573. /* Update existing VSI list to add new VSI id only if it used
  1574. * by one VLAN rule.
  1575. */
  1576. cur_fltr = &v_list_itr->fltr_info;
  1577. status = ice_add_update_vsi_list(hw, v_list_itr, cur_fltr,
  1578. new_fltr);
  1579. } else {
  1580. /* If VLAN rule exists and VSI list being used by this rule is
  1581. * referenced by more than 1 VLAN rule. Then create a new VSI
  1582. * list appending previous VSI with new VSI and update existing
  1583. * VLAN rule to point to new VSI list id
  1584. */
  1585. struct ice_fltr_info tmp_fltr;
  1586. u16 vsi_handle_arr[2];
  1587. u16 cur_handle;
  1588. /* Current implementation only supports reusing VSI list with
  1589. * one VSI count. We should never hit below condition
  1590. */
  1591. if (v_list_itr->vsi_count > 1 &&
  1592. v_list_itr->vsi_list_info->ref_cnt > 1) {
  1593. ice_debug(hw, ICE_DBG_SW,
  1594. "Invalid configuration: Optimization to reuse VSI list with more than one VSI is not being done yet\n");
  1595. status = ICE_ERR_CFG;
  1596. goto exit;
  1597. }
  1598. cur_handle =
  1599. find_first_bit(v_list_itr->vsi_list_info->vsi_map,
  1600. ICE_MAX_VSI);
  1601. /* A rule already exists with the new VSI being added */
  1602. if (cur_handle == vsi_handle) {
  1603. status = ICE_ERR_ALREADY_EXISTS;
  1604. goto exit;
  1605. }
  1606. vsi_handle_arr[0] = cur_handle;
  1607. vsi_handle_arr[1] = vsi_handle;
  1608. status = ice_create_vsi_list_rule(hw, &vsi_handle_arr[0], 2,
  1609. &vsi_list_id, lkup_type);
  1610. if (status)
  1611. goto exit;
  1612. tmp_fltr = v_list_itr->fltr_info;
  1613. tmp_fltr.fltr_rule_id = v_list_itr->fltr_info.fltr_rule_id;
  1614. tmp_fltr.fwd_id.vsi_list_id = vsi_list_id;
  1615. tmp_fltr.fltr_act = ICE_FWD_TO_VSI_LIST;
  1616. /* Update the previous switch rule to a new VSI list which
  1617. * includes current VSI thats requested
  1618. */
  1619. status = ice_update_pkt_fwd_rule(hw, &tmp_fltr);
  1620. if (status)
  1621. goto exit;
  1622. /* before overriding VSI list map info. decrement ref_cnt of
  1623. * previous VSI list
  1624. */
  1625. v_list_itr->vsi_list_info->ref_cnt--;
  1626. /* now update to newly created list */
  1627. v_list_itr->fltr_info.fwd_id.vsi_list_id = vsi_list_id;
  1628. v_list_itr->vsi_list_info =
  1629. ice_create_vsi_list_map(hw, &vsi_handle_arr[0], 2,
  1630. vsi_list_id);
  1631. v_list_itr->vsi_count++;
  1632. }
  1633. exit:
  1634. mutex_unlock(rule_lock);
  1635. return status;
  1636. }
  1637. /**
  1638. * ice_add_vlan - Add VLAN based filter rule
  1639. * @hw: pointer to the hardware structure
  1640. * @v_list: list of VLAN entries and forwarding information
  1641. */
  1642. enum ice_status
  1643. ice_add_vlan(struct ice_hw *hw, struct list_head *v_list)
  1644. {
  1645. struct ice_fltr_list_entry *v_list_itr;
  1646. if (!v_list || !hw)
  1647. return ICE_ERR_PARAM;
  1648. list_for_each_entry(v_list_itr, v_list, list_entry) {
  1649. if (v_list_itr->fltr_info.lkup_type != ICE_SW_LKUP_VLAN)
  1650. return ICE_ERR_PARAM;
  1651. v_list_itr->fltr_info.flag = ICE_FLTR_TX;
  1652. v_list_itr->status = ice_add_vlan_internal(hw, v_list_itr);
  1653. if (v_list_itr->status)
  1654. return v_list_itr->status;
  1655. }
  1656. return 0;
  1657. }
  1658. /**
  1659. * ice_rem_sw_rule_info
  1660. * @hw: pointer to the hardware structure
  1661. * @rule_head: pointer to the switch list structure that we want to delete
  1662. */
  1663. static void
  1664. ice_rem_sw_rule_info(struct ice_hw *hw, struct list_head *rule_head)
  1665. {
  1666. if (!list_empty(rule_head)) {
  1667. struct ice_fltr_mgmt_list_entry *entry;
  1668. struct ice_fltr_mgmt_list_entry *tmp;
  1669. list_for_each_entry_safe(entry, tmp, rule_head, list_entry) {
  1670. list_del(&entry->list_entry);
  1671. devm_kfree(ice_hw_to_dev(hw), entry);
  1672. }
  1673. }
  1674. }
  1675. /**
  1676. * ice_cfg_dflt_vsi - change state of VSI to set/clear default
  1677. * @hw: pointer to the hardware structure
  1678. * @vsi_handle: VSI handle to set as default
  1679. * @set: true to add the above mentioned switch rule, false to remove it
  1680. * @direction: ICE_FLTR_RX or ICE_FLTR_TX
  1681. *
  1682. * add filter rule to set/unset given VSI as default VSI for the switch
  1683. * (represented by swid)
  1684. */
  1685. enum ice_status
  1686. ice_cfg_dflt_vsi(struct ice_hw *hw, u16 vsi_handle, bool set, u8 direction)
  1687. {
  1688. struct ice_aqc_sw_rules_elem *s_rule;
  1689. struct ice_fltr_info f_info;
  1690. enum ice_adminq_opc opcode;
  1691. enum ice_status status;
  1692. u16 s_rule_size;
  1693. u16 hw_vsi_id;
  1694. if (!ice_is_vsi_valid(hw, vsi_handle))
  1695. return ICE_ERR_PARAM;
  1696. hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
  1697. s_rule_size = set ? ICE_SW_RULE_RX_TX_ETH_HDR_SIZE :
  1698. ICE_SW_RULE_RX_TX_NO_HDR_SIZE;
  1699. s_rule = devm_kzalloc(ice_hw_to_dev(hw), s_rule_size, GFP_KERNEL);
  1700. if (!s_rule)
  1701. return ICE_ERR_NO_MEMORY;
  1702. memset(&f_info, 0, sizeof(f_info));
  1703. f_info.lkup_type = ICE_SW_LKUP_DFLT;
  1704. f_info.flag = direction;
  1705. f_info.fltr_act = ICE_FWD_TO_VSI;
  1706. f_info.fwd_id.hw_vsi_id = hw_vsi_id;
  1707. if (f_info.flag & ICE_FLTR_RX) {
  1708. f_info.src = hw->port_info->lport;
  1709. f_info.src_id = ICE_SRC_ID_LPORT;
  1710. if (!set)
  1711. f_info.fltr_rule_id =
  1712. hw->port_info->dflt_rx_vsi_rule_id;
  1713. } else if (f_info.flag & ICE_FLTR_TX) {
  1714. f_info.src_id = ICE_SRC_ID_VSI;
  1715. f_info.src = hw_vsi_id;
  1716. if (!set)
  1717. f_info.fltr_rule_id =
  1718. hw->port_info->dflt_tx_vsi_rule_id;
  1719. }
  1720. if (set)
  1721. opcode = ice_aqc_opc_add_sw_rules;
  1722. else
  1723. opcode = ice_aqc_opc_remove_sw_rules;
  1724. ice_fill_sw_rule(hw, &f_info, s_rule, opcode);
  1725. status = ice_aq_sw_rules(hw, s_rule, s_rule_size, 1, opcode, NULL);
  1726. if (status || !(f_info.flag & ICE_FLTR_TX_RX))
  1727. goto out;
  1728. if (set) {
  1729. u16 index = le16_to_cpu(s_rule->pdata.lkup_tx_rx.index);
  1730. if (f_info.flag & ICE_FLTR_TX) {
  1731. hw->port_info->dflt_tx_vsi_num = hw_vsi_id;
  1732. hw->port_info->dflt_tx_vsi_rule_id = index;
  1733. } else if (f_info.flag & ICE_FLTR_RX) {
  1734. hw->port_info->dflt_rx_vsi_num = hw_vsi_id;
  1735. hw->port_info->dflt_rx_vsi_rule_id = index;
  1736. }
  1737. } else {
  1738. if (f_info.flag & ICE_FLTR_TX) {
  1739. hw->port_info->dflt_tx_vsi_num = ICE_DFLT_VSI_INVAL;
  1740. hw->port_info->dflt_tx_vsi_rule_id = ICE_INVAL_ACT;
  1741. } else if (f_info.flag & ICE_FLTR_RX) {
  1742. hw->port_info->dflt_rx_vsi_num = ICE_DFLT_VSI_INVAL;
  1743. hw->port_info->dflt_rx_vsi_rule_id = ICE_INVAL_ACT;
  1744. }
  1745. }
  1746. out:
  1747. devm_kfree(ice_hw_to_dev(hw), s_rule);
  1748. return status;
  1749. }
  1750. /**
  1751. * ice_remove_mac - remove a MAC address based filter rule
  1752. * @hw: pointer to the hardware structure
  1753. * @m_list: list of MAC addresses and forwarding information
  1754. *
  1755. * This function removes either a MAC filter rule or a specific VSI from a
  1756. * VSI list for a multicast MAC address.
  1757. *
  1758. * Returns ICE_ERR_DOES_NOT_EXIST if a given entry was not added by
  1759. * ice_add_mac. Caller should be aware that this call will only work if all
  1760. * the entries passed into m_list were added previously. It will not attempt to
  1761. * do a partial remove of entries that were found.
  1762. */
  1763. enum ice_status
  1764. ice_remove_mac(struct ice_hw *hw, struct list_head *m_list)
  1765. {
  1766. struct ice_fltr_list_entry *list_itr;
  1767. if (!m_list)
  1768. return ICE_ERR_PARAM;
  1769. list_for_each_entry(list_itr, m_list, list_entry) {
  1770. enum ice_sw_lkup_type l_type = list_itr->fltr_info.lkup_type;
  1771. if (l_type != ICE_SW_LKUP_MAC)
  1772. return ICE_ERR_PARAM;
  1773. list_itr->status = ice_remove_rule_internal(hw,
  1774. ICE_SW_LKUP_MAC,
  1775. list_itr);
  1776. if (list_itr->status)
  1777. return list_itr->status;
  1778. }
  1779. return 0;
  1780. }
  1781. /**
  1782. * ice_remove_vlan - Remove VLAN based filter rule
  1783. * @hw: pointer to the hardware structure
  1784. * @v_list: list of VLAN entries and forwarding information
  1785. */
  1786. enum ice_status
  1787. ice_remove_vlan(struct ice_hw *hw, struct list_head *v_list)
  1788. {
  1789. struct ice_fltr_list_entry *v_list_itr;
  1790. if (!v_list || !hw)
  1791. return ICE_ERR_PARAM;
  1792. list_for_each_entry(v_list_itr, v_list, list_entry) {
  1793. enum ice_sw_lkup_type l_type = v_list_itr->fltr_info.lkup_type;
  1794. if (l_type != ICE_SW_LKUP_VLAN)
  1795. return ICE_ERR_PARAM;
  1796. v_list_itr->status = ice_remove_rule_internal(hw,
  1797. ICE_SW_LKUP_VLAN,
  1798. v_list_itr);
  1799. if (v_list_itr->status)
  1800. return v_list_itr->status;
  1801. }
  1802. return 0;
  1803. }
  1804. /**
  1805. * ice_vsi_uses_fltr - Determine if given VSI uses specified filter
  1806. * @fm_entry: filter entry to inspect
  1807. * @vsi_handle: VSI handle to compare with filter info
  1808. */
  1809. static bool
  1810. ice_vsi_uses_fltr(struct ice_fltr_mgmt_list_entry *fm_entry, u16 vsi_handle)
  1811. {
  1812. return ((fm_entry->fltr_info.fltr_act == ICE_FWD_TO_VSI &&
  1813. fm_entry->fltr_info.vsi_handle == vsi_handle) ||
  1814. (fm_entry->fltr_info.fltr_act == ICE_FWD_TO_VSI_LIST &&
  1815. (test_bit(vsi_handle, fm_entry->vsi_list_info->vsi_map))));
  1816. }
  1817. /**
  1818. * ice_add_entry_to_vsi_fltr_list - Add copy of fltr_list_entry to remove list
  1819. * @hw: pointer to the hardware structure
  1820. * @vsi_handle: VSI handle to remove filters from
  1821. * @vsi_list_head: pointer to the list to add entry to
  1822. * @fi: pointer to fltr_info of filter entry to copy & add
  1823. *
  1824. * Helper function, used when creating a list of filters to remove from
  1825. * a specific VSI. The entry added to vsi_list_head is a COPY of the
  1826. * original filter entry, with the exception of fltr_info.fltr_act and
  1827. * fltr_info.fwd_id fields. These are set such that later logic can
  1828. * extract which VSI to remove the fltr from, and pass on that information.
  1829. */
  1830. static enum ice_status
  1831. ice_add_entry_to_vsi_fltr_list(struct ice_hw *hw, u16 vsi_handle,
  1832. struct list_head *vsi_list_head,
  1833. struct ice_fltr_info *fi)
  1834. {
  1835. struct ice_fltr_list_entry *tmp;
  1836. /* this memory is freed up in the caller function
  1837. * once filters for this VSI are removed
  1838. */
  1839. tmp = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*tmp), GFP_KERNEL);
  1840. if (!tmp)
  1841. return ICE_ERR_NO_MEMORY;
  1842. tmp->fltr_info = *fi;
  1843. /* Overwrite these fields to indicate which VSI to remove filter from,
  1844. * so find and remove logic can extract the information from the
  1845. * list entries. Note that original entries will still have proper
  1846. * values.
  1847. */
  1848. tmp->fltr_info.fltr_act = ICE_FWD_TO_VSI;
  1849. tmp->fltr_info.vsi_handle = vsi_handle;
  1850. tmp->fltr_info.fwd_id.hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
  1851. list_add(&tmp->list_entry, vsi_list_head);
  1852. return 0;
  1853. }
  1854. /**
  1855. * ice_add_to_vsi_fltr_list - Add VSI filters to the list
  1856. * @hw: pointer to the hardware structure
  1857. * @vsi_handle: VSI handle to remove filters from
  1858. * @lkup_list_head: pointer to the list that has certain lookup type filters
  1859. * @vsi_list_head: pointer to the list pertaining to VSI with vsi_handle
  1860. *
  1861. * Locates all filters in lkup_list_head that are used by the given VSI,
  1862. * and adds COPIES of those entries to vsi_list_head (intended to be used
  1863. * to remove the listed filters).
  1864. * Note that this means all entries in vsi_list_head must be explicitly
  1865. * deallocated by the caller when done with list.
  1866. */
  1867. static enum ice_status
  1868. ice_add_to_vsi_fltr_list(struct ice_hw *hw, u16 vsi_handle,
  1869. struct list_head *lkup_list_head,
  1870. struct list_head *vsi_list_head)
  1871. {
  1872. struct ice_fltr_mgmt_list_entry *fm_entry;
  1873. enum ice_status status = 0;
  1874. /* check to make sure VSI id is valid and within boundary */
  1875. if (!ice_is_vsi_valid(hw, vsi_handle))
  1876. return ICE_ERR_PARAM;
  1877. list_for_each_entry(fm_entry, lkup_list_head, list_entry) {
  1878. struct ice_fltr_info *fi;
  1879. fi = &fm_entry->fltr_info;
  1880. if (!ice_vsi_uses_fltr(fm_entry, vsi_handle))
  1881. continue;
  1882. status = ice_add_entry_to_vsi_fltr_list(hw, vsi_handle,
  1883. vsi_list_head, fi);
  1884. if (status)
  1885. return status;
  1886. }
  1887. return status;
  1888. }
  1889. /**
  1890. * ice_remove_vsi_lkup_fltr - Remove lookup type filters for a VSI
  1891. * @hw: pointer to the hardware structure
  1892. * @vsi_handle: VSI handle to remove filters from
  1893. * @lkup: switch rule filter lookup type
  1894. */
  1895. static void
  1896. ice_remove_vsi_lkup_fltr(struct ice_hw *hw, u16 vsi_handle,
  1897. enum ice_sw_lkup_type lkup)
  1898. {
  1899. struct ice_switch_info *sw = hw->switch_info;
  1900. struct ice_fltr_list_entry *fm_entry;
  1901. struct list_head remove_list_head;
  1902. struct list_head *rule_head;
  1903. struct ice_fltr_list_entry *tmp;
  1904. struct mutex *rule_lock; /* Lock to protect filter rule list */
  1905. enum ice_status status;
  1906. INIT_LIST_HEAD(&remove_list_head);
  1907. rule_lock = &sw->recp_list[lkup].filt_rule_lock;
  1908. rule_head = &sw->recp_list[lkup].filt_rules;
  1909. mutex_lock(rule_lock);
  1910. status = ice_add_to_vsi_fltr_list(hw, vsi_handle, rule_head,
  1911. &remove_list_head);
  1912. mutex_unlock(rule_lock);
  1913. if (status)
  1914. return;
  1915. switch (lkup) {
  1916. case ICE_SW_LKUP_MAC:
  1917. ice_remove_mac(hw, &remove_list_head);
  1918. break;
  1919. case ICE_SW_LKUP_VLAN:
  1920. ice_remove_vlan(hw, &remove_list_head);
  1921. break;
  1922. case ICE_SW_LKUP_MAC_VLAN:
  1923. case ICE_SW_LKUP_ETHERTYPE:
  1924. case ICE_SW_LKUP_ETHERTYPE_MAC:
  1925. case ICE_SW_LKUP_PROMISC:
  1926. case ICE_SW_LKUP_DFLT:
  1927. case ICE_SW_LKUP_PROMISC_VLAN:
  1928. case ICE_SW_LKUP_LAST:
  1929. default:
  1930. ice_debug(hw, ICE_DBG_SW, "Unsupported lookup type %d\n", lkup);
  1931. break;
  1932. }
  1933. list_for_each_entry_safe(fm_entry, tmp, &remove_list_head, list_entry) {
  1934. list_del(&fm_entry->list_entry);
  1935. devm_kfree(ice_hw_to_dev(hw), fm_entry);
  1936. }
  1937. }
  1938. /**
  1939. * ice_remove_vsi_fltr - Remove all filters for a VSI
  1940. * @hw: pointer to the hardware structure
  1941. * @vsi_handle: VSI handle to remove filters from
  1942. */
  1943. void ice_remove_vsi_fltr(struct ice_hw *hw, u16 vsi_handle)
  1944. {
  1945. ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_MAC);
  1946. ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_MAC_VLAN);
  1947. ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_PROMISC);
  1948. ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_VLAN);
  1949. ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_DFLT);
  1950. ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_ETHERTYPE);
  1951. ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_ETHERTYPE_MAC);
  1952. ice_remove_vsi_lkup_fltr(hw, vsi_handle, ICE_SW_LKUP_PROMISC_VLAN);
  1953. }
  1954. /**
  1955. * ice_replay_vsi_fltr - Replay filters for requested VSI
  1956. * @hw: pointer to the hardware structure
  1957. * @vsi_handle: driver VSI handle
  1958. * @recp_id: Recipe id for which rules need to be replayed
  1959. * @list_head: list for which filters need to be replayed
  1960. *
  1961. * Replays the filter of recipe recp_id for a VSI represented via vsi_handle.
  1962. * It is required to pass valid VSI handle.
  1963. */
  1964. static enum ice_status
  1965. ice_replay_vsi_fltr(struct ice_hw *hw, u16 vsi_handle, u8 recp_id,
  1966. struct list_head *list_head)
  1967. {
  1968. struct ice_fltr_mgmt_list_entry *itr;
  1969. enum ice_status status = 0;
  1970. u16 hw_vsi_id;
  1971. if (list_empty(list_head))
  1972. return status;
  1973. hw_vsi_id = ice_get_hw_vsi_num(hw, vsi_handle);
  1974. list_for_each_entry(itr, list_head, list_entry) {
  1975. struct ice_fltr_list_entry f_entry;
  1976. f_entry.fltr_info = itr->fltr_info;
  1977. if (itr->vsi_count < 2 && recp_id != ICE_SW_LKUP_VLAN &&
  1978. itr->fltr_info.vsi_handle == vsi_handle) {
  1979. /* update the src in case it is vsi num */
  1980. if (f_entry.fltr_info.src_id == ICE_SRC_ID_VSI)
  1981. f_entry.fltr_info.src = hw_vsi_id;
  1982. status = ice_add_rule_internal(hw, recp_id, &f_entry);
  1983. if (status)
  1984. goto end;
  1985. continue;
  1986. }
  1987. if (!test_bit(vsi_handle, itr->vsi_list_info->vsi_map))
  1988. continue;
  1989. /* Clearing it so that the logic can add it back */
  1990. clear_bit(vsi_handle, itr->vsi_list_info->vsi_map);
  1991. f_entry.fltr_info.vsi_handle = vsi_handle;
  1992. f_entry.fltr_info.fltr_act = ICE_FWD_TO_VSI;
  1993. /* update the src in case it is vsi num */
  1994. if (f_entry.fltr_info.src_id == ICE_SRC_ID_VSI)
  1995. f_entry.fltr_info.src = hw_vsi_id;
  1996. if (recp_id == ICE_SW_LKUP_VLAN)
  1997. status = ice_add_vlan_internal(hw, &f_entry);
  1998. else
  1999. status = ice_add_rule_internal(hw, recp_id, &f_entry);
  2000. if (status)
  2001. goto end;
  2002. }
  2003. end:
  2004. return status;
  2005. }
  2006. /**
  2007. * ice_replay_vsi_all_fltr - replay all filters stored in bookkeeping lists
  2008. * @hw: pointer to the hardware structure
  2009. * @vsi_handle: driver VSI handle
  2010. *
  2011. * Replays filters for requested VSI via vsi_handle.
  2012. */
  2013. enum ice_status ice_replay_vsi_all_fltr(struct ice_hw *hw, u16 vsi_handle)
  2014. {
  2015. struct ice_switch_info *sw = hw->switch_info;
  2016. enum ice_status status = 0;
  2017. u8 i;
  2018. for (i = 0; i < ICE_SW_LKUP_LAST; i++) {
  2019. struct list_head *head;
  2020. head = &sw->recp_list[i].filt_replay_rules;
  2021. status = ice_replay_vsi_fltr(hw, vsi_handle, i, head);
  2022. if (status)
  2023. return status;
  2024. }
  2025. return status;
  2026. }
  2027. /**
  2028. * ice_rm_all_sw_replay_rule_info - deletes filter replay rules
  2029. * @hw: pointer to the hw struct
  2030. *
  2031. * Deletes the filter replay rules.
  2032. */
  2033. void ice_rm_all_sw_replay_rule_info(struct ice_hw *hw)
  2034. {
  2035. struct ice_switch_info *sw = hw->switch_info;
  2036. u8 i;
  2037. if (!sw)
  2038. return;
  2039. for (i = 0; i < ICE_SW_LKUP_LAST; i++) {
  2040. if (!list_empty(&sw->recp_list[i].filt_replay_rules)) {
  2041. struct list_head *l_head;
  2042. l_head = &sw->recp_list[i].filt_replay_rules;
  2043. ice_rem_sw_rule_info(hw, l_head);
  2044. }
  2045. }
  2046. }