en_netdev.c 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380
  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/etherdevice.h>
  34. #include <linux/tcp.h>
  35. #include <linux/if_vlan.h>
  36. #include <linux/delay.h>
  37. #include <linux/slab.h>
  38. #include <linux/hash.h>
  39. #include <net/ip.h>
  40. #include <net/busy_poll.h>
  41. #include <linux/mlx4/driver.h>
  42. #include <linux/mlx4/device.h>
  43. #include <linux/mlx4/cmd.h>
  44. #include <linux/mlx4/cq.h>
  45. #include "mlx4_en.h"
  46. #include "en_port.h"
  47. int mlx4_en_setup_tc(struct net_device *dev, u8 up)
  48. {
  49. struct mlx4_en_priv *priv = netdev_priv(dev);
  50. int i;
  51. unsigned int offset = 0;
  52. if (up && up != MLX4_EN_NUM_UP)
  53. return -EINVAL;
  54. netdev_set_num_tc(dev, up);
  55. /* Partition Tx queues evenly amongst UP's */
  56. for (i = 0; i < up; i++) {
  57. netdev_set_tc_queue(dev, i, priv->num_tx_rings_p_up, offset);
  58. offset += priv->num_tx_rings_p_up;
  59. }
  60. return 0;
  61. }
  62. #ifdef CONFIG_NET_RX_BUSY_POLL
  63. /* must be called with local_bh_disable()d */
  64. static int mlx4_en_low_latency_recv(struct napi_struct *napi)
  65. {
  66. struct mlx4_en_cq *cq = container_of(napi, struct mlx4_en_cq, napi);
  67. struct net_device *dev = cq->dev;
  68. struct mlx4_en_priv *priv = netdev_priv(dev);
  69. struct mlx4_en_rx_ring *rx_ring = &priv->rx_ring[cq->ring];
  70. int done;
  71. if (!priv->port_up)
  72. return LL_FLUSH_FAILED;
  73. if (!mlx4_en_cq_lock_poll(cq))
  74. return LL_FLUSH_BUSY;
  75. done = mlx4_en_process_rx_cq(dev, cq, 4);
  76. if (likely(done))
  77. rx_ring->cleaned += done;
  78. else
  79. rx_ring->misses++;
  80. mlx4_en_cq_unlock_poll(cq);
  81. return done;
  82. }
  83. #endif /* CONFIG_NET_RX_BUSY_POLL */
  84. #ifdef CONFIG_RFS_ACCEL
  85. struct mlx4_en_filter {
  86. struct list_head next;
  87. struct work_struct work;
  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 void mlx4_en_filter_work(struct work_struct *work)
  104. {
  105. struct mlx4_en_filter *filter = container_of(work,
  106. struct mlx4_en_filter,
  107. work);
  108. struct mlx4_en_priv *priv = filter->priv;
  109. struct mlx4_spec_list spec_tcp = {
  110. .id = MLX4_NET_TRANS_RULE_ID_TCP,
  111. {
  112. .tcp_udp = {
  113. .dst_port = filter->dst_port,
  114. .dst_port_msk = (__force __be16)-1,
  115. .src_port = filter->src_port,
  116. .src_port_msk = (__force __be16)-1,
  117. },
  118. },
  119. };
  120. struct mlx4_spec_list spec_ip = {
  121. .id = MLX4_NET_TRANS_RULE_ID_IPV4,
  122. {
  123. .ipv4 = {
  124. .dst_ip = filter->dst_ip,
  125. .dst_ip_msk = (__force __be32)-1,
  126. .src_ip = filter->src_ip,
  127. .src_ip_msk = (__force __be32)-1,
  128. },
  129. },
  130. };
  131. struct mlx4_spec_list spec_eth = {
  132. .id = MLX4_NET_TRANS_RULE_ID_ETH,
  133. };
  134. struct mlx4_net_trans_rule rule = {
  135. .list = LIST_HEAD_INIT(rule.list),
  136. .queue_mode = MLX4_NET_TRANS_Q_LIFO,
  137. .exclusive = 1,
  138. .allow_loopback = 1,
  139. .promisc_mode = MLX4_FS_REGULAR,
  140. .port = priv->port,
  141. .priority = MLX4_DOMAIN_RFS,
  142. };
  143. int rc;
  144. __be64 mac_mask = cpu_to_be64(MLX4_MAC_MASK << 16);
  145. list_add_tail(&spec_eth.list, &rule.list);
  146. list_add_tail(&spec_ip.list, &rule.list);
  147. list_add_tail(&spec_tcp.list, &rule.list);
  148. rule.qpn = priv->rss_map.qps[filter->rxq_index].qpn;
  149. memcpy(spec_eth.eth.dst_mac, priv->dev->dev_addr, ETH_ALEN);
  150. memcpy(spec_eth.eth.dst_mac_msk, &mac_mask, ETH_ALEN);
  151. filter->activated = 0;
  152. if (filter->reg_id) {
  153. rc = mlx4_flow_detach(priv->mdev->dev, filter->reg_id);
  154. if (rc && rc != -ENOENT)
  155. en_err(priv, "Error detaching flow. rc = %d\n", rc);
  156. }
  157. rc = mlx4_flow_attach(priv->mdev->dev, &rule, &filter->reg_id);
  158. if (rc)
  159. en_err(priv, "Error attaching flow. err = %d\n", rc);
  160. mlx4_en_filter_rfs_expire(priv);
  161. filter->activated = 1;
  162. }
  163. static inline struct hlist_head *
  164. filter_hash_bucket(struct mlx4_en_priv *priv, __be32 src_ip, __be32 dst_ip,
  165. __be16 src_port, __be16 dst_port)
  166. {
  167. unsigned long l;
  168. int bucket_idx;
  169. l = (__force unsigned long)src_port |
  170. ((__force unsigned long)dst_port << 2);
  171. l ^= (__force unsigned long)(src_ip ^ dst_ip);
  172. bucket_idx = hash_long(l, MLX4_EN_FILTER_HASH_SHIFT);
  173. return &priv->filter_hash[bucket_idx];
  174. }
  175. static struct mlx4_en_filter *
  176. mlx4_en_filter_alloc(struct mlx4_en_priv *priv, int rxq_index, __be32 src_ip,
  177. __be32 dst_ip, __be16 src_port, __be16 dst_port,
  178. u32 flow_id)
  179. {
  180. struct mlx4_en_filter *filter = NULL;
  181. filter = kzalloc(sizeof(struct mlx4_en_filter), GFP_ATOMIC);
  182. if (!filter)
  183. return NULL;
  184. filter->priv = priv;
  185. filter->rxq_index = rxq_index;
  186. INIT_WORK(&filter->work, mlx4_en_filter_work);
  187. filter->src_ip = src_ip;
  188. filter->dst_ip = dst_ip;
  189. filter->src_port = src_port;
  190. filter->dst_port = dst_port;
  191. filter->flow_id = flow_id;
  192. filter->id = priv->last_filter_id++ % RPS_NO_FILTER;
  193. list_add_tail(&filter->next, &priv->filters);
  194. hlist_add_head(&filter->filter_chain,
  195. filter_hash_bucket(priv, src_ip, dst_ip, src_port,
  196. dst_port));
  197. return filter;
  198. }
  199. static void mlx4_en_filter_free(struct mlx4_en_filter *filter)
  200. {
  201. struct mlx4_en_priv *priv = filter->priv;
  202. int rc;
  203. list_del(&filter->next);
  204. rc = mlx4_flow_detach(priv->mdev->dev, filter->reg_id);
  205. if (rc && rc != -ENOENT)
  206. en_err(priv, "Error detaching flow. rc = %d\n", rc);
  207. kfree(filter);
  208. }
  209. static inline struct mlx4_en_filter *
  210. mlx4_en_filter_find(struct mlx4_en_priv *priv, __be32 src_ip, __be32 dst_ip,
  211. __be16 src_port, __be16 dst_port)
  212. {
  213. struct mlx4_en_filter *filter;
  214. struct mlx4_en_filter *ret = NULL;
  215. hlist_for_each_entry(filter,
  216. filter_hash_bucket(priv, src_ip, dst_ip,
  217. src_port, dst_port),
  218. filter_chain) {
  219. if (filter->src_ip == src_ip &&
  220. filter->dst_ip == dst_ip &&
  221. filter->src_port == src_port &&
  222. filter->dst_port == dst_port) {
  223. ret = filter;
  224. break;
  225. }
  226. }
  227. return ret;
  228. }
  229. static int
  230. mlx4_en_filter_rfs(struct net_device *net_dev, const struct sk_buff *skb,
  231. u16 rxq_index, u32 flow_id)
  232. {
  233. struct mlx4_en_priv *priv = netdev_priv(net_dev);
  234. struct mlx4_en_filter *filter;
  235. const struct iphdr *ip;
  236. const __be16 *ports;
  237. __be32 src_ip;
  238. __be32 dst_ip;
  239. __be16 src_port;
  240. __be16 dst_port;
  241. int nhoff = skb_network_offset(skb);
  242. int ret = 0;
  243. if (skb->protocol != htons(ETH_P_IP))
  244. return -EPROTONOSUPPORT;
  245. ip = (const struct iphdr *)(skb->data + nhoff);
  246. if (ip_is_fragment(ip))
  247. return -EPROTONOSUPPORT;
  248. ports = (const __be16 *)(skb->data + nhoff + 4 * ip->ihl);
  249. src_ip = ip->saddr;
  250. dst_ip = ip->daddr;
  251. src_port = ports[0];
  252. dst_port = ports[1];
  253. if (ip->protocol != IPPROTO_TCP)
  254. return -EPROTONOSUPPORT;
  255. spin_lock_bh(&priv->filters_lock);
  256. filter = mlx4_en_filter_find(priv, src_ip, dst_ip, src_port, dst_port);
  257. if (filter) {
  258. if (filter->rxq_index == rxq_index)
  259. goto out;
  260. filter->rxq_index = rxq_index;
  261. } else {
  262. filter = mlx4_en_filter_alloc(priv, rxq_index,
  263. src_ip, dst_ip,
  264. src_port, dst_port, flow_id);
  265. if (!filter) {
  266. ret = -ENOMEM;
  267. goto err;
  268. }
  269. }
  270. queue_work(priv->mdev->workqueue, &filter->work);
  271. out:
  272. ret = filter->id;
  273. err:
  274. spin_unlock_bh(&priv->filters_lock);
  275. return ret;
  276. }
  277. void mlx4_en_cleanup_filters(struct mlx4_en_priv *priv,
  278. struct mlx4_en_rx_ring *rx_ring)
  279. {
  280. struct mlx4_en_filter *filter, *tmp;
  281. LIST_HEAD(del_list);
  282. spin_lock_bh(&priv->filters_lock);
  283. list_for_each_entry_safe(filter, tmp, &priv->filters, next) {
  284. list_move(&filter->next, &del_list);
  285. hlist_del(&filter->filter_chain);
  286. }
  287. spin_unlock_bh(&priv->filters_lock);
  288. list_for_each_entry_safe(filter, tmp, &del_list, next) {
  289. cancel_work_sync(&filter->work);
  290. mlx4_en_filter_free(filter);
  291. }
  292. }
  293. static void mlx4_en_filter_rfs_expire(struct mlx4_en_priv *priv)
  294. {
  295. struct mlx4_en_filter *filter = NULL, *tmp, *last_filter = NULL;
  296. LIST_HEAD(del_list);
  297. int i = 0;
  298. spin_lock_bh(&priv->filters_lock);
  299. list_for_each_entry_safe(filter, tmp, &priv->filters, next) {
  300. if (i > MLX4_EN_FILTER_EXPIRY_QUOTA)
  301. break;
  302. if (filter->activated &&
  303. !work_pending(&filter->work) &&
  304. rps_may_expire_flow(priv->dev,
  305. filter->rxq_index, filter->flow_id,
  306. filter->id)) {
  307. list_move(&filter->next, &del_list);
  308. hlist_del(&filter->filter_chain);
  309. } else
  310. last_filter = filter;
  311. i++;
  312. }
  313. if (last_filter && (&last_filter->next != priv->filters.next))
  314. list_move(&priv->filters, &last_filter->next);
  315. spin_unlock_bh(&priv->filters_lock);
  316. list_for_each_entry_safe(filter, tmp, &del_list, next)
  317. mlx4_en_filter_free(filter);
  318. }
  319. #endif
  320. static int mlx4_en_vlan_rx_add_vid(struct net_device *dev,
  321. __be16 proto, u16 vid)
  322. {
  323. struct mlx4_en_priv *priv = netdev_priv(dev);
  324. struct mlx4_en_dev *mdev = priv->mdev;
  325. int err;
  326. int idx;
  327. en_dbg(HW, priv, "adding VLAN:%d\n", vid);
  328. set_bit(vid, priv->active_vlans);
  329. /* Add VID to port VLAN filter */
  330. mutex_lock(&mdev->state_lock);
  331. if (mdev->device_up && priv->port_up) {
  332. err = mlx4_SET_VLAN_FLTR(mdev->dev, priv);
  333. if (err)
  334. en_err(priv, "Failed configuring VLAN filter\n");
  335. }
  336. if (mlx4_register_vlan(mdev->dev, priv->port, vid, &idx))
  337. en_dbg(HW, priv, "failed adding vlan %d\n", vid);
  338. mutex_unlock(&mdev->state_lock);
  339. return 0;
  340. }
  341. static int mlx4_en_vlan_rx_kill_vid(struct net_device *dev,
  342. __be16 proto, u16 vid)
  343. {
  344. struct mlx4_en_priv *priv = netdev_priv(dev);
  345. struct mlx4_en_dev *mdev = priv->mdev;
  346. int err;
  347. int idx;
  348. en_dbg(HW, priv, "Killing VID:%d\n", vid);
  349. clear_bit(vid, priv->active_vlans);
  350. /* Remove VID from port VLAN filter */
  351. mutex_lock(&mdev->state_lock);
  352. if (!mlx4_find_cached_vlan(mdev->dev, priv->port, vid, &idx))
  353. mlx4_unregister_vlan(mdev->dev, priv->port, idx);
  354. else
  355. en_dbg(HW, priv, "could not find vid %d in cache\n", vid);
  356. if (mdev->device_up && priv->port_up) {
  357. err = mlx4_SET_VLAN_FLTR(mdev->dev, priv);
  358. if (err)
  359. en_err(priv, "Failed configuring VLAN filter\n");
  360. }
  361. mutex_unlock(&mdev->state_lock);
  362. return 0;
  363. }
  364. static void mlx4_en_u64_to_mac(unsigned char dst_mac[ETH_ALEN + 2], u64 src_mac)
  365. {
  366. int i;
  367. for (i = ETH_ALEN - 1; i >= 0; --i) {
  368. dst_mac[i] = src_mac & 0xff;
  369. src_mac >>= 8;
  370. }
  371. memset(&dst_mac[ETH_ALEN], 0, 2);
  372. }
  373. static int mlx4_en_uc_steer_add(struct mlx4_en_priv *priv,
  374. unsigned char *mac, int *qpn, u64 *reg_id)
  375. {
  376. struct mlx4_en_dev *mdev = priv->mdev;
  377. struct mlx4_dev *dev = mdev->dev;
  378. int err;
  379. switch (dev->caps.steering_mode) {
  380. case MLX4_STEERING_MODE_B0: {
  381. struct mlx4_qp qp;
  382. u8 gid[16] = {0};
  383. qp.qpn = *qpn;
  384. memcpy(&gid[10], mac, ETH_ALEN);
  385. gid[5] = priv->port;
  386. err = mlx4_unicast_attach(dev, &qp, gid, 0, MLX4_PROT_ETH);
  387. break;
  388. }
  389. case MLX4_STEERING_MODE_DEVICE_MANAGED: {
  390. struct mlx4_spec_list spec_eth = { {NULL} };
  391. __be64 mac_mask = cpu_to_be64(MLX4_MAC_MASK << 16);
  392. struct mlx4_net_trans_rule rule = {
  393. .queue_mode = MLX4_NET_TRANS_Q_FIFO,
  394. .exclusive = 0,
  395. .allow_loopback = 1,
  396. .promisc_mode = MLX4_FS_REGULAR,
  397. .priority = MLX4_DOMAIN_NIC,
  398. };
  399. rule.port = priv->port;
  400. rule.qpn = *qpn;
  401. INIT_LIST_HEAD(&rule.list);
  402. spec_eth.id = MLX4_NET_TRANS_RULE_ID_ETH;
  403. memcpy(spec_eth.eth.dst_mac, mac, ETH_ALEN);
  404. memcpy(spec_eth.eth.dst_mac_msk, &mac_mask, ETH_ALEN);
  405. list_add_tail(&spec_eth.list, &rule.list);
  406. err = mlx4_flow_attach(dev, &rule, reg_id);
  407. break;
  408. }
  409. default:
  410. return -EINVAL;
  411. }
  412. if (err)
  413. en_warn(priv, "Failed Attaching Unicast\n");
  414. return err;
  415. }
  416. static void mlx4_en_uc_steer_release(struct mlx4_en_priv *priv,
  417. unsigned char *mac, int qpn, u64 reg_id)
  418. {
  419. struct mlx4_en_dev *mdev = priv->mdev;
  420. struct mlx4_dev *dev = mdev->dev;
  421. switch (dev->caps.steering_mode) {
  422. case MLX4_STEERING_MODE_B0: {
  423. struct mlx4_qp qp;
  424. u8 gid[16] = {0};
  425. qp.qpn = qpn;
  426. memcpy(&gid[10], mac, ETH_ALEN);
  427. gid[5] = priv->port;
  428. mlx4_unicast_detach(dev, &qp, gid, MLX4_PROT_ETH);
  429. break;
  430. }
  431. case MLX4_STEERING_MODE_DEVICE_MANAGED: {
  432. mlx4_flow_detach(dev, reg_id);
  433. break;
  434. }
  435. default:
  436. en_err(priv, "Invalid steering mode.\n");
  437. }
  438. }
  439. static int mlx4_en_get_qp(struct mlx4_en_priv *priv)
  440. {
  441. struct mlx4_en_dev *mdev = priv->mdev;
  442. struct mlx4_dev *dev = mdev->dev;
  443. struct mlx4_mac_entry *entry;
  444. int index = 0;
  445. int err = 0;
  446. u64 reg_id;
  447. int *qpn = &priv->base_qpn;
  448. u64 mac = mlx4_en_mac_to_u64(priv->dev->dev_addr);
  449. en_dbg(DRV, priv, "Registering MAC: %pM for adding\n",
  450. priv->dev->dev_addr);
  451. index = mlx4_register_mac(dev, priv->port, mac);
  452. if (index < 0) {
  453. err = index;
  454. en_err(priv, "Failed adding MAC: %pM\n",
  455. priv->dev->dev_addr);
  456. return err;
  457. }
  458. if (dev->caps.steering_mode == MLX4_STEERING_MODE_A0) {
  459. int base_qpn = mlx4_get_base_qpn(dev, priv->port);
  460. *qpn = base_qpn + index;
  461. return 0;
  462. }
  463. err = mlx4_qp_reserve_range(dev, 1, 1, qpn);
  464. en_dbg(DRV, priv, "Reserved qp %d\n", *qpn);
  465. if (err) {
  466. en_err(priv, "Failed to reserve qp for mac registration\n");
  467. goto qp_err;
  468. }
  469. err = mlx4_en_uc_steer_add(priv, priv->dev->dev_addr, qpn, &reg_id);
  470. if (err)
  471. goto steer_err;
  472. entry = kmalloc(sizeof(*entry), GFP_KERNEL);
  473. if (!entry) {
  474. err = -ENOMEM;
  475. goto alloc_err;
  476. }
  477. memcpy(entry->mac, priv->dev->dev_addr, sizeof(entry->mac));
  478. entry->reg_id = reg_id;
  479. hlist_add_head_rcu(&entry->hlist,
  480. &priv->mac_hash[entry->mac[MLX4_EN_MAC_HASH_IDX]]);
  481. return 0;
  482. alloc_err:
  483. mlx4_en_uc_steer_release(priv, priv->dev->dev_addr, *qpn, reg_id);
  484. steer_err:
  485. mlx4_qp_release_range(dev, *qpn, 1);
  486. qp_err:
  487. mlx4_unregister_mac(dev, priv->port, mac);
  488. return err;
  489. }
  490. static void mlx4_en_put_qp(struct mlx4_en_priv *priv)
  491. {
  492. struct mlx4_en_dev *mdev = priv->mdev;
  493. struct mlx4_dev *dev = mdev->dev;
  494. int qpn = priv->base_qpn;
  495. u64 mac;
  496. if (dev->caps.steering_mode == MLX4_STEERING_MODE_A0) {
  497. mac = mlx4_en_mac_to_u64(priv->dev->dev_addr);
  498. en_dbg(DRV, priv, "Registering MAC: %pM for deleting\n",
  499. priv->dev->dev_addr);
  500. mlx4_unregister_mac(dev, priv->port, mac);
  501. } else {
  502. struct mlx4_mac_entry *entry;
  503. struct hlist_node *tmp;
  504. struct hlist_head *bucket;
  505. unsigned int i;
  506. for (i = 0; i < MLX4_EN_MAC_HASH_SIZE; ++i) {
  507. bucket = &priv->mac_hash[i];
  508. hlist_for_each_entry_safe(entry, tmp, bucket, hlist) {
  509. mac = mlx4_en_mac_to_u64(entry->mac);
  510. en_dbg(DRV, priv, "Registering MAC: %pM for deleting\n",
  511. entry->mac);
  512. mlx4_en_uc_steer_release(priv, entry->mac,
  513. qpn, entry->reg_id);
  514. mlx4_unregister_mac(dev, priv->port, mac);
  515. hlist_del_rcu(&entry->hlist);
  516. kfree_rcu(entry, rcu);
  517. }
  518. }
  519. en_dbg(DRV, priv, "Releasing qp: port %d, qpn %d\n",
  520. priv->port, qpn);
  521. mlx4_qp_release_range(dev, qpn, 1);
  522. priv->flags &= ~MLX4_EN_FLAG_FORCE_PROMISC;
  523. }
  524. }
  525. static int mlx4_en_replace_mac(struct mlx4_en_priv *priv, int qpn,
  526. unsigned char *new_mac, unsigned char *prev_mac)
  527. {
  528. struct mlx4_en_dev *mdev = priv->mdev;
  529. struct mlx4_dev *dev = mdev->dev;
  530. int err = 0;
  531. u64 new_mac_u64 = mlx4_en_mac_to_u64(new_mac);
  532. if (dev->caps.steering_mode != MLX4_STEERING_MODE_A0) {
  533. struct hlist_head *bucket;
  534. unsigned int mac_hash;
  535. struct mlx4_mac_entry *entry;
  536. struct hlist_node *tmp;
  537. u64 prev_mac_u64 = mlx4_en_mac_to_u64(prev_mac);
  538. bucket = &priv->mac_hash[prev_mac[MLX4_EN_MAC_HASH_IDX]];
  539. hlist_for_each_entry_safe(entry, tmp, bucket, hlist) {
  540. if (ether_addr_equal_64bits(entry->mac, prev_mac)) {
  541. mlx4_en_uc_steer_release(priv, entry->mac,
  542. qpn, entry->reg_id);
  543. mlx4_unregister_mac(dev, priv->port,
  544. prev_mac_u64);
  545. hlist_del_rcu(&entry->hlist);
  546. synchronize_rcu();
  547. memcpy(entry->mac, new_mac, ETH_ALEN);
  548. entry->reg_id = 0;
  549. mac_hash = new_mac[MLX4_EN_MAC_HASH_IDX];
  550. hlist_add_head_rcu(&entry->hlist,
  551. &priv->mac_hash[mac_hash]);
  552. mlx4_register_mac(dev, priv->port, new_mac_u64);
  553. err = mlx4_en_uc_steer_add(priv, new_mac,
  554. &qpn,
  555. &entry->reg_id);
  556. return err;
  557. }
  558. }
  559. return -EINVAL;
  560. }
  561. return __mlx4_replace_mac(dev, priv->port, qpn, new_mac_u64);
  562. }
  563. u64 mlx4_en_mac_to_u64(u8 *addr)
  564. {
  565. u64 mac = 0;
  566. int i;
  567. for (i = 0; i < ETH_ALEN; i++) {
  568. mac <<= 8;
  569. mac |= addr[i];
  570. }
  571. return mac;
  572. }
  573. static int mlx4_en_do_set_mac(struct mlx4_en_priv *priv)
  574. {
  575. int err = 0;
  576. if (priv->port_up) {
  577. /* Remove old MAC and insert the new one */
  578. err = mlx4_en_replace_mac(priv, priv->base_qpn,
  579. priv->dev->dev_addr, priv->prev_mac);
  580. if (err)
  581. en_err(priv, "Failed changing HW MAC address\n");
  582. memcpy(priv->prev_mac, priv->dev->dev_addr,
  583. sizeof(priv->prev_mac));
  584. } else
  585. en_dbg(HW, priv, "Port is down while registering mac, exiting...\n");
  586. return err;
  587. }
  588. static int mlx4_en_set_mac(struct net_device *dev, void *addr)
  589. {
  590. struct mlx4_en_priv *priv = netdev_priv(dev);
  591. struct mlx4_en_dev *mdev = priv->mdev;
  592. struct sockaddr *saddr = addr;
  593. int err;
  594. if (!is_valid_ether_addr(saddr->sa_data))
  595. return -EADDRNOTAVAIL;
  596. memcpy(dev->dev_addr, saddr->sa_data, ETH_ALEN);
  597. mutex_lock(&mdev->state_lock);
  598. err = mlx4_en_do_set_mac(priv);
  599. mutex_unlock(&mdev->state_lock);
  600. return err;
  601. }
  602. static void mlx4_en_clear_list(struct net_device *dev)
  603. {
  604. struct mlx4_en_priv *priv = netdev_priv(dev);
  605. struct mlx4_en_mc_list *tmp, *mc_to_del;
  606. list_for_each_entry_safe(mc_to_del, tmp, &priv->mc_list, list) {
  607. list_del(&mc_to_del->list);
  608. kfree(mc_to_del);
  609. }
  610. }
  611. static void mlx4_en_cache_mclist(struct net_device *dev)
  612. {
  613. struct mlx4_en_priv *priv = netdev_priv(dev);
  614. struct netdev_hw_addr *ha;
  615. struct mlx4_en_mc_list *tmp;
  616. mlx4_en_clear_list(dev);
  617. netdev_for_each_mc_addr(ha, dev) {
  618. tmp = kzalloc(sizeof(struct mlx4_en_mc_list), GFP_ATOMIC);
  619. if (!tmp) {
  620. mlx4_en_clear_list(dev);
  621. return;
  622. }
  623. memcpy(tmp->addr, ha->addr, ETH_ALEN);
  624. list_add_tail(&tmp->list, &priv->mc_list);
  625. }
  626. }
  627. static void update_mclist_flags(struct mlx4_en_priv *priv,
  628. struct list_head *dst,
  629. struct list_head *src)
  630. {
  631. struct mlx4_en_mc_list *dst_tmp, *src_tmp, *new_mc;
  632. bool found;
  633. /* Find all the entries that should be removed from dst,
  634. * These are the entries that are not found in src
  635. */
  636. list_for_each_entry(dst_tmp, dst, list) {
  637. found = false;
  638. list_for_each_entry(src_tmp, src, list) {
  639. if (!memcmp(dst_tmp->addr, src_tmp->addr, ETH_ALEN)) {
  640. found = true;
  641. break;
  642. }
  643. }
  644. if (!found)
  645. dst_tmp->action = MCLIST_REM;
  646. }
  647. /* Add entries that exist in src but not in dst
  648. * mark them as need to add
  649. */
  650. list_for_each_entry(src_tmp, src, list) {
  651. found = false;
  652. list_for_each_entry(dst_tmp, dst, list) {
  653. if (!memcmp(dst_tmp->addr, src_tmp->addr, ETH_ALEN)) {
  654. dst_tmp->action = MCLIST_NONE;
  655. found = true;
  656. break;
  657. }
  658. }
  659. if (!found) {
  660. new_mc = kmemdup(src_tmp,
  661. sizeof(struct mlx4_en_mc_list),
  662. GFP_KERNEL);
  663. if (!new_mc)
  664. return;
  665. new_mc->action = MCLIST_ADD;
  666. list_add_tail(&new_mc->list, dst);
  667. }
  668. }
  669. }
  670. static void mlx4_en_set_rx_mode(struct net_device *dev)
  671. {
  672. struct mlx4_en_priv *priv = netdev_priv(dev);
  673. if (!priv->port_up)
  674. return;
  675. queue_work(priv->mdev->workqueue, &priv->rx_mode_task);
  676. }
  677. static void mlx4_en_set_promisc_mode(struct mlx4_en_priv *priv,
  678. struct mlx4_en_dev *mdev)
  679. {
  680. int err = 0;
  681. if (!(priv->flags & MLX4_EN_FLAG_PROMISC)) {
  682. if (netif_msg_rx_status(priv))
  683. en_warn(priv, "Entering promiscuous mode\n");
  684. priv->flags |= MLX4_EN_FLAG_PROMISC;
  685. /* Enable promiscouos mode */
  686. switch (mdev->dev->caps.steering_mode) {
  687. case MLX4_STEERING_MODE_DEVICE_MANAGED:
  688. err = mlx4_flow_steer_promisc_add(mdev->dev,
  689. priv->port,
  690. priv->base_qpn,
  691. MLX4_FS_ALL_DEFAULT);
  692. if (err)
  693. en_err(priv, "Failed enabling promiscuous mode\n");
  694. priv->flags |= MLX4_EN_FLAG_MC_PROMISC;
  695. break;
  696. case MLX4_STEERING_MODE_B0:
  697. err = mlx4_unicast_promisc_add(mdev->dev,
  698. priv->base_qpn,
  699. priv->port);
  700. if (err)
  701. en_err(priv, "Failed enabling unicast promiscuous mode\n");
  702. /* Add the default qp number as multicast
  703. * promisc
  704. */
  705. if (!(priv->flags & MLX4_EN_FLAG_MC_PROMISC)) {
  706. err = mlx4_multicast_promisc_add(mdev->dev,
  707. priv->base_qpn,
  708. priv->port);
  709. if (err)
  710. en_err(priv, "Failed enabling multicast promiscuous mode\n");
  711. priv->flags |= MLX4_EN_FLAG_MC_PROMISC;
  712. }
  713. break;
  714. case MLX4_STEERING_MODE_A0:
  715. err = mlx4_SET_PORT_qpn_calc(mdev->dev,
  716. priv->port,
  717. priv->base_qpn,
  718. 1);
  719. if (err)
  720. en_err(priv, "Failed enabling promiscuous mode\n");
  721. break;
  722. }
  723. /* Disable port multicast filter (unconditionally) */
  724. err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
  725. 0, MLX4_MCAST_DISABLE);
  726. if (err)
  727. en_err(priv, "Failed disabling multicast filter\n");
  728. /* Disable port VLAN filter */
  729. err = mlx4_SET_VLAN_FLTR(mdev->dev, priv);
  730. if (err)
  731. en_err(priv, "Failed disabling VLAN 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. /* Enable port VLAN filter */
  776. err = mlx4_SET_VLAN_FLTR(mdev->dev, priv);
  777. if (err)
  778. en_err(priv, "Failed enabling VLAN filter\n");
  779. }
  780. static void mlx4_en_do_multicast(struct mlx4_en_priv *priv,
  781. struct net_device *dev,
  782. struct mlx4_en_dev *mdev)
  783. {
  784. struct mlx4_en_mc_list *mclist, *tmp;
  785. u64 mcast_addr = 0;
  786. u8 mc_list[16] = {0};
  787. int err = 0;
  788. /* Enable/disable the multicast filter according to IFF_ALLMULTI */
  789. if (dev->flags & IFF_ALLMULTI) {
  790. err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
  791. 0, MLX4_MCAST_DISABLE);
  792. if (err)
  793. en_err(priv, "Failed disabling multicast filter\n");
  794. /* Add the default qp number as multicast promisc */
  795. if (!(priv->flags & MLX4_EN_FLAG_MC_PROMISC)) {
  796. switch (mdev->dev->caps.steering_mode) {
  797. case MLX4_STEERING_MODE_DEVICE_MANAGED:
  798. err = mlx4_flow_steer_promisc_add(mdev->dev,
  799. priv->port,
  800. priv->base_qpn,
  801. MLX4_FS_MC_DEFAULT);
  802. break;
  803. case MLX4_STEERING_MODE_B0:
  804. err = mlx4_multicast_promisc_add(mdev->dev,
  805. priv->base_qpn,
  806. priv->port);
  807. break;
  808. case MLX4_STEERING_MODE_A0:
  809. break;
  810. }
  811. if (err)
  812. en_err(priv, "Failed entering multicast promisc mode\n");
  813. priv->flags |= MLX4_EN_FLAG_MC_PROMISC;
  814. }
  815. } else {
  816. /* Disable Multicast promisc */
  817. if (priv->flags & MLX4_EN_FLAG_MC_PROMISC) {
  818. switch (mdev->dev->caps.steering_mode) {
  819. case MLX4_STEERING_MODE_DEVICE_MANAGED:
  820. err = mlx4_flow_steer_promisc_remove(mdev->dev,
  821. priv->port,
  822. MLX4_FS_MC_DEFAULT);
  823. break;
  824. case MLX4_STEERING_MODE_B0:
  825. err = mlx4_multicast_promisc_remove(mdev->dev,
  826. priv->base_qpn,
  827. priv->port);
  828. break;
  829. case MLX4_STEERING_MODE_A0:
  830. break;
  831. }
  832. if (err)
  833. en_err(priv, "Failed disabling multicast promiscuous mode\n");
  834. priv->flags &= ~MLX4_EN_FLAG_MC_PROMISC;
  835. }
  836. err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
  837. 0, MLX4_MCAST_DISABLE);
  838. if (err)
  839. en_err(priv, "Failed disabling multicast filter\n");
  840. /* Flush mcast filter and init it with broadcast address */
  841. mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, ETH_BCAST,
  842. 1, MLX4_MCAST_CONFIG);
  843. /* Update multicast list - we cache all addresses so they won't
  844. * change while HW is updated holding the command semaphor */
  845. netif_addr_lock_bh(dev);
  846. mlx4_en_cache_mclist(dev);
  847. netif_addr_unlock_bh(dev);
  848. list_for_each_entry(mclist, &priv->mc_list, list) {
  849. mcast_addr = mlx4_en_mac_to_u64(mclist->addr);
  850. mlx4_SET_MCAST_FLTR(mdev->dev, priv->port,
  851. mcast_addr, 0, MLX4_MCAST_CONFIG);
  852. }
  853. err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
  854. 0, MLX4_MCAST_ENABLE);
  855. if (err)
  856. en_err(priv, "Failed enabling multicast filter\n");
  857. update_mclist_flags(priv, &priv->curr_list, &priv->mc_list);
  858. list_for_each_entry_safe(mclist, tmp, &priv->curr_list, list) {
  859. if (mclist->action == MCLIST_REM) {
  860. /* detach this address and delete from list */
  861. memcpy(&mc_list[10], mclist->addr, ETH_ALEN);
  862. mc_list[5] = priv->port;
  863. err = mlx4_multicast_detach(mdev->dev,
  864. &priv->rss_map.indir_qp,
  865. mc_list,
  866. MLX4_PROT_ETH,
  867. mclist->reg_id);
  868. if (err)
  869. en_err(priv, "Fail to detach multicast address\n");
  870. /* remove from list */
  871. list_del(&mclist->list);
  872. kfree(mclist);
  873. } else if (mclist->action == MCLIST_ADD) {
  874. /* attach the address */
  875. memcpy(&mc_list[10], mclist->addr, ETH_ALEN);
  876. /* needed for B0 steering support */
  877. mc_list[5] = priv->port;
  878. err = mlx4_multicast_attach(mdev->dev,
  879. &priv->rss_map.indir_qp,
  880. mc_list,
  881. priv->port, 0,
  882. MLX4_PROT_ETH,
  883. &mclist->reg_id);
  884. if (err)
  885. en_err(priv, "Fail to attach multicast address\n");
  886. }
  887. }
  888. }
  889. }
  890. static void mlx4_en_do_uc_filter(struct mlx4_en_priv *priv,
  891. struct net_device *dev,
  892. struct mlx4_en_dev *mdev)
  893. {
  894. struct netdev_hw_addr *ha;
  895. struct mlx4_mac_entry *entry;
  896. struct hlist_node *tmp;
  897. bool found;
  898. u64 mac;
  899. int err = 0;
  900. struct hlist_head *bucket;
  901. unsigned int i;
  902. int removed = 0;
  903. u32 prev_flags;
  904. /* Note that we do not need to protect our mac_hash traversal with rcu,
  905. * since all modification code is protected by mdev->state_lock
  906. */
  907. /* find what to remove */
  908. for (i = 0; i < MLX4_EN_MAC_HASH_SIZE; ++i) {
  909. bucket = &priv->mac_hash[i];
  910. hlist_for_each_entry_safe(entry, tmp, bucket, hlist) {
  911. found = false;
  912. netdev_for_each_uc_addr(ha, dev) {
  913. if (ether_addr_equal_64bits(entry->mac,
  914. ha->addr)) {
  915. found = true;
  916. break;
  917. }
  918. }
  919. /* MAC address of the port is not in uc list */
  920. if (ether_addr_equal_64bits(entry->mac, dev->dev_addr))
  921. found = true;
  922. if (!found) {
  923. mac = mlx4_en_mac_to_u64(entry->mac);
  924. mlx4_en_uc_steer_release(priv, entry->mac,
  925. priv->base_qpn,
  926. entry->reg_id);
  927. mlx4_unregister_mac(mdev->dev, priv->port, mac);
  928. hlist_del_rcu(&entry->hlist);
  929. kfree_rcu(entry, rcu);
  930. en_dbg(DRV, priv, "Removed MAC %pM on port:%d\n",
  931. entry->mac, priv->port);
  932. ++removed;
  933. }
  934. }
  935. }
  936. /* if we didn't remove anything, there is no use in trying to add
  937. * again once we are in a forced promisc mode state
  938. */
  939. if ((priv->flags & MLX4_EN_FLAG_FORCE_PROMISC) && 0 == removed)
  940. return;
  941. prev_flags = priv->flags;
  942. priv->flags &= ~MLX4_EN_FLAG_FORCE_PROMISC;
  943. /* find what to add */
  944. netdev_for_each_uc_addr(ha, dev) {
  945. found = false;
  946. bucket = &priv->mac_hash[ha->addr[MLX4_EN_MAC_HASH_IDX]];
  947. hlist_for_each_entry(entry, bucket, hlist) {
  948. if (ether_addr_equal_64bits(entry->mac, ha->addr)) {
  949. found = true;
  950. break;
  951. }
  952. }
  953. if (!found) {
  954. entry = kmalloc(sizeof(*entry), GFP_KERNEL);
  955. if (!entry) {
  956. en_err(priv, "Failed adding MAC %pM on port:%d (out of memory)\n",
  957. ha->addr, priv->port);
  958. priv->flags |= MLX4_EN_FLAG_FORCE_PROMISC;
  959. break;
  960. }
  961. mac = mlx4_en_mac_to_u64(ha->addr);
  962. memcpy(entry->mac, ha->addr, ETH_ALEN);
  963. err = mlx4_register_mac(mdev->dev, priv->port, mac);
  964. if (err < 0) {
  965. en_err(priv, "Failed registering MAC %pM on port %d: %d\n",
  966. ha->addr, priv->port, err);
  967. kfree(entry);
  968. priv->flags |= MLX4_EN_FLAG_FORCE_PROMISC;
  969. break;
  970. }
  971. err = mlx4_en_uc_steer_add(priv, ha->addr,
  972. &priv->base_qpn,
  973. &entry->reg_id);
  974. if (err) {
  975. en_err(priv, "Failed adding MAC %pM on port %d: %d\n",
  976. ha->addr, priv->port, err);
  977. mlx4_unregister_mac(mdev->dev, priv->port, mac);
  978. kfree(entry);
  979. priv->flags |= MLX4_EN_FLAG_FORCE_PROMISC;
  980. break;
  981. } else {
  982. unsigned int mac_hash;
  983. en_dbg(DRV, priv, "Added MAC %pM on port:%d\n",
  984. ha->addr, priv->port);
  985. mac_hash = ha->addr[MLX4_EN_MAC_HASH_IDX];
  986. bucket = &priv->mac_hash[mac_hash];
  987. hlist_add_head_rcu(&entry->hlist, bucket);
  988. }
  989. }
  990. }
  991. if (priv->flags & MLX4_EN_FLAG_FORCE_PROMISC) {
  992. en_warn(priv, "Forcing promiscuous mode on port:%d\n",
  993. priv->port);
  994. } else if (prev_flags & MLX4_EN_FLAG_FORCE_PROMISC) {
  995. en_warn(priv, "Stop forcing promiscuous mode on port:%d\n",
  996. priv->port);
  997. }
  998. }
  999. static void mlx4_en_do_set_rx_mode(struct work_struct *work)
  1000. {
  1001. struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
  1002. rx_mode_task);
  1003. struct mlx4_en_dev *mdev = priv->mdev;
  1004. struct net_device *dev = priv->dev;
  1005. mutex_lock(&mdev->state_lock);
  1006. if (!mdev->device_up) {
  1007. en_dbg(HW, priv, "Card is not up, ignoring rx mode change.\n");
  1008. goto out;
  1009. }
  1010. if (!priv->port_up) {
  1011. en_dbg(HW, priv, "Port is down, ignoring rx mode change.\n");
  1012. goto out;
  1013. }
  1014. if (!netif_carrier_ok(dev)) {
  1015. if (!mlx4_en_QUERY_PORT(mdev, priv->port)) {
  1016. if (priv->port_state.link_state) {
  1017. priv->last_link_state = MLX4_DEV_EVENT_PORT_UP;
  1018. netif_carrier_on(dev);
  1019. en_dbg(LINK, priv, "Link Up\n");
  1020. }
  1021. }
  1022. }
  1023. if (dev->priv_flags & IFF_UNICAST_FLT)
  1024. mlx4_en_do_uc_filter(priv, dev, mdev);
  1025. /* Promsicuous mode: disable all filters */
  1026. if ((dev->flags & IFF_PROMISC) ||
  1027. (priv->flags & MLX4_EN_FLAG_FORCE_PROMISC)) {
  1028. mlx4_en_set_promisc_mode(priv, mdev);
  1029. goto out;
  1030. }
  1031. /* Not in promiscuous mode */
  1032. if (priv->flags & MLX4_EN_FLAG_PROMISC)
  1033. mlx4_en_clear_promisc_mode(priv, mdev);
  1034. mlx4_en_do_multicast(priv, dev, mdev);
  1035. out:
  1036. mutex_unlock(&mdev->state_lock);
  1037. }
  1038. #ifdef CONFIG_NET_POLL_CONTROLLER
  1039. static void mlx4_en_netpoll(struct net_device *dev)
  1040. {
  1041. struct mlx4_en_priv *priv = netdev_priv(dev);
  1042. struct mlx4_en_cq *cq;
  1043. unsigned long flags;
  1044. int i;
  1045. for (i = 0; i < priv->rx_ring_num; i++) {
  1046. cq = &priv->rx_cq[i];
  1047. spin_lock_irqsave(&cq->lock, flags);
  1048. napi_synchronize(&cq->napi);
  1049. mlx4_en_process_rx_cq(dev, cq, 0);
  1050. spin_unlock_irqrestore(&cq->lock, flags);
  1051. }
  1052. }
  1053. #endif
  1054. static void mlx4_en_tx_timeout(struct net_device *dev)
  1055. {
  1056. struct mlx4_en_priv *priv = netdev_priv(dev);
  1057. struct mlx4_en_dev *mdev = priv->mdev;
  1058. int i;
  1059. if (netif_msg_timer(priv))
  1060. en_warn(priv, "Tx timeout called on port:%d\n", priv->port);
  1061. for (i = 0; i < priv->tx_ring_num; i++) {
  1062. if (!netif_tx_queue_stopped(netdev_get_tx_queue(dev, i)))
  1063. continue;
  1064. en_warn(priv, "TX timeout on queue: %d, QP: 0x%x, CQ: 0x%x, Cons: 0x%x, Prod: 0x%x\n",
  1065. i, priv->tx_ring[i].qpn, priv->tx_ring[i].cqn,
  1066. priv->tx_ring[i].cons, priv->tx_ring[i].prod);
  1067. }
  1068. priv->port_stats.tx_timeout++;
  1069. en_dbg(DRV, priv, "Scheduling watchdog\n");
  1070. queue_work(mdev->workqueue, &priv->watchdog_task);
  1071. }
  1072. static struct net_device_stats *mlx4_en_get_stats(struct net_device *dev)
  1073. {
  1074. struct mlx4_en_priv *priv = netdev_priv(dev);
  1075. spin_lock_bh(&priv->stats_lock);
  1076. memcpy(&priv->ret_stats, &priv->stats, sizeof(priv->stats));
  1077. spin_unlock_bh(&priv->stats_lock);
  1078. return &priv->ret_stats;
  1079. }
  1080. static void mlx4_en_set_default_moderation(struct mlx4_en_priv *priv)
  1081. {
  1082. struct mlx4_en_cq *cq;
  1083. int i;
  1084. /* If we haven't received a specific coalescing setting
  1085. * (module param), we set the moderation parameters as follows:
  1086. * - moder_cnt is set to the number of mtu sized packets to
  1087. * satisfy our coalescing target.
  1088. * - moder_time is set to a fixed value.
  1089. */
  1090. priv->rx_frames = MLX4_EN_RX_COAL_TARGET;
  1091. priv->rx_usecs = MLX4_EN_RX_COAL_TIME;
  1092. priv->tx_frames = MLX4_EN_TX_COAL_PKTS;
  1093. priv->tx_usecs = MLX4_EN_TX_COAL_TIME;
  1094. en_dbg(INTR, priv, "Default coalesing params for mtu:%d - rx_frames:%d rx_usecs:%d\n",
  1095. priv->dev->mtu, priv->rx_frames, priv->rx_usecs);
  1096. /* Setup cq moderation params */
  1097. for (i = 0; i < priv->rx_ring_num; i++) {
  1098. cq = &priv->rx_cq[i];
  1099. cq->moder_cnt = priv->rx_frames;
  1100. cq->moder_time = priv->rx_usecs;
  1101. priv->last_moder_time[i] = MLX4_EN_AUTO_CONF;
  1102. priv->last_moder_packets[i] = 0;
  1103. priv->last_moder_bytes[i] = 0;
  1104. }
  1105. for (i = 0; i < priv->tx_ring_num; i++) {
  1106. cq = &priv->tx_cq[i];
  1107. cq->moder_cnt = priv->tx_frames;
  1108. cq->moder_time = priv->tx_usecs;
  1109. }
  1110. /* Reset auto-moderation params */
  1111. priv->pkt_rate_low = MLX4_EN_RX_RATE_LOW;
  1112. priv->rx_usecs_low = MLX4_EN_RX_COAL_TIME_LOW;
  1113. priv->pkt_rate_high = MLX4_EN_RX_RATE_HIGH;
  1114. priv->rx_usecs_high = MLX4_EN_RX_COAL_TIME_HIGH;
  1115. priv->sample_interval = MLX4_EN_SAMPLE_INTERVAL;
  1116. priv->adaptive_rx_coal = 1;
  1117. priv->last_moder_jiffies = 0;
  1118. priv->last_moder_tx_packets = 0;
  1119. }
  1120. static void mlx4_en_auto_moderation(struct mlx4_en_priv *priv)
  1121. {
  1122. unsigned long period = (unsigned long) (jiffies - priv->last_moder_jiffies);
  1123. struct mlx4_en_cq *cq;
  1124. unsigned long packets;
  1125. unsigned long rate;
  1126. unsigned long avg_pkt_size;
  1127. unsigned long rx_packets;
  1128. unsigned long rx_bytes;
  1129. unsigned long rx_pkt_diff;
  1130. int moder_time;
  1131. int ring, err;
  1132. if (!priv->adaptive_rx_coal || period < priv->sample_interval * HZ)
  1133. return;
  1134. for (ring = 0; ring < priv->rx_ring_num; ring++) {
  1135. spin_lock_bh(&priv->stats_lock);
  1136. rx_packets = priv->rx_ring[ring].packets;
  1137. rx_bytes = priv->rx_ring[ring].bytes;
  1138. spin_unlock_bh(&priv->stats_lock);
  1139. rx_pkt_diff = ((unsigned long) (rx_packets -
  1140. priv->last_moder_packets[ring]));
  1141. packets = rx_pkt_diff;
  1142. rate = packets * HZ / period;
  1143. avg_pkt_size = packets ? ((unsigned long) (rx_bytes -
  1144. priv->last_moder_bytes[ring])) / packets : 0;
  1145. /* Apply auto-moderation only when packet rate
  1146. * exceeds a rate that it matters */
  1147. if (rate > (MLX4_EN_RX_RATE_THRESH / priv->rx_ring_num) &&
  1148. avg_pkt_size > MLX4_EN_AVG_PKT_SMALL) {
  1149. if (rate < priv->pkt_rate_low)
  1150. moder_time = priv->rx_usecs_low;
  1151. else if (rate > priv->pkt_rate_high)
  1152. moder_time = priv->rx_usecs_high;
  1153. else
  1154. moder_time = (rate - priv->pkt_rate_low) *
  1155. (priv->rx_usecs_high - priv->rx_usecs_low) /
  1156. (priv->pkt_rate_high - priv->pkt_rate_low) +
  1157. priv->rx_usecs_low;
  1158. } else {
  1159. moder_time = priv->rx_usecs_low;
  1160. }
  1161. if (moder_time != priv->last_moder_time[ring]) {
  1162. priv->last_moder_time[ring] = moder_time;
  1163. cq = &priv->rx_cq[ring];
  1164. cq->moder_time = moder_time;
  1165. cq->moder_cnt = priv->rx_frames;
  1166. err = mlx4_en_set_cq_moder(priv, cq);
  1167. if (err)
  1168. en_err(priv, "Failed modifying moderation for cq:%d\n",
  1169. ring);
  1170. }
  1171. priv->last_moder_packets[ring] = rx_packets;
  1172. priv->last_moder_bytes[ring] = rx_bytes;
  1173. }
  1174. priv->last_moder_jiffies = jiffies;
  1175. }
  1176. static void mlx4_en_do_get_stats(struct work_struct *work)
  1177. {
  1178. struct delayed_work *delay = to_delayed_work(work);
  1179. struct mlx4_en_priv *priv = container_of(delay, struct mlx4_en_priv,
  1180. stats_task);
  1181. struct mlx4_en_dev *mdev = priv->mdev;
  1182. int err;
  1183. mutex_lock(&mdev->state_lock);
  1184. if (mdev->device_up) {
  1185. if (priv->port_up) {
  1186. err = mlx4_en_DUMP_ETH_STATS(mdev, priv->port, 0);
  1187. if (err)
  1188. en_dbg(HW, priv, "Could not update stats\n");
  1189. mlx4_en_auto_moderation(priv);
  1190. }
  1191. queue_delayed_work(mdev->workqueue, &priv->stats_task, STATS_DELAY);
  1192. }
  1193. if (mdev->mac_removed[MLX4_MAX_PORTS + 1 - priv->port]) {
  1194. mlx4_en_do_set_mac(priv);
  1195. mdev->mac_removed[MLX4_MAX_PORTS + 1 - priv->port] = 0;
  1196. }
  1197. mutex_unlock(&mdev->state_lock);
  1198. }
  1199. /* mlx4_en_service_task - Run service task for tasks that needed to be done
  1200. * periodically
  1201. */
  1202. static void mlx4_en_service_task(struct work_struct *work)
  1203. {
  1204. struct delayed_work *delay = to_delayed_work(work);
  1205. struct mlx4_en_priv *priv = container_of(delay, struct mlx4_en_priv,
  1206. service_task);
  1207. struct mlx4_en_dev *mdev = priv->mdev;
  1208. mutex_lock(&mdev->state_lock);
  1209. if (mdev->device_up) {
  1210. if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_TS)
  1211. mlx4_en_ptp_overflow_check(mdev);
  1212. queue_delayed_work(mdev->workqueue, &priv->service_task,
  1213. SERVICE_TASK_DELAY);
  1214. }
  1215. mutex_unlock(&mdev->state_lock);
  1216. }
  1217. static void mlx4_en_linkstate(struct work_struct *work)
  1218. {
  1219. struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
  1220. linkstate_task);
  1221. struct mlx4_en_dev *mdev = priv->mdev;
  1222. int linkstate = priv->link_state;
  1223. mutex_lock(&mdev->state_lock);
  1224. /* If observable port state changed set carrier state and
  1225. * report to system log */
  1226. if (priv->last_link_state != linkstate) {
  1227. if (linkstate == MLX4_DEV_EVENT_PORT_DOWN) {
  1228. en_info(priv, "Link Down\n");
  1229. netif_carrier_off(priv->dev);
  1230. } else {
  1231. en_info(priv, "Link Up\n");
  1232. netif_carrier_on(priv->dev);
  1233. }
  1234. }
  1235. priv->last_link_state = linkstate;
  1236. mutex_unlock(&mdev->state_lock);
  1237. }
  1238. int mlx4_en_start_port(struct net_device *dev)
  1239. {
  1240. struct mlx4_en_priv *priv = netdev_priv(dev);
  1241. struct mlx4_en_dev *mdev = priv->mdev;
  1242. struct mlx4_en_cq *cq;
  1243. struct mlx4_en_tx_ring *tx_ring;
  1244. int rx_index = 0;
  1245. int tx_index = 0;
  1246. int err = 0;
  1247. int i;
  1248. int j;
  1249. u8 mc_list[16] = {0};
  1250. if (priv->port_up) {
  1251. en_dbg(DRV, priv, "start port called while port already up\n");
  1252. return 0;
  1253. }
  1254. INIT_LIST_HEAD(&priv->mc_list);
  1255. INIT_LIST_HEAD(&priv->curr_list);
  1256. INIT_LIST_HEAD(&priv->ethtool_list);
  1257. memset(&priv->ethtool_rules[0], 0,
  1258. sizeof(struct ethtool_flow_id) * MAX_NUM_OF_FS_RULES);
  1259. /* Calculate Rx buf size */
  1260. dev->mtu = min(dev->mtu, priv->max_mtu);
  1261. mlx4_en_calc_rx_buf(dev);
  1262. en_dbg(DRV, priv, "Rx buf size:%d\n", priv->rx_skb_size);
  1263. /* Configure rx cq's and rings */
  1264. err = mlx4_en_activate_rx_rings(priv);
  1265. if (err) {
  1266. en_err(priv, "Failed to activate RX rings\n");
  1267. return err;
  1268. }
  1269. for (i = 0; i < priv->rx_ring_num; i++) {
  1270. cq = &priv->rx_cq[i];
  1271. mlx4_en_cq_init_lock(cq);
  1272. err = mlx4_en_activate_cq(priv, cq, i);
  1273. if (err) {
  1274. en_err(priv, "Failed activating Rx CQ\n");
  1275. goto cq_err;
  1276. }
  1277. for (j = 0; j < cq->size; j++)
  1278. cq->buf[j].owner_sr_opcode = MLX4_CQE_OWNER_MASK;
  1279. err = mlx4_en_set_cq_moder(priv, cq);
  1280. if (err) {
  1281. en_err(priv, "Failed setting cq moderation parameters");
  1282. mlx4_en_deactivate_cq(priv, cq);
  1283. goto cq_err;
  1284. }
  1285. mlx4_en_arm_cq(priv, cq);
  1286. priv->rx_ring[i].cqn = cq->mcq.cqn;
  1287. ++rx_index;
  1288. }
  1289. /* Set qp number */
  1290. en_dbg(DRV, priv, "Getting qp number for port %d\n", priv->port);
  1291. err = mlx4_en_get_qp(priv);
  1292. if (err) {
  1293. en_err(priv, "Failed getting eth qp\n");
  1294. goto cq_err;
  1295. }
  1296. mdev->mac_removed[priv->port] = 0;
  1297. err = mlx4_en_config_rss_steer(priv);
  1298. if (err) {
  1299. en_err(priv, "Failed configuring rss steering\n");
  1300. goto mac_err;
  1301. }
  1302. err = mlx4_en_create_drop_qp(priv);
  1303. if (err)
  1304. goto rss_err;
  1305. /* Configure tx cq's and rings */
  1306. for (i = 0; i < priv->tx_ring_num; i++) {
  1307. /* Configure cq */
  1308. cq = &priv->tx_cq[i];
  1309. err = mlx4_en_activate_cq(priv, cq, i);
  1310. if (err) {
  1311. en_err(priv, "Failed allocating Tx CQ\n");
  1312. goto tx_err;
  1313. }
  1314. err = mlx4_en_set_cq_moder(priv, cq);
  1315. if (err) {
  1316. en_err(priv, "Failed setting cq moderation parameters");
  1317. mlx4_en_deactivate_cq(priv, cq);
  1318. goto tx_err;
  1319. }
  1320. en_dbg(DRV, priv, "Resetting index of collapsed CQ:%d to -1\n", i);
  1321. cq->buf->wqe_index = cpu_to_be16(0xffff);
  1322. /* Configure ring */
  1323. tx_ring = &priv->tx_ring[i];
  1324. err = mlx4_en_activate_tx_ring(priv, tx_ring, cq->mcq.cqn,
  1325. i / priv->num_tx_rings_p_up);
  1326. if (err) {
  1327. en_err(priv, "Failed allocating Tx ring\n");
  1328. mlx4_en_deactivate_cq(priv, cq);
  1329. goto tx_err;
  1330. }
  1331. tx_ring->tx_queue = netdev_get_tx_queue(dev, i);
  1332. /* Arm CQ for TX completions */
  1333. mlx4_en_arm_cq(priv, cq);
  1334. /* Set initial ownership of all Tx TXBBs to SW (1) */
  1335. for (j = 0; j < tx_ring->buf_size; j += STAMP_STRIDE)
  1336. *((u32 *) (tx_ring->buf + j)) = 0xffffffff;
  1337. ++tx_index;
  1338. }
  1339. /* Configure port */
  1340. err = mlx4_SET_PORT_general(mdev->dev, priv->port,
  1341. priv->rx_skb_size + ETH_FCS_LEN,
  1342. priv->prof->tx_pause,
  1343. priv->prof->tx_ppp,
  1344. priv->prof->rx_pause,
  1345. priv->prof->rx_ppp);
  1346. if (err) {
  1347. en_err(priv, "Failed setting port general configurations for port %d, with error %d\n",
  1348. priv->port, err);
  1349. goto tx_err;
  1350. }
  1351. /* Set default qp number */
  1352. err = mlx4_SET_PORT_qpn_calc(mdev->dev, priv->port, priv->base_qpn, 0);
  1353. if (err) {
  1354. en_err(priv, "Failed setting default qp numbers\n");
  1355. goto tx_err;
  1356. }
  1357. /* Init port */
  1358. en_dbg(HW, priv, "Initializing port\n");
  1359. err = mlx4_INIT_PORT(mdev->dev, priv->port);
  1360. if (err) {
  1361. en_err(priv, "Failed Initializing port\n");
  1362. goto tx_err;
  1363. }
  1364. /* Attach rx QP to bradcast address */
  1365. memset(&mc_list[10], 0xff, ETH_ALEN);
  1366. mc_list[5] = priv->port; /* needed for B0 steering support */
  1367. if (mlx4_multicast_attach(mdev->dev, &priv->rss_map.indir_qp, mc_list,
  1368. priv->port, 0, MLX4_PROT_ETH,
  1369. &priv->broadcast_id))
  1370. mlx4_warn(mdev, "Failed Attaching Broadcast\n");
  1371. /* Must redo promiscuous mode setup. */
  1372. priv->flags &= ~(MLX4_EN_FLAG_PROMISC | MLX4_EN_FLAG_MC_PROMISC);
  1373. /* Schedule multicast task to populate multicast list */
  1374. queue_work(mdev->workqueue, &priv->rx_mode_task);
  1375. mlx4_set_stats_bitmap(mdev->dev, &priv->stats_bitmap);
  1376. priv->port_up = true;
  1377. netif_tx_start_all_queues(dev);
  1378. netif_device_attach(dev);
  1379. return 0;
  1380. tx_err:
  1381. while (tx_index--) {
  1382. mlx4_en_deactivate_tx_ring(priv, &priv->tx_ring[tx_index]);
  1383. mlx4_en_deactivate_cq(priv, &priv->tx_cq[tx_index]);
  1384. }
  1385. mlx4_en_destroy_drop_qp(priv);
  1386. rss_err:
  1387. mlx4_en_release_rss_steer(priv);
  1388. mac_err:
  1389. mlx4_en_put_qp(priv);
  1390. cq_err:
  1391. while (rx_index--)
  1392. mlx4_en_deactivate_cq(priv, &priv->rx_cq[rx_index]);
  1393. for (i = 0; i < priv->rx_ring_num; i++)
  1394. mlx4_en_deactivate_rx_ring(priv, &priv->rx_ring[i]);
  1395. return err; /* need to close devices */
  1396. }
  1397. void mlx4_en_stop_port(struct net_device *dev, int detach)
  1398. {
  1399. struct mlx4_en_priv *priv = netdev_priv(dev);
  1400. struct mlx4_en_dev *mdev = priv->mdev;
  1401. struct mlx4_en_mc_list *mclist, *tmp;
  1402. struct ethtool_flow_id *flow, *tmp_flow;
  1403. int i;
  1404. u8 mc_list[16] = {0};
  1405. if (!priv->port_up) {
  1406. en_dbg(DRV, priv, "stop port called while port already down\n");
  1407. return;
  1408. }
  1409. /* close port*/
  1410. mlx4_CLOSE_PORT(mdev->dev, priv->port);
  1411. /* Synchronize with tx routine */
  1412. netif_tx_lock_bh(dev);
  1413. if (detach)
  1414. netif_device_detach(dev);
  1415. netif_tx_stop_all_queues(dev);
  1416. netif_tx_unlock_bh(dev);
  1417. netif_tx_disable(dev);
  1418. /* Set port as not active */
  1419. priv->port_up = false;
  1420. /* Promsicuous mode */
  1421. if (mdev->dev->caps.steering_mode ==
  1422. MLX4_STEERING_MODE_DEVICE_MANAGED) {
  1423. priv->flags &= ~(MLX4_EN_FLAG_PROMISC |
  1424. MLX4_EN_FLAG_MC_PROMISC);
  1425. mlx4_flow_steer_promisc_remove(mdev->dev,
  1426. priv->port,
  1427. MLX4_FS_ALL_DEFAULT);
  1428. mlx4_flow_steer_promisc_remove(mdev->dev,
  1429. priv->port,
  1430. MLX4_FS_MC_DEFAULT);
  1431. } else if (priv->flags & MLX4_EN_FLAG_PROMISC) {
  1432. priv->flags &= ~MLX4_EN_FLAG_PROMISC;
  1433. /* Disable promiscouos mode */
  1434. mlx4_unicast_promisc_remove(mdev->dev, priv->base_qpn,
  1435. priv->port);
  1436. /* Disable Multicast promisc */
  1437. if (priv->flags & MLX4_EN_FLAG_MC_PROMISC) {
  1438. mlx4_multicast_promisc_remove(mdev->dev, priv->base_qpn,
  1439. priv->port);
  1440. priv->flags &= ~MLX4_EN_FLAG_MC_PROMISC;
  1441. }
  1442. }
  1443. /* Detach All multicasts */
  1444. memset(&mc_list[10], 0xff, ETH_ALEN);
  1445. mc_list[5] = priv->port; /* needed for B0 steering support */
  1446. mlx4_multicast_detach(mdev->dev, &priv->rss_map.indir_qp, mc_list,
  1447. MLX4_PROT_ETH, priv->broadcast_id);
  1448. list_for_each_entry(mclist, &priv->curr_list, list) {
  1449. memcpy(&mc_list[10], mclist->addr, ETH_ALEN);
  1450. mc_list[5] = priv->port;
  1451. mlx4_multicast_detach(mdev->dev, &priv->rss_map.indir_qp,
  1452. mc_list, MLX4_PROT_ETH, mclist->reg_id);
  1453. }
  1454. mlx4_en_clear_list(dev);
  1455. list_for_each_entry_safe(mclist, tmp, &priv->curr_list, list) {
  1456. list_del(&mclist->list);
  1457. kfree(mclist);
  1458. }
  1459. /* Flush multicast filter */
  1460. mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0, 1, MLX4_MCAST_CONFIG);
  1461. /* Remove flow steering rules for the port*/
  1462. if (mdev->dev->caps.steering_mode ==
  1463. MLX4_STEERING_MODE_DEVICE_MANAGED) {
  1464. ASSERT_RTNL();
  1465. list_for_each_entry_safe(flow, tmp_flow,
  1466. &priv->ethtool_list, list) {
  1467. mlx4_flow_detach(mdev->dev, flow->id);
  1468. list_del(&flow->list);
  1469. }
  1470. }
  1471. mlx4_en_destroy_drop_qp(priv);
  1472. /* Free TX Rings */
  1473. for (i = 0; i < priv->tx_ring_num; i++) {
  1474. mlx4_en_deactivate_tx_ring(priv, &priv->tx_ring[i]);
  1475. mlx4_en_deactivate_cq(priv, &priv->tx_cq[i]);
  1476. }
  1477. msleep(10);
  1478. for (i = 0; i < priv->tx_ring_num; i++)
  1479. mlx4_en_free_tx_buf(dev, &priv->tx_ring[i]);
  1480. /* Free RSS qps */
  1481. mlx4_en_release_rss_steer(priv);
  1482. /* Unregister Mac address for the port */
  1483. mlx4_en_put_qp(priv);
  1484. if (!(mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAGS2_REASSIGN_MAC_EN))
  1485. mdev->mac_removed[priv->port] = 1;
  1486. /* Free RX Rings */
  1487. for (i = 0; i < priv->rx_ring_num; i++) {
  1488. struct mlx4_en_cq *cq = &priv->rx_cq[i];
  1489. local_bh_disable();
  1490. while (!mlx4_en_cq_lock_napi(cq)) {
  1491. pr_info("CQ %d locked\n", i);
  1492. mdelay(1);
  1493. }
  1494. local_bh_enable();
  1495. while (test_bit(NAPI_STATE_SCHED, &cq->napi.state))
  1496. msleep(1);
  1497. mlx4_en_deactivate_rx_ring(priv, &priv->rx_ring[i]);
  1498. mlx4_en_deactivate_cq(priv, cq);
  1499. }
  1500. }
  1501. static void mlx4_en_restart(struct work_struct *work)
  1502. {
  1503. struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
  1504. watchdog_task);
  1505. struct mlx4_en_dev *mdev = priv->mdev;
  1506. struct net_device *dev = priv->dev;
  1507. en_dbg(DRV, priv, "Watchdog task called for port %d\n", priv->port);
  1508. mutex_lock(&mdev->state_lock);
  1509. if (priv->port_up) {
  1510. mlx4_en_stop_port(dev, 1);
  1511. if (mlx4_en_start_port(dev))
  1512. en_err(priv, "Failed restarting port %d\n", priv->port);
  1513. }
  1514. mutex_unlock(&mdev->state_lock);
  1515. }
  1516. static void mlx4_en_clear_stats(struct net_device *dev)
  1517. {
  1518. struct mlx4_en_priv *priv = netdev_priv(dev);
  1519. struct mlx4_en_dev *mdev = priv->mdev;
  1520. int i;
  1521. if (mlx4_en_DUMP_ETH_STATS(mdev, priv->port, 1))
  1522. en_dbg(HW, priv, "Failed dumping statistics\n");
  1523. memset(&priv->stats, 0, sizeof(priv->stats));
  1524. memset(&priv->pstats, 0, sizeof(priv->pstats));
  1525. memset(&priv->pkstats, 0, sizeof(priv->pkstats));
  1526. memset(&priv->port_stats, 0, sizeof(priv->port_stats));
  1527. for (i = 0; i < priv->tx_ring_num; i++) {
  1528. priv->tx_ring[i].bytes = 0;
  1529. priv->tx_ring[i].packets = 0;
  1530. priv->tx_ring[i].tx_csum = 0;
  1531. }
  1532. for (i = 0; i < priv->rx_ring_num; i++) {
  1533. priv->rx_ring[i].bytes = 0;
  1534. priv->rx_ring[i].packets = 0;
  1535. priv->rx_ring[i].csum_ok = 0;
  1536. priv->rx_ring[i].csum_none = 0;
  1537. }
  1538. }
  1539. static int mlx4_en_open(struct net_device *dev)
  1540. {
  1541. struct mlx4_en_priv *priv = netdev_priv(dev);
  1542. struct mlx4_en_dev *mdev = priv->mdev;
  1543. int err = 0;
  1544. mutex_lock(&mdev->state_lock);
  1545. if (!mdev->device_up) {
  1546. en_err(priv, "Cannot open - device down/disabled\n");
  1547. err = -EBUSY;
  1548. goto out;
  1549. }
  1550. /* Reset HW statistics and SW counters */
  1551. mlx4_en_clear_stats(dev);
  1552. err = mlx4_en_start_port(dev);
  1553. if (err)
  1554. en_err(priv, "Failed starting port:%d\n", priv->port);
  1555. out:
  1556. mutex_unlock(&mdev->state_lock);
  1557. return err;
  1558. }
  1559. static int mlx4_en_close(struct net_device *dev)
  1560. {
  1561. struct mlx4_en_priv *priv = netdev_priv(dev);
  1562. struct mlx4_en_dev *mdev = priv->mdev;
  1563. en_dbg(IFDOWN, priv, "Close port called\n");
  1564. mutex_lock(&mdev->state_lock);
  1565. mlx4_en_stop_port(dev, 0);
  1566. netif_carrier_off(dev);
  1567. mutex_unlock(&mdev->state_lock);
  1568. return 0;
  1569. }
  1570. void mlx4_en_free_resources(struct mlx4_en_priv *priv)
  1571. {
  1572. int i;
  1573. #ifdef CONFIG_RFS_ACCEL
  1574. free_irq_cpu_rmap(priv->dev->rx_cpu_rmap);
  1575. priv->dev->rx_cpu_rmap = NULL;
  1576. #endif
  1577. for (i = 0; i < priv->tx_ring_num; i++) {
  1578. if (priv->tx_ring[i].tx_info)
  1579. mlx4_en_destroy_tx_ring(priv, &priv->tx_ring[i]);
  1580. if (priv->tx_cq[i].buf)
  1581. mlx4_en_destroy_cq(priv, &priv->tx_cq[i]);
  1582. }
  1583. for (i = 0; i < priv->rx_ring_num; i++) {
  1584. if (priv->rx_ring[i].rx_info)
  1585. mlx4_en_destroy_rx_ring(priv, &priv->rx_ring[i],
  1586. priv->prof->rx_ring_size, priv->stride);
  1587. if (priv->rx_cq[i].buf)
  1588. mlx4_en_destroy_cq(priv, &priv->rx_cq[i]);
  1589. }
  1590. if (priv->base_tx_qpn) {
  1591. mlx4_qp_release_range(priv->mdev->dev, priv->base_tx_qpn, priv->tx_ring_num);
  1592. priv->base_tx_qpn = 0;
  1593. }
  1594. }
  1595. int mlx4_en_alloc_resources(struct mlx4_en_priv *priv)
  1596. {
  1597. struct mlx4_en_port_profile *prof = priv->prof;
  1598. int i;
  1599. int err;
  1600. err = mlx4_qp_reserve_range(priv->mdev->dev, priv->tx_ring_num, 256, &priv->base_tx_qpn);
  1601. if (err) {
  1602. en_err(priv, "failed reserving range for TX rings\n");
  1603. return err;
  1604. }
  1605. /* Create tx Rings */
  1606. for (i = 0; i < priv->tx_ring_num; i++) {
  1607. if (mlx4_en_create_cq(priv, &priv->tx_cq[i],
  1608. prof->tx_ring_size, i, TX))
  1609. goto err;
  1610. if (mlx4_en_create_tx_ring(priv, &priv->tx_ring[i], priv->base_tx_qpn + i,
  1611. prof->tx_ring_size, TXBB_SIZE))
  1612. goto err;
  1613. }
  1614. /* Create rx Rings */
  1615. for (i = 0; i < priv->rx_ring_num; i++) {
  1616. if (mlx4_en_create_cq(priv, &priv->rx_cq[i],
  1617. prof->rx_ring_size, i, RX))
  1618. goto err;
  1619. if (mlx4_en_create_rx_ring(priv, &priv->rx_ring[i],
  1620. prof->rx_ring_size, priv->stride))
  1621. goto err;
  1622. }
  1623. #ifdef CONFIG_RFS_ACCEL
  1624. if (priv->mdev->dev->caps.comp_pool) {
  1625. priv->dev->rx_cpu_rmap = alloc_irq_cpu_rmap(priv->mdev->dev->caps.comp_pool);
  1626. if (!priv->dev->rx_cpu_rmap)
  1627. goto err;
  1628. }
  1629. #endif
  1630. return 0;
  1631. err:
  1632. en_err(priv, "Failed to allocate NIC resources\n");
  1633. return -ENOMEM;
  1634. }
  1635. void mlx4_en_destroy_netdev(struct net_device *dev)
  1636. {
  1637. struct mlx4_en_priv *priv = netdev_priv(dev);
  1638. struct mlx4_en_dev *mdev = priv->mdev;
  1639. en_dbg(DRV, priv, "Destroying netdev on port:%d\n", priv->port);
  1640. /* Unregister device - this will close the port if it was up */
  1641. if (priv->registered)
  1642. unregister_netdev(dev);
  1643. if (priv->allocated)
  1644. mlx4_free_hwq_res(mdev->dev, &priv->res, MLX4_EN_PAGE_SIZE);
  1645. cancel_delayed_work(&priv->stats_task);
  1646. cancel_delayed_work(&priv->service_task);
  1647. /* flush any pending task for this netdev */
  1648. flush_workqueue(mdev->workqueue);
  1649. /* Detach the netdev so tasks would not attempt to access it */
  1650. mutex_lock(&mdev->state_lock);
  1651. mdev->pndev[priv->port] = NULL;
  1652. mutex_unlock(&mdev->state_lock);
  1653. mlx4_en_free_resources(priv);
  1654. kfree(priv->tx_ring);
  1655. kfree(priv->tx_cq);
  1656. free_netdev(dev);
  1657. }
  1658. static int mlx4_en_change_mtu(struct net_device *dev, int new_mtu)
  1659. {
  1660. struct mlx4_en_priv *priv = netdev_priv(dev);
  1661. struct mlx4_en_dev *mdev = priv->mdev;
  1662. int err = 0;
  1663. en_dbg(DRV, priv, "Change MTU called - current:%d new:%d\n",
  1664. dev->mtu, new_mtu);
  1665. if ((new_mtu < MLX4_EN_MIN_MTU) || (new_mtu > priv->max_mtu)) {
  1666. en_err(priv, "Bad MTU size:%d.\n", new_mtu);
  1667. return -EPERM;
  1668. }
  1669. dev->mtu = new_mtu;
  1670. if (netif_running(dev)) {
  1671. mutex_lock(&mdev->state_lock);
  1672. if (!mdev->device_up) {
  1673. /* NIC is probably restarting - let watchdog task reset
  1674. * the port */
  1675. en_dbg(DRV, priv, "Change MTU called with card down!?\n");
  1676. } else {
  1677. mlx4_en_stop_port(dev, 1);
  1678. err = mlx4_en_start_port(dev);
  1679. if (err) {
  1680. en_err(priv, "Failed restarting port:%d\n",
  1681. priv->port);
  1682. queue_work(mdev->workqueue, &priv->watchdog_task);
  1683. }
  1684. }
  1685. mutex_unlock(&mdev->state_lock);
  1686. }
  1687. return 0;
  1688. }
  1689. static int mlx4_en_hwtstamp_ioctl(struct net_device *dev, struct ifreq *ifr)
  1690. {
  1691. struct mlx4_en_priv *priv = netdev_priv(dev);
  1692. struct mlx4_en_dev *mdev = priv->mdev;
  1693. struct hwtstamp_config config;
  1694. if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
  1695. return -EFAULT;
  1696. /* reserved for future extensions */
  1697. if (config.flags)
  1698. return -EINVAL;
  1699. /* device doesn't support time stamping */
  1700. if (!(mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_TS))
  1701. return -EINVAL;
  1702. /* TX HW timestamp */
  1703. switch (config.tx_type) {
  1704. case HWTSTAMP_TX_OFF:
  1705. case HWTSTAMP_TX_ON:
  1706. break;
  1707. default:
  1708. return -ERANGE;
  1709. }
  1710. /* RX HW timestamp */
  1711. switch (config.rx_filter) {
  1712. case HWTSTAMP_FILTER_NONE:
  1713. break;
  1714. case HWTSTAMP_FILTER_ALL:
  1715. case HWTSTAMP_FILTER_SOME:
  1716. case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
  1717. case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
  1718. case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
  1719. case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
  1720. case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
  1721. case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
  1722. case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
  1723. case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
  1724. case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
  1725. case HWTSTAMP_FILTER_PTP_V2_EVENT:
  1726. case HWTSTAMP_FILTER_PTP_V2_SYNC:
  1727. case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
  1728. config.rx_filter = HWTSTAMP_FILTER_ALL;
  1729. break;
  1730. default:
  1731. return -ERANGE;
  1732. }
  1733. if (mlx4_en_timestamp_config(dev, config.tx_type, config.rx_filter)) {
  1734. config.tx_type = HWTSTAMP_TX_OFF;
  1735. config.rx_filter = HWTSTAMP_FILTER_NONE;
  1736. }
  1737. return copy_to_user(ifr->ifr_data, &config,
  1738. sizeof(config)) ? -EFAULT : 0;
  1739. }
  1740. static int mlx4_en_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  1741. {
  1742. switch (cmd) {
  1743. case SIOCSHWTSTAMP:
  1744. return mlx4_en_hwtstamp_ioctl(dev, ifr);
  1745. default:
  1746. return -EOPNOTSUPP;
  1747. }
  1748. }
  1749. static int mlx4_en_set_features(struct net_device *netdev,
  1750. netdev_features_t features)
  1751. {
  1752. struct mlx4_en_priv *priv = netdev_priv(netdev);
  1753. if (features & NETIF_F_LOOPBACK)
  1754. priv->ctrl_flags |= cpu_to_be32(MLX4_WQE_CTRL_FORCE_LOOPBACK);
  1755. else
  1756. priv->ctrl_flags &=
  1757. cpu_to_be32(~MLX4_WQE_CTRL_FORCE_LOOPBACK);
  1758. mlx4_en_update_loopback_state(netdev, features);
  1759. return 0;
  1760. }
  1761. static int mlx4_en_set_vf_mac(struct net_device *dev, int queue, u8 *mac)
  1762. {
  1763. struct mlx4_en_priv *en_priv = netdev_priv(dev);
  1764. struct mlx4_en_dev *mdev = en_priv->mdev;
  1765. u64 mac_u64 = mlx4_en_mac_to_u64(mac);
  1766. if (!is_valid_ether_addr(mac))
  1767. return -EINVAL;
  1768. return mlx4_set_vf_mac(mdev->dev, en_priv->port, queue, mac_u64);
  1769. }
  1770. static int mlx4_en_set_vf_vlan(struct net_device *dev, int vf, u16 vlan, u8 qos)
  1771. {
  1772. struct mlx4_en_priv *en_priv = netdev_priv(dev);
  1773. struct mlx4_en_dev *mdev = en_priv->mdev;
  1774. return mlx4_set_vf_vlan(mdev->dev, en_priv->port, vf, vlan, qos);
  1775. }
  1776. static int mlx4_en_set_vf_spoofchk(struct net_device *dev, int vf, bool setting)
  1777. {
  1778. struct mlx4_en_priv *en_priv = netdev_priv(dev);
  1779. struct mlx4_en_dev *mdev = en_priv->mdev;
  1780. return mlx4_set_vf_spoofchk(mdev->dev, en_priv->port, vf, setting);
  1781. }
  1782. static int mlx4_en_get_vf_config(struct net_device *dev, int vf, struct ifla_vf_info *ivf)
  1783. {
  1784. struct mlx4_en_priv *en_priv = netdev_priv(dev);
  1785. struct mlx4_en_dev *mdev = en_priv->mdev;
  1786. return mlx4_get_vf_config(mdev->dev, en_priv->port, vf, ivf);
  1787. }
  1788. static int mlx4_en_set_vf_link_state(struct net_device *dev, int vf, int link_state)
  1789. {
  1790. struct mlx4_en_priv *en_priv = netdev_priv(dev);
  1791. struct mlx4_en_dev *mdev = en_priv->mdev;
  1792. return mlx4_set_vf_link_state(mdev->dev, en_priv->port, vf, link_state);
  1793. }
  1794. static const struct net_device_ops mlx4_netdev_ops = {
  1795. .ndo_open = mlx4_en_open,
  1796. .ndo_stop = mlx4_en_close,
  1797. .ndo_start_xmit = mlx4_en_xmit,
  1798. .ndo_select_queue = mlx4_en_select_queue,
  1799. .ndo_get_stats = mlx4_en_get_stats,
  1800. .ndo_set_rx_mode = mlx4_en_set_rx_mode,
  1801. .ndo_set_mac_address = mlx4_en_set_mac,
  1802. .ndo_validate_addr = eth_validate_addr,
  1803. .ndo_change_mtu = mlx4_en_change_mtu,
  1804. .ndo_do_ioctl = mlx4_en_ioctl,
  1805. .ndo_tx_timeout = mlx4_en_tx_timeout,
  1806. .ndo_vlan_rx_add_vid = mlx4_en_vlan_rx_add_vid,
  1807. .ndo_vlan_rx_kill_vid = mlx4_en_vlan_rx_kill_vid,
  1808. #ifdef CONFIG_NET_POLL_CONTROLLER
  1809. .ndo_poll_controller = mlx4_en_netpoll,
  1810. #endif
  1811. .ndo_set_features = mlx4_en_set_features,
  1812. .ndo_setup_tc = mlx4_en_setup_tc,
  1813. #ifdef CONFIG_RFS_ACCEL
  1814. .ndo_rx_flow_steer = mlx4_en_filter_rfs,
  1815. #endif
  1816. #ifdef CONFIG_NET_RX_BUSY_POLL
  1817. .ndo_busy_poll = mlx4_en_low_latency_recv,
  1818. #endif
  1819. };
  1820. static const struct net_device_ops mlx4_netdev_ops_master = {
  1821. .ndo_open = mlx4_en_open,
  1822. .ndo_stop = mlx4_en_close,
  1823. .ndo_start_xmit = mlx4_en_xmit,
  1824. .ndo_select_queue = mlx4_en_select_queue,
  1825. .ndo_get_stats = mlx4_en_get_stats,
  1826. .ndo_set_rx_mode = mlx4_en_set_rx_mode,
  1827. .ndo_set_mac_address = mlx4_en_set_mac,
  1828. .ndo_validate_addr = eth_validate_addr,
  1829. .ndo_change_mtu = mlx4_en_change_mtu,
  1830. .ndo_tx_timeout = mlx4_en_tx_timeout,
  1831. .ndo_vlan_rx_add_vid = mlx4_en_vlan_rx_add_vid,
  1832. .ndo_vlan_rx_kill_vid = mlx4_en_vlan_rx_kill_vid,
  1833. .ndo_set_vf_mac = mlx4_en_set_vf_mac,
  1834. .ndo_set_vf_vlan = mlx4_en_set_vf_vlan,
  1835. .ndo_set_vf_spoofchk = mlx4_en_set_vf_spoofchk,
  1836. .ndo_set_vf_link_state = mlx4_en_set_vf_link_state,
  1837. .ndo_get_vf_config = mlx4_en_get_vf_config,
  1838. #ifdef CONFIG_NET_POLL_CONTROLLER
  1839. .ndo_poll_controller = mlx4_en_netpoll,
  1840. #endif
  1841. .ndo_set_features = mlx4_en_set_features,
  1842. .ndo_setup_tc = mlx4_en_setup_tc,
  1843. #ifdef CONFIG_RFS_ACCEL
  1844. .ndo_rx_flow_steer = mlx4_en_filter_rfs,
  1845. #endif
  1846. };
  1847. int mlx4_en_init_netdev(struct mlx4_en_dev *mdev, int port,
  1848. struct mlx4_en_port_profile *prof)
  1849. {
  1850. struct net_device *dev;
  1851. struct mlx4_en_priv *priv;
  1852. int i;
  1853. int err;
  1854. u64 mac_u64;
  1855. dev = alloc_etherdev_mqs(sizeof(struct mlx4_en_priv),
  1856. MAX_TX_RINGS, MAX_RX_RINGS);
  1857. if (dev == NULL)
  1858. return -ENOMEM;
  1859. netif_set_real_num_tx_queues(dev, prof->tx_ring_num);
  1860. netif_set_real_num_rx_queues(dev, prof->rx_ring_num);
  1861. SET_NETDEV_DEV(dev, &mdev->dev->pdev->dev);
  1862. dev->dev_id = port - 1;
  1863. /*
  1864. * Initialize driver private data
  1865. */
  1866. priv = netdev_priv(dev);
  1867. memset(priv, 0, sizeof(struct mlx4_en_priv));
  1868. priv->dev = dev;
  1869. priv->mdev = mdev;
  1870. priv->ddev = &mdev->pdev->dev;
  1871. priv->prof = prof;
  1872. priv->port = port;
  1873. priv->port_up = false;
  1874. priv->flags = prof->flags;
  1875. priv->ctrl_flags = cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE |
  1876. MLX4_WQE_CTRL_SOLICITED);
  1877. priv->num_tx_rings_p_up = mdev->profile.num_tx_rings_p_up;
  1878. priv->tx_ring_num = prof->tx_ring_num;
  1879. priv->tx_ring = kzalloc(sizeof(struct mlx4_en_tx_ring) * MAX_TX_RINGS,
  1880. GFP_KERNEL);
  1881. if (!priv->tx_ring) {
  1882. err = -ENOMEM;
  1883. goto out;
  1884. }
  1885. priv->tx_cq = kzalloc(sizeof(struct mlx4_en_cq) * MAX_TX_RINGS,
  1886. GFP_KERNEL);
  1887. if (!priv->tx_cq) {
  1888. err = -ENOMEM;
  1889. goto out;
  1890. }
  1891. priv->rx_ring_num = prof->rx_ring_num;
  1892. priv->cqe_factor = (mdev->dev->caps.cqe_size == 64) ? 1 : 0;
  1893. priv->mac_index = -1;
  1894. priv->msg_enable = MLX4_EN_MSG_LEVEL;
  1895. spin_lock_init(&priv->stats_lock);
  1896. INIT_WORK(&priv->rx_mode_task, mlx4_en_do_set_rx_mode);
  1897. INIT_WORK(&priv->watchdog_task, mlx4_en_restart);
  1898. INIT_WORK(&priv->linkstate_task, mlx4_en_linkstate);
  1899. INIT_DELAYED_WORK(&priv->stats_task, mlx4_en_do_get_stats);
  1900. INIT_DELAYED_WORK(&priv->service_task, mlx4_en_service_task);
  1901. #ifdef CONFIG_MLX4_EN_DCB
  1902. if (!mlx4_is_slave(priv->mdev->dev)) {
  1903. if (mdev->dev->caps.flags & MLX4_DEV_CAP_FLAG_SET_ETH_SCHED) {
  1904. dev->dcbnl_ops = &mlx4_en_dcbnl_ops;
  1905. } else {
  1906. en_info(priv, "enabling only PFC DCB ops\n");
  1907. dev->dcbnl_ops = &mlx4_en_dcbnl_pfc_ops;
  1908. }
  1909. }
  1910. #endif
  1911. for (i = 0; i < MLX4_EN_MAC_HASH_SIZE; ++i)
  1912. INIT_HLIST_HEAD(&priv->mac_hash[i]);
  1913. /* Query for default mac and max mtu */
  1914. priv->max_mtu = mdev->dev->caps.eth_mtu_cap[priv->port];
  1915. /* Set default MAC */
  1916. dev->addr_len = ETH_ALEN;
  1917. mlx4_en_u64_to_mac(dev->dev_addr, mdev->dev->caps.def_mac[priv->port]);
  1918. if (!is_valid_ether_addr(dev->dev_addr)) {
  1919. if (mlx4_is_slave(priv->mdev->dev)) {
  1920. eth_hw_addr_random(dev);
  1921. en_warn(priv, "Assigned random MAC address %pM\n", dev->dev_addr);
  1922. mac_u64 = mlx4_en_mac_to_u64(dev->dev_addr);
  1923. mdev->dev->caps.def_mac[priv->port] = mac_u64;
  1924. } else {
  1925. en_err(priv, "Port: %d, invalid mac burned: %pM, quiting\n",
  1926. priv->port, dev->dev_addr);
  1927. err = -EINVAL;
  1928. goto out;
  1929. }
  1930. }
  1931. memcpy(priv->prev_mac, dev->dev_addr, sizeof(priv->prev_mac));
  1932. priv->stride = roundup_pow_of_two(sizeof(struct mlx4_en_rx_desc) +
  1933. DS_SIZE * MLX4_EN_MAX_RX_FRAGS);
  1934. err = mlx4_en_alloc_resources(priv);
  1935. if (err)
  1936. goto out;
  1937. #ifdef CONFIG_RFS_ACCEL
  1938. INIT_LIST_HEAD(&priv->filters);
  1939. spin_lock_init(&priv->filters_lock);
  1940. #endif
  1941. /* Initialize time stamping config */
  1942. priv->hwtstamp_config.flags = 0;
  1943. priv->hwtstamp_config.tx_type = HWTSTAMP_TX_OFF;
  1944. priv->hwtstamp_config.rx_filter = HWTSTAMP_FILTER_NONE;
  1945. /* Allocate page for receive rings */
  1946. err = mlx4_alloc_hwq_res(mdev->dev, &priv->res,
  1947. MLX4_EN_PAGE_SIZE, MLX4_EN_PAGE_SIZE);
  1948. if (err) {
  1949. en_err(priv, "Failed to allocate page for rx qps\n");
  1950. goto out;
  1951. }
  1952. priv->allocated = 1;
  1953. /*
  1954. * Initialize netdev entry points
  1955. */
  1956. if (mlx4_is_master(priv->mdev->dev))
  1957. dev->netdev_ops = &mlx4_netdev_ops_master;
  1958. else
  1959. dev->netdev_ops = &mlx4_netdev_ops;
  1960. dev->watchdog_timeo = MLX4_EN_WATCHDOG_TIMEOUT;
  1961. netif_set_real_num_tx_queues(dev, priv->tx_ring_num);
  1962. netif_set_real_num_rx_queues(dev, priv->rx_ring_num);
  1963. SET_ETHTOOL_OPS(dev, &mlx4_en_ethtool_ops);
  1964. /*
  1965. * Set driver features
  1966. */
  1967. dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
  1968. if (mdev->LSO_support)
  1969. dev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6;
  1970. dev->vlan_features = dev->hw_features;
  1971. dev->hw_features |= NETIF_F_RXCSUM | NETIF_F_RXHASH;
  1972. dev->features = dev->hw_features | NETIF_F_HIGHDMA |
  1973. NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX |
  1974. NETIF_F_HW_VLAN_CTAG_FILTER;
  1975. dev->hw_features |= NETIF_F_LOOPBACK;
  1976. if (mdev->dev->caps.steering_mode ==
  1977. MLX4_STEERING_MODE_DEVICE_MANAGED)
  1978. dev->hw_features |= NETIF_F_NTUPLE;
  1979. if (mdev->dev->caps.steering_mode != MLX4_STEERING_MODE_A0)
  1980. dev->priv_flags |= IFF_UNICAST_FLT;
  1981. mdev->pndev[port] = dev;
  1982. netif_carrier_off(dev);
  1983. mlx4_en_set_default_moderation(priv);
  1984. err = register_netdev(dev);
  1985. if (err) {
  1986. en_err(priv, "Netdev registration failed for port %d\n", port);
  1987. goto out;
  1988. }
  1989. priv->registered = 1;
  1990. en_warn(priv, "Using %d TX rings\n", prof->tx_ring_num);
  1991. en_warn(priv, "Using %d RX rings\n", prof->rx_ring_num);
  1992. mlx4_en_update_loopback_state(priv->dev, priv->dev->features);
  1993. /* Configure port */
  1994. mlx4_en_calc_rx_buf(dev);
  1995. err = mlx4_SET_PORT_general(mdev->dev, priv->port,
  1996. priv->rx_skb_size + ETH_FCS_LEN,
  1997. prof->tx_pause, prof->tx_ppp,
  1998. prof->rx_pause, prof->rx_ppp);
  1999. if (err) {
  2000. en_err(priv, "Failed setting port general configurations "
  2001. "for port %d, with error %d\n", priv->port, err);
  2002. goto out;
  2003. }
  2004. /* Init port */
  2005. en_warn(priv, "Initializing port\n");
  2006. err = mlx4_INIT_PORT(mdev->dev, priv->port);
  2007. if (err) {
  2008. en_err(priv, "Failed Initializing port\n");
  2009. goto out;
  2010. }
  2011. queue_delayed_work(mdev->workqueue, &priv->stats_task, STATS_DELAY);
  2012. if (mdev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_TS)
  2013. queue_delayed_work(mdev->workqueue, &priv->service_task,
  2014. SERVICE_TASK_DELAY);
  2015. return 0;
  2016. out:
  2017. mlx4_en_destroy_netdev(dev);
  2018. return err;
  2019. }