en_netdev.c 29 KB

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