en_netdev.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079
  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. 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. if (mdev->mac_removed[MLX4_MAX_PORTS + 1 - priv->port]) {
  442. queue_work(mdev->workqueue, &priv->mac_task);
  443. mdev->mac_removed[MLX4_MAX_PORTS + 1 - priv->port] = 0;
  444. }
  445. mutex_unlock(&mdev->state_lock);
  446. }
  447. static void mlx4_en_linkstate(struct work_struct *work)
  448. {
  449. struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
  450. linkstate_task);
  451. struct mlx4_en_dev *mdev = priv->mdev;
  452. int linkstate = priv->link_state;
  453. mutex_lock(&mdev->state_lock);
  454. /* If observable port state changed set carrier state and
  455. * report to system log */
  456. if (priv->last_link_state != linkstate) {
  457. if (linkstate == MLX4_DEV_EVENT_PORT_DOWN) {
  458. en_info(priv, "Link Down\n");
  459. netif_carrier_off(priv->dev);
  460. } else {
  461. en_info(priv, "Link Up\n");
  462. netif_carrier_on(priv->dev);
  463. }
  464. }
  465. priv->last_link_state = linkstate;
  466. mutex_unlock(&mdev->state_lock);
  467. }
  468. int mlx4_en_start_port(struct net_device *dev)
  469. {
  470. struct mlx4_en_priv *priv = netdev_priv(dev);
  471. struct mlx4_en_dev *mdev = priv->mdev;
  472. struct mlx4_en_cq *cq;
  473. struct mlx4_en_tx_ring *tx_ring;
  474. int rx_index = 0;
  475. int tx_index = 0;
  476. int err = 0;
  477. int i;
  478. int j;
  479. if (priv->port_up) {
  480. en_dbg(DRV, priv, "start port called while port already up\n");
  481. return 0;
  482. }
  483. /* Calculate Rx buf size */
  484. dev->mtu = min(dev->mtu, priv->max_mtu);
  485. mlx4_en_calc_rx_buf(dev);
  486. en_dbg(DRV, priv, "Rx buf size:%d\n", priv->rx_skb_size);
  487. /* Configure rx cq's and rings */
  488. err = mlx4_en_activate_rx_rings(priv);
  489. if (err) {
  490. en_err(priv, "Failed to activate RX rings\n");
  491. return err;
  492. }
  493. for (i = 0; i < priv->rx_ring_num; i++) {
  494. cq = &priv->rx_cq[i];
  495. err = mlx4_en_activate_cq(priv, cq);
  496. if (err) {
  497. en_err(priv, "Failed activating Rx CQ\n");
  498. goto cq_err;
  499. }
  500. for (j = 0; j < cq->size; j++)
  501. cq->buf[j].owner_sr_opcode = MLX4_CQE_OWNER_MASK;
  502. err = mlx4_en_set_cq_moder(priv, cq);
  503. if (err) {
  504. en_err(priv, "Failed setting cq moderation parameters");
  505. mlx4_en_deactivate_cq(priv, cq);
  506. goto cq_err;
  507. }
  508. mlx4_en_arm_cq(priv, cq);
  509. priv->rx_ring[i].cqn = cq->mcq.cqn;
  510. ++rx_index;
  511. }
  512. err = mlx4_en_config_rss_steer(priv);
  513. if (err) {
  514. en_err(priv, "Failed configuring rss steering\n");
  515. goto cq_err;
  516. }
  517. /* Configure tx cq's and rings */
  518. for (i = 0; i < priv->tx_ring_num; i++) {
  519. /* Configure cq */
  520. cq = &priv->tx_cq[i];
  521. err = mlx4_en_activate_cq(priv, cq);
  522. if (err) {
  523. en_err(priv, "Failed allocating Tx CQ\n");
  524. goto tx_err;
  525. }
  526. err = mlx4_en_set_cq_moder(priv, cq);
  527. if (err) {
  528. en_err(priv, "Failed setting cq moderation parameters");
  529. mlx4_en_deactivate_cq(priv, cq);
  530. goto tx_err;
  531. }
  532. en_dbg(DRV, priv, "Resetting index of collapsed CQ:%d to -1\n", i);
  533. cq->buf->wqe_index = cpu_to_be16(0xffff);
  534. /* Configure ring */
  535. tx_ring = &priv->tx_ring[i];
  536. err = mlx4_en_activate_tx_ring(priv, tx_ring, cq->mcq.cqn);
  537. if (err) {
  538. en_err(priv, "Failed allocating Tx ring\n");
  539. mlx4_en_deactivate_cq(priv, cq);
  540. goto tx_err;
  541. }
  542. /* Set initial ownership of all Tx TXBBs to SW (1) */
  543. for (j = 0; j < tx_ring->buf_size; j += STAMP_STRIDE)
  544. *((u32 *) (tx_ring->buf + j)) = 0xffffffff;
  545. ++tx_index;
  546. }
  547. /* Configure port */
  548. err = mlx4_SET_PORT_general(mdev->dev, priv->port,
  549. priv->rx_skb_size + ETH_FCS_LEN,
  550. priv->prof->tx_pause,
  551. priv->prof->tx_ppp,
  552. priv->prof->rx_pause,
  553. priv->prof->rx_ppp);
  554. if (err) {
  555. en_err(priv, "Failed setting port general configurations "
  556. "for port %d, with error %d\n", priv->port, err);
  557. goto tx_err;
  558. }
  559. /* Set default qp number */
  560. err = mlx4_SET_PORT_qpn_calc(mdev->dev, priv->port, priv->base_qpn, 0);
  561. if (err) {
  562. en_err(priv, "Failed setting default qp numbers\n");
  563. goto tx_err;
  564. }
  565. /* Set port mac number */
  566. en_dbg(DRV, priv, "Setting mac for port %d\n", priv->port);
  567. err = mlx4_register_mac(mdev->dev, priv->port,
  568. priv->mac, &priv->mac_index);
  569. if (err) {
  570. en_err(priv, "Failed setting port mac\n");
  571. goto tx_err;
  572. }
  573. mdev->mac_removed[priv->port] = 0;
  574. /* Init port */
  575. en_dbg(HW, priv, "Initializing port\n");
  576. err = mlx4_INIT_PORT(mdev->dev, priv->port);
  577. if (err) {
  578. en_err(priv, "Failed Initializing port\n");
  579. goto mac_err;
  580. }
  581. /* Schedule multicast task to populate multicast list */
  582. queue_work(mdev->workqueue, &priv->mcast_task);
  583. priv->port_up = true;
  584. netif_tx_start_all_queues(dev);
  585. return 0;
  586. mac_err:
  587. mlx4_unregister_mac(mdev->dev, priv->port, priv->mac_index);
  588. tx_err:
  589. while (tx_index--) {
  590. mlx4_en_deactivate_tx_ring(priv, &priv->tx_ring[tx_index]);
  591. mlx4_en_deactivate_cq(priv, &priv->tx_cq[tx_index]);
  592. }
  593. mlx4_en_release_rss_steer(priv);
  594. cq_err:
  595. while (rx_index--)
  596. mlx4_en_deactivate_cq(priv, &priv->rx_cq[rx_index]);
  597. for (i = 0; i < priv->rx_ring_num; i++)
  598. mlx4_en_deactivate_rx_ring(priv, &priv->rx_ring[i]);
  599. return err; /* need to close devices */
  600. }
  601. void mlx4_en_stop_port(struct net_device *dev)
  602. {
  603. struct mlx4_en_priv *priv = netdev_priv(dev);
  604. struct mlx4_en_dev *mdev = priv->mdev;
  605. int i;
  606. if (!priv->port_up) {
  607. en_dbg(DRV, priv, "stop port called while port already down\n");
  608. return;
  609. }
  610. /* Synchronize with tx routine */
  611. netif_tx_lock_bh(dev);
  612. netif_tx_stop_all_queues(dev);
  613. netif_tx_unlock_bh(dev);
  614. /* Set port as not active */
  615. priv->port_up = false;
  616. /* Unregister Mac address for the port */
  617. mlx4_unregister_mac(mdev->dev, priv->port, priv->mac_index);
  618. mdev->mac_removed[priv->port] = 1;
  619. /* Free TX Rings */
  620. for (i = 0; i < priv->tx_ring_num; i++) {
  621. mlx4_en_deactivate_tx_ring(priv, &priv->tx_ring[i]);
  622. mlx4_en_deactivate_cq(priv, &priv->tx_cq[i]);
  623. }
  624. msleep(10);
  625. for (i = 0; i < priv->tx_ring_num; i++)
  626. mlx4_en_free_tx_buf(dev, &priv->tx_ring[i]);
  627. /* Free RSS qps */
  628. mlx4_en_release_rss_steer(priv);
  629. /* Free RX Rings */
  630. for (i = 0; i < priv->rx_ring_num; i++) {
  631. mlx4_en_deactivate_rx_ring(priv, &priv->rx_ring[i]);
  632. while (test_bit(NAPI_STATE_SCHED, &priv->rx_cq[i].napi.state))
  633. msleep(1);
  634. mlx4_en_deactivate_cq(priv, &priv->rx_cq[i]);
  635. }
  636. /* close port*/
  637. mlx4_CLOSE_PORT(mdev->dev, priv->port);
  638. }
  639. static void mlx4_en_restart(struct work_struct *work)
  640. {
  641. struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
  642. watchdog_task);
  643. struct mlx4_en_dev *mdev = priv->mdev;
  644. struct net_device *dev = priv->dev;
  645. en_dbg(DRV, priv, "Watchdog task called for port %d\n", priv->port);
  646. mutex_lock(&mdev->state_lock);
  647. if (priv->port_up) {
  648. mlx4_en_stop_port(dev);
  649. if (mlx4_en_start_port(dev))
  650. en_err(priv, "Failed restarting port %d\n", priv->port);
  651. }
  652. mutex_unlock(&mdev->state_lock);
  653. }
  654. static int mlx4_en_open(struct net_device *dev)
  655. {
  656. struct mlx4_en_priv *priv = netdev_priv(dev);
  657. struct mlx4_en_dev *mdev = priv->mdev;
  658. int i;
  659. int err = 0;
  660. mutex_lock(&mdev->state_lock);
  661. if (!mdev->device_up) {
  662. en_err(priv, "Cannot open - device down/disabled\n");
  663. err = -EBUSY;
  664. goto out;
  665. }
  666. /* Reset HW statistics and performance counters */
  667. if (mlx4_en_DUMP_ETH_STATS(mdev, priv->port, 1))
  668. en_dbg(HW, priv, "Failed dumping statistics\n");
  669. memset(&priv->stats, 0, sizeof(priv->stats));
  670. memset(&priv->pstats, 0, sizeof(priv->pstats));
  671. for (i = 0; i < priv->tx_ring_num; i++) {
  672. priv->tx_ring[i].bytes = 0;
  673. priv->tx_ring[i].packets = 0;
  674. }
  675. for (i = 0; i < priv->rx_ring_num; i++) {
  676. priv->rx_ring[i].bytes = 0;
  677. priv->rx_ring[i].packets = 0;
  678. }
  679. mlx4_en_set_default_moderation(priv);
  680. err = mlx4_en_start_port(dev);
  681. if (err)
  682. en_err(priv, "Failed starting port:%d\n", priv->port);
  683. out:
  684. mutex_unlock(&mdev->state_lock);
  685. return err;
  686. }
  687. static int mlx4_en_close(struct net_device *dev)
  688. {
  689. struct mlx4_en_priv *priv = netdev_priv(dev);
  690. struct mlx4_en_dev *mdev = priv->mdev;
  691. en_dbg(IFDOWN, priv, "Close port called\n");
  692. mutex_lock(&mdev->state_lock);
  693. mlx4_en_stop_port(dev);
  694. netif_carrier_off(dev);
  695. mutex_unlock(&mdev->state_lock);
  696. return 0;
  697. }
  698. void mlx4_en_free_resources(struct mlx4_en_priv *priv)
  699. {
  700. int i;
  701. for (i = 0; i < priv->tx_ring_num; i++) {
  702. if (priv->tx_ring[i].tx_info)
  703. mlx4_en_destroy_tx_ring(priv, &priv->tx_ring[i]);
  704. if (priv->tx_cq[i].buf)
  705. mlx4_en_destroy_cq(priv, &priv->tx_cq[i]);
  706. }
  707. for (i = 0; i < priv->rx_ring_num; i++) {
  708. if (priv->rx_ring[i].rx_info)
  709. mlx4_en_destroy_rx_ring(priv, &priv->rx_ring[i]);
  710. if (priv->rx_cq[i].buf)
  711. mlx4_en_destroy_cq(priv, &priv->rx_cq[i]);
  712. }
  713. }
  714. int mlx4_en_alloc_resources(struct mlx4_en_priv *priv)
  715. {
  716. struct mlx4_en_port_profile *prof = priv->prof;
  717. int i;
  718. /* Create tx Rings */
  719. for (i = 0; i < priv->tx_ring_num; i++) {
  720. if (mlx4_en_create_cq(priv, &priv->tx_cq[i],
  721. prof->tx_ring_size, i, TX))
  722. goto err;
  723. if (mlx4_en_create_tx_ring(priv, &priv->tx_ring[i],
  724. prof->tx_ring_size, TXBB_SIZE))
  725. goto err;
  726. }
  727. /* Create rx Rings */
  728. for (i = 0; i < priv->rx_ring_num; i++) {
  729. if (mlx4_en_create_cq(priv, &priv->rx_cq[i],
  730. prof->rx_ring_size, i, RX))
  731. goto err;
  732. if (mlx4_en_create_rx_ring(priv, &priv->rx_ring[i],
  733. prof->rx_ring_size, priv->stride))
  734. goto err;
  735. }
  736. return 0;
  737. err:
  738. en_err(priv, "Failed to allocate NIC resources\n");
  739. return -ENOMEM;
  740. }
  741. void mlx4_en_destroy_netdev(struct net_device *dev)
  742. {
  743. struct mlx4_en_priv *priv = netdev_priv(dev);
  744. struct mlx4_en_dev *mdev = priv->mdev;
  745. en_dbg(DRV, priv, "Destroying netdev on port:%d\n", priv->port);
  746. /* Unregister device - this will close the port if it was up */
  747. if (priv->registered)
  748. unregister_netdev(dev);
  749. if (priv->allocated)
  750. mlx4_free_hwq_res(mdev->dev, &priv->res, MLX4_EN_PAGE_SIZE);
  751. cancel_delayed_work(&priv->stats_task);
  752. /* flush any pending task for this netdev */
  753. flush_workqueue(mdev->workqueue);
  754. /* Detach the netdev so tasks would not attempt to access it */
  755. mutex_lock(&mdev->state_lock);
  756. mdev->pndev[priv->port] = NULL;
  757. mutex_unlock(&mdev->state_lock);
  758. mlx4_en_free_resources(priv);
  759. free_netdev(dev);
  760. }
  761. static int mlx4_en_change_mtu(struct net_device *dev, int new_mtu)
  762. {
  763. struct mlx4_en_priv *priv = netdev_priv(dev);
  764. struct mlx4_en_dev *mdev = priv->mdev;
  765. int err = 0;
  766. en_dbg(DRV, priv, "Change MTU called - current:%d new:%d\n",
  767. dev->mtu, new_mtu);
  768. if ((new_mtu < MLX4_EN_MIN_MTU) || (new_mtu > priv->max_mtu)) {
  769. en_err(priv, "Bad MTU size:%d.\n", new_mtu);
  770. return -EPERM;
  771. }
  772. dev->mtu = new_mtu;
  773. if (netif_running(dev)) {
  774. mutex_lock(&mdev->state_lock);
  775. if (!mdev->device_up) {
  776. /* NIC is probably restarting - let watchdog task reset
  777. * the port */
  778. en_dbg(DRV, priv, "Change MTU called with card down!?\n");
  779. } else {
  780. mlx4_en_stop_port(dev);
  781. mlx4_en_set_default_moderation(priv);
  782. err = mlx4_en_start_port(dev);
  783. if (err) {
  784. en_err(priv, "Failed restarting port:%d\n",
  785. priv->port);
  786. queue_work(mdev->workqueue, &priv->watchdog_task);
  787. }
  788. }
  789. mutex_unlock(&mdev->state_lock);
  790. }
  791. return 0;
  792. }
  793. static const struct net_device_ops mlx4_netdev_ops = {
  794. .ndo_open = mlx4_en_open,
  795. .ndo_stop = mlx4_en_close,
  796. .ndo_start_xmit = mlx4_en_xmit,
  797. .ndo_select_queue = mlx4_en_select_queue,
  798. .ndo_get_stats = mlx4_en_get_stats,
  799. .ndo_set_multicast_list = mlx4_en_set_multicast,
  800. .ndo_set_mac_address = mlx4_en_set_mac,
  801. .ndo_validate_addr = eth_validate_addr,
  802. .ndo_change_mtu = mlx4_en_change_mtu,
  803. .ndo_tx_timeout = mlx4_en_tx_timeout,
  804. .ndo_vlan_rx_register = mlx4_en_vlan_rx_register,
  805. .ndo_vlan_rx_add_vid = mlx4_en_vlan_rx_add_vid,
  806. .ndo_vlan_rx_kill_vid = mlx4_en_vlan_rx_kill_vid,
  807. #ifdef CONFIG_NET_POLL_CONTROLLER
  808. .ndo_poll_controller = mlx4_en_netpoll,
  809. #endif
  810. };
  811. int mlx4_en_init_netdev(struct mlx4_en_dev *mdev, int port,
  812. struct mlx4_en_port_profile *prof)
  813. {
  814. struct net_device *dev;
  815. struct mlx4_en_priv *priv;
  816. int i;
  817. int err;
  818. dev = alloc_etherdev_mq(sizeof(struct mlx4_en_priv), prof->tx_ring_num);
  819. if (dev == NULL) {
  820. mlx4_err(mdev, "Net device allocation failed\n");
  821. return -ENOMEM;
  822. }
  823. SET_NETDEV_DEV(dev, &mdev->dev->pdev->dev);
  824. dev->dev_id = port - 1;
  825. /*
  826. * Initialize driver private data
  827. */
  828. priv = netdev_priv(dev);
  829. memset(priv, 0, sizeof(struct mlx4_en_priv));
  830. priv->dev = dev;
  831. priv->mdev = mdev;
  832. priv->prof = prof;
  833. priv->port = port;
  834. priv->port_up = false;
  835. priv->rx_csum = 1;
  836. priv->flags = prof->flags;
  837. priv->tx_ring_num = prof->tx_ring_num;
  838. priv->rx_ring_num = prof->rx_ring_num;
  839. priv->mac_index = -1;
  840. priv->msg_enable = MLX4_EN_MSG_LEVEL;
  841. spin_lock_init(&priv->stats_lock);
  842. INIT_WORK(&priv->mcast_task, mlx4_en_do_set_multicast);
  843. INIT_WORK(&priv->mac_task, mlx4_en_do_set_mac);
  844. INIT_WORK(&priv->watchdog_task, mlx4_en_restart);
  845. INIT_WORK(&priv->linkstate_task, mlx4_en_linkstate);
  846. INIT_DELAYED_WORK(&priv->stats_task, mlx4_en_do_get_stats);
  847. /* Query for default mac and max mtu */
  848. priv->max_mtu = mdev->dev->caps.eth_mtu_cap[priv->port];
  849. priv->mac = mdev->dev->caps.def_mac[priv->port];
  850. if (ILLEGAL_MAC(priv->mac)) {
  851. en_err(priv, "Port: %d, invalid mac burned: 0x%llx, quiting\n",
  852. priv->port, priv->mac);
  853. err = -EINVAL;
  854. goto out;
  855. }
  856. priv->stride = roundup_pow_of_two(sizeof(struct mlx4_en_rx_desc) +
  857. DS_SIZE * MLX4_EN_MAX_RX_FRAGS);
  858. err = mlx4_en_alloc_resources(priv);
  859. if (err)
  860. goto out;
  861. /* Allocate page for receive rings */
  862. err = mlx4_alloc_hwq_res(mdev->dev, &priv->res,
  863. MLX4_EN_PAGE_SIZE, MLX4_EN_PAGE_SIZE);
  864. if (err) {
  865. en_err(priv, "Failed to allocate page for rx qps\n");
  866. goto out;
  867. }
  868. priv->allocated = 1;
  869. /*
  870. * Initialize netdev entry points
  871. */
  872. dev->netdev_ops = &mlx4_netdev_ops;
  873. dev->watchdog_timeo = MLX4_EN_WATCHDOG_TIMEOUT;
  874. netif_set_real_num_tx_queues(dev, priv->tx_ring_num);
  875. netif_set_real_num_rx_queues(dev, priv->rx_ring_num);
  876. SET_ETHTOOL_OPS(dev, &mlx4_en_ethtool_ops);
  877. /* Set defualt MAC */
  878. dev->addr_len = ETH_ALEN;
  879. for (i = 0; i < ETH_ALEN; i++) {
  880. dev->dev_addr[ETH_ALEN - 1 - i] = (u8) (priv->mac >> (8 * i));
  881. dev->perm_addr[ETH_ALEN - 1 - i] = (u8) (priv->mac >> (8 * i));
  882. }
  883. /*
  884. * Set driver features
  885. */
  886. dev->features |= NETIF_F_SG;
  887. dev->vlan_features |= NETIF_F_SG;
  888. dev->features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
  889. dev->vlan_features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
  890. dev->features |= NETIF_F_HIGHDMA;
  891. dev->features |= NETIF_F_HW_VLAN_TX |
  892. NETIF_F_HW_VLAN_RX |
  893. NETIF_F_HW_VLAN_FILTER;
  894. dev->features |= NETIF_F_GRO;
  895. if (mdev->LSO_support) {
  896. dev->features |= NETIF_F_TSO;
  897. dev->features |= NETIF_F_TSO6;
  898. dev->vlan_features |= NETIF_F_TSO;
  899. dev->vlan_features |= NETIF_F_TSO6;
  900. }
  901. mdev->pndev[port] = dev;
  902. netif_carrier_off(dev);
  903. err = register_netdev(dev);
  904. if (err) {
  905. en_err(priv, "Netdev registration failed for port %d\n", port);
  906. goto out;
  907. }
  908. en_warn(priv, "Using %d TX rings\n", prof->tx_ring_num);
  909. en_warn(priv, "Using %d RX rings\n", prof->rx_ring_num);
  910. priv->registered = 1;
  911. queue_delayed_work(mdev->workqueue, &priv->stats_task, STATS_DELAY);
  912. return 0;
  913. out:
  914. mlx4_en_destroy_netdev(dev);
  915. return err;
  916. }