en_netdev.c 35 KB

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