en_netdev.c 33 KB

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