en_netdev.c 93 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463
  1. /*
  2. * Copyright (c) 2007 Mellanox Technologies. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. *
  32. */
  33. #include <linux/bpf.h>
  34. #include <linux/etherdevice.h>
  35. #include <linux/tcp.h>
  36. #include <linux/if_vlan.h>
  37. #include <linux/delay.h>
  38. #include <linux/slab.h>
  39. #include <linux/hash.h>
  40. #include <net/ip.h>
  41. #include <net/busy_poll.h>
  42. #include <net/vxlan.h>
  43. #include <net/devlink.h>
  44. #include <linux/mlx4/driver.h>
  45. #include <linux/mlx4/device.h>
  46. #include <linux/mlx4/cmd.h>
  47. #include <linux/mlx4/cq.h>
  48. #include "mlx4_en.h"
  49. #include "en_port.h"
  50. int mlx4_en_setup_tc(struct net_device *dev, u8 up)
  51. {
  52. struct mlx4_en_priv *priv = netdev_priv(dev);
  53. int i;
  54. unsigned int offset = 0;
  55. if (up && up != MLX4_EN_NUM_UP)
  56. return -EINVAL;
  57. netdev_set_num_tc(dev, up);
  58. /* Partition Tx queues evenly amongst UP's */
  59. for (i = 0; i < up; i++) {
  60. netdev_set_tc_queue(dev, i, priv->num_tx_rings_p_up, offset);
  61. offset += priv->num_tx_rings_p_up;
  62. }
  63. #ifdef CONFIG_MLX4_EN_DCB
  64. if (!mlx4_is_slave(priv->mdev->dev)) {
  65. if (up) {
  66. if (priv->dcbx_cap)
  67. priv->flags |= MLX4_EN_FLAG_DCB_ENABLED;
  68. } else {
  69. priv->flags &= ~MLX4_EN_FLAG_DCB_ENABLED;
  70. priv->cee_config.pfc_state = false;
  71. }
  72. }
  73. #endif /* CONFIG_MLX4_EN_DCB */
  74. return 0;
  75. }
  76. static int __mlx4_en_setup_tc(struct net_device *dev, u32 handle, __be16 proto,
  77. struct tc_to_netdev *tc)
  78. {
  79. if (tc->type != TC_SETUP_MQPRIO)
  80. return -EINVAL;
  81. return mlx4_en_setup_tc(dev, tc->tc);
  82. }
  83. #ifdef CONFIG_RFS_ACCEL
  84. struct mlx4_en_filter {
  85. struct list_head next;
  86. struct work_struct work;
  87. u8 ip_proto;
  88. __be32 src_ip;
  89. __be32 dst_ip;
  90. __be16 src_port;
  91. __be16 dst_port;
  92. int rxq_index;
  93. struct mlx4_en_priv *priv;
  94. u32 flow_id; /* RFS infrastructure id */
  95. int id; /* mlx4_en driver id */
  96. u64 reg_id; /* Flow steering API id */
  97. u8 activated; /* Used to prevent expiry before filter
  98. * is attached
  99. */
  100. struct hlist_node filter_chain;
  101. };
  102. static void mlx4_en_filter_rfs_expire(struct mlx4_en_priv *priv);
  103. static enum mlx4_net_trans_rule_id mlx4_ip_proto_to_trans_rule_id(u8 ip_proto)
  104. {
  105. switch (ip_proto) {
  106. case IPPROTO_UDP:
  107. return MLX4_NET_TRANS_RULE_ID_UDP;
  108. case IPPROTO_TCP:
  109. return MLX4_NET_TRANS_RULE_ID_TCP;
  110. default:
  111. return MLX4_NET_TRANS_RULE_NUM;
  112. }
  113. };
  114. /* Must not acquire state_lock, as its corresponding work_sync
  115. * is done under it.
  116. */
  117. static void mlx4_en_filter_work(struct work_struct *work)
  118. {
  119. struct mlx4_en_filter *filter = container_of(work,
  120. struct mlx4_en_filter,
  121. work);
  122. struct mlx4_en_priv *priv = filter->priv;
  123. struct mlx4_spec_list spec_tcp_udp = {
  124. .id = mlx4_ip_proto_to_trans_rule_id(filter->ip_proto),
  125. {
  126. .tcp_udp = {
  127. .dst_port = filter->dst_port,
  128. .dst_port_msk = (__force __be16)-1,
  129. .src_port = filter->src_port,
  130. .src_port_msk = (__force __be16)-1,
  131. },
  132. },
  133. };
  134. struct mlx4_spec_list spec_ip = {
  135. .id = MLX4_NET_TRANS_RULE_ID_IPV4,
  136. {
  137. .ipv4 = {
  138. .dst_ip = filter->dst_ip,
  139. .dst_ip_msk = (__force __be32)-1,
  140. .src_ip = filter->src_ip,
  141. .src_ip_msk = (__force __be32)-1,
  142. },
  143. },
  144. };
  145. struct mlx4_spec_list spec_eth = {
  146. .id = MLX4_NET_TRANS_RULE_ID_ETH,
  147. };
  148. struct mlx4_net_trans_rule rule = {
  149. .list = LIST_HEAD_INIT(rule.list),
  150. .queue_mode = MLX4_NET_TRANS_Q_LIFO,
  151. .exclusive = 1,
  152. .allow_loopback = 1,
  153. .promisc_mode = MLX4_FS_REGULAR,
  154. .port = priv->port,
  155. .priority = MLX4_DOMAIN_RFS,
  156. };
  157. int rc;
  158. __be64 mac_mask = cpu_to_be64(MLX4_MAC_MASK << 16);
  159. if (spec_tcp_udp.id >= MLX4_NET_TRANS_RULE_NUM) {
  160. en_warn(priv, "RFS: ignoring unsupported ip protocol (%d)\n",
  161. filter->ip_proto);
  162. goto ignore;
  163. }
  164. list_add_tail(&spec_eth.list, &rule.list);
  165. list_add_tail(&spec_ip.list, &rule.list);
  166. list_add_tail(&spec_tcp_udp.list, &rule.list);
  167. rule.qpn = priv->rss_map.qps[filter->rxq_index].qpn;
  168. memcpy(spec_eth.eth.dst_mac, priv->dev->dev_addr, ETH_ALEN);
  169. memcpy(spec_eth.eth.dst_mac_msk, &mac_mask, ETH_ALEN);
  170. filter->activated = 0;
  171. if (filter->reg_id) {
  172. rc = mlx4_flow_detach(priv->mdev->dev, filter->reg_id);
  173. if (rc && rc != -ENOENT)
  174. en_err(priv, "Error detaching flow. rc = %d\n", rc);
  175. }
  176. rc = mlx4_flow_attach(priv->mdev->dev, &rule, &filter->reg_id);
  177. if (rc)
  178. en_err(priv, "Error attaching flow. err = %d\n", rc);
  179. ignore:
  180. mlx4_en_filter_rfs_expire(priv);
  181. filter->activated = 1;
  182. }
  183. static inline struct hlist_head *
  184. filter_hash_bucket(struct mlx4_en_priv *priv, __be32 src_ip, __be32 dst_ip,
  185. __be16 src_port, __be16 dst_port)
  186. {
  187. unsigned long l;
  188. int bucket_idx;
  189. l = (__force unsigned long)src_port |
  190. ((__force unsigned long)dst_port << 2);
  191. l ^= (__force unsigned long)(src_ip ^ dst_ip);
  192. bucket_idx = hash_long(l, MLX4_EN_FILTER_HASH_SHIFT);
  193. return &priv->filter_hash[bucket_idx];
  194. }
  195. static struct mlx4_en_filter *
  196. mlx4_en_filter_alloc(struct mlx4_en_priv *priv, int rxq_index, __be32 src_ip,
  197. __be32 dst_ip, u8 ip_proto, __be16 src_port,
  198. __be16 dst_port, u32 flow_id)
  199. {
  200. struct mlx4_en_filter *filter = NULL;
  201. filter = kzalloc(sizeof(struct mlx4_en_filter), GFP_ATOMIC);
  202. if (!filter)
  203. return NULL;
  204. filter->priv = priv;
  205. filter->rxq_index = rxq_index;
  206. INIT_WORK(&filter->work, mlx4_en_filter_work);
  207. filter->src_ip = src_ip;
  208. filter->dst_ip = dst_ip;
  209. filter->ip_proto = ip_proto;
  210. filter->src_port = src_port;
  211. filter->dst_port = dst_port;
  212. filter->flow_id = flow_id;
  213. filter->id = priv->last_filter_id++ % RPS_NO_FILTER;
  214. list_add_tail(&filter->next, &priv->filters);
  215. hlist_add_head(&filter->filter_chain,
  216. filter_hash_bucket(priv, src_ip, dst_ip, src_port,
  217. dst_port));
  218. return filter;
  219. }
  220. static void mlx4_en_filter_free(struct mlx4_en_filter *filter)
  221. {
  222. struct mlx4_en_priv *priv = filter->priv;
  223. int rc;
  224. list_del(&filter->next);
  225. rc = mlx4_flow_detach(priv->mdev->dev, filter->reg_id);
  226. if (rc && rc != -ENOENT)
  227. en_err(priv, "Error detaching flow. rc = %d\n", rc);
  228. kfree(filter);
  229. }
  230. static inline struct mlx4_en_filter *
  231. mlx4_en_filter_find(struct mlx4_en_priv *priv, __be32 src_ip, __be32 dst_ip,
  232. u8 ip_proto, __be16 src_port, __be16 dst_port)
  233. {
  234. struct mlx4_en_filter *filter;
  235. struct mlx4_en_filter *ret = NULL;
  236. hlist_for_each_entry(filter,
  237. filter_hash_bucket(priv, src_ip, dst_ip,
  238. src_port, dst_port),
  239. filter_chain) {
  240. if (filter->src_ip == src_ip &&
  241. filter->dst_ip == dst_ip &&
  242. filter->ip_proto == ip_proto &&
  243. filter->src_port == src_port &&
  244. filter->dst_port == dst_port) {
  245. ret = filter;
  246. break;
  247. }
  248. }
  249. return ret;
  250. }
  251. static int
  252. mlx4_en_filter_rfs(struct net_device *net_dev, const struct sk_buff *skb,
  253. u16 rxq_index, u32 flow_id)
  254. {
  255. struct mlx4_en_priv *priv = netdev_priv(net_dev);
  256. struct mlx4_en_filter *filter;
  257. const struct iphdr *ip;
  258. const __be16 *ports;
  259. u8 ip_proto;
  260. __be32 src_ip;
  261. __be32 dst_ip;
  262. __be16 src_port;
  263. __be16 dst_port;
  264. int nhoff = skb_network_offset(skb);
  265. int ret = 0;
  266. if (skb->protocol != htons(ETH_P_IP))
  267. return -EPROTONOSUPPORT;
  268. ip = (const struct iphdr *)(skb->data + nhoff);
  269. if (ip_is_fragment(ip))
  270. return -EPROTONOSUPPORT;
  271. if ((ip->protocol != IPPROTO_TCP) && (ip->protocol != IPPROTO_UDP))
  272. return -EPROTONOSUPPORT;
  273. ports = (const __be16 *)(skb->data + nhoff + 4 * ip->ihl);
  274. ip_proto = ip->protocol;
  275. src_ip = ip->saddr;
  276. dst_ip = ip->daddr;
  277. src_port = ports[0];
  278. dst_port = ports[1];
  279. spin_lock_bh(&priv->filters_lock);
  280. filter = mlx4_en_filter_find(priv, src_ip, dst_ip, ip_proto,
  281. src_port, dst_port);
  282. if (filter) {
  283. if (filter->rxq_index == rxq_index)
  284. goto out;
  285. filter->rxq_index = rxq_index;
  286. } else {
  287. filter = mlx4_en_filter_alloc(priv, rxq_index,
  288. src_ip, dst_ip, ip_proto,
  289. src_port, dst_port, flow_id);
  290. if (!filter) {
  291. ret = -ENOMEM;
  292. goto err;
  293. }
  294. }
  295. queue_work(priv->mdev->workqueue, &filter->work);
  296. out:
  297. ret = filter->id;
  298. err:
  299. spin_unlock_bh(&priv->filters_lock);
  300. return ret;
  301. }
  302. void mlx4_en_cleanup_filters(struct mlx4_en_priv *priv)
  303. {
  304. struct mlx4_en_filter *filter, *tmp;
  305. LIST_HEAD(del_list);
  306. spin_lock_bh(&priv->filters_lock);
  307. list_for_each_entry_safe(filter, tmp, &priv->filters, next) {
  308. list_move(&filter->next, &del_list);
  309. hlist_del(&filter->filter_chain);
  310. }
  311. spin_unlock_bh(&priv->filters_lock);
  312. list_for_each_entry_safe(filter, tmp, &del_list, next) {
  313. cancel_work_sync(&filter->work);
  314. mlx4_en_filter_free(filter);
  315. }
  316. }
  317. static void mlx4_en_filter_rfs_expire(struct mlx4_en_priv *priv)
  318. {
  319. struct mlx4_en_filter *filter = NULL, *tmp, *last_filter = NULL;
  320. LIST_HEAD(del_list);
  321. int i = 0;
  322. spin_lock_bh(&priv->filters_lock);
  323. list_for_each_entry_safe(filter, tmp, &priv->filters, next) {
  324. if (i > MLX4_EN_FILTER_EXPIRY_QUOTA)
  325. break;
  326. if (filter->activated &&
  327. !work_pending(&filter->work) &&
  328. rps_may_expire_flow(priv->dev,
  329. filter->rxq_index, filter->flow_id,
  330. filter->id)) {
  331. list_move(&filter->next, &del_list);
  332. hlist_del(&filter->filter_chain);
  333. } else
  334. last_filter = filter;
  335. i++;
  336. }
  337. if (last_filter && (&last_filter->next != priv->filters.next))
  338. list_move(&priv->filters, &last_filter->next);
  339. spin_unlock_bh(&priv->filters_lock);
  340. list_for_each_entry_safe(filter, tmp, &del_list, next)
  341. mlx4_en_filter_free(filter);
  342. }
  343. #endif
  344. static int mlx4_en_vlan_rx_add_vid(struct net_device *dev,
  345. __be16 proto, u16 vid)
  346. {
  347. struct mlx4_en_priv *priv = netdev_priv(dev);
  348. struct mlx4_en_dev *mdev = priv->mdev;
  349. int err;
  350. int idx;
  351. en_dbg(HW, priv, "adding VLAN:%d\n", vid);
  352. set_bit(vid, priv->active_vlans);
  353. /* Add VID to port VLAN filter */
  354. mutex_lock(&mdev->state_lock);
  355. if (mdev->device_up && priv->port_up) {
  356. err = mlx4_SET_VLAN_FLTR(mdev->dev, priv);
  357. if (err) {
  358. en_err(priv, "Failed configuring VLAN filter\n");
  359. goto out;
  360. }
  361. }
  362. err = mlx4_register_vlan(mdev->dev, priv->port, vid, &idx);
  363. if (err)
  364. en_dbg(HW, priv, "Failed adding vlan %d\n", vid);
  365. out:
  366. mutex_unlock(&mdev->state_lock);
  367. return err;
  368. }
  369. static int mlx4_en_vlan_rx_kill_vid(struct net_device *dev,
  370. __be16 proto, u16 vid)
  371. {
  372. struct mlx4_en_priv *priv = netdev_priv(dev);
  373. struct mlx4_en_dev *mdev = priv->mdev;
  374. int err = 0;
  375. en_dbg(HW, priv, "Killing VID:%d\n", vid);
  376. clear_bit(vid, priv->active_vlans);
  377. /* Remove VID from port VLAN filter */
  378. mutex_lock(&mdev->state_lock);
  379. mlx4_unregister_vlan(mdev->dev, priv->port, vid);
  380. if (mdev->device_up && priv->port_up) {
  381. err = mlx4_SET_VLAN_FLTR(mdev->dev, priv);
  382. if (err)
  383. en_err(priv, "Failed configuring VLAN filter\n");
  384. }
  385. mutex_unlock(&mdev->state_lock);
  386. return err;
  387. }
  388. static void mlx4_en_u64_to_mac(unsigned char dst_mac[ETH_ALEN + 2], u64 src_mac)
  389. {
  390. int i;
  391. for (i = ETH_ALEN - 1; i >= 0; --i) {
  392. dst_mac[i] = src_mac & 0xff;
  393. src_mac >>= 8;
  394. }
  395. memset(&dst_mac[ETH_ALEN], 0, 2);
  396. }
  397. static int mlx4_en_tunnel_steer_add(struct mlx4_en_priv *priv, unsigned char *addr,
  398. int qpn, u64 *reg_id)
  399. {
  400. int err;
  401. if (priv->mdev->dev->caps.tunnel_offload_mode != MLX4_TUNNEL_OFFLOAD_MODE_VXLAN ||
  402. priv->mdev->dev->caps.dmfs_high_steer_mode == MLX4_STEERING_DMFS_A0_STATIC)
  403. return 0; /* do nothing */
  404. err = mlx4_tunnel_steer_add(priv->mdev->dev, addr, priv->port, qpn,
  405. MLX4_DOMAIN_NIC, reg_id);
  406. if (err) {
  407. en_err(priv, "failed to add vxlan steering rule, err %d\n", err);
  408. return err;
  409. }
  410. en_dbg(DRV, priv, "added vxlan steering rule, mac %pM reg_id %llx\n", addr, *reg_id);
  411. return 0;
  412. }
  413. static int mlx4_en_uc_steer_add(struct mlx4_en_priv *priv,
  414. unsigned char *mac, int *qpn, u64 *reg_id)
  415. {
  416. struct mlx4_en_dev *mdev = priv->mdev;
  417. struct mlx4_dev *dev = mdev->dev;
  418. int err;
  419. switch (dev->caps.steering_mode) {
  420. case MLX4_STEERING_MODE_B0: {
  421. struct mlx4_qp qp;
  422. u8 gid[16] = {0};
  423. qp.qpn = *qpn;
  424. memcpy(&gid[10], mac, ETH_ALEN);
  425. gid[5] = priv->port;
  426. err = mlx4_unicast_attach(dev, &qp, gid, 0, MLX4_PROT_ETH);
  427. break;
  428. }
  429. case MLX4_STEERING_MODE_DEVICE_MANAGED: {
  430. struct mlx4_spec_list spec_eth = { {NULL} };
  431. __be64 mac_mask = cpu_to_be64(MLX4_MAC_MASK << 16);
  432. struct mlx4_net_trans_rule rule = {
  433. .queue_mode = MLX4_NET_TRANS_Q_FIFO,
  434. .exclusive = 0,
  435. .allow_loopback = 1,
  436. .promisc_mode = MLX4_FS_REGULAR,
  437. .priority = MLX4_DOMAIN_NIC,
  438. };
  439. rule.port = priv->port;
  440. rule.qpn = *qpn;
  441. INIT_LIST_HEAD(&rule.list);
  442. spec_eth.id = MLX4_NET_TRANS_RULE_ID_ETH;
  443. memcpy(spec_eth.eth.dst_mac, mac, ETH_ALEN);
  444. memcpy(spec_eth.eth.dst_mac_msk, &mac_mask, ETH_ALEN);
  445. list_add_tail(&spec_eth.list, &rule.list);
  446. err = mlx4_flow_attach(dev, &rule, reg_id);
  447. break;
  448. }
  449. default:
  450. return -EINVAL;
  451. }
  452. if (err)
  453. en_warn(priv, "Failed Attaching Unicast\n");
  454. return err;
  455. }
  456. static void mlx4_en_uc_steer_release(struct mlx4_en_priv *priv,
  457. unsigned char *mac, int qpn, u64 reg_id)
  458. {
  459. struct mlx4_en_dev *mdev = priv->mdev;
  460. struct mlx4_dev *dev = mdev->dev;
  461. switch (dev->caps.steering_mode) {
  462. case MLX4_STEERING_MODE_B0: {
  463. struct mlx4_qp qp;
  464. u8 gid[16] = {0};
  465. qp.qpn = qpn;
  466. memcpy(&gid[10], mac, ETH_ALEN);
  467. gid[5] = priv->port;
  468. mlx4_unicast_detach(dev, &qp, gid, MLX4_PROT_ETH);
  469. break;
  470. }
  471. case MLX4_STEERING_MODE_DEVICE_MANAGED: {
  472. mlx4_flow_detach(dev, reg_id);
  473. break;
  474. }
  475. default:
  476. en_err(priv, "Invalid steering mode.\n");
  477. }
  478. }
  479. static int mlx4_en_get_qp(struct mlx4_en_priv *priv)
  480. {
  481. struct mlx4_en_dev *mdev = priv->mdev;
  482. struct mlx4_dev *dev = mdev->dev;
  483. int index = 0;
  484. int err = 0;
  485. int *qpn = &priv->base_qpn;
  486. u64 mac = mlx4_mac_to_u64(priv->dev->dev_addr);
  487. en_dbg(DRV, priv, "Registering MAC: %pM for adding\n",
  488. priv->dev->dev_addr);
  489. index = mlx4_register_mac(dev, priv->port, mac);
  490. if (index < 0) {
  491. err = index;
  492. en_err(priv, "Failed adding MAC: %pM\n",
  493. priv->dev->dev_addr);
  494. return err;
  495. }
  496. if (dev->caps.steering_mode == MLX4_STEERING_MODE_A0) {
  497. int base_qpn = mlx4_get_base_qpn(dev, priv->port);
  498. *qpn = base_qpn + index;
  499. return 0;
  500. }
  501. err = mlx4_qp_reserve_range(dev, 1, 1, qpn, MLX4_RESERVE_A0_QP);
  502. en_dbg(DRV, priv, "Reserved qp %d\n", *qpn);
  503. if (err) {
  504. en_err(priv, "Failed to reserve qp for mac registration\n");
  505. mlx4_unregister_mac(dev, priv->port, mac);
  506. return err;
  507. }
  508. return 0;
  509. }
  510. static void mlx4_en_put_qp(struct mlx4_en_priv *priv)
  511. {
  512. struct mlx4_en_dev *mdev = priv->mdev;
  513. struct mlx4_dev *dev = mdev->dev;
  514. int qpn = priv->base_qpn;
  515. if (dev->caps.steering_mode == MLX4_STEERING_MODE_A0) {
  516. u64 mac = mlx4_mac_to_u64(priv->dev->dev_addr);
  517. en_dbg(DRV, priv, "Registering MAC: %pM for deleting\n",
  518. priv->dev->dev_addr);
  519. mlx4_unregister_mac(dev, priv->port, mac);
  520. } else {
  521. en_dbg(DRV, priv, "Releasing qp: port %d, qpn %d\n",
  522. priv->port, qpn);
  523. mlx4_qp_release_range(dev, qpn, 1);
  524. priv->flags &= ~MLX4_EN_FLAG_FORCE_PROMISC;
  525. }
  526. }
  527. static int mlx4_en_replace_mac(struct mlx4_en_priv *priv, int qpn,
  528. unsigned char *new_mac, unsigned char *prev_mac)
  529. {
  530. struct mlx4_en_dev *mdev = priv->mdev;
  531. struct mlx4_dev *dev = mdev->dev;
  532. int err = 0;
  533. u64 new_mac_u64 = mlx4_mac_to_u64(new_mac);
  534. if (dev->caps.steering_mode != MLX4_STEERING_MODE_A0) {
  535. struct hlist_head *bucket;
  536. unsigned int mac_hash;
  537. struct mlx4_mac_entry *entry;
  538. struct hlist_node *tmp;
  539. u64 prev_mac_u64 = mlx4_mac_to_u64(prev_mac);
  540. bucket = &priv->mac_hash[prev_mac[MLX4_EN_MAC_HASH_IDX]];
  541. hlist_for_each_entry_safe(entry, tmp, bucket, hlist) {
  542. if (ether_addr_equal_64bits(entry->mac, prev_mac)) {
  543. mlx4_en_uc_steer_release(priv, entry->mac,
  544. qpn, entry->reg_id);
  545. mlx4_unregister_mac(dev, priv->port,
  546. prev_mac_u64);
  547. hlist_del_rcu(&entry->hlist);
  548. synchronize_rcu();
  549. memcpy(entry->mac, new_mac, ETH_ALEN);
  550. entry->reg_id = 0;
  551. mac_hash = new_mac[MLX4_EN_MAC_HASH_IDX];
  552. hlist_add_head_rcu(&entry->hlist,
  553. &priv->mac_hash[mac_hash]);
  554. mlx4_register_mac(dev, priv->port, new_mac_u64);
  555. err = mlx4_en_uc_steer_add(priv, new_mac,
  556. &qpn,
  557. &entry->reg_id);
  558. if (err)
  559. return err;
  560. if (priv->tunnel_reg_id) {
  561. mlx4_flow_detach(priv->mdev->dev, priv->tunnel_reg_id);
  562. priv->tunnel_reg_id = 0;
  563. }
  564. err = mlx4_en_tunnel_steer_add(priv, new_mac, qpn,
  565. &priv->tunnel_reg_id);
  566. return err;
  567. }
  568. }
  569. return -EINVAL;
  570. }
  571. return __mlx4_replace_mac(dev, priv->port, qpn, new_mac_u64);
  572. }
  573. static int mlx4_en_do_set_mac(struct mlx4_en_priv *priv,
  574. unsigned char new_mac[ETH_ALEN + 2])
  575. {
  576. int err = 0;
  577. if (priv->port_up) {
  578. /* Remove old MAC and insert the new one */
  579. err = mlx4_en_replace_mac(priv, priv->base_qpn,
  580. new_mac, priv->current_mac);
  581. if (err)
  582. en_err(priv, "Failed changing HW MAC address\n");
  583. } else
  584. en_dbg(HW, priv, "Port is down while registering mac, exiting...\n");
  585. if (!err)
  586. memcpy(priv->current_mac, new_mac, sizeof(priv->current_mac));
  587. return err;
  588. }
  589. static int mlx4_en_set_mac(struct net_device *dev, void *addr)
  590. {
  591. struct mlx4_en_priv *priv = netdev_priv(dev);
  592. struct mlx4_en_dev *mdev = priv->mdev;
  593. struct sockaddr *saddr = addr;
  594. unsigned char new_mac[ETH_ALEN + 2];
  595. int err;
  596. if (!is_valid_ether_addr(saddr->sa_data))
  597. return -EADDRNOTAVAIL;
  598. mutex_lock(&mdev->state_lock);
  599. memcpy(new_mac, saddr->sa_data, ETH_ALEN);
  600. err = mlx4_en_do_set_mac(priv, new_mac);
  601. if (!err)
  602. memcpy(dev->dev_addr, saddr->sa_data, ETH_ALEN);
  603. mutex_unlock(&mdev->state_lock);
  604. return err;
  605. }
  606. static void mlx4_en_clear_list(struct net_device *dev)
  607. {
  608. struct mlx4_en_priv *priv = netdev_priv(dev);
  609. struct mlx4_en_mc_list *tmp, *mc_to_del;
  610. list_for_each_entry_safe(mc_to_del, tmp, &priv->mc_list, list) {
  611. list_del(&mc_to_del->list);
  612. kfree(mc_to_del);
  613. }
  614. }
  615. static void mlx4_en_cache_mclist(struct net_device *dev)
  616. {
  617. struct mlx4_en_priv *priv = netdev_priv(dev);
  618. struct netdev_hw_addr *ha;
  619. struct mlx4_en_mc_list *tmp;
  620. mlx4_en_clear_list(dev);
  621. netdev_for_each_mc_addr(ha, dev) {
  622. tmp = kzalloc(sizeof(struct mlx4_en_mc_list), GFP_ATOMIC);
  623. if (!tmp) {
  624. mlx4_en_clear_list(dev);
  625. return;
  626. }
  627. memcpy(tmp->addr, ha->addr, ETH_ALEN);
  628. list_add_tail(&tmp->list, &priv->mc_list);
  629. }
  630. }
  631. static void update_mclist_flags(struct mlx4_en_priv *priv,
  632. struct list_head *dst,
  633. struct list_head *src)
  634. {
  635. struct mlx4_en_mc_list *dst_tmp, *src_tmp, *new_mc;
  636. bool found;
  637. /* Find all the entries that should be removed from dst,
  638. * These are the entries that are not found in src
  639. */
  640. list_for_each_entry(dst_tmp, dst, list) {
  641. found = false;
  642. list_for_each_entry(src_tmp, src, list) {
  643. if (ether_addr_equal(dst_tmp->addr, src_tmp->addr)) {
  644. found = true;
  645. break;
  646. }
  647. }
  648. if (!found)
  649. dst_tmp->action = MCLIST_REM;
  650. }
  651. /* Add entries that exist in src but not in dst
  652. * mark them as need to add
  653. */
  654. list_for_each_entry(src_tmp, src, list) {
  655. found = false;
  656. list_for_each_entry(dst_tmp, dst, list) {
  657. if (ether_addr_equal(dst_tmp->addr, src_tmp->addr)) {
  658. dst_tmp->action = MCLIST_NONE;
  659. found = true;
  660. break;
  661. }
  662. }
  663. if (!found) {
  664. new_mc = kmemdup(src_tmp,
  665. sizeof(struct mlx4_en_mc_list),
  666. GFP_KERNEL);
  667. if (!new_mc)
  668. return;
  669. new_mc->action = MCLIST_ADD;
  670. list_add_tail(&new_mc->list, dst);
  671. }
  672. }
  673. }
  674. static void mlx4_en_set_rx_mode(struct net_device *dev)
  675. {
  676. struct mlx4_en_priv *priv = netdev_priv(dev);
  677. if (!priv->port_up)
  678. return;
  679. queue_work(priv->mdev->workqueue, &priv->rx_mode_task);
  680. }
  681. static void mlx4_en_set_promisc_mode(struct mlx4_en_priv *priv,
  682. struct mlx4_en_dev *mdev)
  683. {
  684. int err = 0;
  685. if (!(priv->flags & MLX4_EN_FLAG_PROMISC)) {
  686. if (netif_msg_rx_status(priv))
  687. en_warn(priv, "Entering promiscuous mode\n");
  688. priv->flags |= MLX4_EN_FLAG_PROMISC;
  689. /* Enable promiscouos mode */
  690. switch (mdev->dev->caps.steering_mode) {
  691. case MLX4_STEERING_MODE_DEVICE_MANAGED:
  692. err = mlx4_flow_steer_promisc_add(mdev->dev,
  693. priv->port,
  694. priv->base_qpn,
  695. MLX4_FS_ALL_DEFAULT);
  696. if (err)
  697. en_err(priv, "Failed enabling promiscuous mode\n");
  698. priv->flags |= MLX4_EN_FLAG_MC_PROMISC;
  699. break;
  700. case MLX4_STEERING_MODE_B0:
  701. err = mlx4_unicast_promisc_add(mdev->dev,
  702. priv->base_qpn,
  703. priv->port);
  704. if (err)
  705. en_err(priv, "Failed enabling unicast promiscuous mode\n");
  706. /* Add the default qp number as multicast
  707. * promisc
  708. */
  709. if (!(priv->flags & MLX4_EN_FLAG_MC_PROMISC)) {
  710. err = mlx4_multicast_promisc_add(mdev->dev,
  711. priv->base_qpn,
  712. priv->port);
  713. if (err)
  714. en_err(priv, "Failed enabling multicast promiscuous mode\n");
  715. priv->flags |= MLX4_EN_FLAG_MC_PROMISC;
  716. }
  717. break;
  718. case MLX4_STEERING_MODE_A0:
  719. err = mlx4_SET_PORT_qpn_calc(mdev->dev,
  720. priv->port,
  721. priv->base_qpn,
  722. 1);
  723. if (err)
  724. en_err(priv, "Failed enabling promiscuous mode\n");
  725. break;
  726. }
  727. /* Disable port multicast filter (unconditionally) */
  728. err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
  729. 0, MLX4_MCAST_DISABLE);
  730. if (err)
  731. en_err(priv, "Failed disabling multicast filter\n");
  732. }
  733. }
  734. static void mlx4_en_clear_promisc_mode(struct mlx4_en_priv *priv,
  735. struct mlx4_en_dev *mdev)
  736. {
  737. int err = 0;
  738. if (netif_msg_rx_status(priv))
  739. en_warn(priv, "Leaving promiscuous mode\n");
  740. priv->flags &= ~MLX4_EN_FLAG_PROMISC;
  741. /* Disable promiscouos mode */
  742. switch (mdev->dev->caps.steering_mode) {
  743. case MLX4_STEERING_MODE_DEVICE_MANAGED:
  744. err = mlx4_flow_steer_promisc_remove(mdev->dev,
  745. priv->port,
  746. MLX4_FS_ALL_DEFAULT);
  747. if (err)
  748. en_err(priv, "Failed disabling promiscuous mode\n");
  749. priv->flags &= ~MLX4_EN_FLAG_MC_PROMISC;
  750. break;
  751. case MLX4_STEERING_MODE_B0:
  752. err = mlx4_unicast_promisc_remove(mdev->dev,
  753. priv->base_qpn,
  754. priv->port);
  755. if (err)
  756. en_err(priv, "Failed disabling unicast promiscuous mode\n");
  757. /* Disable Multicast promisc */
  758. if (priv->flags & MLX4_EN_FLAG_MC_PROMISC) {
  759. err = mlx4_multicast_promisc_remove(mdev->dev,
  760. priv->base_qpn,
  761. priv->port);
  762. if (err)
  763. en_err(priv, "Failed disabling multicast promiscuous mode\n");
  764. priv->flags &= ~MLX4_EN_FLAG_MC_PROMISC;
  765. }
  766. break;
  767. case MLX4_STEERING_MODE_A0:
  768. err = mlx4_SET_PORT_qpn_calc(mdev->dev,
  769. priv->port,
  770. priv->base_qpn, 0);
  771. if (err)
  772. en_err(priv, "Failed disabling promiscuous mode\n");
  773. break;
  774. }
  775. }
  776. static void mlx4_en_do_multicast(struct mlx4_en_priv *priv,
  777. struct net_device *dev,
  778. struct mlx4_en_dev *mdev)
  779. {
  780. struct mlx4_en_mc_list *mclist, *tmp;
  781. u64 mcast_addr = 0;
  782. u8 mc_list[16] = {0};
  783. int err = 0;
  784. /* Enable/disable the multicast filter according to IFF_ALLMULTI */
  785. if (dev->flags & IFF_ALLMULTI) {
  786. err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
  787. 0, MLX4_MCAST_DISABLE);
  788. if (err)
  789. en_err(priv, "Failed disabling multicast filter\n");
  790. /* Add the default qp number as multicast promisc */
  791. if (!(priv->flags & MLX4_EN_FLAG_MC_PROMISC)) {
  792. switch (mdev->dev->caps.steering_mode) {
  793. case MLX4_STEERING_MODE_DEVICE_MANAGED:
  794. err = mlx4_flow_steer_promisc_add(mdev->dev,
  795. priv->port,
  796. priv->base_qpn,
  797. MLX4_FS_MC_DEFAULT);
  798. break;
  799. case MLX4_STEERING_MODE_B0:
  800. err = mlx4_multicast_promisc_add(mdev->dev,
  801. priv->base_qpn,
  802. priv->port);
  803. break;
  804. case MLX4_STEERING_MODE_A0:
  805. break;
  806. }
  807. if (err)
  808. en_err(priv, "Failed entering multicast promisc mode\n");
  809. priv->flags |= MLX4_EN_FLAG_MC_PROMISC;
  810. }
  811. } else {
  812. /* Disable Multicast promisc */
  813. if (priv->flags & MLX4_EN_FLAG_MC_PROMISC) {
  814. switch (mdev->dev->caps.steering_mode) {
  815. case MLX4_STEERING_MODE_DEVICE_MANAGED:
  816. err = mlx4_flow_steer_promisc_remove(mdev->dev,
  817. priv->port,
  818. MLX4_FS_MC_DEFAULT);
  819. break;
  820. case MLX4_STEERING_MODE_B0:
  821. err = mlx4_multicast_promisc_remove(mdev->dev,
  822. priv->base_qpn,
  823. priv->port);
  824. break;
  825. case MLX4_STEERING_MODE_A0:
  826. break;
  827. }
  828. if (err)
  829. en_err(priv, "Failed disabling multicast promiscuous mode\n");
  830. priv->flags &= ~MLX4_EN_FLAG_MC_PROMISC;
  831. }
  832. err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
  833. 0, MLX4_MCAST_DISABLE);
  834. if (err)
  835. en_err(priv, "Failed disabling multicast filter\n");
  836. /* Flush mcast filter and init it with broadcast address */
  837. mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, ETH_BCAST,
  838. 1, MLX4_MCAST_CONFIG);
  839. /* Update multicast list - we cache all addresses so they won't
  840. * change while HW is updated holding the command semaphor */
  841. netif_addr_lock_bh(dev);
  842. mlx4_en_cache_mclist(dev);
  843. netif_addr_unlock_bh(dev);
  844. list_for_each_entry(mclist, &priv->mc_list, list) {
  845. mcast_addr = mlx4_mac_to_u64(mclist->addr);
  846. mlx4_SET_MCAST_FLTR(mdev->dev, priv->port,
  847. mcast_addr, 0, MLX4_MCAST_CONFIG);
  848. }
  849. err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
  850. 0, MLX4_MCAST_ENABLE);
  851. if (err)
  852. en_err(priv, "Failed enabling multicast filter\n");
  853. update_mclist_flags(priv, &priv->curr_list, &priv->mc_list);
  854. list_for_each_entry_safe(mclist, tmp, &priv->curr_list, list) {
  855. if (mclist->action == MCLIST_REM) {
  856. /* detach this address and delete from list */
  857. memcpy(&mc_list[10], mclist->addr, ETH_ALEN);
  858. mc_list[5] = priv->port;
  859. err = mlx4_multicast_detach(mdev->dev,
  860. &priv->rss_map.indir_qp,
  861. mc_list,
  862. MLX4_PROT_ETH,
  863. mclist->reg_id);
  864. if (err)
  865. en_err(priv, "Fail to detach multicast address\n");
  866. if (mclist->tunnel_reg_id) {
  867. err = mlx4_flow_detach(priv->mdev->dev, mclist->tunnel_reg_id);
  868. if (err)
  869. en_err(priv, "Failed to detach multicast address\n");
  870. }
  871. /* remove from list */
  872. list_del(&mclist->list);
  873. kfree(mclist);
  874. } else if (mclist->action == MCLIST_ADD) {
  875. /* attach the address */
  876. memcpy(&mc_list[10], mclist->addr, ETH_ALEN);
  877. /* needed for B0 steering support */
  878. mc_list[5] = priv->port;
  879. err = mlx4_multicast_attach(mdev->dev,
  880. &priv->rss_map.indir_qp,
  881. mc_list,
  882. priv->port, 0,
  883. MLX4_PROT_ETH,
  884. &mclist->reg_id);
  885. if (err)
  886. en_err(priv, "Fail to attach multicast address\n");
  887. err = mlx4_en_tunnel_steer_add(priv, &mc_list[10], priv->base_qpn,
  888. &mclist->tunnel_reg_id);
  889. if (err)
  890. en_err(priv, "Failed to attach multicast address\n");
  891. }
  892. }
  893. }
  894. }
  895. static void mlx4_en_do_uc_filter(struct mlx4_en_priv *priv,
  896. struct net_device *dev,
  897. struct mlx4_en_dev *mdev)
  898. {
  899. struct netdev_hw_addr *ha;
  900. struct mlx4_mac_entry *entry;
  901. struct hlist_node *tmp;
  902. bool found;
  903. u64 mac;
  904. int err = 0;
  905. struct hlist_head *bucket;
  906. unsigned int i;
  907. int removed = 0;
  908. u32 prev_flags;
  909. /* Note that we do not need to protect our mac_hash traversal with rcu,
  910. * since all modification code is protected by mdev->state_lock
  911. */
  912. /* find what to remove */
  913. for (i = 0; i < MLX4_EN_MAC_HASH_SIZE; ++i) {
  914. bucket = &priv->mac_hash[i];
  915. hlist_for_each_entry_safe(entry, tmp, bucket, hlist) {
  916. found = false;
  917. netdev_for_each_uc_addr(ha, dev) {
  918. if (ether_addr_equal_64bits(entry->mac,
  919. ha->addr)) {
  920. found = true;
  921. break;
  922. }
  923. }
  924. /* MAC address of the port is not in uc list */
  925. if (ether_addr_equal_64bits(entry->mac,
  926. priv->current_mac))
  927. found = true;
  928. if (!found) {
  929. mac = mlx4_mac_to_u64(entry->mac);
  930. mlx4_en_uc_steer_release(priv, entry->mac,
  931. priv->base_qpn,
  932. entry->reg_id);
  933. mlx4_unregister_mac(mdev->dev, priv->port, mac);
  934. hlist_del_rcu(&entry->hlist);
  935. kfree_rcu(entry, rcu);
  936. en_dbg(DRV, priv, "Removed MAC %pM on port:%d\n",
  937. entry->mac, priv->port);
  938. ++removed;
  939. }
  940. }
  941. }
  942. /* if we didn't remove anything, there is no use in trying to add
  943. * again once we are in a forced promisc mode state
  944. */
  945. if ((priv->flags & MLX4_EN_FLAG_FORCE_PROMISC) && 0 == removed)
  946. return;
  947. prev_flags = priv->flags;
  948. priv->flags &= ~MLX4_EN_FLAG_FORCE_PROMISC;
  949. /* find what to add */
  950. netdev_for_each_uc_addr(ha, dev) {
  951. found = false;
  952. bucket = &priv->mac_hash[ha->addr[MLX4_EN_MAC_HASH_IDX]];
  953. hlist_for_each_entry(entry, bucket, hlist) {
  954. if (ether_addr_equal_64bits(entry->mac, ha->addr)) {
  955. found = true;
  956. break;
  957. }
  958. }
  959. if (!found) {
  960. entry = kmalloc(sizeof(*entry), GFP_KERNEL);
  961. if (!entry) {
  962. en_err(priv, "Failed adding MAC %pM on port:%d (out of memory)\n",
  963. ha->addr, priv->port);
  964. priv->flags |= MLX4_EN_FLAG_FORCE_PROMISC;
  965. break;
  966. }
  967. mac = mlx4_mac_to_u64(ha->addr);
  968. memcpy(entry->mac, ha->addr, ETH_ALEN);
  969. err = mlx4_register_mac(mdev->dev, priv->port, mac);
  970. if (err < 0) {
  971. en_err(priv, "Failed registering MAC %pM on port %d: %d\n",
  972. ha->addr, priv->port, err);
  973. kfree(entry);
  974. priv->flags |= MLX4_EN_FLAG_FORCE_PROMISC;
  975. break;
  976. }
  977. err = mlx4_en_uc_steer_add(priv, ha->addr,
  978. &priv->base_qpn,
  979. &entry->reg_id);
  980. if (err) {
  981. en_err(priv, "Failed adding MAC %pM on port %d: %d\n",
  982. ha->addr, priv->port, err);
  983. mlx4_unregister_mac(mdev->dev, priv->port, mac);
  984. kfree(entry);
  985. priv->flags |= MLX4_EN_FLAG_FORCE_PROMISC;
  986. break;
  987. } else {
  988. unsigned int mac_hash;
  989. en_dbg(DRV, priv, "Added MAC %pM on port:%d\n",
  990. ha->addr, priv->port);
  991. mac_hash = ha->addr[MLX4_EN_MAC_HASH_IDX];
  992. bucket = &priv->mac_hash[mac_hash];
  993. hlist_add_head_rcu(&entry->hlist, bucket);
  994. }
  995. }
  996. }
  997. if (priv->flags & MLX4_EN_FLAG_FORCE_PROMISC) {
  998. en_warn(priv, "Forcing promiscuous mode on port:%d\n",
  999. priv->port);
  1000. } else if (prev_flags & MLX4_EN_FLAG_FORCE_PROMISC) {
  1001. en_warn(priv, "Stop forcing promiscuous mode on port:%d\n",
  1002. priv->port);
  1003. }
  1004. }
  1005. static void mlx4_en_do_set_rx_mode(struct work_struct *work)
  1006. {
  1007. struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
  1008. rx_mode_task);
  1009. struct mlx4_en_dev *mdev = priv->mdev;
  1010. struct net_device *dev = priv->dev;
  1011. mutex_lock(&mdev->state_lock);
  1012. if (!mdev->device_up) {
  1013. en_dbg(HW, priv, "Card is not up, ignoring rx mode change.\n");
  1014. goto out;
  1015. }
  1016. if (!priv->port_up) {
  1017. en_dbg(HW, priv, "Port is down, ignoring rx mode change.\n");
  1018. goto out;
  1019. }
  1020. if (!netif_carrier_ok(dev)) {
  1021. if (!mlx4_en_QUERY_PORT(mdev, priv->port)) {
  1022. if (priv->port_state.link_state) {
  1023. priv->last_link_state = MLX4_DEV_EVENT_PORT_UP;
  1024. netif_carrier_on(dev);
  1025. en_dbg(LINK, priv, "Link Up\n");
  1026. }
  1027. }
  1028. }
  1029. if (dev->priv_flags & IFF_UNICAST_FLT)
  1030. mlx4_en_do_uc_filter(priv, dev, mdev);
  1031. /* Promsicuous mode: disable all filters */
  1032. if ((dev->flags & IFF_PROMISC) ||
  1033. (priv->flags & MLX4_EN_FLAG_FORCE_PROMISC)) {
  1034. mlx4_en_set_promisc_mode(priv, mdev);
  1035. goto out;
  1036. }
  1037. /* Not in promiscuous mode */
  1038. if (priv->flags & MLX4_EN_FLAG_PROMISC)
  1039. mlx4_en_clear_promisc_mode(priv, mdev);
  1040. mlx4_en_do_multicast(priv, dev, mdev);
  1041. out:
  1042. mutex_unlock(&mdev->state_lock);
  1043. }
  1044. #ifdef CONFIG_NET_POLL_CONTROLLER
  1045. static void mlx4_en_netpoll(struct net_device *dev)
  1046. {
  1047. struct mlx4_en_priv *priv = netdev_priv(dev);
  1048. struct mlx4_en_cq *cq;
  1049. int i;
  1050. for (i = 0; i < priv->tx_ring_num; i++) {
  1051. cq = priv->tx_cq[i];
  1052. napi_schedule(&cq->napi);
  1053. }
  1054. }
  1055. #endif
  1056. static int mlx4_en_set_rss_steer_rules(struct mlx4_en_priv *priv)
  1057. {
  1058. u64 reg_id;
  1059. int err = 0;
  1060. int *qpn = &priv->base_qpn;
  1061. struct mlx4_mac_entry *entry;
  1062. err = mlx4_en_uc_steer_add(priv, priv->dev->dev_addr, qpn, &reg_id);
  1063. if (err)
  1064. return err;
  1065. err = mlx4_en_tunnel_steer_add(priv, priv->dev->dev_addr, *qpn,
  1066. &priv->tunnel_reg_id);
  1067. if (err)
  1068. goto tunnel_err;
  1069. entry = kmalloc(sizeof(*entry), GFP_KERNEL);
  1070. if (!entry) {
  1071. err = -ENOMEM;
  1072. goto alloc_err;
  1073. }
  1074. memcpy(entry->mac, priv->dev->dev_addr, sizeof(entry->mac));
  1075. memcpy(priv->current_mac, entry->mac, sizeof(priv->current_mac));
  1076. entry->reg_id = reg_id;
  1077. hlist_add_head_rcu(&entry->hlist,
  1078. &priv->mac_hash[entry->mac[MLX4_EN_MAC_HASH_IDX]]);
  1079. return 0;
  1080. alloc_err:
  1081. if (priv->tunnel_reg_id)
  1082. mlx4_flow_detach(priv->mdev->dev, priv->tunnel_reg_id);
  1083. tunnel_err:
  1084. mlx4_en_uc_steer_release(priv, priv->dev->dev_addr, *qpn, reg_id);
  1085. return err;
  1086. }
  1087. static void mlx4_en_delete_rss_steer_rules(struct mlx4_en_priv *priv)
  1088. {
  1089. u64 mac;
  1090. unsigned int i;
  1091. int qpn = priv->base_qpn;
  1092. struct hlist_head *bucket;
  1093. struct hlist_node *tmp;
  1094. struct mlx4_mac_entry *entry;
  1095. for (i = 0; i < MLX4_EN_MAC_HASH_SIZE; ++i) {
  1096. bucket = &priv->mac_hash[i];
  1097. hlist_for_each_entry_safe(entry, tmp, bucket, hlist) {
  1098. mac = mlx4_mac_to_u64(entry->mac);
  1099. en_dbg(DRV, priv, "Registering MAC:%pM for deleting\n",
  1100. entry->mac);
  1101. mlx4_en_uc_steer_release(priv, entry->mac,
  1102. qpn, entry->reg_id);
  1103. mlx4_unregister_mac(priv->mdev->dev, priv->port, mac);
  1104. hlist_del_rcu(&entry->hlist);
  1105. kfree_rcu(entry, rcu);
  1106. }
  1107. }
  1108. if (priv->tunnel_reg_id) {
  1109. mlx4_flow_detach(priv->mdev->dev, priv->tunnel_reg_id);
  1110. priv->tunnel_reg_id = 0;
  1111. }
  1112. }
  1113. static void mlx4_en_tx_timeout(struct net_device *dev)
  1114. {
  1115. struct mlx4_en_priv *priv = netdev_priv(dev);
  1116. struct mlx4_en_dev *mdev = priv->mdev;
  1117. int i;
  1118. if (netif_msg_timer(priv))
  1119. en_warn(priv, "Tx timeout called on port:%d\n", priv->port);
  1120. for (i = 0; i < priv->tx_ring_num; i++) {
  1121. if (!netif_tx_queue_stopped(netdev_get_tx_queue(dev, i)))
  1122. continue;
  1123. en_warn(priv, "TX timeout on queue: %d, QP: 0x%x, CQ: 0x%x, Cons: 0x%x, Prod: 0x%x\n",
  1124. i, priv->tx_ring[i]->qpn, priv->tx_ring[i]->cqn,
  1125. priv->tx_ring[i]->cons, priv->tx_ring[i]->prod);
  1126. }
  1127. priv->port_stats.tx_timeout++;
  1128. en_dbg(DRV, priv, "Scheduling watchdog\n");
  1129. queue_work(mdev->workqueue, &priv->watchdog_task);
  1130. }
  1131. static struct rtnl_link_stats64 *
  1132. mlx4_en_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
  1133. {
  1134. struct mlx4_en_priv *priv = netdev_priv(dev);
  1135. spin_lock_bh(&priv->stats_lock);
  1136. netdev_stats_to_stats64(stats, &dev->stats);
  1137. spin_unlock_bh(&priv->stats_lock);
  1138. return stats;
  1139. }
  1140. static void mlx4_en_set_default_moderation(struct mlx4_en_priv *priv)
  1141. {
  1142. struct mlx4_en_cq *cq;
  1143. int i;
  1144. /* If we haven't received a specific coalescing setting
  1145. * (module param), we set the moderation parameters as follows:
  1146. * - moder_cnt is set to the number of mtu sized packets to
  1147. * satisfy our coalescing target.
  1148. * - moder_time is set to a fixed value.
  1149. */
  1150. priv->rx_frames = MLX4_EN_RX_COAL_TARGET;
  1151. priv->rx_usecs = MLX4_EN_RX_COAL_TIME;
  1152. priv->tx_frames = MLX4_EN_TX_COAL_PKTS;
  1153. priv->tx_usecs = MLX4_EN_TX_COAL_TIME;
  1154. en_dbg(INTR, priv, "Default coalesing params for mtu:%d - rx_frames:%d rx_usecs:%d\n",
  1155. priv->dev->mtu, priv->rx_frames, priv->rx_usecs);
  1156. /* Setup cq moderation params */
  1157. for (i = 0; i < priv->rx_ring_num; i++) {
  1158. cq = priv->rx_cq[i];
  1159. cq->moder_cnt = priv->rx_frames;
  1160. cq->moder_time = priv->rx_usecs;
  1161. priv->last_moder_time[i] = MLX4_EN_AUTO_CONF;
  1162. priv->last_moder_packets[i] = 0;
  1163. priv->last_moder_bytes[i] = 0;
  1164. }
  1165. for (i = 0; i < priv->tx_ring_num; i++) {
  1166. cq = priv->tx_cq[i];
  1167. cq->moder_cnt = priv->tx_frames;
  1168. cq->moder_time = priv->tx_usecs;
  1169. }
  1170. /* Reset auto-moderation params */
  1171. priv->pkt_rate_low = MLX4_EN_RX_RATE_LOW;
  1172. priv->rx_usecs_low = MLX4_EN_RX_COAL_TIME_LOW;
  1173. priv->pkt_rate_high = MLX4_EN_RX_RATE_HIGH;
  1174. priv->rx_usecs_high = MLX4_EN_RX_COAL_TIME_HIGH;
  1175. priv->sample_interval = MLX4_EN_SAMPLE_INTERVAL;
  1176. priv->adaptive_rx_coal = 1;
  1177. priv->last_moder_jiffies = 0;
  1178. priv->last_moder_tx_packets = 0;
  1179. }
  1180. static void mlx4_en_auto_moderation(struct mlx4_en_priv *priv)
  1181. {
  1182. unsigned long period = (unsigned long) (jiffies - priv->last_moder_jiffies);
  1183. struct mlx4_en_cq *cq;
  1184. unsigned long packets;
  1185. unsigned long rate;
  1186. unsigned long avg_pkt_size;
  1187. unsigned long rx_packets;
  1188. unsigned long rx_bytes;
  1189. unsigned long rx_pkt_diff;
  1190. int moder_time;
  1191. int ring, err;
  1192. if (!priv->adaptive_rx_coal || period < priv->sample_interval * HZ)
  1193. return;
  1194. for (ring = 0; ring < priv->rx_ring_num; ring++) {
  1195. spin_lock_bh(&priv->stats_lock);
  1196. rx_packets = priv->rx_ring[ring]->packets;
  1197. rx_bytes = priv->rx_ring[ring]->bytes;
  1198. spin_unlock_bh(&priv->stats_lock);
  1199. rx_pkt_diff = ((unsigned long) (rx_packets -
  1200. priv->last_moder_packets[ring]));
  1201. packets = rx_pkt_diff;
  1202. rate = packets * HZ / period;
  1203. avg_pkt_size = packets ? ((unsigned long) (rx_bytes -
  1204. priv->last_moder_bytes[ring])) / packets : 0;
  1205. /* Apply auto-moderation only when packet rate
  1206. * exceeds a rate that it matters */
  1207. if (rate > (MLX4_EN_RX_RATE_THRESH / priv->rx_ring_num) &&
  1208. avg_pkt_size > MLX4_EN_AVG_PKT_SMALL) {
  1209. if (rate < priv->pkt_rate_low)
  1210. moder_time = priv->rx_usecs_low;
  1211. else if (rate > priv->pkt_rate_high)
  1212. moder_time = priv->rx_usecs_high;
  1213. else
  1214. moder_time = (rate - priv->pkt_rate_low) *
  1215. (priv->rx_usecs_high - priv->rx_usecs_low) /
  1216. (priv->pkt_rate_high - priv->pkt_rate_low) +
  1217. priv->rx_usecs_low;
  1218. } else {
  1219. moder_time = priv->rx_usecs_low;
  1220. }
  1221. if (moder_time != priv->last_moder_time[ring]) {
  1222. priv->last_moder_time[ring] = moder_time;
  1223. cq = priv->rx_cq[ring];
  1224. cq->moder_time = moder_time;
  1225. cq->moder_cnt = priv->rx_frames;
  1226. err = mlx4_en_set_cq_moder(priv, cq);
  1227. if (err)
  1228. en_err(priv, "Failed modifying moderation for cq:%d\n",
  1229. ring);
  1230. }
  1231. priv->last_moder_packets[ring] = rx_packets;
  1232. priv->last_moder_bytes[ring] = rx_bytes;
  1233. }
  1234. priv->last_moder_jiffies = jiffies;
  1235. }
  1236. static void mlx4_en_do_get_stats(struct work_struct *work)
  1237. {
  1238. struct delayed_work *delay = to_delayed_work(work);
  1239. struct mlx4_en_priv *priv = container_of(delay, struct mlx4_en_priv,
  1240. stats_task);
  1241. struct mlx4_en_dev *mdev = priv->mdev;
  1242. int err;
  1243. mutex_lock(&mdev->state_lock);
  1244. if (mdev->device_up) {
  1245. if (priv->port_up) {
  1246. err = mlx4_en_DUMP_ETH_STATS(mdev, priv->port, 0);
  1247. if (err)
  1248. en_dbg(HW, priv, "Could not update stats\n");
  1249. mlx4_en_auto_moderation(priv);
  1250. }
  1251. queue_delayed_work(mdev->workqueue, &priv->stats_task, STATS_DELAY);
  1252. }
  1253. if (mdev->mac_removed[MLX4_MAX_PORTS + 1 - priv->port]) {
  1254. mlx4_en_do_set_mac(priv, priv->current_mac);
  1255. mdev->mac_removed[MLX4_MAX_PORTS + 1 - priv->port] = 0;
  1256. }
  1257. mutex_unlock(&mdev->state_lock);
  1258. }
  1259. /* mlx4_en_service_task - Run service task for tasks that needed to be done
  1260. * periodically
  1261. */
  1262. static void mlx4_en_service_task(struct work_struct *work)
  1263. {
  1264. struct delayed_work *delay = to_delayed_work(work);
  1265. struct mlx4_en_priv *priv = container_of(delay, struct mlx4_en_priv,
  1266. service_task);
  1267. struct mlx4_en_dev *mdev = priv->mdev;
  1268. mutex_lock(&mdev->state_lock);
  1269. if (mdev->device_up) {
  1270. if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_TS)
  1271. mlx4_en_ptp_overflow_check(mdev);
  1272. mlx4_en_recover_from_oom(priv);
  1273. queue_delayed_work(mdev->workqueue, &priv->service_task,
  1274. SERVICE_TASK_DELAY);
  1275. }
  1276. mutex_unlock(&mdev->state_lock);
  1277. }
  1278. static void mlx4_en_linkstate(struct work_struct *work)
  1279. {
  1280. struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
  1281. linkstate_task);
  1282. struct mlx4_en_dev *mdev = priv->mdev;
  1283. int linkstate = priv->link_state;
  1284. mutex_lock(&mdev->state_lock);
  1285. /* If observable port state changed set carrier state and
  1286. * report to system log */
  1287. if (priv->last_link_state != linkstate) {
  1288. if (linkstate == MLX4_DEV_EVENT_PORT_DOWN) {
  1289. en_info(priv, "Link Down\n");
  1290. netif_carrier_off(priv->dev);
  1291. } else {
  1292. en_info(priv, "Link Up\n");
  1293. netif_carrier_on(priv->dev);
  1294. }
  1295. }
  1296. priv->last_link_state = linkstate;
  1297. mutex_unlock(&mdev->state_lock);
  1298. }
  1299. static int mlx4_en_init_affinity_hint(struct mlx4_en_priv *priv, int ring_idx)
  1300. {
  1301. struct mlx4_en_rx_ring *ring = priv->rx_ring[ring_idx];
  1302. int numa_node = priv->mdev->dev->numa_node;
  1303. if (!zalloc_cpumask_var(&ring->affinity_mask, GFP_KERNEL))
  1304. return -ENOMEM;
  1305. cpumask_set_cpu(cpumask_local_spread(ring_idx, numa_node),
  1306. ring->affinity_mask);
  1307. return 0;
  1308. }
  1309. static void mlx4_en_free_affinity_hint(struct mlx4_en_priv *priv, int ring_idx)
  1310. {
  1311. free_cpumask_var(priv->rx_ring[ring_idx]->affinity_mask);
  1312. }
  1313. static void mlx4_en_init_recycle_ring(struct mlx4_en_priv *priv,
  1314. int tx_ring_idx)
  1315. {
  1316. struct mlx4_en_tx_ring *tx_ring = priv->tx_ring[tx_ring_idx];
  1317. int rr_index;
  1318. rr_index = (priv->xdp_ring_num - priv->tx_ring_num) + tx_ring_idx;
  1319. if (rr_index >= 0) {
  1320. tx_ring->free_tx_desc = mlx4_en_recycle_tx_desc;
  1321. tx_ring->recycle_ring = priv->rx_ring[rr_index];
  1322. en_dbg(DRV, priv,
  1323. "Set tx_ring[%d]->recycle_ring = rx_ring[%d]\n",
  1324. tx_ring_idx, rr_index);
  1325. } else {
  1326. tx_ring->recycle_ring = NULL;
  1327. }
  1328. }
  1329. int mlx4_en_start_port(struct net_device *dev)
  1330. {
  1331. struct mlx4_en_priv *priv = netdev_priv(dev);
  1332. struct mlx4_en_dev *mdev = priv->mdev;
  1333. struct mlx4_en_cq *cq;
  1334. struct mlx4_en_tx_ring *tx_ring;
  1335. int rx_index = 0;
  1336. int tx_index = 0;
  1337. int err = 0;
  1338. int i;
  1339. int j;
  1340. u8 mc_list[16] = {0};
  1341. if (priv->port_up) {
  1342. en_dbg(DRV, priv, "start port called while port already up\n");
  1343. return 0;
  1344. }
  1345. INIT_LIST_HEAD(&priv->mc_list);
  1346. INIT_LIST_HEAD(&priv->curr_list);
  1347. INIT_LIST_HEAD(&priv->ethtool_list);
  1348. memset(&priv->ethtool_rules[0], 0,
  1349. sizeof(struct ethtool_flow_id) * MAX_NUM_OF_FS_RULES);
  1350. /* Calculate Rx buf size */
  1351. dev->mtu = min(dev->mtu, priv->max_mtu);
  1352. mlx4_en_calc_rx_buf(dev);
  1353. en_dbg(DRV, priv, "Rx buf size:%d\n", priv->rx_skb_size);
  1354. /* Configure rx cq's and rings */
  1355. err = mlx4_en_activate_rx_rings(priv);
  1356. if (err) {
  1357. en_err(priv, "Failed to activate RX rings\n");
  1358. return err;
  1359. }
  1360. for (i = 0; i < priv->rx_ring_num; i++) {
  1361. cq = priv->rx_cq[i];
  1362. err = mlx4_en_init_affinity_hint(priv, i);
  1363. if (err) {
  1364. en_err(priv, "Failed preparing IRQ affinity hint\n");
  1365. goto cq_err;
  1366. }
  1367. err = mlx4_en_activate_cq(priv, cq, i);
  1368. if (err) {
  1369. en_err(priv, "Failed activating Rx CQ\n");
  1370. mlx4_en_free_affinity_hint(priv, i);
  1371. goto cq_err;
  1372. }
  1373. for (j = 0; j < cq->size; j++) {
  1374. struct mlx4_cqe *cqe = NULL;
  1375. cqe = mlx4_en_get_cqe(cq->buf, j, priv->cqe_size) +
  1376. priv->cqe_factor;
  1377. cqe->owner_sr_opcode = MLX4_CQE_OWNER_MASK;
  1378. }
  1379. err = mlx4_en_set_cq_moder(priv, cq);
  1380. if (err) {
  1381. en_err(priv, "Failed setting cq moderation parameters\n");
  1382. mlx4_en_deactivate_cq(priv, cq);
  1383. mlx4_en_free_affinity_hint(priv, i);
  1384. goto cq_err;
  1385. }
  1386. mlx4_en_arm_cq(priv, cq);
  1387. priv->rx_ring[i]->cqn = cq->mcq.cqn;
  1388. ++rx_index;
  1389. }
  1390. /* Set qp number */
  1391. en_dbg(DRV, priv, "Getting qp number for port %d\n", priv->port);
  1392. err = mlx4_en_get_qp(priv);
  1393. if (err) {
  1394. en_err(priv, "Failed getting eth qp\n");
  1395. goto cq_err;
  1396. }
  1397. mdev->mac_removed[priv->port] = 0;
  1398. priv->counter_index =
  1399. mlx4_get_default_counter_index(mdev->dev, priv->port);
  1400. err = mlx4_en_config_rss_steer(priv);
  1401. if (err) {
  1402. en_err(priv, "Failed configuring rss steering\n");
  1403. goto mac_err;
  1404. }
  1405. err = mlx4_en_create_drop_qp(priv);
  1406. if (err)
  1407. goto rss_err;
  1408. /* Configure tx cq's and rings */
  1409. for (i = 0; i < priv->tx_ring_num; i++) {
  1410. /* Configure cq */
  1411. cq = priv->tx_cq[i];
  1412. err = mlx4_en_activate_cq(priv, cq, i);
  1413. if (err) {
  1414. en_err(priv, "Failed allocating Tx CQ\n");
  1415. goto tx_err;
  1416. }
  1417. err = mlx4_en_set_cq_moder(priv, cq);
  1418. if (err) {
  1419. en_err(priv, "Failed setting cq moderation parameters\n");
  1420. mlx4_en_deactivate_cq(priv, cq);
  1421. goto tx_err;
  1422. }
  1423. en_dbg(DRV, priv, "Resetting index of collapsed CQ:%d to -1\n", i);
  1424. cq->buf->wqe_index = cpu_to_be16(0xffff);
  1425. /* Configure ring */
  1426. tx_ring = priv->tx_ring[i];
  1427. err = mlx4_en_activate_tx_ring(priv, tx_ring, cq->mcq.cqn,
  1428. i / priv->num_tx_rings_p_up);
  1429. if (err) {
  1430. en_err(priv, "Failed allocating Tx ring\n");
  1431. mlx4_en_deactivate_cq(priv, cq);
  1432. goto tx_err;
  1433. }
  1434. tx_ring->tx_queue = netdev_get_tx_queue(dev, i);
  1435. mlx4_en_init_recycle_ring(priv, i);
  1436. /* Arm CQ for TX completions */
  1437. mlx4_en_arm_cq(priv, cq);
  1438. /* Set initial ownership of all Tx TXBBs to SW (1) */
  1439. for (j = 0; j < tx_ring->buf_size; j += STAMP_STRIDE)
  1440. *((u32 *) (tx_ring->buf + j)) = 0xffffffff;
  1441. ++tx_index;
  1442. }
  1443. /* Configure port */
  1444. err = mlx4_SET_PORT_general(mdev->dev, priv->port,
  1445. priv->rx_skb_size + ETH_FCS_LEN,
  1446. priv->prof->tx_pause,
  1447. priv->prof->tx_ppp,
  1448. priv->prof->rx_pause,
  1449. priv->prof->rx_ppp);
  1450. if (err) {
  1451. en_err(priv, "Failed setting port general configurations for port %d, with error %d\n",
  1452. priv->port, err);
  1453. goto tx_err;
  1454. }
  1455. /* Set default qp number */
  1456. err = mlx4_SET_PORT_qpn_calc(mdev->dev, priv->port, priv->base_qpn, 0);
  1457. if (err) {
  1458. en_err(priv, "Failed setting default qp numbers\n");
  1459. goto tx_err;
  1460. }
  1461. if (mdev->dev->caps.tunnel_offload_mode == MLX4_TUNNEL_OFFLOAD_MODE_VXLAN) {
  1462. err = mlx4_SET_PORT_VXLAN(mdev->dev, priv->port, VXLAN_STEER_BY_OUTER_MAC, 1);
  1463. if (err) {
  1464. en_err(priv, "Failed setting port L2 tunnel configuration, err %d\n",
  1465. err);
  1466. goto tx_err;
  1467. }
  1468. }
  1469. /* Init port */
  1470. en_dbg(HW, priv, "Initializing port\n");
  1471. err = mlx4_INIT_PORT(mdev->dev, priv->port);
  1472. if (err) {
  1473. en_err(priv, "Failed Initializing port\n");
  1474. goto tx_err;
  1475. }
  1476. /* Set Unicast and VXLAN steering rules */
  1477. if (mdev->dev->caps.steering_mode != MLX4_STEERING_MODE_A0 &&
  1478. mlx4_en_set_rss_steer_rules(priv))
  1479. mlx4_warn(mdev, "Failed setting steering rules\n");
  1480. /* Attach rx QP to bradcast address */
  1481. eth_broadcast_addr(&mc_list[10]);
  1482. mc_list[5] = priv->port; /* needed for B0 steering support */
  1483. if (mlx4_multicast_attach(mdev->dev, &priv->rss_map.indir_qp, mc_list,
  1484. priv->port, 0, MLX4_PROT_ETH,
  1485. &priv->broadcast_id))
  1486. mlx4_warn(mdev, "Failed Attaching Broadcast\n");
  1487. /* Must redo promiscuous mode setup. */
  1488. priv->flags &= ~(MLX4_EN_FLAG_PROMISC | MLX4_EN_FLAG_MC_PROMISC);
  1489. /* Schedule multicast task to populate multicast list */
  1490. queue_work(mdev->workqueue, &priv->rx_mode_task);
  1491. if (priv->mdev->dev->caps.tunnel_offload_mode == MLX4_TUNNEL_OFFLOAD_MODE_VXLAN)
  1492. udp_tunnel_get_rx_info(dev);
  1493. priv->port_up = true;
  1494. /* Process all completions if exist to prevent
  1495. * the queues freezing if they are full
  1496. */
  1497. for (i = 0; i < priv->rx_ring_num; i++) {
  1498. local_bh_disable();
  1499. napi_schedule(&priv->rx_cq[i]->napi);
  1500. local_bh_enable();
  1501. }
  1502. netif_tx_start_all_queues(dev);
  1503. netif_device_attach(dev);
  1504. return 0;
  1505. tx_err:
  1506. while (tx_index--) {
  1507. mlx4_en_deactivate_tx_ring(priv, priv->tx_ring[tx_index]);
  1508. mlx4_en_deactivate_cq(priv, priv->tx_cq[tx_index]);
  1509. }
  1510. mlx4_en_destroy_drop_qp(priv);
  1511. rss_err:
  1512. mlx4_en_release_rss_steer(priv);
  1513. mac_err:
  1514. mlx4_en_put_qp(priv);
  1515. cq_err:
  1516. while (rx_index--) {
  1517. mlx4_en_deactivate_cq(priv, priv->rx_cq[rx_index]);
  1518. mlx4_en_free_affinity_hint(priv, rx_index);
  1519. }
  1520. for (i = 0; i < priv->rx_ring_num; i++)
  1521. mlx4_en_deactivate_rx_ring(priv, priv->rx_ring[i]);
  1522. return err; /* need to close devices */
  1523. }
  1524. void mlx4_en_stop_port(struct net_device *dev, int detach)
  1525. {
  1526. struct mlx4_en_priv *priv = netdev_priv(dev);
  1527. struct mlx4_en_dev *mdev = priv->mdev;
  1528. struct mlx4_en_mc_list *mclist, *tmp;
  1529. struct ethtool_flow_id *flow, *tmp_flow;
  1530. int i;
  1531. u8 mc_list[16] = {0};
  1532. if (!priv->port_up) {
  1533. en_dbg(DRV, priv, "stop port called while port already down\n");
  1534. return;
  1535. }
  1536. /* close port*/
  1537. mlx4_CLOSE_PORT(mdev->dev, priv->port);
  1538. /* Synchronize with tx routine */
  1539. netif_tx_lock_bh(dev);
  1540. if (detach)
  1541. netif_device_detach(dev);
  1542. netif_tx_stop_all_queues(dev);
  1543. netif_tx_unlock_bh(dev);
  1544. netif_tx_disable(dev);
  1545. /* Set port as not active */
  1546. priv->port_up = false;
  1547. priv->counter_index = MLX4_SINK_COUNTER_INDEX(mdev->dev);
  1548. /* Promsicuous mode */
  1549. if (mdev->dev->caps.steering_mode ==
  1550. MLX4_STEERING_MODE_DEVICE_MANAGED) {
  1551. priv->flags &= ~(MLX4_EN_FLAG_PROMISC |
  1552. MLX4_EN_FLAG_MC_PROMISC);
  1553. mlx4_flow_steer_promisc_remove(mdev->dev,
  1554. priv->port,
  1555. MLX4_FS_ALL_DEFAULT);
  1556. mlx4_flow_steer_promisc_remove(mdev->dev,
  1557. priv->port,
  1558. MLX4_FS_MC_DEFAULT);
  1559. } else if (priv->flags & MLX4_EN_FLAG_PROMISC) {
  1560. priv->flags &= ~MLX4_EN_FLAG_PROMISC;
  1561. /* Disable promiscouos mode */
  1562. mlx4_unicast_promisc_remove(mdev->dev, priv->base_qpn,
  1563. priv->port);
  1564. /* Disable Multicast promisc */
  1565. if (priv->flags & MLX4_EN_FLAG_MC_PROMISC) {
  1566. mlx4_multicast_promisc_remove(mdev->dev, priv->base_qpn,
  1567. priv->port);
  1568. priv->flags &= ~MLX4_EN_FLAG_MC_PROMISC;
  1569. }
  1570. }
  1571. /* Detach All multicasts */
  1572. eth_broadcast_addr(&mc_list[10]);
  1573. mc_list[5] = priv->port; /* needed for B0 steering support */
  1574. mlx4_multicast_detach(mdev->dev, &priv->rss_map.indir_qp, mc_list,
  1575. MLX4_PROT_ETH, priv->broadcast_id);
  1576. list_for_each_entry(mclist, &priv->curr_list, list) {
  1577. memcpy(&mc_list[10], mclist->addr, ETH_ALEN);
  1578. mc_list[5] = priv->port;
  1579. mlx4_multicast_detach(mdev->dev, &priv->rss_map.indir_qp,
  1580. mc_list, MLX4_PROT_ETH, mclist->reg_id);
  1581. if (mclist->tunnel_reg_id)
  1582. mlx4_flow_detach(mdev->dev, mclist->tunnel_reg_id);
  1583. }
  1584. mlx4_en_clear_list(dev);
  1585. list_for_each_entry_safe(mclist, tmp, &priv->curr_list, list) {
  1586. list_del(&mclist->list);
  1587. kfree(mclist);
  1588. }
  1589. /* Flush multicast filter */
  1590. mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0, 1, MLX4_MCAST_CONFIG);
  1591. /* Remove flow steering rules for the port*/
  1592. if (mdev->dev->caps.steering_mode ==
  1593. MLX4_STEERING_MODE_DEVICE_MANAGED) {
  1594. ASSERT_RTNL();
  1595. list_for_each_entry_safe(flow, tmp_flow,
  1596. &priv->ethtool_list, list) {
  1597. mlx4_flow_detach(mdev->dev, flow->id);
  1598. list_del(&flow->list);
  1599. }
  1600. }
  1601. mlx4_en_destroy_drop_qp(priv);
  1602. /* Free TX Rings */
  1603. for (i = 0; i < priv->tx_ring_num; i++) {
  1604. mlx4_en_deactivate_tx_ring(priv, priv->tx_ring[i]);
  1605. mlx4_en_deactivate_cq(priv, priv->tx_cq[i]);
  1606. }
  1607. msleep(10);
  1608. for (i = 0; i < priv->tx_ring_num; i++)
  1609. mlx4_en_free_tx_buf(dev, priv->tx_ring[i]);
  1610. if (mdev->dev->caps.steering_mode != MLX4_STEERING_MODE_A0)
  1611. mlx4_en_delete_rss_steer_rules(priv);
  1612. /* Free RSS qps */
  1613. mlx4_en_release_rss_steer(priv);
  1614. /* Unregister Mac address for the port */
  1615. mlx4_en_put_qp(priv);
  1616. if (!(mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_REASSIGN_MAC_EN))
  1617. mdev->mac_removed[priv->port] = 1;
  1618. /* Free RX Rings */
  1619. for (i = 0; i < priv->rx_ring_num; i++) {
  1620. struct mlx4_en_cq *cq = priv->rx_cq[i];
  1621. napi_synchronize(&cq->napi);
  1622. mlx4_en_deactivate_rx_ring(priv, priv->rx_ring[i]);
  1623. mlx4_en_deactivate_cq(priv, cq);
  1624. mlx4_en_free_affinity_hint(priv, i);
  1625. }
  1626. }
  1627. static void mlx4_en_restart(struct work_struct *work)
  1628. {
  1629. struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
  1630. watchdog_task);
  1631. struct mlx4_en_dev *mdev = priv->mdev;
  1632. struct net_device *dev = priv->dev;
  1633. en_dbg(DRV, priv, "Watchdog task called for port %d\n", priv->port);
  1634. rtnl_lock();
  1635. mutex_lock(&mdev->state_lock);
  1636. if (priv->port_up) {
  1637. mlx4_en_stop_port(dev, 1);
  1638. if (mlx4_en_start_port(dev))
  1639. en_err(priv, "Failed restarting port %d\n", priv->port);
  1640. }
  1641. mutex_unlock(&mdev->state_lock);
  1642. rtnl_unlock();
  1643. }
  1644. static void mlx4_en_clear_stats(struct net_device *dev)
  1645. {
  1646. struct mlx4_en_priv *priv = netdev_priv(dev);
  1647. struct mlx4_en_dev *mdev = priv->mdev;
  1648. int i;
  1649. if (!mlx4_is_slave(mdev->dev))
  1650. if (mlx4_en_DUMP_ETH_STATS(mdev, priv->port, 1))
  1651. en_dbg(HW, priv, "Failed dumping statistics\n");
  1652. memset(&priv->pstats, 0, sizeof(priv->pstats));
  1653. memset(&priv->pkstats, 0, sizeof(priv->pkstats));
  1654. memset(&priv->port_stats, 0, sizeof(priv->port_stats));
  1655. memset(&priv->rx_flowstats, 0, sizeof(priv->rx_flowstats));
  1656. memset(&priv->tx_flowstats, 0, sizeof(priv->tx_flowstats));
  1657. memset(&priv->rx_priority_flowstats, 0,
  1658. sizeof(priv->rx_priority_flowstats));
  1659. memset(&priv->tx_priority_flowstats, 0,
  1660. sizeof(priv->tx_priority_flowstats));
  1661. memset(&priv->pf_stats, 0, sizeof(priv->pf_stats));
  1662. for (i = 0; i < priv->tx_ring_num; i++) {
  1663. priv->tx_ring[i]->bytes = 0;
  1664. priv->tx_ring[i]->packets = 0;
  1665. priv->tx_ring[i]->tx_csum = 0;
  1666. priv->tx_ring[i]->tx_dropped = 0;
  1667. priv->tx_ring[i]->queue_stopped = 0;
  1668. priv->tx_ring[i]->wake_queue = 0;
  1669. priv->tx_ring[i]->tso_packets = 0;
  1670. priv->tx_ring[i]->xmit_more = 0;
  1671. }
  1672. for (i = 0; i < priv->rx_ring_num; i++) {
  1673. priv->rx_ring[i]->bytes = 0;
  1674. priv->rx_ring[i]->packets = 0;
  1675. priv->rx_ring[i]->csum_ok = 0;
  1676. priv->rx_ring[i]->csum_none = 0;
  1677. priv->rx_ring[i]->csum_complete = 0;
  1678. }
  1679. }
  1680. static int mlx4_en_open(struct net_device *dev)
  1681. {
  1682. struct mlx4_en_priv *priv = netdev_priv(dev);
  1683. struct mlx4_en_dev *mdev = priv->mdev;
  1684. int err = 0;
  1685. mutex_lock(&mdev->state_lock);
  1686. if (!mdev->device_up) {
  1687. en_err(priv, "Cannot open - device down/disabled\n");
  1688. err = -EBUSY;
  1689. goto out;
  1690. }
  1691. /* Reset HW statistics and SW counters */
  1692. mlx4_en_clear_stats(dev);
  1693. err = mlx4_en_start_port(dev);
  1694. if (err)
  1695. en_err(priv, "Failed starting port:%d\n", priv->port);
  1696. out:
  1697. mutex_unlock(&mdev->state_lock);
  1698. return err;
  1699. }
  1700. static int mlx4_en_close(struct net_device *dev)
  1701. {
  1702. struct mlx4_en_priv *priv = netdev_priv(dev);
  1703. struct mlx4_en_dev *mdev = priv->mdev;
  1704. en_dbg(IFDOWN, priv, "Close port called\n");
  1705. mutex_lock(&mdev->state_lock);
  1706. mlx4_en_stop_port(dev, 0);
  1707. netif_carrier_off(dev);
  1708. mutex_unlock(&mdev->state_lock);
  1709. return 0;
  1710. }
  1711. static void mlx4_en_free_resources(struct mlx4_en_priv *priv)
  1712. {
  1713. int i;
  1714. #ifdef CONFIG_RFS_ACCEL
  1715. priv->dev->rx_cpu_rmap = NULL;
  1716. #endif
  1717. for (i = 0; i < priv->tx_ring_num; i++) {
  1718. if (priv->tx_ring && priv->tx_ring[i])
  1719. mlx4_en_destroy_tx_ring(priv, &priv->tx_ring[i]);
  1720. if (priv->tx_cq && priv->tx_cq[i])
  1721. mlx4_en_destroy_cq(priv, &priv->tx_cq[i]);
  1722. }
  1723. for (i = 0; i < priv->rx_ring_num; i++) {
  1724. if (priv->rx_ring[i])
  1725. mlx4_en_destroy_rx_ring(priv, &priv->rx_ring[i],
  1726. priv->prof->rx_ring_size, priv->stride);
  1727. if (priv->rx_cq[i])
  1728. mlx4_en_destroy_cq(priv, &priv->rx_cq[i]);
  1729. }
  1730. }
  1731. static int mlx4_en_alloc_resources(struct mlx4_en_priv *priv)
  1732. {
  1733. struct mlx4_en_port_profile *prof = priv->prof;
  1734. int i;
  1735. int node;
  1736. /* Create tx Rings */
  1737. for (i = 0; i < priv->tx_ring_num; i++) {
  1738. node = cpu_to_node(i % num_online_cpus());
  1739. if (mlx4_en_create_cq(priv, &priv->tx_cq[i],
  1740. prof->tx_ring_size, i, TX, node))
  1741. goto err;
  1742. if (mlx4_en_create_tx_ring(priv, &priv->tx_ring[i],
  1743. prof->tx_ring_size, TXBB_SIZE,
  1744. node, i))
  1745. goto err;
  1746. }
  1747. /* Create rx Rings */
  1748. for (i = 0; i < priv->rx_ring_num; i++) {
  1749. node = cpu_to_node(i % num_online_cpus());
  1750. if (mlx4_en_create_cq(priv, &priv->rx_cq[i],
  1751. prof->rx_ring_size, i, RX, node))
  1752. goto err;
  1753. if (mlx4_en_create_rx_ring(priv, &priv->rx_ring[i],
  1754. prof->rx_ring_size, priv->stride,
  1755. node))
  1756. goto err;
  1757. }
  1758. #ifdef CONFIG_RFS_ACCEL
  1759. priv->dev->rx_cpu_rmap = mlx4_get_cpu_rmap(priv->mdev->dev, priv->port);
  1760. #endif
  1761. return 0;
  1762. err:
  1763. en_err(priv, "Failed to allocate NIC resources\n");
  1764. for (i = 0; i < priv->rx_ring_num; i++) {
  1765. if (priv->rx_ring[i])
  1766. mlx4_en_destroy_rx_ring(priv, &priv->rx_ring[i],
  1767. prof->rx_ring_size,
  1768. priv->stride);
  1769. if (priv->rx_cq[i])
  1770. mlx4_en_destroy_cq(priv, &priv->rx_cq[i]);
  1771. }
  1772. for (i = 0; i < priv->tx_ring_num; i++) {
  1773. if (priv->tx_ring[i])
  1774. mlx4_en_destroy_tx_ring(priv, &priv->tx_ring[i]);
  1775. if (priv->tx_cq[i])
  1776. mlx4_en_destroy_cq(priv, &priv->tx_cq[i]);
  1777. }
  1778. return -ENOMEM;
  1779. }
  1780. static int mlx4_en_copy_priv(struct mlx4_en_priv *dst,
  1781. struct mlx4_en_priv *src,
  1782. struct mlx4_en_port_profile *prof)
  1783. {
  1784. memcpy(&dst->hwtstamp_config, &prof->hwtstamp_config,
  1785. sizeof(dst->hwtstamp_config));
  1786. dst->num_tx_rings_p_up = src->mdev->profile.num_tx_rings_p_up;
  1787. dst->tx_ring_num = prof->tx_ring_num;
  1788. dst->rx_ring_num = prof->rx_ring_num;
  1789. dst->flags = prof->flags;
  1790. dst->mdev = src->mdev;
  1791. dst->port = src->port;
  1792. dst->dev = src->dev;
  1793. dst->prof = prof;
  1794. dst->stride = roundup_pow_of_two(sizeof(struct mlx4_en_rx_desc) +
  1795. DS_SIZE * MLX4_EN_MAX_RX_FRAGS);
  1796. dst->tx_ring = kzalloc(sizeof(struct mlx4_en_tx_ring *) * MAX_TX_RINGS,
  1797. GFP_KERNEL);
  1798. if (!dst->tx_ring)
  1799. return -ENOMEM;
  1800. dst->tx_cq = kzalloc(sizeof(struct mlx4_en_cq *) * MAX_TX_RINGS,
  1801. GFP_KERNEL);
  1802. if (!dst->tx_cq) {
  1803. kfree(dst->tx_ring);
  1804. return -ENOMEM;
  1805. }
  1806. return 0;
  1807. }
  1808. static void mlx4_en_update_priv(struct mlx4_en_priv *dst,
  1809. struct mlx4_en_priv *src)
  1810. {
  1811. memcpy(dst->rx_ring, src->rx_ring,
  1812. sizeof(struct mlx4_en_rx_ring *) * src->rx_ring_num);
  1813. memcpy(dst->rx_cq, src->rx_cq,
  1814. sizeof(struct mlx4_en_cq *) * src->rx_ring_num);
  1815. memcpy(&dst->hwtstamp_config, &src->hwtstamp_config,
  1816. sizeof(dst->hwtstamp_config));
  1817. dst->tx_ring_num = src->tx_ring_num;
  1818. dst->rx_ring_num = src->rx_ring_num;
  1819. dst->tx_ring = src->tx_ring;
  1820. dst->tx_cq = src->tx_cq;
  1821. memcpy(dst->prof, src->prof, sizeof(struct mlx4_en_port_profile));
  1822. }
  1823. int mlx4_en_try_alloc_resources(struct mlx4_en_priv *priv,
  1824. struct mlx4_en_priv *tmp,
  1825. struct mlx4_en_port_profile *prof)
  1826. {
  1827. mlx4_en_copy_priv(tmp, priv, prof);
  1828. if (mlx4_en_alloc_resources(tmp)) {
  1829. en_warn(priv,
  1830. "%s: Resource allocation failed, using previous configuration\n",
  1831. __func__);
  1832. kfree(tmp->tx_ring);
  1833. kfree(tmp->tx_cq);
  1834. return -ENOMEM;
  1835. }
  1836. return 0;
  1837. }
  1838. void mlx4_en_safe_replace_resources(struct mlx4_en_priv *priv,
  1839. struct mlx4_en_priv *tmp)
  1840. {
  1841. mlx4_en_free_resources(priv);
  1842. mlx4_en_update_priv(priv, tmp);
  1843. }
  1844. void mlx4_en_destroy_netdev(struct net_device *dev)
  1845. {
  1846. struct mlx4_en_priv *priv = netdev_priv(dev);
  1847. struct mlx4_en_dev *mdev = priv->mdev;
  1848. en_dbg(DRV, priv, "Destroying netdev on port:%d\n", priv->port);
  1849. /* Unregister device - this will close the port if it was up */
  1850. if (priv->registered) {
  1851. devlink_port_type_clear(mlx4_get_devlink_port(mdev->dev,
  1852. priv->port));
  1853. unregister_netdev(dev);
  1854. }
  1855. if (priv->allocated)
  1856. mlx4_free_hwq_res(mdev->dev, &priv->res, MLX4_EN_PAGE_SIZE);
  1857. cancel_delayed_work(&priv->stats_task);
  1858. cancel_delayed_work(&priv->service_task);
  1859. /* flush any pending task for this netdev */
  1860. flush_workqueue(mdev->workqueue);
  1861. if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_TS)
  1862. mlx4_en_remove_timestamp(mdev);
  1863. /* Detach the netdev so tasks would not attempt to access it */
  1864. mutex_lock(&mdev->state_lock);
  1865. mdev->pndev[priv->port] = NULL;
  1866. mdev->upper[priv->port] = NULL;
  1867. #ifdef CONFIG_RFS_ACCEL
  1868. mlx4_en_cleanup_filters(priv);
  1869. #endif
  1870. mlx4_en_free_resources(priv);
  1871. mutex_unlock(&mdev->state_lock);
  1872. kfree(priv->tx_ring);
  1873. kfree(priv->tx_cq);
  1874. free_netdev(dev);
  1875. }
  1876. static int mlx4_en_change_mtu(struct net_device *dev, int new_mtu)
  1877. {
  1878. struct mlx4_en_priv *priv = netdev_priv(dev);
  1879. struct mlx4_en_dev *mdev = priv->mdev;
  1880. int err = 0;
  1881. en_dbg(DRV, priv, "Change MTU called - current:%d new:%d\n",
  1882. dev->mtu, new_mtu);
  1883. if ((new_mtu < MLX4_EN_MIN_MTU) || (new_mtu > priv->max_mtu)) {
  1884. en_err(priv, "Bad MTU size:%d.\n", new_mtu);
  1885. return -EPERM;
  1886. }
  1887. if (priv->xdp_ring_num && MLX4_EN_EFF_MTU(new_mtu) > FRAG_SZ0) {
  1888. en_err(priv, "MTU size:%d requires frags but XDP running\n",
  1889. new_mtu);
  1890. return -EOPNOTSUPP;
  1891. }
  1892. dev->mtu = new_mtu;
  1893. if (netif_running(dev)) {
  1894. mutex_lock(&mdev->state_lock);
  1895. if (!mdev->device_up) {
  1896. /* NIC is probably restarting - let watchdog task reset
  1897. * the port */
  1898. en_dbg(DRV, priv, "Change MTU called with card down!?\n");
  1899. } else {
  1900. mlx4_en_stop_port(dev, 1);
  1901. err = mlx4_en_start_port(dev);
  1902. if (err) {
  1903. en_err(priv, "Failed restarting port:%d\n",
  1904. priv->port);
  1905. queue_work(mdev->workqueue, &priv->watchdog_task);
  1906. }
  1907. }
  1908. mutex_unlock(&mdev->state_lock);
  1909. }
  1910. return 0;
  1911. }
  1912. static int mlx4_en_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
  1913. {
  1914. struct mlx4_en_priv *priv = netdev_priv(dev);
  1915. struct mlx4_en_dev *mdev = priv->mdev;
  1916. struct hwtstamp_config config;
  1917. if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
  1918. return -EFAULT;
  1919. /* reserved for future extensions */
  1920. if (config.flags)
  1921. return -EINVAL;
  1922. /* device doesn't support time stamping */
  1923. if (!(mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_TS))
  1924. return -EINVAL;
  1925. /* TX HW timestamp */
  1926. switch (config.tx_type) {
  1927. case HWTSTAMP_TX_OFF:
  1928. case HWTSTAMP_TX_ON:
  1929. break;
  1930. default:
  1931. return -ERANGE;
  1932. }
  1933. /* RX HW timestamp */
  1934. switch (config.rx_filter) {
  1935. case HWTSTAMP_FILTER_NONE:
  1936. break;
  1937. case HWTSTAMP_FILTER_ALL:
  1938. case HWTSTAMP_FILTER_SOME:
  1939. case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
  1940. case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
  1941. case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
  1942. case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
  1943. case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
  1944. case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
  1945. case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
  1946. case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
  1947. case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
  1948. case HWTSTAMP_FILTER_PTP_V2_EVENT:
  1949. case HWTSTAMP_FILTER_PTP_V2_SYNC:
  1950. case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
  1951. config.rx_filter = HWTSTAMP_FILTER_ALL;
  1952. break;
  1953. default:
  1954. return -ERANGE;
  1955. }
  1956. if (mlx4_en_reset_config(dev, config, dev->features)) {
  1957. config.tx_type = HWTSTAMP_TX_OFF;
  1958. config.rx_filter = HWTSTAMP_FILTER_NONE;
  1959. }
  1960. return copy_to_user(ifr->ifr_data, &config,
  1961. sizeof(config)) ? -EFAULT : 0;
  1962. }
  1963. static int mlx4_en_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
  1964. {
  1965. struct mlx4_en_priv *priv = netdev_priv(dev);
  1966. return copy_to_user(ifr->ifr_data, &priv->hwtstamp_config,
  1967. sizeof(priv->hwtstamp_config)) ? -EFAULT : 0;
  1968. }
  1969. static int mlx4_en_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  1970. {
  1971. switch (cmd) {
  1972. case SIOCSHWTSTAMP:
  1973. return mlx4_en_hwtstamp_set(dev, ifr);
  1974. case SIOCGHWTSTAMP:
  1975. return mlx4_en_hwtstamp_get(dev, ifr);
  1976. default:
  1977. return -EOPNOTSUPP;
  1978. }
  1979. }
  1980. static netdev_features_t mlx4_en_fix_features(struct net_device *netdev,
  1981. netdev_features_t features)
  1982. {
  1983. struct mlx4_en_priv *en_priv = netdev_priv(netdev);
  1984. struct mlx4_en_dev *mdev = en_priv->mdev;
  1985. /* Since there is no support for separate RX C-TAG/S-TAG vlan accel
  1986. * enable/disable make sure S-TAG flag is always in same state as
  1987. * C-TAG.
  1988. */
  1989. if (features & NETIF_F_HW_VLAN_CTAG_RX &&
  1990. !(mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_SKIP_OUTER_VLAN))
  1991. features |= NETIF_F_HW_VLAN_STAG_RX;
  1992. else
  1993. features &= ~NETIF_F_HW_VLAN_STAG_RX;
  1994. return features;
  1995. }
  1996. static int mlx4_en_set_features(struct net_device *netdev,
  1997. netdev_features_t features)
  1998. {
  1999. struct mlx4_en_priv *priv = netdev_priv(netdev);
  2000. bool reset = false;
  2001. int ret = 0;
  2002. if (DEV_FEATURE_CHANGED(netdev, features, NETIF_F_RXFCS)) {
  2003. en_info(priv, "Turn %s RX-FCS\n",
  2004. (features & NETIF_F_RXFCS) ? "ON" : "OFF");
  2005. reset = true;
  2006. }
  2007. if (DEV_FEATURE_CHANGED(netdev, features, NETIF_F_RXALL)) {
  2008. u8 ignore_fcs_value = (features & NETIF_F_RXALL) ? 1 : 0;
  2009. en_info(priv, "Turn %s RX-ALL\n",
  2010. ignore_fcs_value ? "ON" : "OFF");
  2011. ret = mlx4_SET_PORT_fcs_check(priv->mdev->dev,
  2012. priv->port, ignore_fcs_value);
  2013. if (ret)
  2014. return ret;
  2015. }
  2016. if (DEV_FEATURE_CHANGED(netdev, features, NETIF_F_HW_VLAN_CTAG_RX)) {
  2017. en_info(priv, "Turn %s RX vlan strip offload\n",
  2018. (features & NETIF_F_HW_VLAN_CTAG_RX) ? "ON" : "OFF");
  2019. reset = true;
  2020. }
  2021. if (DEV_FEATURE_CHANGED(netdev, features, NETIF_F_HW_VLAN_CTAG_TX))
  2022. en_info(priv, "Turn %s TX vlan strip offload\n",
  2023. (features & NETIF_F_HW_VLAN_CTAG_TX) ? "ON" : "OFF");
  2024. if (DEV_FEATURE_CHANGED(netdev, features, NETIF_F_HW_VLAN_STAG_TX))
  2025. en_info(priv, "Turn %s TX S-VLAN strip offload\n",
  2026. (features & NETIF_F_HW_VLAN_STAG_TX) ? "ON" : "OFF");
  2027. if (DEV_FEATURE_CHANGED(netdev, features, NETIF_F_LOOPBACK)) {
  2028. en_info(priv, "Turn %s loopback\n",
  2029. (features & NETIF_F_LOOPBACK) ? "ON" : "OFF");
  2030. mlx4_en_update_loopback_state(netdev, features);
  2031. }
  2032. if (reset) {
  2033. ret = mlx4_en_reset_config(netdev, priv->hwtstamp_config,
  2034. features);
  2035. if (ret)
  2036. return ret;
  2037. }
  2038. return 0;
  2039. }
  2040. static int mlx4_en_set_vf_mac(struct net_device *dev, int queue, u8 *mac)
  2041. {
  2042. struct mlx4_en_priv *en_priv = netdev_priv(dev);
  2043. struct mlx4_en_dev *mdev = en_priv->mdev;
  2044. u64 mac_u64 = mlx4_mac_to_u64(mac);
  2045. if (is_multicast_ether_addr(mac))
  2046. return -EINVAL;
  2047. return mlx4_set_vf_mac(mdev->dev, en_priv->port, queue, mac_u64);
  2048. }
  2049. static int mlx4_en_set_vf_vlan(struct net_device *dev, int vf, u16 vlan, u8 qos,
  2050. __be16 vlan_proto)
  2051. {
  2052. struct mlx4_en_priv *en_priv = netdev_priv(dev);
  2053. struct mlx4_en_dev *mdev = en_priv->mdev;
  2054. return mlx4_set_vf_vlan(mdev->dev, en_priv->port, vf, vlan, qos,
  2055. vlan_proto);
  2056. }
  2057. static int mlx4_en_set_vf_rate(struct net_device *dev, int vf, int min_tx_rate,
  2058. int max_tx_rate)
  2059. {
  2060. struct mlx4_en_priv *en_priv = netdev_priv(dev);
  2061. struct mlx4_en_dev *mdev = en_priv->mdev;
  2062. return mlx4_set_vf_rate(mdev->dev, en_priv->port, vf, min_tx_rate,
  2063. max_tx_rate);
  2064. }
  2065. static int mlx4_en_set_vf_spoofchk(struct net_device *dev, int vf, bool setting)
  2066. {
  2067. struct mlx4_en_priv *en_priv = netdev_priv(dev);
  2068. struct mlx4_en_dev *mdev = en_priv->mdev;
  2069. return mlx4_set_vf_spoofchk(mdev->dev, en_priv->port, vf, setting);
  2070. }
  2071. static int mlx4_en_get_vf_config(struct net_device *dev, int vf, struct ifla_vf_info *ivf)
  2072. {
  2073. struct mlx4_en_priv *en_priv = netdev_priv(dev);
  2074. struct mlx4_en_dev *mdev = en_priv->mdev;
  2075. return mlx4_get_vf_config(mdev->dev, en_priv->port, vf, ivf);
  2076. }
  2077. static int mlx4_en_set_vf_link_state(struct net_device *dev, int vf, int link_state)
  2078. {
  2079. struct mlx4_en_priv *en_priv = netdev_priv(dev);
  2080. struct mlx4_en_dev *mdev = en_priv->mdev;
  2081. return mlx4_set_vf_link_state(mdev->dev, en_priv->port, vf, link_state);
  2082. }
  2083. static int mlx4_en_get_vf_stats(struct net_device *dev, int vf,
  2084. struct ifla_vf_stats *vf_stats)
  2085. {
  2086. struct mlx4_en_priv *en_priv = netdev_priv(dev);
  2087. struct mlx4_en_dev *mdev = en_priv->mdev;
  2088. return mlx4_get_vf_stats(mdev->dev, en_priv->port, vf, vf_stats);
  2089. }
  2090. #define PORT_ID_BYTE_LEN 8
  2091. static int mlx4_en_get_phys_port_id(struct net_device *dev,
  2092. struct netdev_phys_item_id *ppid)
  2093. {
  2094. struct mlx4_en_priv *priv = netdev_priv(dev);
  2095. struct mlx4_dev *mdev = priv->mdev->dev;
  2096. int i;
  2097. u64 phys_port_id = mdev->caps.phys_port_id[priv->port];
  2098. if (!phys_port_id)
  2099. return -EOPNOTSUPP;
  2100. ppid->id_len = sizeof(phys_port_id);
  2101. for (i = PORT_ID_BYTE_LEN - 1; i >= 0; --i) {
  2102. ppid->id[i] = phys_port_id & 0xff;
  2103. phys_port_id >>= 8;
  2104. }
  2105. return 0;
  2106. }
  2107. static void mlx4_en_add_vxlan_offloads(struct work_struct *work)
  2108. {
  2109. int ret;
  2110. struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
  2111. vxlan_add_task);
  2112. ret = mlx4_config_vxlan_port(priv->mdev->dev, priv->vxlan_port);
  2113. if (ret)
  2114. goto out;
  2115. ret = mlx4_SET_PORT_VXLAN(priv->mdev->dev, priv->port,
  2116. VXLAN_STEER_BY_OUTER_MAC, 1);
  2117. out:
  2118. if (ret) {
  2119. en_err(priv, "failed setting L2 tunnel configuration ret %d\n", ret);
  2120. return;
  2121. }
  2122. /* set offloads */
  2123. priv->dev->hw_enc_features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
  2124. NETIF_F_RXCSUM |
  2125. NETIF_F_TSO | NETIF_F_TSO6 |
  2126. NETIF_F_GSO_UDP_TUNNEL |
  2127. NETIF_F_GSO_UDP_TUNNEL_CSUM |
  2128. NETIF_F_GSO_PARTIAL;
  2129. }
  2130. static void mlx4_en_del_vxlan_offloads(struct work_struct *work)
  2131. {
  2132. int ret;
  2133. struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
  2134. vxlan_del_task);
  2135. /* unset offloads */
  2136. priv->dev->hw_enc_features &= ~(NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
  2137. NETIF_F_RXCSUM |
  2138. NETIF_F_TSO | NETIF_F_TSO6 |
  2139. NETIF_F_GSO_UDP_TUNNEL |
  2140. NETIF_F_GSO_UDP_TUNNEL_CSUM |
  2141. NETIF_F_GSO_PARTIAL);
  2142. ret = mlx4_SET_PORT_VXLAN(priv->mdev->dev, priv->port,
  2143. VXLAN_STEER_BY_OUTER_MAC, 0);
  2144. if (ret)
  2145. en_err(priv, "failed setting L2 tunnel configuration ret %d\n", ret);
  2146. priv->vxlan_port = 0;
  2147. }
  2148. static void mlx4_en_add_vxlan_port(struct net_device *dev,
  2149. struct udp_tunnel_info *ti)
  2150. {
  2151. struct mlx4_en_priv *priv = netdev_priv(dev);
  2152. __be16 port = ti->port;
  2153. __be16 current_port;
  2154. if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
  2155. return;
  2156. if (ti->sa_family != AF_INET)
  2157. return;
  2158. if (priv->mdev->dev->caps.tunnel_offload_mode != MLX4_TUNNEL_OFFLOAD_MODE_VXLAN)
  2159. return;
  2160. current_port = priv->vxlan_port;
  2161. if (current_port && current_port != port) {
  2162. en_warn(priv, "vxlan port %d configured, can't add port %d\n",
  2163. ntohs(current_port), ntohs(port));
  2164. return;
  2165. }
  2166. priv->vxlan_port = port;
  2167. queue_work(priv->mdev->workqueue, &priv->vxlan_add_task);
  2168. }
  2169. static void mlx4_en_del_vxlan_port(struct net_device *dev,
  2170. struct udp_tunnel_info *ti)
  2171. {
  2172. struct mlx4_en_priv *priv = netdev_priv(dev);
  2173. __be16 port = ti->port;
  2174. __be16 current_port;
  2175. if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
  2176. return;
  2177. if (ti->sa_family != AF_INET)
  2178. return;
  2179. if (priv->mdev->dev->caps.tunnel_offload_mode != MLX4_TUNNEL_OFFLOAD_MODE_VXLAN)
  2180. return;
  2181. current_port = priv->vxlan_port;
  2182. if (current_port != port) {
  2183. en_dbg(DRV, priv, "vxlan port %d isn't configured, ignoring\n", ntohs(port));
  2184. return;
  2185. }
  2186. queue_work(priv->mdev->workqueue, &priv->vxlan_del_task);
  2187. }
  2188. static netdev_features_t mlx4_en_features_check(struct sk_buff *skb,
  2189. struct net_device *dev,
  2190. netdev_features_t features)
  2191. {
  2192. features = vlan_features_check(skb, features);
  2193. features = vxlan_features_check(skb, features);
  2194. /* The ConnectX-3 doesn't support outer IPv6 checksums but it does
  2195. * support inner IPv6 checksums and segmentation so we need to
  2196. * strip that feature if this is an IPv6 encapsulated frame.
  2197. */
  2198. if (skb->encapsulation &&
  2199. (skb->ip_summed == CHECKSUM_PARTIAL)) {
  2200. struct mlx4_en_priv *priv = netdev_priv(dev);
  2201. if (!priv->vxlan_port ||
  2202. (ip_hdr(skb)->version != 4) ||
  2203. (udp_hdr(skb)->dest != priv->vxlan_port))
  2204. features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
  2205. }
  2206. return features;
  2207. }
  2208. static int mlx4_en_set_tx_maxrate(struct net_device *dev, int queue_index, u32 maxrate)
  2209. {
  2210. struct mlx4_en_priv *priv = netdev_priv(dev);
  2211. struct mlx4_en_tx_ring *tx_ring = priv->tx_ring[queue_index];
  2212. struct mlx4_update_qp_params params;
  2213. int err;
  2214. if (!(priv->mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_QP_RATE_LIMIT))
  2215. return -EOPNOTSUPP;
  2216. /* rate provided to us in Mbs, check if it fits into 12 bits, if not use Gbs */
  2217. if (maxrate >> 12) {
  2218. params.rate_unit = MLX4_QP_RATE_LIMIT_GBS;
  2219. params.rate_val = maxrate / 1000;
  2220. } else if (maxrate) {
  2221. params.rate_unit = MLX4_QP_RATE_LIMIT_MBS;
  2222. params.rate_val = maxrate;
  2223. } else { /* zero serves to revoke the QP rate-limitation */
  2224. params.rate_unit = 0;
  2225. params.rate_val = 0;
  2226. }
  2227. err = mlx4_update_qp(priv->mdev->dev, tx_ring->qpn, MLX4_UPDATE_QP_RATE_LIMIT,
  2228. &params);
  2229. return err;
  2230. }
  2231. static int mlx4_xdp_set(struct net_device *dev, struct bpf_prog *prog)
  2232. {
  2233. struct mlx4_en_priv *priv = netdev_priv(dev);
  2234. struct mlx4_en_dev *mdev = priv->mdev;
  2235. struct bpf_prog *old_prog;
  2236. int xdp_ring_num;
  2237. int port_up = 0;
  2238. int err;
  2239. int i;
  2240. xdp_ring_num = prog ? ALIGN(priv->rx_ring_num, MLX4_EN_NUM_UP) : 0;
  2241. /* No need to reconfigure buffers when simply swapping the
  2242. * program for a new one.
  2243. */
  2244. if (priv->xdp_ring_num == xdp_ring_num) {
  2245. if (prog) {
  2246. prog = bpf_prog_add(prog, priv->rx_ring_num - 1);
  2247. if (IS_ERR(prog))
  2248. return PTR_ERR(prog);
  2249. }
  2250. mutex_lock(&mdev->state_lock);
  2251. for (i = 0; i < priv->rx_ring_num; i++) {
  2252. old_prog = rcu_dereference_protected(
  2253. priv->rx_ring[i]->xdp_prog,
  2254. lockdep_is_held(&mdev->state_lock));
  2255. rcu_assign_pointer(priv->rx_ring[i]->xdp_prog, prog);
  2256. if (old_prog)
  2257. bpf_prog_put(old_prog);
  2258. }
  2259. mutex_unlock(&mdev->state_lock);
  2260. return 0;
  2261. }
  2262. if (priv->num_frags > 1) {
  2263. en_err(priv, "Cannot set XDP if MTU requires multiple frags\n");
  2264. return -EOPNOTSUPP;
  2265. }
  2266. if (priv->tx_ring_num < xdp_ring_num + MLX4_EN_NUM_UP) {
  2267. en_err(priv,
  2268. "Minimum %d tx channels required to run XDP\n",
  2269. (xdp_ring_num + MLX4_EN_NUM_UP) / MLX4_EN_NUM_UP);
  2270. return -EINVAL;
  2271. }
  2272. if (prog) {
  2273. prog = bpf_prog_add(prog, priv->rx_ring_num - 1);
  2274. if (IS_ERR(prog))
  2275. return PTR_ERR(prog);
  2276. }
  2277. mutex_lock(&mdev->state_lock);
  2278. if (priv->port_up) {
  2279. port_up = 1;
  2280. mlx4_en_stop_port(dev, 1);
  2281. }
  2282. priv->xdp_ring_num = xdp_ring_num;
  2283. netif_set_real_num_tx_queues(dev, priv->tx_ring_num -
  2284. priv->xdp_ring_num);
  2285. for (i = 0; i < priv->rx_ring_num; i++) {
  2286. old_prog = rcu_dereference_protected(
  2287. priv->rx_ring[i]->xdp_prog,
  2288. lockdep_is_held(&mdev->state_lock));
  2289. rcu_assign_pointer(priv->rx_ring[i]->xdp_prog, prog);
  2290. if (old_prog)
  2291. bpf_prog_put(old_prog);
  2292. }
  2293. if (port_up) {
  2294. err = mlx4_en_start_port(dev);
  2295. if (err) {
  2296. en_err(priv, "Failed starting port %d for XDP change\n",
  2297. priv->port);
  2298. queue_work(mdev->workqueue, &priv->watchdog_task);
  2299. }
  2300. }
  2301. mutex_unlock(&mdev->state_lock);
  2302. return 0;
  2303. }
  2304. static bool mlx4_xdp_attached(struct net_device *dev)
  2305. {
  2306. struct mlx4_en_priv *priv = netdev_priv(dev);
  2307. return !!priv->xdp_ring_num;
  2308. }
  2309. static int mlx4_xdp(struct net_device *dev, struct netdev_xdp *xdp)
  2310. {
  2311. switch (xdp->command) {
  2312. case XDP_SETUP_PROG:
  2313. return mlx4_xdp_set(dev, xdp->prog);
  2314. case XDP_QUERY_PROG:
  2315. xdp->prog_attached = mlx4_xdp_attached(dev);
  2316. return 0;
  2317. default:
  2318. return -EINVAL;
  2319. }
  2320. }
  2321. static const struct net_device_ops mlx4_netdev_ops = {
  2322. .ndo_open = mlx4_en_open,
  2323. .ndo_stop = mlx4_en_close,
  2324. .ndo_start_xmit = mlx4_en_xmit,
  2325. .ndo_select_queue = mlx4_en_select_queue,
  2326. .ndo_get_stats64 = mlx4_en_get_stats64,
  2327. .ndo_set_rx_mode = mlx4_en_set_rx_mode,
  2328. .ndo_set_mac_address = mlx4_en_set_mac,
  2329. .ndo_validate_addr = eth_validate_addr,
  2330. .ndo_change_mtu = mlx4_en_change_mtu,
  2331. .ndo_do_ioctl = mlx4_en_ioctl,
  2332. .ndo_tx_timeout = mlx4_en_tx_timeout,
  2333. .ndo_vlan_rx_add_vid = mlx4_en_vlan_rx_add_vid,
  2334. .ndo_vlan_rx_kill_vid = mlx4_en_vlan_rx_kill_vid,
  2335. #ifdef CONFIG_NET_POLL_CONTROLLER
  2336. .ndo_poll_controller = mlx4_en_netpoll,
  2337. #endif
  2338. .ndo_set_features = mlx4_en_set_features,
  2339. .ndo_fix_features = mlx4_en_fix_features,
  2340. .ndo_setup_tc = __mlx4_en_setup_tc,
  2341. #ifdef CONFIG_RFS_ACCEL
  2342. .ndo_rx_flow_steer = mlx4_en_filter_rfs,
  2343. #endif
  2344. .ndo_get_phys_port_id = mlx4_en_get_phys_port_id,
  2345. .ndo_udp_tunnel_add = mlx4_en_add_vxlan_port,
  2346. .ndo_udp_tunnel_del = mlx4_en_del_vxlan_port,
  2347. .ndo_features_check = mlx4_en_features_check,
  2348. .ndo_set_tx_maxrate = mlx4_en_set_tx_maxrate,
  2349. .ndo_xdp = mlx4_xdp,
  2350. };
  2351. static const struct net_device_ops mlx4_netdev_ops_master = {
  2352. .ndo_open = mlx4_en_open,
  2353. .ndo_stop = mlx4_en_close,
  2354. .ndo_start_xmit = mlx4_en_xmit,
  2355. .ndo_select_queue = mlx4_en_select_queue,
  2356. .ndo_get_stats64 = mlx4_en_get_stats64,
  2357. .ndo_set_rx_mode = mlx4_en_set_rx_mode,
  2358. .ndo_set_mac_address = mlx4_en_set_mac,
  2359. .ndo_validate_addr = eth_validate_addr,
  2360. .ndo_change_mtu = mlx4_en_change_mtu,
  2361. .ndo_tx_timeout = mlx4_en_tx_timeout,
  2362. .ndo_vlan_rx_add_vid = mlx4_en_vlan_rx_add_vid,
  2363. .ndo_vlan_rx_kill_vid = mlx4_en_vlan_rx_kill_vid,
  2364. .ndo_set_vf_mac = mlx4_en_set_vf_mac,
  2365. .ndo_set_vf_vlan = mlx4_en_set_vf_vlan,
  2366. .ndo_set_vf_rate = mlx4_en_set_vf_rate,
  2367. .ndo_set_vf_spoofchk = mlx4_en_set_vf_spoofchk,
  2368. .ndo_set_vf_link_state = mlx4_en_set_vf_link_state,
  2369. .ndo_get_vf_stats = mlx4_en_get_vf_stats,
  2370. .ndo_get_vf_config = mlx4_en_get_vf_config,
  2371. #ifdef CONFIG_NET_POLL_CONTROLLER
  2372. .ndo_poll_controller = mlx4_en_netpoll,
  2373. #endif
  2374. .ndo_set_features = mlx4_en_set_features,
  2375. .ndo_fix_features = mlx4_en_fix_features,
  2376. .ndo_setup_tc = __mlx4_en_setup_tc,
  2377. #ifdef CONFIG_RFS_ACCEL
  2378. .ndo_rx_flow_steer = mlx4_en_filter_rfs,
  2379. #endif
  2380. .ndo_get_phys_port_id = mlx4_en_get_phys_port_id,
  2381. .ndo_udp_tunnel_add = mlx4_en_add_vxlan_port,
  2382. .ndo_udp_tunnel_del = mlx4_en_del_vxlan_port,
  2383. .ndo_features_check = mlx4_en_features_check,
  2384. .ndo_set_tx_maxrate = mlx4_en_set_tx_maxrate,
  2385. .ndo_xdp = mlx4_xdp,
  2386. };
  2387. struct mlx4_en_bond {
  2388. struct work_struct work;
  2389. struct mlx4_en_priv *priv;
  2390. int is_bonded;
  2391. struct mlx4_port_map port_map;
  2392. };
  2393. static void mlx4_en_bond_work(struct work_struct *work)
  2394. {
  2395. struct mlx4_en_bond *bond = container_of(work,
  2396. struct mlx4_en_bond,
  2397. work);
  2398. int err = 0;
  2399. struct mlx4_dev *dev = bond->priv->mdev->dev;
  2400. if (bond->is_bonded) {
  2401. if (!mlx4_is_bonded(dev)) {
  2402. err = mlx4_bond(dev);
  2403. if (err)
  2404. en_err(bond->priv, "Fail to bond device\n");
  2405. }
  2406. if (!err) {
  2407. err = mlx4_port_map_set(dev, &bond->port_map);
  2408. if (err)
  2409. en_err(bond->priv, "Fail to set port map [%d][%d]: %d\n",
  2410. bond->port_map.port1,
  2411. bond->port_map.port2,
  2412. err);
  2413. }
  2414. } else if (mlx4_is_bonded(dev)) {
  2415. err = mlx4_unbond(dev);
  2416. if (err)
  2417. en_err(bond->priv, "Fail to unbond device\n");
  2418. }
  2419. dev_put(bond->priv->dev);
  2420. kfree(bond);
  2421. }
  2422. static int mlx4_en_queue_bond_work(struct mlx4_en_priv *priv, int is_bonded,
  2423. u8 v2p_p1, u8 v2p_p2)
  2424. {
  2425. struct mlx4_en_bond *bond = NULL;
  2426. bond = kzalloc(sizeof(*bond), GFP_ATOMIC);
  2427. if (!bond)
  2428. return -ENOMEM;
  2429. INIT_WORK(&bond->work, mlx4_en_bond_work);
  2430. bond->priv = priv;
  2431. bond->is_bonded = is_bonded;
  2432. bond->port_map.port1 = v2p_p1;
  2433. bond->port_map.port2 = v2p_p2;
  2434. dev_hold(priv->dev);
  2435. queue_work(priv->mdev->workqueue, &bond->work);
  2436. return 0;
  2437. }
  2438. int mlx4_en_netdev_event(struct notifier_block *this,
  2439. unsigned long event, void *ptr)
  2440. {
  2441. struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
  2442. u8 port = 0;
  2443. struct mlx4_en_dev *mdev;
  2444. struct mlx4_dev *dev;
  2445. int i, num_eth_ports = 0;
  2446. bool do_bond = true;
  2447. struct mlx4_en_priv *priv;
  2448. u8 v2p_port1 = 0;
  2449. u8 v2p_port2 = 0;
  2450. if (!net_eq(dev_net(ndev), &init_net))
  2451. return NOTIFY_DONE;
  2452. mdev = container_of(this, struct mlx4_en_dev, nb);
  2453. dev = mdev->dev;
  2454. /* Go into this mode only when two network devices set on two ports
  2455. * of the same mlx4 device are slaves of the same bonding master
  2456. */
  2457. mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_ETH) {
  2458. ++num_eth_ports;
  2459. if (!port && (mdev->pndev[i] == ndev))
  2460. port = i;
  2461. mdev->upper[i] = mdev->pndev[i] ?
  2462. netdev_master_upper_dev_get(mdev->pndev[i]) : NULL;
  2463. /* condition not met: network device is a slave */
  2464. if (!mdev->upper[i])
  2465. do_bond = false;
  2466. if (num_eth_ports < 2)
  2467. continue;
  2468. /* condition not met: same master */
  2469. if (mdev->upper[i] != mdev->upper[i-1])
  2470. do_bond = false;
  2471. }
  2472. /* condition not met: 2 salves */
  2473. do_bond = (num_eth_ports == 2) ? do_bond : false;
  2474. /* handle only events that come with enough info */
  2475. if ((do_bond && (event != NETDEV_BONDING_INFO)) || !port)
  2476. return NOTIFY_DONE;
  2477. priv = netdev_priv(ndev);
  2478. if (do_bond) {
  2479. struct netdev_notifier_bonding_info *notifier_info = ptr;
  2480. struct netdev_bonding_info *bonding_info =
  2481. &notifier_info->bonding_info;
  2482. /* required mode 1, 2 or 4 */
  2483. if ((bonding_info->master.bond_mode != BOND_MODE_ACTIVEBACKUP) &&
  2484. (bonding_info->master.bond_mode != BOND_MODE_XOR) &&
  2485. (bonding_info->master.bond_mode != BOND_MODE_8023AD))
  2486. do_bond = false;
  2487. /* require exactly 2 slaves */
  2488. if (bonding_info->master.num_slaves != 2)
  2489. do_bond = false;
  2490. /* calc v2p */
  2491. if (do_bond) {
  2492. if (bonding_info->master.bond_mode ==
  2493. BOND_MODE_ACTIVEBACKUP) {
  2494. /* in active-backup mode virtual ports are
  2495. * mapped to the physical port of the active
  2496. * slave */
  2497. if (bonding_info->slave.state ==
  2498. BOND_STATE_BACKUP) {
  2499. if (port == 1) {
  2500. v2p_port1 = 2;
  2501. v2p_port2 = 2;
  2502. } else {
  2503. v2p_port1 = 1;
  2504. v2p_port2 = 1;
  2505. }
  2506. } else { /* BOND_STATE_ACTIVE */
  2507. if (port == 1) {
  2508. v2p_port1 = 1;
  2509. v2p_port2 = 1;
  2510. } else {
  2511. v2p_port1 = 2;
  2512. v2p_port2 = 2;
  2513. }
  2514. }
  2515. } else { /* Active-Active */
  2516. /* in active-active mode a virtual port is
  2517. * mapped to the native physical port if and only
  2518. * if the physical port is up */
  2519. __s8 link = bonding_info->slave.link;
  2520. if (port == 1)
  2521. v2p_port2 = 2;
  2522. else
  2523. v2p_port1 = 1;
  2524. if ((link == BOND_LINK_UP) ||
  2525. (link == BOND_LINK_FAIL)) {
  2526. if (port == 1)
  2527. v2p_port1 = 1;
  2528. else
  2529. v2p_port2 = 2;
  2530. } else { /* BOND_LINK_DOWN || BOND_LINK_BACK */
  2531. if (port == 1)
  2532. v2p_port1 = 2;
  2533. else
  2534. v2p_port2 = 1;
  2535. }
  2536. }
  2537. }
  2538. }
  2539. mlx4_en_queue_bond_work(priv, do_bond,
  2540. v2p_port1, v2p_port2);
  2541. return NOTIFY_DONE;
  2542. }
  2543. void mlx4_en_update_pfc_stats_bitmap(struct mlx4_dev *dev,
  2544. struct mlx4_en_stats_bitmap *stats_bitmap,
  2545. u8 rx_ppp, u8 rx_pause,
  2546. u8 tx_ppp, u8 tx_pause)
  2547. {
  2548. int last_i = NUM_MAIN_STATS + NUM_PORT_STATS + NUM_PF_STATS;
  2549. if (!mlx4_is_slave(dev) &&
  2550. (dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_FLOWSTATS_EN)) {
  2551. mutex_lock(&stats_bitmap->mutex);
  2552. bitmap_clear(stats_bitmap->bitmap, last_i, NUM_FLOW_STATS);
  2553. if (rx_ppp)
  2554. bitmap_set(stats_bitmap->bitmap, last_i,
  2555. NUM_FLOW_PRIORITY_STATS_RX);
  2556. last_i += NUM_FLOW_PRIORITY_STATS_RX;
  2557. if (rx_pause && !(rx_ppp))
  2558. bitmap_set(stats_bitmap->bitmap, last_i,
  2559. NUM_FLOW_STATS_RX);
  2560. last_i += NUM_FLOW_STATS_RX;
  2561. if (tx_ppp)
  2562. bitmap_set(stats_bitmap->bitmap, last_i,
  2563. NUM_FLOW_PRIORITY_STATS_TX);
  2564. last_i += NUM_FLOW_PRIORITY_STATS_TX;
  2565. if (tx_pause && !(tx_ppp))
  2566. bitmap_set(stats_bitmap->bitmap, last_i,
  2567. NUM_FLOW_STATS_TX);
  2568. last_i += NUM_FLOW_STATS_TX;
  2569. mutex_unlock(&stats_bitmap->mutex);
  2570. }
  2571. }
  2572. void mlx4_en_set_stats_bitmap(struct mlx4_dev *dev,
  2573. struct mlx4_en_stats_bitmap *stats_bitmap,
  2574. u8 rx_ppp, u8 rx_pause,
  2575. u8 tx_ppp, u8 tx_pause)
  2576. {
  2577. int last_i = 0;
  2578. mutex_init(&stats_bitmap->mutex);
  2579. bitmap_zero(stats_bitmap->bitmap, NUM_ALL_STATS);
  2580. if (mlx4_is_slave(dev)) {
  2581. bitmap_set(stats_bitmap->bitmap, last_i +
  2582. MLX4_FIND_NETDEV_STAT(rx_packets), 1);
  2583. bitmap_set(stats_bitmap->bitmap, last_i +
  2584. MLX4_FIND_NETDEV_STAT(tx_packets), 1);
  2585. bitmap_set(stats_bitmap->bitmap, last_i +
  2586. MLX4_FIND_NETDEV_STAT(rx_bytes), 1);
  2587. bitmap_set(stats_bitmap->bitmap, last_i +
  2588. MLX4_FIND_NETDEV_STAT(tx_bytes), 1);
  2589. bitmap_set(stats_bitmap->bitmap, last_i +
  2590. MLX4_FIND_NETDEV_STAT(rx_dropped), 1);
  2591. bitmap_set(stats_bitmap->bitmap, last_i +
  2592. MLX4_FIND_NETDEV_STAT(tx_dropped), 1);
  2593. } else {
  2594. bitmap_set(stats_bitmap->bitmap, last_i, NUM_MAIN_STATS);
  2595. }
  2596. last_i += NUM_MAIN_STATS;
  2597. bitmap_set(stats_bitmap->bitmap, last_i, NUM_PORT_STATS);
  2598. last_i += NUM_PORT_STATS;
  2599. if (mlx4_is_master(dev))
  2600. bitmap_set(stats_bitmap->bitmap, last_i,
  2601. NUM_PF_STATS);
  2602. last_i += NUM_PF_STATS;
  2603. mlx4_en_update_pfc_stats_bitmap(dev, stats_bitmap,
  2604. rx_ppp, rx_pause,
  2605. tx_ppp, tx_pause);
  2606. last_i += NUM_FLOW_STATS;
  2607. if (!mlx4_is_slave(dev))
  2608. bitmap_set(stats_bitmap->bitmap, last_i, NUM_PKT_STATS);
  2609. }
  2610. int mlx4_en_init_netdev(struct mlx4_en_dev *mdev, int port,
  2611. struct mlx4_en_port_profile *prof)
  2612. {
  2613. struct net_device *dev;
  2614. struct mlx4_en_priv *priv;
  2615. int i;
  2616. int err;
  2617. dev = alloc_etherdev_mqs(sizeof(struct mlx4_en_priv),
  2618. MAX_TX_RINGS, MAX_RX_RINGS);
  2619. if (dev == NULL)
  2620. return -ENOMEM;
  2621. netif_set_real_num_tx_queues(dev, prof->tx_ring_num);
  2622. netif_set_real_num_rx_queues(dev, prof->rx_ring_num);
  2623. SET_NETDEV_DEV(dev, &mdev->dev->persist->pdev->dev);
  2624. dev->dev_port = port - 1;
  2625. /*
  2626. * Initialize driver private data
  2627. */
  2628. priv = netdev_priv(dev);
  2629. memset(priv, 0, sizeof(struct mlx4_en_priv));
  2630. priv->counter_index = MLX4_SINK_COUNTER_INDEX(mdev->dev);
  2631. spin_lock_init(&priv->stats_lock);
  2632. INIT_WORK(&priv->rx_mode_task, mlx4_en_do_set_rx_mode);
  2633. INIT_WORK(&priv->watchdog_task, mlx4_en_restart);
  2634. INIT_WORK(&priv->linkstate_task, mlx4_en_linkstate);
  2635. INIT_DELAYED_WORK(&priv->stats_task, mlx4_en_do_get_stats);
  2636. INIT_DELAYED_WORK(&priv->service_task, mlx4_en_service_task);
  2637. INIT_WORK(&priv->vxlan_add_task, mlx4_en_add_vxlan_offloads);
  2638. INIT_WORK(&priv->vxlan_del_task, mlx4_en_del_vxlan_offloads);
  2639. #ifdef CONFIG_RFS_ACCEL
  2640. INIT_LIST_HEAD(&priv->filters);
  2641. spin_lock_init(&priv->filters_lock);
  2642. #endif
  2643. priv->dev = dev;
  2644. priv->mdev = mdev;
  2645. priv->ddev = &mdev->pdev->dev;
  2646. priv->prof = prof;
  2647. priv->port = port;
  2648. priv->port_up = false;
  2649. priv->flags = prof->flags;
  2650. priv->pflags = MLX4_EN_PRIV_FLAGS_BLUEFLAME;
  2651. priv->ctrl_flags = cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE |
  2652. MLX4_WQE_CTRL_SOLICITED);
  2653. priv->num_tx_rings_p_up = mdev->profile.num_tx_rings_p_up;
  2654. priv->tx_ring_num = prof->tx_ring_num;
  2655. priv->tx_work_limit = MLX4_EN_DEFAULT_TX_WORK;
  2656. netdev_rss_key_fill(priv->rss_key, sizeof(priv->rss_key));
  2657. priv->tx_ring = kzalloc(sizeof(struct mlx4_en_tx_ring *) * MAX_TX_RINGS,
  2658. GFP_KERNEL);
  2659. if (!priv->tx_ring) {
  2660. err = -ENOMEM;
  2661. goto out;
  2662. }
  2663. priv->tx_cq = kzalloc(sizeof(struct mlx4_en_cq *) * MAX_TX_RINGS,
  2664. GFP_KERNEL);
  2665. if (!priv->tx_cq) {
  2666. err = -ENOMEM;
  2667. goto out;
  2668. }
  2669. priv->rx_ring_num = prof->rx_ring_num;
  2670. priv->cqe_factor = (mdev->dev->caps.cqe_size == 64) ? 1 : 0;
  2671. priv->cqe_size = mdev->dev->caps.cqe_size;
  2672. priv->mac_index = -1;
  2673. priv->msg_enable = MLX4_EN_MSG_LEVEL;
  2674. #ifdef CONFIG_MLX4_EN_DCB
  2675. if (!mlx4_is_slave(priv->mdev->dev)) {
  2676. u8 prio;
  2677. for (prio = 0; prio < IEEE_8021QAZ_MAX_TCS; ++prio) {
  2678. priv->ets.prio_tc[prio] = prio;
  2679. priv->ets.tc_tsa[prio] = IEEE_8021QAZ_TSA_VENDOR;
  2680. }
  2681. priv->dcbx_cap = DCB_CAP_DCBX_VER_CEE | DCB_CAP_DCBX_HOST |
  2682. DCB_CAP_DCBX_VER_IEEE;
  2683. priv->flags |= MLX4_EN_DCB_ENABLED;
  2684. priv->cee_config.pfc_state = false;
  2685. for (i = 0; i < MLX4_EN_NUM_UP; i++)
  2686. priv->cee_config.dcb_pfc[i] = pfc_disabled;
  2687. if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_ETS_CFG) {
  2688. dev->dcbnl_ops = &mlx4_en_dcbnl_ops;
  2689. } else {
  2690. en_info(priv, "enabling only PFC DCB ops\n");
  2691. dev->dcbnl_ops = &mlx4_en_dcbnl_pfc_ops;
  2692. }
  2693. }
  2694. #endif
  2695. for (i = 0; i < MLX4_EN_MAC_HASH_SIZE; ++i)
  2696. INIT_HLIST_HEAD(&priv->mac_hash[i]);
  2697. /* Query for default mac and max mtu */
  2698. priv->max_mtu = mdev->dev->caps.eth_mtu_cap[priv->port];
  2699. if (mdev->dev->caps.rx_checksum_flags_port[priv->port] &
  2700. MLX4_RX_CSUM_MODE_VAL_NON_TCP_UDP)
  2701. priv->flags |= MLX4_EN_FLAG_RX_CSUM_NON_TCP_UDP;
  2702. /* Set default MAC */
  2703. dev->addr_len = ETH_ALEN;
  2704. mlx4_en_u64_to_mac(dev->dev_addr, mdev->dev->caps.def_mac[priv->port]);
  2705. if (!is_valid_ether_addr(dev->dev_addr)) {
  2706. en_err(priv, "Port: %d, invalid mac burned: %pM, quiting\n",
  2707. priv->port, dev->dev_addr);
  2708. err = -EINVAL;
  2709. goto out;
  2710. } else if (mlx4_is_slave(priv->mdev->dev) &&
  2711. (priv->mdev->dev->port_random_macs & 1 << priv->port)) {
  2712. /* Random MAC was assigned in mlx4_slave_cap
  2713. * in mlx4_core module
  2714. */
  2715. dev->addr_assign_type |= NET_ADDR_RANDOM;
  2716. en_warn(priv, "Assigned random MAC address %pM\n", dev->dev_addr);
  2717. }
  2718. memcpy(priv->current_mac, dev->dev_addr, sizeof(priv->current_mac));
  2719. priv->stride = roundup_pow_of_two(sizeof(struct mlx4_en_rx_desc) +
  2720. DS_SIZE * MLX4_EN_MAX_RX_FRAGS);
  2721. err = mlx4_en_alloc_resources(priv);
  2722. if (err)
  2723. goto out;
  2724. /* Initialize time stamping config */
  2725. priv->hwtstamp_config.flags = 0;
  2726. priv->hwtstamp_config.tx_type = HWTSTAMP_TX_OFF;
  2727. priv->hwtstamp_config.rx_filter = HWTSTAMP_FILTER_NONE;
  2728. /* Allocate page for receive rings */
  2729. err = mlx4_alloc_hwq_res(mdev->dev, &priv->res,
  2730. MLX4_EN_PAGE_SIZE);
  2731. if (err) {
  2732. en_err(priv, "Failed to allocate page for rx qps\n");
  2733. goto out;
  2734. }
  2735. priv->allocated = 1;
  2736. /*
  2737. * Initialize netdev entry points
  2738. */
  2739. if (mlx4_is_master(priv->mdev->dev))
  2740. dev->netdev_ops = &mlx4_netdev_ops_master;
  2741. else
  2742. dev->netdev_ops = &mlx4_netdev_ops;
  2743. dev->watchdog_timeo = MLX4_EN_WATCHDOG_TIMEOUT;
  2744. netif_set_real_num_tx_queues(dev, priv->tx_ring_num);
  2745. netif_set_real_num_rx_queues(dev, priv->rx_ring_num);
  2746. dev->ethtool_ops = &mlx4_en_ethtool_ops;
  2747. /*
  2748. * Set driver features
  2749. */
  2750. dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
  2751. if (mdev->LSO_support)
  2752. dev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6;
  2753. dev->vlan_features = dev->hw_features;
  2754. dev->hw_features |= NETIF_F_RXCSUM | NETIF_F_RXHASH;
  2755. dev->features = dev->hw_features | NETIF_F_HIGHDMA |
  2756. NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX |
  2757. NETIF_F_HW_VLAN_CTAG_FILTER;
  2758. dev->hw_features |= NETIF_F_LOOPBACK |
  2759. NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
  2760. if (!(mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_SKIP_OUTER_VLAN)) {
  2761. dev->features |= NETIF_F_HW_VLAN_STAG_RX |
  2762. NETIF_F_HW_VLAN_STAG_FILTER;
  2763. dev->hw_features |= NETIF_F_HW_VLAN_STAG_RX;
  2764. }
  2765. if (mlx4_is_slave(mdev->dev)) {
  2766. bool vlan_offload_disabled;
  2767. int phv;
  2768. err = get_phv_bit(mdev->dev, port, &phv);
  2769. if (!err && phv) {
  2770. dev->hw_features |= NETIF_F_HW_VLAN_STAG_TX;
  2771. priv->pflags |= MLX4_EN_PRIV_FLAGS_PHV;
  2772. }
  2773. err = mlx4_get_is_vlan_offload_disabled(mdev->dev, port,
  2774. &vlan_offload_disabled);
  2775. if (!err && vlan_offload_disabled) {
  2776. dev->hw_features &= ~(NETIF_F_HW_VLAN_CTAG_TX |
  2777. NETIF_F_HW_VLAN_CTAG_RX |
  2778. NETIF_F_HW_VLAN_STAG_TX |
  2779. NETIF_F_HW_VLAN_STAG_RX);
  2780. dev->features &= ~(NETIF_F_HW_VLAN_CTAG_TX |
  2781. NETIF_F_HW_VLAN_CTAG_RX |
  2782. NETIF_F_HW_VLAN_STAG_TX |
  2783. NETIF_F_HW_VLAN_STAG_RX);
  2784. }
  2785. } else {
  2786. if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_PHV_EN &&
  2787. !(mdev->dev->caps.flags2 &
  2788. MLX4_DEV_CAP_FLAG2_SKIP_OUTER_VLAN))
  2789. dev->hw_features |= NETIF_F_HW_VLAN_STAG_TX;
  2790. }
  2791. if (mdev->dev->caps.flags & MLX4_DEV_CAP_FLAG_FCS_KEEP)
  2792. dev->hw_features |= NETIF_F_RXFCS;
  2793. if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_IGNORE_FCS)
  2794. dev->hw_features |= NETIF_F_RXALL;
  2795. if (mdev->dev->caps.steering_mode ==
  2796. MLX4_STEERING_MODE_DEVICE_MANAGED &&
  2797. mdev->dev->caps.dmfs_high_steer_mode != MLX4_STEERING_DMFS_A0_STATIC)
  2798. dev->hw_features |= NETIF_F_NTUPLE;
  2799. if (mdev->dev->caps.steering_mode != MLX4_STEERING_MODE_A0)
  2800. dev->priv_flags |= IFF_UNICAST_FLT;
  2801. /* Setting a default hash function value */
  2802. if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_RSS_TOP) {
  2803. priv->rss_hash_fn = ETH_RSS_HASH_TOP;
  2804. } else if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_RSS_XOR) {
  2805. priv->rss_hash_fn = ETH_RSS_HASH_XOR;
  2806. } else {
  2807. en_warn(priv,
  2808. "No RSS hash capabilities exposed, using Toeplitz\n");
  2809. priv->rss_hash_fn = ETH_RSS_HASH_TOP;
  2810. }
  2811. if (mdev->dev->caps.tunnel_offload_mode == MLX4_TUNNEL_OFFLOAD_MODE_VXLAN) {
  2812. dev->hw_features |= NETIF_F_GSO_UDP_TUNNEL |
  2813. NETIF_F_GSO_UDP_TUNNEL_CSUM |
  2814. NETIF_F_GSO_PARTIAL;
  2815. dev->features |= NETIF_F_GSO_UDP_TUNNEL |
  2816. NETIF_F_GSO_UDP_TUNNEL_CSUM |
  2817. NETIF_F_GSO_PARTIAL;
  2818. dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM;
  2819. }
  2820. mdev->pndev[port] = dev;
  2821. mdev->upper[port] = NULL;
  2822. netif_carrier_off(dev);
  2823. mlx4_en_set_default_moderation(priv);
  2824. en_warn(priv, "Using %d TX rings\n", prof->tx_ring_num);
  2825. en_warn(priv, "Using %d RX rings\n", prof->rx_ring_num);
  2826. mlx4_en_update_loopback_state(priv->dev, priv->dev->features);
  2827. /* Configure port */
  2828. mlx4_en_calc_rx_buf(dev);
  2829. err = mlx4_SET_PORT_general(mdev->dev, priv->port,
  2830. priv->rx_skb_size + ETH_FCS_LEN,
  2831. prof->tx_pause, prof->tx_ppp,
  2832. prof->rx_pause, prof->rx_ppp);
  2833. if (err) {
  2834. en_err(priv, "Failed setting port general configurations for port %d, with error %d\n",
  2835. priv->port, err);
  2836. goto out;
  2837. }
  2838. if (mdev->dev->caps.tunnel_offload_mode == MLX4_TUNNEL_OFFLOAD_MODE_VXLAN) {
  2839. err = mlx4_SET_PORT_VXLAN(mdev->dev, priv->port, VXLAN_STEER_BY_OUTER_MAC, 1);
  2840. if (err) {
  2841. en_err(priv, "Failed setting port L2 tunnel configuration, err %d\n",
  2842. err);
  2843. goto out;
  2844. }
  2845. }
  2846. /* Init port */
  2847. en_warn(priv, "Initializing port\n");
  2848. err = mlx4_INIT_PORT(mdev->dev, priv->port);
  2849. if (err) {
  2850. en_err(priv, "Failed Initializing port\n");
  2851. goto out;
  2852. }
  2853. queue_delayed_work(mdev->workqueue, &priv->stats_task, STATS_DELAY);
  2854. /* Initialize time stamp mechanism */
  2855. if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_TS)
  2856. mlx4_en_init_timestamp(mdev);
  2857. queue_delayed_work(mdev->workqueue, &priv->service_task,
  2858. SERVICE_TASK_DELAY);
  2859. mlx4_en_set_stats_bitmap(mdev->dev, &priv->stats_bitmap,
  2860. mdev->profile.prof[priv->port].rx_ppp,
  2861. mdev->profile.prof[priv->port].rx_pause,
  2862. mdev->profile.prof[priv->port].tx_ppp,
  2863. mdev->profile.prof[priv->port].tx_pause);
  2864. err = register_netdev(dev);
  2865. if (err) {
  2866. en_err(priv, "Netdev registration failed for port %d\n", port);
  2867. goto out;
  2868. }
  2869. priv->registered = 1;
  2870. devlink_port_type_eth_set(mlx4_get_devlink_port(mdev->dev, priv->port),
  2871. dev);
  2872. return 0;
  2873. out:
  2874. mlx4_en_destroy_netdev(dev);
  2875. return err;
  2876. }
  2877. int mlx4_en_reset_config(struct net_device *dev,
  2878. struct hwtstamp_config ts_config,
  2879. netdev_features_t features)
  2880. {
  2881. struct mlx4_en_priv *priv = netdev_priv(dev);
  2882. struct mlx4_en_dev *mdev = priv->mdev;
  2883. struct mlx4_en_port_profile new_prof;
  2884. struct mlx4_en_priv *tmp;
  2885. int port_up = 0;
  2886. int err = 0;
  2887. if (priv->hwtstamp_config.tx_type == ts_config.tx_type &&
  2888. priv->hwtstamp_config.rx_filter == ts_config.rx_filter &&
  2889. !DEV_FEATURE_CHANGED(dev, features, NETIF_F_HW_VLAN_CTAG_RX) &&
  2890. !DEV_FEATURE_CHANGED(dev, features, NETIF_F_RXFCS))
  2891. return 0; /* Nothing to change */
  2892. if (DEV_FEATURE_CHANGED(dev, features, NETIF_F_HW_VLAN_CTAG_RX) &&
  2893. (features & NETIF_F_HW_VLAN_CTAG_RX) &&
  2894. (priv->hwtstamp_config.rx_filter != HWTSTAMP_FILTER_NONE)) {
  2895. en_warn(priv, "Can't turn ON rx vlan offload while time-stamping rx filter is ON\n");
  2896. return -EINVAL;
  2897. }
  2898. tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
  2899. if (!tmp)
  2900. return -ENOMEM;
  2901. mutex_lock(&mdev->state_lock);
  2902. memcpy(&new_prof, priv->prof, sizeof(struct mlx4_en_port_profile));
  2903. memcpy(&new_prof.hwtstamp_config, &ts_config, sizeof(ts_config));
  2904. err = mlx4_en_try_alloc_resources(priv, tmp, &new_prof);
  2905. if (err)
  2906. goto out;
  2907. if (priv->port_up) {
  2908. port_up = 1;
  2909. mlx4_en_stop_port(dev, 1);
  2910. }
  2911. en_warn(priv, "Changing device configuration rx filter(%x) rx vlan(%x)\n",
  2912. ts_config.rx_filter,
  2913. !!(features & NETIF_F_HW_VLAN_CTAG_RX));
  2914. mlx4_en_safe_replace_resources(priv, tmp);
  2915. if (DEV_FEATURE_CHANGED(dev, features, NETIF_F_HW_VLAN_CTAG_RX)) {
  2916. if (features & NETIF_F_HW_VLAN_CTAG_RX)
  2917. dev->features |= NETIF_F_HW_VLAN_CTAG_RX;
  2918. else
  2919. dev->features &= ~NETIF_F_HW_VLAN_CTAG_RX;
  2920. } else if (ts_config.rx_filter == HWTSTAMP_FILTER_NONE) {
  2921. /* RX time-stamping is OFF, update the RX vlan offload
  2922. * to the latest wanted state
  2923. */
  2924. if (dev->wanted_features & NETIF_F_HW_VLAN_CTAG_RX)
  2925. dev->features |= NETIF_F_HW_VLAN_CTAG_RX;
  2926. else
  2927. dev->features &= ~NETIF_F_HW_VLAN_CTAG_RX;
  2928. }
  2929. if (DEV_FEATURE_CHANGED(dev, features, NETIF_F_RXFCS)) {
  2930. if (features & NETIF_F_RXFCS)
  2931. dev->features |= NETIF_F_RXFCS;
  2932. else
  2933. dev->features &= ~NETIF_F_RXFCS;
  2934. }
  2935. /* RX vlan offload and RX time-stamping can't co-exist !
  2936. * Regardless of the caller's choice,
  2937. * Turn Off RX vlan offload in case of time-stamping is ON
  2938. */
  2939. if (ts_config.rx_filter != HWTSTAMP_FILTER_NONE) {
  2940. if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
  2941. en_warn(priv, "Turning off RX vlan offload since RX time-stamping is ON\n");
  2942. dev->features &= ~NETIF_F_HW_VLAN_CTAG_RX;
  2943. }
  2944. if (port_up) {
  2945. err = mlx4_en_start_port(dev);
  2946. if (err)
  2947. en_err(priv, "Failed starting port\n");
  2948. }
  2949. out:
  2950. mutex_unlock(&mdev->state_lock);
  2951. kfree(tmp);
  2952. if (!err)
  2953. netdev_features_change(dev);
  2954. return err;
  2955. }