en_netdev.c 28 KB

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