en_netdev.c 29 KB

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