en_netdev.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193
  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/mlx4/driver.h>
  39. #include <linux/mlx4/device.h>
  40. #include <linux/mlx4/cmd.h>
  41. #include <linux/mlx4/cq.h>
  42. #include "mlx4_en.h"
  43. #include "en_port.h"
  44. static void mlx4_en_vlan_rx_register(struct net_device *dev, struct vlan_group *grp)
  45. {
  46. struct mlx4_en_priv *priv = netdev_priv(dev);
  47. struct mlx4_en_dev *mdev = priv->mdev;
  48. int err;
  49. en_dbg(HW, priv, "Registering VLAN group:%p\n", grp);
  50. priv->vlgrp = grp;
  51. mutex_lock(&mdev->state_lock);
  52. if (mdev->device_up && priv->port_up) {
  53. err = mlx4_SET_VLAN_FLTR(mdev->dev, priv->port, grp);
  54. if (err)
  55. en_err(priv, "Failed configuring VLAN filter\n");
  56. }
  57. mutex_unlock(&mdev->state_lock);
  58. }
  59. static void mlx4_en_vlan_rx_add_vid(struct net_device *dev, unsigned short vid)
  60. {
  61. struct mlx4_en_priv *priv = netdev_priv(dev);
  62. struct mlx4_en_dev *mdev = priv->mdev;
  63. int err;
  64. int idx;
  65. if (!priv->vlgrp)
  66. return;
  67. en_dbg(HW, priv, "adding VLAN:%d (vlgrp entry:%p)\n",
  68. vid, vlan_group_get_device(priv->vlgrp, vid));
  69. /* Add VID to port VLAN filter */
  70. mutex_lock(&mdev->state_lock);
  71. if (mdev->device_up && priv->port_up) {
  72. err = mlx4_SET_VLAN_FLTR(mdev->dev, priv->port, priv->vlgrp);
  73. if (err)
  74. en_err(priv, "Failed configuring VLAN filter\n");
  75. }
  76. if (mlx4_register_vlan(mdev->dev, priv->port, vid, &idx))
  77. en_err(priv, "failed adding vlan %d\n", vid);
  78. mutex_unlock(&mdev->state_lock);
  79. }
  80. static void mlx4_en_vlan_rx_kill_vid(struct net_device *dev, unsigned short vid)
  81. {
  82. struct mlx4_en_priv *priv = netdev_priv(dev);
  83. struct mlx4_en_dev *mdev = priv->mdev;
  84. int err;
  85. int idx;
  86. if (!priv->vlgrp)
  87. return;
  88. en_dbg(HW, priv, "Killing VID:%d (vlgrp:%p vlgrp entry:%p)\n",
  89. vid, priv->vlgrp, vlan_group_get_device(priv->vlgrp, vid));
  90. vlan_group_set_device(priv->vlgrp, vid, NULL);
  91. /* Remove VID from port VLAN filter */
  92. mutex_lock(&mdev->state_lock);
  93. if (!mlx4_find_cached_vlan(mdev->dev, priv->port, vid, &idx))
  94. mlx4_unregister_vlan(mdev->dev, priv->port, idx);
  95. else
  96. en_err(priv, "could not find vid %d in cache\n", vid);
  97. if (mdev->device_up && priv->port_up) {
  98. err = mlx4_SET_VLAN_FLTR(mdev->dev, priv->port, priv->vlgrp);
  99. if (err)
  100. en_err(priv, "Failed configuring VLAN filter\n");
  101. }
  102. mutex_unlock(&mdev->state_lock);
  103. }
  104. u64 mlx4_en_mac_to_u64(u8 *addr)
  105. {
  106. u64 mac = 0;
  107. int i;
  108. for (i = 0; i < ETH_ALEN; i++) {
  109. mac <<= 8;
  110. mac |= addr[i];
  111. }
  112. return mac;
  113. }
  114. static int mlx4_en_set_mac(struct net_device *dev, void *addr)
  115. {
  116. struct mlx4_en_priv *priv = netdev_priv(dev);
  117. struct mlx4_en_dev *mdev = priv->mdev;
  118. struct sockaddr *saddr = addr;
  119. if (!is_valid_ether_addr(saddr->sa_data))
  120. return -EADDRNOTAVAIL;
  121. memcpy(dev->dev_addr, saddr->sa_data, ETH_ALEN);
  122. priv->mac = mlx4_en_mac_to_u64(dev->dev_addr);
  123. queue_work(mdev->workqueue, &priv->mac_task);
  124. return 0;
  125. }
  126. static void mlx4_en_do_set_mac(struct work_struct *work)
  127. {
  128. struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
  129. mac_task);
  130. struct mlx4_en_dev *mdev = priv->mdev;
  131. int err = 0;
  132. mutex_lock(&mdev->state_lock);
  133. if (priv->port_up) {
  134. /* Remove old MAC and insert the new one */
  135. err = mlx4_replace_mac(mdev->dev, priv->port,
  136. priv->base_qpn, priv->mac, 0);
  137. if (err)
  138. en_err(priv, "Failed changing HW MAC address\n");
  139. } else
  140. en_dbg(HW, priv, "Port is down while "
  141. "registering mac, exiting...\n");
  142. mutex_unlock(&mdev->state_lock);
  143. }
  144. static void mlx4_en_clear_list(struct net_device *dev)
  145. {
  146. struct mlx4_en_priv *priv = netdev_priv(dev);
  147. kfree(priv->mc_addrs);
  148. priv->mc_addrs_cnt = 0;
  149. }
  150. static void mlx4_en_cache_mclist(struct net_device *dev)
  151. {
  152. struct mlx4_en_priv *priv = netdev_priv(dev);
  153. struct netdev_hw_addr *ha;
  154. char *mc_addrs;
  155. int mc_addrs_cnt = netdev_mc_count(dev);
  156. int i;
  157. mc_addrs = kmalloc(mc_addrs_cnt * ETH_ALEN, GFP_ATOMIC);
  158. if (!mc_addrs) {
  159. en_err(priv, "failed to allocate multicast list\n");
  160. return;
  161. }
  162. i = 0;
  163. netdev_for_each_mc_addr(ha, dev)
  164. memcpy(mc_addrs + i++ * ETH_ALEN, ha->addr, ETH_ALEN);
  165. priv->mc_addrs = mc_addrs;
  166. priv->mc_addrs_cnt = mc_addrs_cnt;
  167. }
  168. static void mlx4_en_set_multicast(struct net_device *dev)
  169. {
  170. struct mlx4_en_priv *priv = netdev_priv(dev);
  171. if (!priv->port_up)
  172. return;
  173. queue_work(priv->mdev->workqueue, &priv->mcast_task);
  174. }
  175. static void mlx4_en_do_set_multicast(struct work_struct *work)
  176. {
  177. struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
  178. mcast_task);
  179. struct mlx4_en_dev *mdev = priv->mdev;
  180. struct net_device *dev = priv->dev;
  181. u64 mcast_addr = 0;
  182. u8 mc_list[16] = {0};
  183. int err;
  184. mutex_lock(&mdev->state_lock);
  185. if (!mdev->device_up) {
  186. en_dbg(HW, priv, "Card is not up, "
  187. "ignoring multicast change.\n");
  188. goto out;
  189. }
  190. if (!priv->port_up) {
  191. en_dbg(HW, priv, "Port is down, "
  192. "ignoring multicast change.\n");
  193. goto out;
  194. }
  195. /*
  196. * Promsicuous mode: disable all filters
  197. */
  198. if (dev->flags & IFF_PROMISC) {
  199. if (!(priv->flags & MLX4_EN_FLAG_PROMISC)) {
  200. if (netif_msg_rx_status(priv))
  201. en_warn(priv, "Entering promiscuous mode\n");
  202. priv->flags |= MLX4_EN_FLAG_PROMISC;
  203. /* Enable promiscouos mode */
  204. if (!mdev->dev->caps.vep_uc_steering)
  205. err = mlx4_SET_PORT_qpn_calc(mdev->dev, priv->port,
  206. priv->base_qpn, 1);
  207. else
  208. err = mlx4_unicast_promisc_add(mdev->dev, priv->base_qpn,
  209. priv->port);
  210. if (err)
  211. en_err(priv, "Failed enabling "
  212. "promiscuous mode\n");
  213. /* Disable port multicast filter (unconditionally) */
  214. err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
  215. 0, MLX4_MCAST_DISABLE);
  216. if (err)
  217. en_err(priv, "Failed disabling "
  218. "multicast filter\n");
  219. /* Add the default qp number as multicast promisc */
  220. if (!(priv->flags & MLX4_EN_FLAG_MC_PROMISC)) {
  221. err = mlx4_multicast_promisc_add(mdev->dev, priv->base_qpn,
  222. priv->port);
  223. if (err)
  224. en_err(priv, "Failed entering multicast promisc mode\n");
  225. priv->flags |= MLX4_EN_FLAG_MC_PROMISC;
  226. }
  227. if (priv->vlgrp) {
  228. /* Disable port VLAN filter */
  229. err = mlx4_SET_VLAN_FLTR(mdev->dev, priv->port, NULL);
  230. if (err)
  231. en_err(priv, "Failed disabling VLAN filter\n");
  232. }
  233. }
  234. goto out;
  235. }
  236. /*
  237. * Not in promiscuous mode
  238. */
  239. if (priv->flags & MLX4_EN_FLAG_PROMISC) {
  240. if (netif_msg_rx_status(priv))
  241. en_warn(priv, "Leaving promiscuous mode\n");
  242. priv->flags &= ~MLX4_EN_FLAG_PROMISC;
  243. /* Disable promiscouos mode */
  244. if (!mdev->dev->caps.vep_uc_steering)
  245. err = mlx4_SET_PORT_qpn_calc(mdev->dev, priv->port,
  246. priv->base_qpn, 0);
  247. else
  248. err = mlx4_unicast_promisc_remove(mdev->dev, priv->base_qpn,
  249. priv->port);
  250. if (err)
  251. en_err(priv, "Failed disabling promiscuous mode\n");
  252. /* Disable Multicast promisc */
  253. if (priv->flags & MLX4_EN_FLAG_MC_PROMISC) {
  254. err = mlx4_multicast_promisc_remove(mdev->dev, priv->base_qpn,
  255. priv->port);
  256. if (err)
  257. en_err(priv, "Failed disabling multicast promiscuous mode\n");
  258. priv->flags &= ~MLX4_EN_FLAG_MC_PROMISC;
  259. }
  260. /* Enable port VLAN filter */
  261. err = mlx4_SET_VLAN_FLTR(mdev->dev, priv->port, priv->vlgrp);
  262. if (err)
  263. en_err(priv, "Failed enabling VLAN filter\n");
  264. }
  265. /* Enable/disable the multicast filter according to IFF_ALLMULTI */
  266. if (dev->flags & IFF_ALLMULTI) {
  267. err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
  268. 0, MLX4_MCAST_DISABLE);
  269. if (err)
  270. en_err(priv, "Failed disabling multicast filter\n");
  271. /* Add the default qp number as multicast promisc */
  272. if (!(priv->flags & MLX4_EN_FLAG_MC_PROMISC)) {
  273. err = mlx4_multicast_promisc_add(mdev->dev, priv->base_qpn,
  274. priv->port);
  275. if (err)
  276. en_err(priv, "Failed entering multicast promisc mode\n");
  277. priv->flags |= MLX4_EN_FLAG_MC_PROMISC;
  278. }
  279. } else {
  280. int i;
  281. /* Disable Multicast promisc */
  282. if (priv->flags & MLX4_EN_FLAG_MC_PROMISC) {
  283. err = mlx4_multicast_promisc_remove(mdev->dev, priv->base_qpn,
  284. priv->port);
  285. if (err)
  286. en_err(priv, "Failed disabling multicast promiscuous mode\n");
  287. priv->flags &= ~MLX4_EN_FLAG_MC_PROMISC;
  288. }
  289. err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
  290. 0, MLX4_MCAST_DISABLE);
  291. if (err)
  292. en_err(priv, "Failed disabling multicast filter\n");
  293. /* Detach our qp from all the multicast addresses */
  294. for (i = 0; i < priv->mc_addrs_cnt; i++) {
  295. memcpy(&mc_list[10], priv->mc_addrs + i * ETH_ALEN, ETH_ALEN);
  296. mc_list[5] = priv->port;
  297. mlx4_multicast_detach(mdev->dev, &priv->rss_map.indir_qp,
  298. mc_list, MLX4_PROT_ETH);
  299. }
  300. /* Flush mcast filter and init it with broadcast address */
  301. mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, ETH_BCAST,
  302. 1, MLX4_MCAST_CONFIG);
  303. /* Update multicast list - we cache all addresses so they won't
  304. * change while HW is updated holding the command semaphor */
  305. netif_tx_lock_bh(dev);
  306. mlx4_en_cache_mclist(dev);
  307. netif_tx_unlock_bh(dev);
  308. for (i = 0; i < priv->mc_addrs_cnt; i++) {
  309. mcast_addr =
  310. mlx4_en_mac_to_u64(priv->mc_addrs + i * ETH_ALEN);
  311. memcpy(&mc_list[10], priv->mc_addrs + i * ETH_ALEN, ETH_ALEN);
  312. mc_list[5] = priv->port;
  313. mlx4_multicast_attach(mdev->dev, &priv->rss_map.indir_qp,
  314. mc_list, 0, MLX4_PROT_ETH);
  315. mlx4_SET_MCAST_FLTR(mdev->dev, priv->port,
  316. mcast_addr, 0, MLX4_MCAST_CONFIG);
  317. }
  318. err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
  319. 0, MLX4_MCAST_ENABLE);
  320. if (err)
  321. en_err(priv, "Failed enabling multicast filter\n");
  322. }
  323. out:
  324. mutex_unlock(&mdev->state_lock);
  325. }
  326. #ifdef CONFIG_NET_POLL_CONTROLLER
  327. static void mlx4_en_netpoll(struct net_device *dev)
  328. {
  329. struct mlx4_en_priv *priv = netdev_priv(dev);
  330. struct mlx4_en_cq *cq;
  331. unsigned long flags;
  332. int i;
  333. for (i = 0; i < priv->rx_ring_num; i++) {
  334. cq = &priv->rx_cq[i];
  335. spin_lock_irqsave(&cq->lock, flags);
  336. napi_synchronize(&cq->napi);
  337. mlx4_en_process_rx_cq(dev, cq, 0);
  338. spin_unlock_irqrestore(&cq->lock, flags);
  339. }
  340. }
  341. #endif
  342. static void mlx4_en_tx_timeout(struct net_device *dev)
  343. {
  344. struct mlx4_en_priv *priv = netdev_priv(dev);
  345. struct mlx4_en_dev *mdev = priv->mdev;
  346. if (netif_msg_timer(priv))
  347. en_warn(priv, "Tx timeout called on port:%d\n", priv->port);
  348. priv->port_stats.tx_timeout++;
  349. en_dbg(DRV, priv, "Scheduling watchdog\n");
  350. queue_work(mdev->workqueue, &priv->watchdog_task);
  351. }
  352. static struct net_device_stats *mlx4_en_get_stats(struct net_device *dev)
  353. {
  354. struct mlx4_en_priv *priv = netdev_priv(dev);
  355. spin_lock_bh(&priv->stats_lock);
  356. memcpy(&priv->ret_stats, &priv->stats, sizeof(priv->stats));
  357. spin_unlock_bh(&priv->stats_lock);
  358. return &priv->ret_stats;
  359. }
  360. static void mlx4_en_set_default_moderation(struct mlx4_en_priv *priv)
  361. {
  362. struct mlx4_en_cq *cq;
  363. int i;
  364. /* If we haven't received a specific coalescing setting
  365. * (module param), we set the moderation parameters as follows:
  366. * - moder_cnt is set to the number of mtu sized packets to
  367. * satisfy our coelsing target.
  368. * - moder_time is set to a fixed value.
  369. */
  370. priv->rx_frames = MLX4_EN_RX_COAL_TARGET;
  371. priv->rx_usecs = MLX4_EN_RX_COAL_TIME;
  372. en_dbg(INTR, priv, "Default coalesing params for mtu:%d - "
  373. "rx_frames:%d rx_usecs:%d\n",
  374. priv->dev->mtu, priv->rx_frames, priv->rx_usecs);
  375. /* Setup cq moderation params */
  376. for (i = 0; i < priv->rx_ring_num; i++) {
  377. cq = &priv->rx_cq[i];
  378. cq->moder_cnt = priv->rx_frames;
  379. cq->moder_time = priv->rx_usecs;
  380. }
  381. for (i = 0; i < priv->tx_ring_num; i++) {
  382. cq = &priv->tx_cq[i];
  383. cq->moder_cnt = MLX4_EN_TX_COAL_PKTS;
  384. cq->moder_time = MLX4_EN_TX_COAL_TIME;
  385. }
  386. /* Reset auto-moderation params */
  387. priv->pkt_rate_low = MLX4_EN_RX_RATE_LOW;
  388. priv->rx_usecs_low = MLX4_EN_RX_COAL_TIME_LOW;
  389. priv->pkt_rate_high = MLX4_EN_RX_RATE_HIGH;
  390. priv->rx_usecs_high = MLX4_EN_RX_COAL_TIME_HIGH;
  391. priv->sample_interval = MLX4_EN_SAMPLE_INTERVAL;
  392. priv->adaptive_rx_coal = 1;
  393. priv->last_moder_time = MLX4_EN_AUTO_CONF;
  394. priv->last_moder_jiffies = 0;
  395. priv->last_moder_packets = 0;
  396. priv->last_moder_tx_packets = 0;
  397. priv->last_moder_bytes = 0;
  398. }
  399. static void mlx4_en_auto_moderation(struct mlx4_en_priv *priv)
  400. {
  401. unsigned long period = (unsigned long) (jiffies - priv->last_moder_jiffies);
  402. struct mlx4_en_cq *cq;
  403. unsigned long packets;
  404. unsigned long rate;
  405. unsigned long avg_pkt_size;
  406. unsigned long rx_packets;
  407. unsigned long rx_bytes;
  408. unsigned long tx_packets;
  409. unsigned long tx_pkt_diff;
  410. unsigned long rx_pkt_diff;
  411. int moder_time;
  412. int i, err;
  413. if (!priv->adaptive_rx_coal || period < priv->sample_interval * HZ)
  414. return;
  415. spin_lock_bh(&priv->stats_lock);
  416. rx_packets = priv->stats.rx_packets;
  417. rx_bytes = priv->stats.rx_bytes;
  418. tx_packets = priv->stats.tx_packets;
  419. spin_unlock_bh(&priv->stats_lock);
  420. if (!priv->last_moder_jiffies || !period)
  421. goto out;
  422. tx_pkt_diff = ((unsigned long) (tx_packets -
  423. priv->last_moder_tx_packets));
  424. rx_pkt_diff = ((unsigned long) (rx_packets -
  425. priv->last_moder_packets));
  426. packets = max(tx_pkt_diff, rx_pkt_diff);
  427. rate = packets * HZ / period;
  428. avg_pkt_size = packets ? ((unsigned long) (rx_bytes -
  429. priv->last_moder_bytes)) / packets : 0;
  430. /* Apply auto-moderation only when packet rate exceeds a rate that
  431. * it matters */
  432. if (rate > MLX4_EN_RX_RATE_THRESH && avg_pkt_size > MLX4_EN_AVG_PKT_SMALL) {
  433. /* If tx and rx packet rates are not balanced, assume that
  434. * traffic is mainly BW bound and apply maximum moderation.
  435. * Otherwise, moderate according to packet rate */
  436. if (2 * tx_pkt_diff > 3 * rx_pkt_diff ||
  437. 2 * rx_pkt_diff > 3 * tx_pkt_diff) {
  438. moder_time = priv->rx_usecs_high;
  439. } else {
  440. if (rate < priv->pkt_rate_low)
  441. moder_time = priv->rx_usecs_low;
  442. else if (rate > priv->pkt_rate_high)
  443. moder_time = priv->rx_usecs_high;
  444. else
  445. moder_time = (rate - priv->pkt_rate_low) *
  446. (priv->rx_usecs_high - priv->rx_usecs_low) /
  447. (priv->pkt_rate_high - priv->pkt_rate_low) +
  448. priv->rx_usecs_low;
  449. }
  450. } else {
  451. moder_time = priv->rx_usecs_low;
  452. }
  453. en_dbg(INTR, priv, "tx rate:%lu rx_rate:%lu\n",
  454. tx_pkt_diff * HZ / period, rx_pkt_diff * HZ / period);
  455. en_dbg(INTR, priv, "Rx moder_time changed from:%d to %d period:%lu "
  456. "[jiff] packets:%lu avg_pkt_size:%lu rate:%lu [p/s])\n",
  457. priv->last_moder_time, moder_time, period, packets,
  458. avg_pkt_size, rate);
  459. if (moder_time != priv->last_moder_time) {
  460. priv->last_moder_time = moder_time;
  461. for (i = 0; i < priv->rx_ring_num; i++) {
  462. cq = &priv->rx_cq[i];
  463. cq->moder_time = moder_time;
  464. err = mlx4_en_set_cq_moder(priv, cq);
  465. if (err) {
  466. en_err(priv, "Failed modifying moderation for cq:%d\n", i);
  467. break;
  468. }
  469. }
  470. }
  471. out:
  472. priv->last_moder_packets = rx_packets;
  473. priv->last_moder_tx_packets = tx_packets;
  474. priv->last_moder_bytes = rx_bytes;
  475. priv->last_moder_jiffies = jiffies;
  476. }
  477. static void mlx4_en_do_get_stats(struct work_struct *work)
  478. {
  479. struct delayed_work *delay = to_delayed_work(work);
  480. struct mlx4_en_priv *priv = container_of(delay, struct mlx4_en_priv,
  481. stats_task);
  482. struct mlx4_en_dev *mdev = priv->mdev;
  483. int err;
  484. err = mlx4_en_DUMP_ETH_STATS(mdev, priv->port, 0);
  485. if (err)
  486. en_dbg(HW, priv, "Could not update stats\n");
  487. mutex_lock(&mdev->state_lock);
  488. if (mdev->device_up) {
  489. if (priv->port_up)
  490. mlx4_en_auto_moderation(priv);
  491. queue_delayed_work(mdev->workqueue, &priv->stats_task, STATS_DELAY);
  492. }
  493. if (mdev->mac_removed[MLX4_MAX_PORTS + 1 - priv->port]) {
  494. queue_work(mdev->workqueue, &priv->mac_task);
  495. mdev->mac_removed[MLX4_MAX_PORTS + 1 - priv->port] = 0;
  496. }
  497. mutex_unlock(&mdev->state_lock);
  498. }
  499. static void mlx4_en_linkstate(struct work_struct *work)
  500. {
  501. struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
  502. linkstate_task);
  503. struct mlx4_en_dev *mdev = priv->mdev;
  504. int linkstate = priv->link_state;
  505. mutex_lock(&mdev->state_lock);
  506. /* If observable port state changed set carrier state and
  507. * report to system log */
  508. if (priv->last_link_state != linkstate) {
  509. if (linkstate == MLX4_DEV_EVENT_PORT_DOWN) {
  510. en_info(priv, "Link Down\n");
  511. netif_carrier_off(priv->dev);
  512. } else {
  513. en_info(priv, "Link Up\n");
  514. netif_carrier_on(priv->dev);
  515. }
  516. }
  517. priv->last_link_state = linkstate;
  518. mutex_unlock(&mdev->state_lock);
  519. }
  520. int mlx4_en_start_port(struct net_device *dev)
  521. {
  522. struct mlx4_en_priv *priv = netdev_priv(dev);
  523. struct mlx4_en_dev *mdev = priv->mdev;
  524. struct mlx4_en_cq *cq;
  525. struct mlx4_en_tx_ring *tx_ring;
  526. int rx_index = 0;
  527. int tx_index = 0;
  528. int err = 0;
  529. int i;
  530. int j;
  531. u8 mc_list[16] = {0};
  532. char name[32];
  533. if (priv->port_up) {
  534. en_dbg(DRV, priv, "start port called while port already up\n");
  535. return 0;
  536. }
  537. /* Calculate Rx buf size */
  538. dev->mtu = min(dev->mtu, priv->max_mtu);
  539. mlx4_en_calc_rx_buf(dev);
  540. en_dbg(DRV, priv, "Rx buf size:%d\n", priv->rx_skb_size);
  541. /* Configure rx cq's and rings */
  542. err = mlx4_en_activate_rx_rings(priv);
  543. if (err) {
  544. en_err(priv, "Failed to activate RX rings\n");
  545. return err;
  546. }
  547. for (i = 0; i < priv->rx_ring_num; i++) {
  548. cq = &priv->rx_cq[i];
  549. err = mlx4_en_activate_cq(priv, cq);
  550. if (err) {
  551. en_err(priv, "Failed activating Rx CQ\n");
  552. goto cq_err;
  553. }
  554. for (j = 0; j < cq->size; j++)
  555. cq->buf[j].owner_sr_opcode = MLX4_CQE_OWNER_MASK;
  556. err = mlx4_en_set_cq_moder(priv, cq);
  557. if (err) {
  558. en_err(priv, "Failed setting cq moderation parameters");
  559. mlx4_en_deactivate_cq(priv, cq);
  560. goto cq_err;
  561. }
  562. mlx4_en_arm_cq(priv, cq);
  563. priv->rx_ring[i].cqn = cq->mcq.cqn;
  564. ++rx_index;
  565. }
  566. /* Set port mac number */
  567. en_dbg(DRV, priv, "Setting mac for port %d\n", priv->port);
  568. err = mlx4_register_mac(mdev->dev, priv->port,
  569. priv->mac, &priv->base_qpn, 0);
  570. if (err) {
  571. en_err(priv, "Failed setting port mac\n");
  572. goto cq_err;
  573. }
  574. mdev->mac_removed[priv->port] = 0;
  575. err = mlx4_en_config_rss_steer(priv);
  576. if (err) {
  577. en_err(priv, "Failed configuring rss steering\n");
  578. goto mac_err;
  579. }
  580. if (mdev->dev->caps.comp_pool && !priv->tx_vector) {
  581. sprintf(name , "%s-tx", priv->dev->name);
  582. if (mlx4_assign_eq(mdev->dev , name, &priv->tx_vector)) {
  583. mlx4_warn(mdev, "Failed Assigning an EQ to "
  584. "%s_tx ,Falling back to legacy "
  585. "EQ's\n", priv->dev->name);
  586. }
  587. }
  588. /* Configure tx cq's and rings */
  589. for (i = 0; i < priv->tx_ring_num; i++) {
  590. /* Configure cq */
  591. cq = &priv->tx_cq[i];
  592. cq->vector = priv->tx_vector;
  593. err = mlx4_en_activate_cq(priv, cq);
  594. if (err) {
  595. en_err(priv, "Failed allocating Tx CQ\n");
  596. goto tx_err;
  597. }
  598. err = mlx4_en_set_cq_moder(priv, cq);
  599. if (err) {
  600. en_err(priv, "Failed setting cq moderation parameters");
  601. mlx4_en_deactivate_cq(priv, cq);
  602. goto tx_err;
  603. }
  604. en_dbg(DRV, priv, "Resetting index of collapsed CQ:%d to -1\n", i);
  605. cq->buf->wqe_index = cpu_to_be16(0xffff);
  606. /* Configure ring */
  607. tx_ring = &priv->tx_ring[i];
  608. err = mlx4_en_activate_tx_ring(priv, tx_ring, cq->mcq.cqn);
  609. if (err) {
  610. en_err(priv, "Failed allocating Tx ring\n");
  611. mlx4_en_deactivate_cq(priv, cq);
  612. goto tx_err;
  613. }
  614. /* Set initial ownership of all Tx TXBBs to SW (1) */
  615. for (j = 0; j < tx_ring->buf_size; j += STAMP_STRIDE)
  616. *((u32 *) (tx_ring->buf + j)) = 0xffffffff;
  617. ++tx_index;
  618. }
  619. /* Configure port */
  620. err = mlx4_SET_PORT_general(mdev->dev, priv->port,
  621. priv->rx_skb_size + ETH_FCS_LEN,
  622. priv->prof->tx_pause,
  623. priv->prof->tx_ppp,
  624. priv->prof->rx_pause,
  625. priv->prof->rx_ppp);
  626. if (err) {
  627. en_err(priv, "Failed setting port general configurations "
  628. "for port %d, with error %d\n", priv->port, err);
  629. goto tx_err;
  630. }
  631. /* Set default qp number */
  632. err = mlx4_SET_PORT_qpn_calc(mdev->dev, priv->port, priv->base_qpn, 0);
  633. if (err) {
  634. en_err(priv, "Failed setting default qp numbers\n");
  635. goto tx_err;
  636. }
  637. /* Init port */
  638. en_dbg(HW, priv, "Initializing port\n");
  639. err = mlx4_INIT_PORT(mdev->dev, priv->port);
  640. if (err) {
  641. en_err(priv, "Failed Initializing port\n");
  642. goto tx_err;
  643. }
  644. /* Attach rx QP to bradcast address */
  645. memset(&mc_list[10], 0xff, ETH_ALEN);
  646. mc_list[5] = priv->port;
  647. if (mlx4_multicast_attach(mdev->dev, &priv->rss_map.indir_qp, mc_list,
  648. 0, MLX4_PROT_ETH))
  649. mlx4_warn(mdev, "Failed Attaching Broadcast\n");
  650. /* Must redo promiscuous mode setup. */
  651. priv->flags &= ~(MLX4_EN_FLAG_PROMISC | MLX4_EN_FLAG_MC_PROMISC);
  652. /* Schedule multicast task to populate multicast list */
  653. queue_work(mdev->workqueue, &priv->mcast_task);
  654. priv->port_up = true;
  655. netif_tx_start_all_queues(dev);
  656. return 0;
  657. tx_err:
  658. while (tx_index--) {
  659. mlx4_en_deactivate_tx_ring(priv, &priv->tx_ring[tx_index]);
  660. mlx4_en_deactivate_cq(priv, &priv->tx_cq[tx_index]);
  661. }
  662. mlx4_en_release_rss_steer(priv);
  663. mac_err:
  664. mlx4_unregister_mac(mdev->dev, priv->port, priv->base_qpn);
  665. cq_err:
  666. while (rx_index--)
  667. mlx4_en_deactivate_cq(priv, &priv->rx_cq[rx_index]);
  668. for (i = 0; i < priv->rx_ring_num; i++)
  669. mlx4_en_deactivate_rx_ring(priv, &priv->rx_ring[i]);
  670. return err; /* need to close devices */
  671. }
  672. void mlx4_en_stop_port(struct net_device *dev)
  673. {
  674. struct mlx4_en_priv *priv = netdev_priv(dev);
  675. struct mlx4_en_dev *mdev = priv->mdev;
  676. int i;
  677. u8 mc_list[16] = {0};
  678. if (!priv->port_up) {
  679. en_dbg(DRV, priv, "stop port called while port already down\n");
  680. return;
  681. }
  682. /* Synchronize with tx routine */
  683. netif_tx_lock_bh(dev);
  684. netif_tx_stop_all_queues(dev);
  685. netif_tx_unlock_bh(dev);
  686. /* Set port as not active */
  687. priv->port_up = false;
  688. /* Detach All multicasts */
  689. memset(&mc_list[10], 0xff, ETH_ALEN);
  690. mc_list[5] = priv->port;
  691. mlx4_multicast_detach(mdev->dev, &priv->rss_map.indir_qp, mc_list,
  692. MLX4_PROT_ETH);
  693. for (i = 0; i < priv->mc_addrs_cnt; i++) {
  694. memcpy(&mc_list[10], priv->mc_addrs + i * ETH_ALEN, ETH_ALEN);
  695. mc_list[5] = priv->port;
  696. mlx4_multicast_detach(mdev->dev, &priv->rss_map.indir_qp,
  697. mc_list, MLX4_PROT_ETH);
  698. }
  699. mlx4_en_clear_list(dev);
  700. /* Flush multicast filter */
  701. mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0, 1, MLX4_MCAST_CONFIG);
  702. /* Unregister Mac address for the port */
  703. mlx4_unregister_mac(mdev->dev, priv->port, priv->base_qpn);
  704. mdev->mac_removed[priv->port] = 1;
  705. /* Free TX Rings */
  706. for (i = 0; i < priv->tx_ring_num; i++) {
  707. mlx4_en_deactivate_tx_ring(priv, &priv->tx_ring[i]);
  708. mlx4_en_deactivate_cq(priv, &priv->tx_cq[i]);
  709. }
  710. msleep(10);
  711. for (i = 0; i < priv->tx_ring_num; i++)
  712. mlx4_en_free_tx_buf(dev, &priv->tx_ring[i]);
  713. /* Free RSS qps */
  714. mlx4_en_release_rss_steer(priv);
  715. /* Free RX Rings */
  716. for (i = 0; i < priv->rx_ring_num; i++) {
  717. mlx4_en_deactivate_rx_ring(priv, &priv->rx_ring[i]);
  718. while (test_bit(NAPI_STATE_SCHED, &priv->rx_cq[i].napi.state))
  719. msleep(1);
  720. mlx4_en_deactivate_cq(priv, &priv->rx_cq[i]);
  721. }
  722. /* close port*/
  723. mlx4_CLOSE_PORT(mdev->dev, priv->port);
  724. }
  725. static void mlx4_en_restart(struct work_struct *work)
  726. {
  727. struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
  728. watchdog_task);
  729. struct mlx4_en_dev *mdev = priv->mdev;
  730. struct net_device *dev = priv->dev;
  731. en_dbg(DRV, priv, "Watchdog task called for port %d\n", priv->port);
  732. mutex_lock(&mdev->state_lock);
  733. if (priv->port_up) {
  734. mlx4_en_stop_port(dev);
  735. if (mlx4_en_start_port(dev))
  736. en_err(priv, "Failed restarting port %d\n", priv->port);
  737. }
  738. mutex_unlock(&mdev->state_lock);
  739. }
  740. static int mlx4_en_open(struct net_device *dev)
  741. {
  742. struct mlx4_en_priv *priv = netdev_priv(dev);
  743. struct mlx4_en_dev *mdev = priv->mdev;
  744. int i;
  745. int err = 0;
  746. mutex_lock(&mdev->state_lock);
  747. if (!mdev->device_up) {
  748. en_err(priv, "Cannot open - device down/disabled\n");
  749. err = -EBUSY;
  750. goto out;
  751. }
  752. /* Reset HW statistics and performance counters */
  753. if (mlx4_en_DUMP_ETH_STATS(mdev, priv->port, 1))
  754. en_dbg(HW, priv, "Failed dumping statistics\n");
  755. memset(&priv->stats, 0, sizeof(priv->stats));
  756. memset(&priv->pstats, 0, sizeof(priv->pstats));
  757. for (i = 0; i < priv->tx_ring_num; i++) {
  758. priv->tx_ring[i].bytes = 0;
  759. priv->tx_ring[i].packets = 0;
  760. }
  761. for (i = 0; i < priv->rx_ring_num; i++) {
  762. priv->rx_ring[i].bytes = 0;
  763. priv->rx_ring[i].packets = 0;
  764. }
  765. err = mlx4_en_start_port(dev);
  766. if (err)
  767. en_err(priv, "Failed starting port:%d\n", priv->port);
  768. out:
  769. mutex_unlock(&mdev->state_lock);
  770. return err;
  771. }
  772. static int mlx4_en_close(struct net_device *dev)
  773. {
  774. struct mlx4_en_priv *priv = netdev_priv(dev);
  775. struct mlx4_en_dev *mdev = priv->mdev;
  776. en_dbg(IFDOWN, priv, "Close port called\n");
  777. mutex_lock(&mdev->state_lock);
  778. mlx4_en_stop_port(dev);
  779. netif_carrier_off(dev);
  780. mutex_unlock(&mdev->state_lock);
  781. return 0;
  782. }
  783. void mlx4_en_free_resources(struct mlx4_en_priv *priv, bool reserve_vectors)
  784. {
  785. int i;
  786. for (i = 0; i < priv->tx_ring_num; i++) {
  787. if (priv->tx_ring[i].tx_info)
  788. mlx4_en_destroy_tx_ring(priv, &priv->tx_ring[i]);
  789. if (priv->tx_cq[i].buf)
  790. mlx4_en_destroy_cq(priv, &priv->tx_cq[i], reserve_vectors);
  791. }
  792. for (i = 0; i < priv->rx_ring_num; i++) {
  793. if (priv->rx_ring[i].rx_info)
  794. mlx4_en_destroy_rx_ring(priv, &priv->rx_ring[i]);
  795. if (priv->rx_cq[i].buf)
  796. mlx4_en_destroy_cq(priv, &priv->rx_cq[i], reserve_vectors);
  797. }
  798. }
  799. int mlx4_en_alloc_resources(struct mlx4_en_priv *priv)
  800. {
  801. struct mlx4_en_port_profile *prof = priv->prof;
  802. int i;
  803. int base_tx_qpn, err;
  804. err = mlx4_qp_reserve_range(priv->mdev->dev, priv->tx_ring_num, 256, &base_tx_qpn);
  805. if (err) {
  806. en_err(priv, "failed reserving range for TX rings\n");
  807. return err;
  808. }
  809. /* Create tx Rings */
  810. for (i = 0; i < priv->tx_ring_num; i++) {
  811. if (mlx4_en_create_cq(priv, &priv->tx_cq[i],
  812. prof->tx_ring_size, i, TX))
  813. goto err;
  814. if (mlx4_en_create_tx_ring(priv, &priv->tx_ring[i], base_tx_qpn + i,
  815. prof->tx_ring_size, TXBB_SIZE))
  816. goto err;
  817. }
  818. /* Create rx Rings */
  819. for (i = 0; i < priv->rx_ring_num; i++) {
  820. if (mlx4_en_create_cq(priv, &priv->rx_cq[i],
  821. prof->rx_ring_size, i, RX))
  822. goto err;
  823. if (mlx4_en_create_rx_ring(priv, &priv->rx_ring[i],
  824. prof->rx_ring_size, priv->stride))
  825. goto err;
  826. }
  827. return 0;
  828. err:
  829. en_err(priv, "Failed to allocate NIC resources\n");
  830. mlx4_qp_release_range(priv->mdev->dev, base_tx_qpn, priv->tx_ring_num);
  831. return -ENOMEM;
  832. }
  833. void mlx4_en_destroy_netdev(struct net_device *dev)
  834. {
  835. struct mlx4_en_priv *priv = netdev_priv(dev);
  836. struct mlx4_en_dev *mdev = priv->mdev;
  837. en_dbg(DRV, priv, "Destroying netdev on port:%d\n", priv->port);
  838. /* Unregister device - this will close the port if it was up */
  839. if (priv->registered)
  840. unregister_netdev(dev);
  841. if (priv->allocated)
  842. mlx4_free_hwq_res(mdev->dev, &priv->res, MLX4_EN_PAGE_SIZE);
  843. cancel_delayed_work(&priv->stats_task);
  844. /* flush any pending task for this netdev */
  845. flush_workqueue(mdev->workqueue);
  846. /* Detach the netdev so tasks would not attempt to access it */
  847. mutex_lock(&mdev->state_lock);
  848. mdev->pndev[priv->port] = NULL;
  849. mutex_unlock(&mdev->state_lock);
  850. mlx4_en_free_resources(priv, false);
  851. free_netdev(dev);
  852. }
  853. static int mlx4_en_change_mtu(struct net_device *dev, int new_mtu)
  854. {
  855. struct mlx4_en_priv *priv = netdev_priv(dev);
  856. struct mlx4_en_dev *mdev = priv->mdev;
  857. int err = 0;
  858. en_dbg(DRV, priv, "Change MTU called - current:%d new:%d\n",
  859. dev->mtu, new_mtu);
  860. if ((new_mtu < MLX4_EN_MIN_MTU) || (new_mtu > priv->max_mtu)) {
  861. en_err(priv, "Bad MTU size:%d.\n", new_mtu);
  862. return -EPERM;
  863. }
  864. dev->mtu = new_mtu;
  865. if (netif_running(dev)) {
  866. mutex_lock(&mdev->state_lock);
  867. if (!mdev->device_up) {
  868. /* NIC is probably restarting - let watchdog task reset
  869. * the port */
  870. en_dbg(DRV, priv, "Change MTU called with card down!?\n");
  871. } else {
  872. mlx4_en_stop_port(dev);
  873. err = mlx4_en_start_port(dev);
  874. if (err) {
  875. en_err(priv, "Failed restarting port:%d\n",
  876. priv->port);
  877. queue_work(mdev->workqueue, &priv->watchdog_task);
  878. }
  879. }
  880. mutex_unlock(&mdev->state_lock);
  881. }
  882. return 0;
  883. }
  884. static const struct net_device_ops mlx4_netdev_ops = {
  885. .ndo_open = mlx4_en_open,
  886. .ndo_stop = mlx4_en_close,
  887. .ndo_start_xmit = mlx4_en_xmit,
  888. .ndo_select_queue = mlx4_en_select_queue,
  889. .ndo_get_stats = mlx4_en_get_stats,
  890. .ndo_set_multicast_list = mlx4_en_set_multicast,
  891. .ndo_set_mac_address = mlx4_en_set_mac,
  892. .ndo_validate_addr = eth_validate_addr,
  893. .ndo_change_mtu = mlx4_en_change_mtu,
  894. .ndo_tx_timeout = mlx4_en_tx_timeout,
  895. .ndo_vlan_rx_register = mlx4_en_vlan_rx_register,
  896. .ndo_vlan_rx_add_vid = mlx4_en_vlan_rx_add_vid,
  897. .ndo_vlan_rx_kill_vid = mlx4_en_vlan_rx_kill_vid,
  898. #ifdef CONFIG_NET_POLL_CONTROLLER
  899. .ndo_poll_controller = mlx4_en_netpoll,
  900. #endif
  901. };
  902. int mlx4_en_init_netdev(struct mlx4_en_dev *mdev, int port,
  903. struct mlx4_en_port_profile *prof)
  904. {
  905. struct net_device *dev;
  906. struct mlx4_en_priv *priv;
  907. int i;
  908. int err;
  909. dev = alloc_etherdev_mqs(sizeof(struct mlx4_en_priv),
  910. prof->tx_ring_num, prof->rx_ring_num);
  911. if (dev == NULL) {
  912. mlx4_err(mdev, "Net device allocation failed\n");
  913. return -ENOMEM;
  914. }
  915. SET_NETDEV_DEV(dev, &mdev->dev->pdev->dev);
  916. dev->dev_id = port - 1;
  917. /*
  918. * Initialize driver private data
  919. */
  920. priv = netdev_priv(dev);
  921. memset(priv, 0, sizeof(struct mlx4_en_priv));
  922. priv->dev = dev;
  923. priv->mdev = mdev;
  924. priv->prof = prof;
  925. priv->port = port;
  926. priv->port_up = false;
  927. priv->flags = prof->flags;
  928. priv->tx_ring_num = prof->tx_ring_num;
  929. priv->rx_ring_num = prof->rx_ring_num;
  930. priv->mac_index = -1;
  931. priv->msg_enable = MLX4_EN_MSG_LEVEL;
  932. spin_lock_init(&priv->stats_lock);
  933. INIT_WORK(&priv->mcast_task, mlx4_en_do_set_multicast);
  934. INIT_WORK(&priv->mac_task, mlx4_en_do_set_mac);
  935. INIT_WORK(&priv->watchdog_task, mlx4_en_restart);
  936. INIT_WORK(&priv->linkstate_task, mlx4_en_linkstate);
  937. INIT_DELAYED_WORK(&priv->stats_task, mlx4_en_do_get_stats);
  938. /* Query for default mac and max mtu */
  939. priv->max_mtu = mdev->dev->caps.eth_mtu_cap[priv->port];
  940. priv->mac = mdev->dev->caps.def_mac[priv->port];
  941. if (ILLEGAL_MAC(priv->mac)) {
  942. en_err(priv, "Port: %d, invalid mac burned: 0x%llx, quiting\n",
  943. priv->port, priv->mac);
  944. err = -EINVAL;
  945. goto out;
  946. }
  947. priv->stride = roundup_pow_of_two(sizeof(struct mlx4_en_rx_desc) +
  948. DS_SIZE * MLX4_EN_MAX_RX_FRAGS);
  949. err = mlx4_en_alloc_resources(priv);
  950. if (err)
  951. goto out;
  952. /* Allocate page for receive rings */
  953. err = mlx4_alloc_hwq_res(mdev->dev, &priv->res,
  954. MLX4_EN_PAGE_SIZE, MLX4_EN_PAGE_SIZE);
  955. if (err) {
  956. en_err(priv, "Failed to allocate page for rx qps\n");
  957. goto out;
  958. }
  959. priv->allocated = 1;
  960. /*
  961. * Initialize netdev entry points
  962. */
  963. dev->netdev_ops = &mlx4_netdev_ops;
  964. dev->watchdog_timeo = MLX4_EN_WATCHDOG_TIMEOUT;
  965. netif_set_real_num_tx_queues(dev, priv->tx_ring_num);
  966. netif_set_real_num_rx_queues(dev, priv->rx_ring_num);
  967. SET_ETHTOOL_OPS(dev, &mlx4_en_ethtool_ops);
  968. /* Set defualt MAC */
  969. dev->addr_len = ETH_ALEN;
  970. for (i = 0; i < ETH_ALEN; i++) {
  971. dev->dev_addr[ETH_ALEN - 1 - i] = (u8) (priv->mac >> (8 * i));
  972. dev->perm_addr[ETH_ALEN - 1 - i] = (u8) (priv->mac >> (8 * i));
  973. }
  974. /*
  975. * Set driver features
  976. */
  977. dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
  978. if (mdev->LSO_support)
  979. dev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6;
  980. dev->vlan_features = dev->hw_features;
  981. dev->hw_features |= NETIF_F_RXCSUM;
  982. dev->features = dev->hw_features | NETIF_F_HIGHDMA |
  983. NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX |
  984. NETIF_F_HW_VLAN_FILTER;
  985. mdev->pndev[port] = dev;
  986. netif_carrier_off(dev);
  987. err = register_netdev(dev);
  988. if (err) {
  989. en_err(priv, "Netdev registration failed for port %d\n", port);
  990. goto out;
  991. }
  992. en_warn(priv, "Using %d TX rings\n", prof->tx_ring_num);
  993. en_warn(priv, "Using %d RX rings\n", prof->rx_ring_num);
  994. /* Configure port */
  995. err = mlx4_SET_PORT_general(mdev->dev, priv->port,
  996. MLX4_EN_MIN_MTU,
  997. 0, 0, 0, 0);
  998. if (err) {
  999. en_err(priv, "Failed setting port general configurations "
  1000. "for port %d, with error %d\n", priv->port, err);
  1001. goto out;
  1002. }
  1003. /* Init port */
  1004. en_warn(priv, "Initializing port\n");
  1005. err = mlx4_INIT_PORT(mdev->dev, priv->port);
  1006. if (err) {
  1007. en_err(priv, "Failed Initializing port\n");
  1008. goto out;
  1009. }
  1010. priv->registered = 1;
  1011. mlx4_en_set_default_moderation(priv);
  1012. queue_delayed_work(mdev->workqueue, &priv->stats_task, STATS_DELAY);
  1013. return 0;
  1014. out:
  1015. mlx4_en_destroy_netdev(dev);
  1016. return err;
  1017. }