en_netdev.c 32 KB

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