en_netdev.c 29 KB

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